Skip to content

Commit 898da12

Browse files
Merge pull request #5 from Cloudrisk/develop
Develop
2 parents 0ef584a + 34df4a4 commit 898da12

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

src/rune/runtime/base_data_class.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def rune_serialize(
7272
self,
7373
*,
7474
validate_model: bool = True,
75+
check_rune_constraints: bool = True,
7576
strict: bool = True,
7677
raise_validation_errors: bool = True,
7778
indent: int | None = None,
@@ -92,6 +93,10 @@ def rune_serialize(
9293
serialization. It checks also all Rune type constraints.
9394
Defaults to True.
9495
96+
`check_rune_constraints (bool, optional):` If `validate_model` is
97+
set to `True`, executes all model defined Rune constraints after
98+
deserialization. Defaults to True.
99+
95100
`strict (bool, optional):` Perform strict attribute validation.
96101
Defaults to True.
97102
@@ -134,8 +139,10 @@ def rune_serialize(
134139
'''
135140
try:
136141
if validate_model:
137-
self.validate_model(strict=strict,
138-
raise_exc=raise_validation_errors)
142+
self.validate_model(
143+
check_rune_constraints=check_rune_constraints,
144+
strict=strict,
145+
raise_exc=raise_validation_errors)
139146

140147
root_meta = self.__dict__.setdefault(ROOT_CONTAINER, {})
141148
root_meta['@type'] = self._FQRTN
@@ -159,6 +166,7 @@ def rune_serialize(
159166
def rune_deserialize(cls,
160167
rune_json: str,
161168
validate_model: bool = True,
169+
check_rune_constraints: bool = True,
162170
strict: bool = True,
163171
raise_validation_errors: bool = True) -> BaseModel:
164172
# pylint: disable=line-too-long
@@ -171,6 +179,10 @@ def rune_deserialize(cls,
171179
deserialization. It checks also all Rune type constraints. Defaults
172180
to True.
173181
182+
`check_rune_constraints (bool, optional):` If `validate_model` is
183+
set to `True`, executes all model defined Rune constraints after
184+
deserialization. Defaults to True.
185+
174186
`strict (bool, optional):` Perform strict attribute validation.
175187
Defaults to True.
176188
@@ -186,7 +198,8 @@ def rune_deserialize(cls,
186198
rune_cls = cls._type_to_cls(rune_dict)
187199
model = rune_cls.model_validate(rune_dict, strict=strict)
188200
if validate_model:
189-
model.validate_model(strict=strict,
201+
model.validate_model(check_rune_constraints=check_rune_constraints,
202+
strict=strict,
190203
raise_exc=raise_validation_errors)
191204
return model
192205

@@ -212,6 +225,7 @@ def resolve_references(self):
212225
self.bind_property_to(prop_nm, ref)
213226

214227
def validate_model(self,
228+
check_rune_constraints=True,
215229
recursively: bool = True,
216230
raise_exc: bool = True,
217231
strict: bool = True) -> list:
@@ -226,8 +240,10 @@ def validate_model(self,
226240
self.disable_meta_checks()
227241
att_errors = self.validate_attribs(raise_exc=raise_exc,
228242
strict=strict)
229-
return att_errors + self.validate_conditions(
230-
recursively=recursively, raise_exc=raise_exc)
243+
if check_rune_constraints:
244+
att_errors.extend(self.validate_conditions(
245+
recursively=recursively, raise_exc=raise_exc))
246+
return att_errors
231247
finally:
232248
self.enable_meta_checks()
233249

src/rune/runtime/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# return eval(expr, globals(), {'self': obj}) # pylint: disable=eval-used
2323

2424

25-
def if_cond_fn(ifexpr, thenexpr: Callable, elseexpr: Callable) -> Any:
25+
def if_cond_fn(ifexpr: bool, thenexpr: Callable, elseexpr: Callable) -> Any:
2626
''' A helper to return the value of the ternary operator
2727
(functional version).
2828
'''

test/cdm/test_trade_creation.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''test the if condition runtime functionality'''
2+
# pylint: disable=invalid-name
3+
from datetime import date
4+
import pytest
5+
try:
6+
# pylint: disable=unused-import
7+
# type: ignore
8+
from cdm.event.common.Trade import Trade
9+
from cdm.event.common.TradeIdentifier import TradeIdentifier
10+
from cdm.product.template.TradableProduct import TradableProduct
11+
from cdm.product.template.Product import Product
12+
from cdm.product.template.TradeLot import TradeLot
13+
from cdm.product.common.settlement.PriceQuantity import PriceQuantity
14+
from cdm.base.staticdata.party.Party import Party
15+
from cdm.base.staticdata.party.PartyIdentifier import PartyIdentifier
16+
from cdm.base.staticdata.party.Counterparty import Counterparty
17+
from cdm.base.staticdata.party.CounterpartyRoleEnum import CounterpartyRoleEnum
18+
from cdm.base.staticdata.asset.common.Index import Index
19+
from cdm.base.staticdata.identifier.AssignedIdentifier import AssignedIdentifier
20+
NO_SER_TEST_MOD = False
21+
except ImportError:
22+
NO_SER_TEST_MOD = True
23+
24+
25+
@pytest.mark.skipif(NO_SER_TEST_MOD, reason='CDM package not found')
26+
def test_simple_trade():
27+
'''Constructs a simple Trade in memory and validates the model.'''
28+
price_quantity = PriceQuantity()
29+
trade_lot = TradeLot(priceQuantity=[price_quantity])
30+
product = Product(index=Index())
31+
counterparty = [
32+
Counterparty(role=CounterpartyRoleEnum.PARTY_1,
33+
partyReference=Party(
34+
partyId=[PartyIdentifier(identifier='Acme Corp')])),
35+
Counterparty(
36+
role=CounterpartyRoleEnum.PARTY_2,
37+
partyReference=Party(
38+
partyId=[PartyIdentifier(identifier='Wile E. Coyote')]))
39+
]
40+
tradable_product = TradableProduct(product=product,
41+
tradeLot=[trade_lot],
42+
counterparty=counterparty)
43+
assigned_identifier = AssignedIdentifier(identifier='BIG DEAL!')
44+
trade_identifier = [
45+
TradeIdentifier(issuer='Acme Corp',
46+
assignedIdentifier=[assigned_identifier])
47+
]
48+
49+
# t = Trade(tradeDate=DateWithMeta(str(date(2023, 1, 1))),
50+
t = Trade(tradeDate=date(2023, 1, 1),
51+
tradableProduct=tradable_product,
52+
tradeIdentifier=trade_identifier)
53+
with pytest.raises(NameError):
54+
exceptions = t.validate_model(raise_exc=False)
55+
exceptions = t.validate_model(raise_exc=False, check_rune_constraints=False)
56+
assert not exceptions
57+
58+
# EOF

0 commit comments

Comments
 (0)