Skip to content

Commit

Permalink
test: split test_immutablity in 2 functions
Browse files Browse the repository at this point in the history
One function tests the behavior: 'the model is mutable'
The other tests the behavior:OC 'the model is immutable'
  • Loading branch information
rhuille committed Feb 23, 2021
1 parent 7c3f4de commit a6943a8
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions tests/test_main.py
Expand Up @@ -351,8 +351,8 @@ class Model(BaseModel):
assert exc_info.value.errors() == [{'loc': ('a',), 'msg': 'field required', 'type': 'value_error.missing'}]


@pytest.mark.parametrize('allow_mutation_, frozen_', [(False, False), (True, False), (False, True), (True, True)])
def test_immutability(allow_mutation_, frozen_):
@pytest.mark.parametrize('allow_mutation_, frozen_', [(True, False)])
def test_mutability(allow_mutation_, frozen_):
class TestModel(BaseModel):
a: int = 10

Expand All @@ -363,24 +363,33 @@ class Config:

m = TestModel()

immutable = allow_mutation_ is False or frozen_ is True
assert m.a == 10
m.a = 11
assert m.a == 11
with pytest.raises(ValueError) as exc_info:
m.b = 11
assert '"TestModel" object has no field "b"' in exc_info.value.args[0]


if immutable:
assert m.a == 10
with pytest.raises(TypeError) as exc_info:
m.a = 11
assert '"TestModel" is immutable and does not support item assignment' in exc_info.value.args[0]
with pytest.raises(ValueError) as exc_info:
m.b = 11
assert '"TestModel" object has no field "b"' in exc_info.value.args[0]
@pytest.mark.parametrize('allow_mutation_, frozen_', [(False, False), (False, True), (True, True)])
def test_immutability(allow_mutation_, frozen_):
class TestModel(BaseModel):
a: int = 10

else:
assert m.a == 10
class Config:
allow_mutation = allow_mutation_
extra = Extra.forbid
frozen = frozen_

m = TestModel()

assert m.a == 10
with pytest.raises(TypeError) as exc_info:
m.a = 11
assert m.a == 11
with pytest.raises(ValueError) as exc_info:
m.b = 11
assert '"TestModel" object has no field "b"' in exc_info.value.args[0]
assert '"TestModel" is immutable and does not support item assignment' in exc_info.value.args[0]
with pytest.raises(ValueError) as exc_info:
m.b = 11
assert '"TestModel" object has no field "b"' in exc_info.value.args[0]


def test_not_frozen_are_not_hashable():
Expand Down

0 comments on commit a6943a8

Please sign in to comment.