Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions tests/unit/test_json_roundtrip.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,63 @@
import pytest

from imednet.models import Form, Job, Subject
from imednet.models import (
Coding,
Form,
Interval,
Job,
Query,
Record,
RecordRevision,
Site,
Study,
Subject,
User,
Variable,
Visit,
)
from imednet.testing import fake_data


@pytest.mark.parametrize(
"cls,payload_func",
[
(Subject, fake_data.fake_subject),
(Coding, fake_data.fake_coding),
(Form, fake_data.fake_form),
(Interval, fake_data.fake_interval),
(Job, fake_data.fake_job),
(Query, fake_data.fake_query),
(Record, fake_data.fake_record),
(RecordRevision, fake_data.fake_record_revision),
(Site, fake_data.fake_site),
(Study, fake_data.fake_study),
(Subject, fake_data.fake_subject),
(User, fake_data.fake_user),
(Variable, fake_data.fake_variable),
(Visit, fake_data.fake_visit),
],
)
def test_json_roundtrip(cls, payload_func):
payload = payload_func()
model = cls.from_json(payload)
dumped = model.model_dump(by_alias=True)
assert cls.from_json(dumped) == model


def test_fake_forms_for_cache():
forms = fake_data.fake_forms_for_cache(num_forms=2, study_key="TEST-1")
assert len(forms) == 2
for form in forms:
assert isinstance(form, Form)
assert form.study_key == "TEST-1"
Comment on lines +48 to +51
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test_fake_forms_for_cache test substantially duplicates test_fake_forms_for_cache_returns_forms in tests/utils/test_fake_data.py, which checks identical properties (list length, isinstance(f, Form), and study_key). Having the same functional coverage spread across two separate test files increases maintenance overhead without providing additional confidence.

Since the purpose of test_json_roundtrip.py is JSON roundtrip testing, these standalone fixture-validation tests may be better placed (or consolidated) in tests/utils/test_fake_data.py, or the new test should exercise something that the existing test does not (e.g., a full roundtrip assertion similar to test_fake_forms_for_cache_from_json).

Suggested change
assert len(forms) == 2
for form in forms:
assert isinstance(form, Form)
assert form.study_key == "TEST-1"
for form in forms:
payload = form.model_dump(by_alias=True)
roundtripped = Form.from_json(payload)
assert roundtripped == form

Copilot uses AI. Check for mistakes.


def test_fake_variables_for_cache():
forms = fake_data.fake_forms_for_cache(num_forms=1)
variables = fake_data.fake_variables_for_cache(forms, vars_per_form=2, study_key="TEST-1")
assert len(variables) == 2
for variable in variables:
assert isinstance(variable, Variable)
assert variable.study_key == "TEST-1"
assert variable.form_id == forms[0].form_id
assert variable.form_key == forms[0].form_key
assert variable.form_name == forms[0].form_name