Visitar URL original
add tests for aliases · ormar-orm/ormar-utils@6e1d0c9 · GitHub
Skip to content

Commit 6e1d0c9

Browse files
committed
add tests for aliases
1 parent bc73ccd commit 6e1d0c9

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/test_all.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,82 @@ def test_plan_merge_items_lists_empty():
237237
"""Test merge plan with empty lists."""
238238
plan = ormar_rust_utils.plan_merge_items_lists([], [])
239239
assert plan == []
240+
241+
242+
def test_build_reverse_alias_map_basic():
243+
"""Test building reverse alias map with aliases."""
244+
field_alias_map = {"name": "user_name", "age": "user_age"}
245+
result = ormar_rust_utils.build_reverse_alias_map(field_alias_map)
246+
# alias -> field_name
247+
assert result["user_name"] == "name"
248+
assert result["user_age"] == "age"
249+
# identity entries for field names
250+
assert result["name"] == "name"
251+
assert result["age"] == "age"
252+
253+
254+
def test_build_reverse_alias_map_identity():
255+
"""Test reverse alias map when alias equals field name."""
256+
field_alias_map = {"name": "name", "age": "age"}
257+
result = ormar_rust_utils.build_reverse_alias_map(field_alias_map)
258+
assert result["name"] == "name"
259+
assert result["age"] == "age"
260+
assert len(result) == 2
261+
262+
263+
def test_build_reverse_alias_map_empty():
264+
"""Test reverse alias map with empty input."""
265+
result = ormar_rust_utils.build_reverse_alias_map({})
266+
assert len(result) == 0
267+
268+
269+
def test_translate_columns_to_aliases_basic():
270+
"""Test translating field names to aliases."""
271+
new_kwargs = {"name": "Alice", "age": 30}
272+
field_to_alias = {"name": "user_name", "age": "user_age"}
273+
result = ormar_rust_utils.translate_columns_to_aliases(new_kwargs, field_to_alias)
274+
assert result["user_name"] == "Alice"
275+
assert result["user_age"] == 30
276+
assert "name" not in result
277+
assert "age" not in result
278+
279+
280+
def test_translate_columns_to_aliases_missing_alias():
281+
"""Test that keys without an alias mapping are kept as-is."""
282+
new_kwargs = {"name": "Alice", "extra": "value"}
283+
field_to_alias = {"name": "user_name"}
284+
result = ormar_rust_utils.translate_columns_to_aliases(new_kwargs, field_to_alias)
285+
assert result["user_name"] == "Alice"
286+
assert result["extra"] == "value"
287+
288+
289+
def test_translate_columns_to_aliases_empty():
290+
"""Test translating with empty inputs."""
291+
result = ormar_rust_utils.translate_columns_to_aliases({}, {})
292+
assert len(result) == 0
293+
294+
295+
def test_translate_aliases_to_columns_basic():
296+
"""Test translating aliases back to field names."""
297+
new_kwargs = {"user_name": "Alice", "user_age": 30}
298+
alias_to_field = {"user_name": "name", "user_age": "age"}
299+
result = ormar_rust_utils.translate_aliases_to_columns(new_kwargs, alias_to_field)
300+
assert result["name"] == "Alice"
301+
assert result["age"] == 30
302+
assert "user_name" not in result
303+
assert "user_age" not in result
304+
305+
306+
def test_translate_aliases_to_columns_missing_mapping():
307+
"""Test that keys without a mapping are kept as-is."""
308+
new_kwargs = {"user_name": "Alice", "extra": "value"}
309+
alias_to_field = {"user_name": "name"}
310+
result = ormar_rust_utils.translate_aliases_to_columns(new_kwargs, alias_to_field)
311+
assert result["name"] == "Alice"
312+
assert result["extra"] == "value"
313+
314+
315+
def test_translate_aliases_to_columns_empty():
316+
"""Test translating with empty inputs."""
317+
result = ormar_rust_utils.translate_aliases_to_columns({}, {})
318+
assert len(result) == 0

0 commit comments

Comments
 (0)