|
| 1 | +import pytest |
| 2 | + |
| 3 | +from seam import Seam |
1 | 4 | from seam.deep_attr_dict import DeepAttrDict |
| 5 | +from seam.resources import seam_event_from_dict |
2 | 6 |
|
3 | 7 |
|
4 | 8 | def test_deep_attr_dict(): |
5 | 9 | attrdict = DeepAttrDict({"a": {"b": {"c": 5}}}) |
6 | 10 |
|
7 | | - assert attrdict.a.b.c == 5 |
| 11 | + assert attrdict.a.b.c == 5 # pylint: disable=no-member |
| 12 | + |
| 13 | + |
| 14 | +def test_nested_dicts_keep_attribute_access(): |
| 15 | + attrdict = DeepAttrDict() |
| 16 | + attrdict.a = {"b": {"c": 5}} |
| 17 | + |
| 18 | + assert attrdict.a.b.c == 5 # pylint: disable=no-member |
| 19 | + assert attrdict["a"]["b"]["c"] == 5 |
| 20 | + |
| 21 | + |
| 22 | +def test_reading_a_missing_key_raises_key_error(): |
| 23 | + attrdict = DeepAttrDict({"reservation_id": "abc"}) |
| 24 | + |
| 25 | + with pytest.raises(KeyError): |
| 26 | + attrdict["reservaton_id"] # pylint: disable=pointless-statement |
| 27 | + |
| 28 | + |
| 29 | +def test_reading_a_missing_attribute_raises_attribute_error(): |
| 30 | + attrdict = DeepAttrDict({"reservation_id": "abc"}) |
| 31 | + |
| 32 | + with pytest.raises(AttributeError): |
| 33 | + attrdict.reservaton_id # pylint: disable=pointless-statement |
| 34 | + |
| 35 | + |
| 36 | +def test_reading_a_missing_key_does_not_insert_it(): |
| 37 | + attrdict = DeepAttrDict({"reservation_id": "abc"}) |
| 38 | + |
| 39 | + with pytest.raises(AttributeError): |
| 40 | + attrdict.reservaton_id # pylint: disable=pointless-statement |
| 41 | + |
| 42 | + assert "reservaton_id" not in attrdict |
| 43 | + assert len(attrdict) == 1 |
| 44 | + assert dict(attrdict) == {"reservation_id": "abc"} |
| 45 | + |
| 46 | + |
| 47 | +def test_missing_keys_work_with_standard_probes(): |
| 48 | + attrdict = DeepAttrDict({"reservation_id": "abc"}) |
| 49 | + |
| 50 | + assert not hasattr(attrdict, "reservaton_id") |
| 51 | + assert getattr(attrdict, "reservaton_id", None) is None |
| 52 | + assert attrdict.get("reservaton_id") is None |
| 53 | + assert "reservaton_id" not in attrdict |
| 54 | + |
| 55 | + |
| 56 | +def test_custom_metadata_reads_do_not_mutate_the_device(recording_server): |
| 57 | + device_payload = { |
| 58 | + "device": { |
| 59 | + "device_id": "44444444-4444-4444-4444-444444444444", |
| 60 | + "custom_metadata": {"reservation_id": "abc"}, |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + with recording_server([(200, device_payload)]) as (endpoint, _): |
| 65 | + seam = Seam.from_api_key("seam_apikey_token", endpoint=endpoint) |
| 66 | + device = seam.devices.get(device_id="44444444-4444-4444-4444-444444444444") |
| 67 | + |
| 68 | + with pytest.raises(AttributeError): |
| 69 | + device.custom_metadata.reservaton_id # pylint: disable=pointless-statement |
| 70 | + |
| 71 | + # The typo'd read leaves no key behind to re-serialize to the API. |
| 72 | + assert dict(device.custom_metadata) == {"reservation_id": "abc"} |
| 73 | + |
| 74 | + |
| 75 | +def test_unknown_event_fallback_fields_stay_readable(): |
| 76 | + event = seam_event_from_dict( |
| 77 | + {"event_id": "e", "event_type": "unknown.event", "foo": {"bar": 1}} |
| 78 | + ) |
| 79 | + |
| 80 | + assert event.event_id == "e" |
| 81 | + assert event.foo.bar == 1 |
0 commit comments