|
25 | 25 | from tests.conftest import data_write_read_check |
26 | 26 |
|
27 | 27 |
|
| 28 | +@pytest.fixture |
| 29 | +def dict_data_str_keys(): |
| 30 | + data = {"1": 1.5, "2": "a", "3": {"1": False}} |
| 31 | + payload = { |
| 32 | + "item_types": { |
| 33 | + "1": {"ptype": "float", "type": "primitive"}, |
| 34 | + "2": {"ptype": "str", "type": "primitive"}, |
| 35 | + "3": { |
| 36 | + "item_types": {"1": {"ptype": "bool", "type": "primitive"}}, |
| 37 | + "type": "dict", |
| 38 | + }, |
| 39 | + }, |
| 40 | + "type": "dict", |
| 41 | + } |
| 42 | + schema = { |
| 43 | + "title": "DictType", |
| 44 | + "type": "object", |
| 45 | + "properties": { |
| 46 | + "1": {"title": "1", "type": "number"}, |
| 47 | + "2": {"title": "2", "type": "string"}, |
| 48 | + "3": {"$ref": "#/definitions/3_DictType"}, |
| 49 | + }, |
| 50 | + "required": ["1", "2", "3"], |
| 51 | + "definitions": { |
| 52 | + "3_DictType": { |
| 53 | + "title": "3_DictType", |
| 54 | + "type": "object", |
| 55 | + "properties": {"1": {"title": "1", "type": "boolean"}}, |
| 56 | + "required": ["1"], |
| 57 | + } |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + return data, payload, schema |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def dict_data_int_keys(dict_data_str_keys): |
| 66 | + data = {"1": 1.5, 2: "a", "3": {1: False}} |
| 67 | + payload = { |
| 68 | + "item_types": { |
| 69 | + "1": {"ptype": "float", "type": "primitive"}, |
| 70 | + 2: {"ptype": "str", "type": "primitive"}, |
| 71 | + "3": { |
| 72 | + "item_types": {1: {"ptype": "bool", "type": "primitive"}}, |
| 73 | + "type": "dict", |
| 74 | + }, |
| 75 | + }, |
| 76 | + "type": "dict", |
| 77 | + } |
| 78 | + return data, payload, dict_data_str_keys[2] |
| 79 | + |
| 80 | + |
28 | 81 | class NotPrimitive: |
29 | 82 | pass |
30 | 83 |
|
@@ -300,7 +353,6 @@ def dynamic_dict_str_val_type_data(): |
300 | 353 | "type": "object", |
301 | 354 | "additionalProperties": {"type": "string"}, |
302 | 355 | } |
303 | | - |
304 | 356 | test_data1 = {"a": "1", "b": "2"} |
305 | 357 | test_data2 = {"a": "1"} |
306 | 358 | test_data3 = {"a": "1", "b": "2", "c": "3", "d": "1"} |
@@ -551,3 +603,50 @@ def custom_assert(x, y): |
551 | 603 | else: |
552 | 604 | assert list(artifacts.keys()) == ["data"] |
553 | 605 | assert artifacts["data"].uri.endswith("data") |
| 606 | + |
| 607 | + |
| 608 | +@pytest.mark.parametrize( |
| 609 | + "dict_data", |
| 610 | + [lazy_fixture("dict_data_str_keys"), lazy_fixture("dict_data_int_keys")], |
| 611 | +) |
| 612 | +def test_dict_key_int_and_str_types(dict_data): |
| 613 | + d_value, payload, schema = dict_data |
| 614 | + data_type = DataType.create(d_value) |
| 615 | + |
| 616 | + assert isinstance(data_type, DictType) |
| 617 | + |
| 618 | + assert data_type.dict() == payload |
| 619 | + dt2 = parse_obj_as(DictType, payload) |
| 620 | + assert dt2 == data_type |
| 621 | + assert d_value == data_type.serialize(d_value) |
| 622 | + assert d_value == data_type.deserialize(d_value) |
| 623 | + assert data_type.get_model().__name__ == "DictType" |
| 624 | + assert data_type.get_model().schema() == schema |
| 625 | + |
| 626 | + |
| 627 | +@pytest.mark.parametrize( |
| 628 | + "d_value", |
| 629 | + [ |
| 630 | + {"1": 1.5, "2": "a", "3": {"1": False}}, |
| 631 | + {"1": 1.5, 2: "a", "3": {1: False}}, |
| 632 | + ], |
| 633 | +) |
| 634 | +def test_dict_source_int_and_str_types(d_value): |
| 635 | + data_type = DataType.create(d_value) |
| 636 | + |
| 637 | + def custom_assert(x, y): |
| 638 | + assert x == y |
| 639 | + assert len(x) == len(y) |
| 640 | + assert isinstance(x, dict) |
| 641 | + assert isinstance(y, dict) |
| 642 | + |
| 643 | + artifacts = data_write_read_check( |
| 644 | + data_type, |
| 645 | + reader_type=DictReader, |
| 646 | + custom_assert=custom_assert, |
| 647 | + ) |
| 648 | + |
| 649 | + assert list(artifacts.keys()) == ["1/data", "2/data", "3/1/data"] |
| 650 | + assert artifacts["1/data"].uri.endswith("data/1") |
| 651 | + assert artifacts["2/data"].uri.endswith("data/2") |
| 652 | + assert artifacts["3/1/data"].uri.endswith("data/3/1") |
0 commit comments