Skip to content

Commit

Permalink
fix(fw): Fix CopyValidateModel (ethereum#679)
Browse files Browse the repository at this point in the history
* fix(fw): Fix `CopyValidateModel`

* fix(fw): Add test for copy
  • Loading branch information
marioevz authored Jul 11, 2024
1 parent f5797b5 commit 8bf86bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ethereum_test_tools/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class CopyValidateModel(BaseModel):

def copy(self: Model, **kwargs) -> Model:
"""
Creates a copy of the model with the updated fields.
Creates a copy of the model with the updated fields that are validated.
"""
return self.__class__(**(self.model_dump() | kwargs))
return self.__class__(**(self.model_dump(exclude_unset=True) | kwargs))


class CamelModel(CopyValidateModel):
Expand Down
19 changes: 18 additions & 1 deletion src/ethereum_test_tools/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from ..common.base_types import Address, Bloom, Bytes, Hash, HeaderNonce, ZeroPaddedHexNumber
from ..common.constants import TestAddress, TestAddress2, TestPrivateKey
from ..common.json import to_json
from ..common.types import Alloc, DepositRequest, Requests
from ..common.types import Alloc, CopyValidateModel, DepositRequest, Requests
from ..eof.v1 import Container
from ..exceptions import BlockException, TransactionException
from ..spec.blockchain.types import (
FixtureBlockBase,
Expand Down Expand Up @@ -1735,3 +1736,19 @@ def test_parsing(json_str: str, type_adapter: TypeAdapter, expected: Any):
Test that parsing the given JSON string returns the expected object.
"""
assert type_adapter.validate_json(json_str) == expected


@pytest.mark.parametrize(
"model",
[
Environment(),
Container(),
],
ids=lambda model: model.__class__.__name__,
)
def test_model_copy(model: CopyValidateModel):
"""
Test that the copy method returns a correct copy of the model.
"""
assert to_json(model.copy()) == to_json(model)
assert model.copy().model_fields_set == model.model_fields_set

0 comments on commit 8bf86bd

Please sign in to comment.