|
| 1 | +"""Unit tests for utils/schema_dumper module.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from utils.schema_dumper import recursive_update |
| 6 | + |
| 7 | + |
| 8 | +def test_update_empty_input() -> None: |
| 9 | + """Test how recursive_update function transforms empty input.""" |
| 10 | + original: dict[str, Any] = {} |
| 11 | + expected: dict[str, Any] = {} |
| 12 | + |
| 13 | + # perform the update |
| 14 | + result = recursive_update(original) |
| 15 | + |
| 16 | + # empty dict should be returned |
| 17 | + assert result == expected |
| 18 | + |
| 19 | + # ensure a new dict is returned, not the same object |
| 20 | + assert result is not original |
| 21 | + |
| 22 | + |
| 23 | +def test_no_change_for_simple_schema() -> None: |
| 24 | + """Test how recursive_update function trasforms simple non-empty input.""" |
| 25 | + original: dict[str, Any] = { |
| 26 | + "type": "string", |
| 27 | + "maxLength": 10, |
| 28 | + } |
| 29 | + |
| 30 | + # we need to distinguish between original and a copy |
| 31 | + expected = original.copy() |
| 32 | + |
| 33 | + # perform the update |
| 34 | + result = recursive_update(original) |
| 35 | + |
| 36 | + # non-empty dict with known content should be returned |
| 37 | + assert result == expected |
| 38 | + |
| 39 | + # ensure a new dict is returned, not the same object |
| 40 | + assert result is not original |
| 41 | + |
| 42 | + |
| 43 | +def test_no_change_for_simple_object() -> None: |
| 44 | + """Test how recursive_update function trasforms simple non-empty input.""" |
| 45 | + original: dict[str, Any] = { |
| 46 | + "type": "object", |
| 47 | + "properties": { |
| 48 | + "name": {"type": "string"}, |
| 49 | + "age": {"type": "integer"}, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + # we need to distinguish between original and a copy |
| 54 | + expected = original.copy() |
| 55 | + |
| 56 | + # perform the update |
| 57 | + result = recursive_update(original) |
| 58 | + |
| 59 | + # non-empty dict with known content should be returned |
| 60 | + assert result == expected |
| 61 | + |
| 62 | + # ensure a new dict is returned, not the same object |
| 63 | + assert result is not original |
| 64 | + |
| 65 | + |
| 66 | +def test_recursive_recurse_into_subdicts() -> None: |
| 67 | + """Test the recursive_update on input containing sub-dictionaries.""" |
| 68 | + original = { |
| 69 | + "type": "object", |
| 70 | + "properties": { |
| 71 | + "name": {"type": "string"}, |
| 72 | + "age": {"type": "integer", "exclusiveMinimum": 0}, |
| 73 | + }, |
| 74 | + } |
| 75 | + expected = { |
| 76 | + "type": "object", |
| 77 | + "properties": { |
| 78 | + "name": {"type": "string"}, |
| 79 | + "age": {"type": "integer", "minimum": 0}, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + # perform the update |
| 84 | + result = recursive_update(original) |
| 85 | + |
| 86 | + # non-empty dict with known content should be returned |
| 87 | + assert result == expected |
| 88 | + |
| 89 | + # ensure a new dict is returned, not the same object |
| 90 | + assert result is not original |
| 91 | + |
| 92 | + |
| 93 | +def test_handles_none_values() -> None: |
| 94 | + """None values should be preserved.""" |
| 95 | + original = {"key": None} |
| 96 | + expected = original.copy() |
| 97 | + |
| 98 | + # perform the update |
| 99 | + result = recursive_update(original) |
| 100 | + |
| 101 | + # non-empty dict with known content should be returned |
| 102 | + assert result == expected |
0 commit comments