Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_scheme_contract_is_satisfied_when_setting_arg(scheme):
@deal.inv(scheme)
class User:
name = ''
user = User()
user.name = 'Chris'
with pytest.raises(deal.InvContractError):
user.name = 123
try:
user.name = 123
except deal.InvContractError as e:
assert e.args[0] == {'name': ['Not a valid string.']}
def test_instance(self):
class A:
x = 2
PatchedA = deal.inv(lambda obj: obj.x > 0)(A) # noQA
a = PatchedA()
with self.subTest(text='isinstance'):
self.assertIsInstance(a, PatchedA)
self.assertIsInstance(a, A)
PatchedA2 = deal.inv(lambda obj: obj.x > 0)(PatchedA) # noQA
a = PatchedA2()
with self.subTest(text='isinstance'):
self.assertIsInstance(a, PatchedA)
self.assertIsInstance(a, PatchedA2)
self.assertIsInstance(a, A)
with self.subTest(text='class name'):
self.assertEqual(a.__class__.__name__.count('Invarianted'), 1)
def test_patched_invariants_instance():
class A:
x = 2
PatchedA = deal.inv(lambda obj: obj.x > 0)(A) # noQA
a = PatchedA()
assert isinstance(a, PatchedA)
assert isinstance(a, A)
PatchedA2 = deal.inv(lambda obj: obj.x > 0)(PatchedA) # noQA
a = PatchedA2()
assert isinstance(a, PatchedA)
assert isinstance(a, PatchedA2)
assert isinstance(a, A)
assert a.__class__.__name__.count('Invarianted') == 1
def test_scheme_contract_is_satisfied_within_chain(scheme):
@deal.inv(lambda user: user.name != 'Oleg')
@deal.inv(scheme)
@deal.inv(lambda user: user.name != 'Chris')
class User:
name = ''
user = User()
user.name = 'Gram'
user = User()
with pytest.raises(deal.InvContractError):
user.name = 'Oleg'
user = User()
with pytest.raises(deal.InvContractError):
user.name = 123
user = User()
def test_invariant_chain(self):
@deal.inv(lambda user: user.name != 'Oleg')
@deal.inv(self.Scheme)
@deal.inv(lambda user: user.name != 'Chris')
class User:
name = ''
user = User()
with self.subTest('simple call'):
user.name = 'Gram'
user = User()
with self.subTest('not passed first validation'):
with pytest.raises(deal.InvContractError):
user.name = 'Oleg'
user = User()
with self.subTest('not passed second validation'):
with pytest.raises(deal.InvContractError):
def test_instance(self):
class A:
x = 2
PatchedA = deal.inv(lambda obj: obj.x > 0)(A) # noQA
a = PatchedA()
with self.subTest(text='isinstance'):
self.assertIsInstance(a, PatchedA)
self.assertIsInstance(a, A)
PatchedA2 = deal.inv(lambda obj: obj.x > 0)(PatchedA) # noQA
a = PatchedA2()
with self.subTest(text='isinstance'):
self.assertIsInstance(a, PatchedA)
self.assertIsInstance(a, PatchedA2)
self.assertIsInstance(a, A)
with self.subTest(text='class name'):
self.assertEqual(a.__class__.__name__.count('Invarianted'), 1)
def test_setattr(self):
@deal.inv(lambda obj: obj.x > 0)
class A:
x = 2
a = A()
with self.subTest(text='good'):
a.x = 4
with self.subTest(text='error'):
with pytest.raises(deal.InvContractError):
a.x = -2
def test_patched_invariants_instance():
class A:
x = 2
PatchedA = deal.inv(lambda obj: obj.x > 0)(A) # noQA
a = PatchedA()
assert isinstance(a, PatchedA)
assert isinstance(a, A)
PatchedA2 = deal.inv(lambda obj: obj.x > 0)(PatchedA) # noQA
a = PatchedA2()
assert isinstance(a, PatchedA)
assert isinstance(a, PatchedA2)
assert isinstance(a, A)
assert a.__class__.__name__.count('Invarianted') == 1