Skip to content

Commit

Permalink
Merge branch 'main' into fixtures_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
faucomte97 committed Feb 28, 2024
2 parents 75f2af4 + 9da0a16 commit 913c825
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

<!--next-version-placeholder-->

## v0.14.2 (2024-02-28)

### Fix

* Assertion helpers ([#98](https://github.com/ocadotechnology/codeforlife-package-python/issues/98)) ([`a347bd3`](https://github.com/ocadotechnology/codeforlife-package-python/commit/a347bd3d74fea6c891855603e5f75f1bdd5b8554))

## v0.14.1 (2024-02-26)

### Fix
Expand Down
12 changes: 12 additions & 0 deletions codeforlife/tests/model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def assert_create(
validated_data: DataDict,
*args,
new_data: t.Optional[DataDict] = None,
non_model_fields: t.Optional[t.Iterable[str]] = None,
**kwargs,
):
"""Assert that the data used to create the model is a subset of the
Expand All @@ -149,10 +150,13 @@ def assert_create(
Args:
validated_data: The data used to create the model.
new_data: Any new data that the model may have after creating.
non_model_fields: Validated data fields that are not in the model.
"""
serializer = self._init_model_serializer(*args, **kwargs)
model = serializer.create(validated_data.copy())
data = {**validated_data, **(new_data or {})}
for field in non_model_fields or []:
data.pop(field)
self._assert_data_is_subset_of_model(data, model)

def assert_update(
Expand All @@ -161,6 +165,7 @@ def assert_update(
validated_data: DataDict,
*args,
new_data: t.Optional[DataDict] = None,
non_model_fields: t.Optional[t.Iterable[str]] = None,
**kwargs,
):
"""Assert that the data used to update the model is a subset of the
Expand All @@ -170,10 +175,13 @@ def assert_update(
instance: The model instance to update.
validated_data: The data used to update the model.
new_data: Any new data that the model may have after updating.
non_model_fields: Validated data fields that are not in the model.
"""
serializer = self._init_model_serializer(*args, **kwargs)
model = serializer.update(instance, validated_data.copy())
data = {**validated_data, **(new_data or {})}
for field in non_model_fields or []:
data.pop(field)
self._assert_data_is_subset_of_model(data, model)

def assert_update_many(
Expand All @@ -182,6 +190,7 @@ def assert_update_many(
validated_data: t.List[DataDict],
*args,
new_data: t.Optional[t.List[DataDict]] = None,
non_model_fields: t.Optional[t.Iterable[str]] = None,
**kwargs,
):
"""Assert that the data used to update the models is a subset of the
Expand All @@ -194,6 +203,7 @@ def assert_update_many(
instance: The model instances to update.
validated_data: The data used to update the models.
new_data: Any new data that the models may have after updating.
non_model_fields: Validated data fields that are not in the model.
"""
kwargs.pop("many", None) # many must be True
serializer = self._init_model_serializer(*args, **kwargs, many=True)
Expand All @@ -203,6 +213,8 @@ def assert_update_many(
new_data = new_data or [{} for _ in range(len(instance))]
for data, _new_data, model in zip(validated_data, new_data, models):
data = {**data, **_new_data}
for field in non_model_fields or []:
data.pop(field)
self._assert_data_is_subset_of_model(data, model)

def assert_to_representation(
Expand Down
2 changes: 1 addition & 1 deletion codeforlife/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Do NOT set manually!
# This is auto-updated by python-semantic-release in the pipeline.
__version__ = "0.14.1"
__version__ = "0.14.2"

0 comments on commit 913c825

Please sign in to comment.