Skip to content

Commit 743010e

Browse files
committed
update: test automatic camel -> snake properties
1 parent df2eb0f commit 743010e

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

tests/py/unit/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,14 @@ class SnakeCaseEntity(CamelCaseSchema, InMemoryEntitySnakeCase):
9898
"applicationVersion": "7.2",
9999
"executable_name": "pw.x",
100100
}
101+
102+
103+
class AutoSnakeCaseTestSchema(BaseModel):
104+
contextProviders: list = []
105+
applicationName: str
106+
applicationVersion: Optional[str] = None
107+
executableName: Optional[str] = None
108+
109+
110+
class AutoSnakeCaseTestEntity(AutoSnakeCaseTestSchema, InMemoryEntitySnakeCase):
111+
pass

tests/py/unit/test_entity.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,3 @@ def test_create_entity_snake_case(config, expected_output):
244244

245245
entity_from_create = SnakeCaseEntity.create(config)
246246
assert entity_from_create.to_dict() == expected_output
247-
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import pytest
2+
from mat3ra.utils import assertion
3+
from . import AutoSnakeCaseTestEntity
4+
5+
BASE = {
6+
"applicationName": "camelCasedValue",
7+
"applicationVersion": "camelCasedVersion",
8+
"executableName": "camelCasedExecutable",
9+
"contextProviders": [],
10+
}
11+
12+
INSTANTIATION = [
13+
{"application_name": BASE["applicationName"], "application_version": BASE["applicationVersion"],
14+
"executable_name": BASE["executableName"]},
15+
{"applicationName": BASE["applicationName"], "applicationVersion": BASE["applicationVersion"],
16+
"executableName": BASE["executableName"]},
17+
{"application_name": BASE["applicationName"], "applicationVersion": BASE["applicationVersion"],
18+
"executable_name": BASE["executableName"]},
19+
]
20+
21+
UPDATES = [
22+
(
23+
{"application_name": "new_value", "context_providers": ["item_snake"]},
24+
{"applicationName": "new_value", "contextProviders": ["item_snake"]},
25+
{"application_name": "new_value", "context_providers": ["item_snake"]},
26+
),
27+
(
28+
{"applicationName": "newValueCamel", "contextProviders": ["itemCamel"]},
29+
{"applicationName": "newValueCamel", "contextProviders": ["itemCamel"]},
30+
{"application_name": "newValueCamel", "context_providers": ["itemCamel"]},
31+
),
32+
(
33+
{"application_name": "new_value_snake", "applicationVersion": "newVersionCamel"},
34+
{"applicationName": "new_value_snake", "applicationVersion": "newVersionCamel"},
35+
{"application_name": "new_value_snake", "application_version": "newVersionCamel"},
36+
),
37+
(
38+
{"application_name": "new_val", "application_version": "new_version",
39+
"executable_name": "new_exec", "context_providers": ["a", "b"]},
40+
{"applicationName": "new_val", "applicationVersion": "new_version",
41+
"executableName": "new_exec", "contextProviders": ["a", "b"]},
42+
{"application_name": "new_val", "application_version": "new_version",
43+
"executable_name": "new_exec", "context_providers": ["a", "b"]},
44+
),
45+
]
46+
47+
48+
def camel(entity):
49+
return dict(
50+
applicationName=entity.applicationName,
51+
applicationVersion=entity.applicationVersion,
52+
executableName=entity.executableName,
53+
contextProviders=entity.contextProviders,
54+
)
55+
56+
57+
def snake(entity):
58+
return dict(
59+
application_name=entity.application_name,
60+
application_version=entity.application_version,
61+
executable_name=entity.executable_name,
62+
context_providers=entity.context_providers,
63+
)
64+
65+
66+
@pytest.mark.parametrize("cfg", INSTANTIATION)
67+
def test_instantiation(cfg):
68+
entity = AutoSnakeCaseTestEntity(**cfg)
69+
assertion.assert_deep_almost_equal(BASE, camel(entity))
70+
assertion.assert_deep_almost_equal(
71+
dict(application_name=BASE["applicationName"],
72+
application_version=BASE["applicationVersion"],
73+
executable_name=BASE["executableName"],
74+
context_providers=[]),
75+
snake(entity),
76+
)
77+
78+
79+
@pytest.mark.parametrize("updates, exp_camel, exp_snake", UPDATES)
80+
def test_updates(updates, exp_camel, exp_snake):
81+
entity = AutoSnakeCaseTestEntity(**BASE)
82+
for k, v in updates.items():
83+
setattr(entity, k, v)
84+
assertion.assert_deep_almost_equal({**BASE, **exp_camel}, camel(entity))
85+
assertion.assert_deep_almost_equal(
86+
{**snake(AutoSnakeCaseTestEntity(**BASE)), **exp_snake},
87+
snake(entity),
88+
)
89+
out = entity.to_dict()
90+
assertion.assert_deep_almost_equal({**BASE, **exp_camel}, out)
91+
assert "application_name" not in out and "context_providers" not in out

0 commit comments

Comments
 (0)