|
| 1 | +from collections import OrderedDict |
| 2 | + |
| 3 | +import niquests |
| 4 | +import pytest |
| 5 | + |
| 6 | +from seam.client import SeamHttpClient |
| 7 | +from seam.null import NULL, Null, is_null, replace_null |
| 8 | + |
| 9 | + |
| 10 | +def test_null_is_a_singleton(): |
| 11 | + assert Null() is NULL |
| 12 | + assert is_null(NULL) |
| 13 | + assert is_null(Null()) |
| 14 | + |
| 15 | + |
| 16 | +def test_null_is_not_none(): |
| 17 | + assert NULL is not None |
| 18 | + assert not is_null(None) |
| 19 | + assert not is_null("") |
| 20 | + assert not is_null(0) |
| 21 | + |
| 22 | + |
| 23 | +def test_null_is_falsy(): |
| 24 | + assert not NULL |
| 25 | + |
| 26 | + |
| 27 | +def test_null_repr(): |
| 28 | + assert repr(NULL) == "NULL" |
| 29 | + |
| 30 | + |
| 31 | +def test_replace_null(): |
| 32 | + assert replace_null(NULL) is None |
| 33 | + assert replace_null(None) is None |
| 34 | + assert replace_null("a") == "a" |
| 35 | + assert replace_null(0) == 0 |
| 36 | + assert replace_null(False) is False |
| 37 | + |
| 38 | + |
| 39 | +def test_replace_null_in_dict(): |
| 40 | + assert replace_null({"a": NULL, "b": 1, "c": None}) == { |
| 41 | + "a": None, |
| 42 | + "b": 1, |
| 43 | + "c": None, |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +def test_replace_null_in_nested_dict(): |
| 48 | + assert replace_null({"a": {"b": {"c": NULL}}}) == {"a": {"b": {"c": None}}} |
| 49 | + |
| 50 | + |
| 51 | +def test_replace_null_in_lists_and_tuples(): |
| 52 | + assert replace_null(["a", NULL]) == ["a", None] |
| 53 | + assert replace_null(("a", NULL)) == ("a", None) |
| 54 | + assert replace_null({"a": [{"b": NULL}]}) == {"a": [{"b": None}]} |
| 55 | + |
| 56 | + |
| 57 | +def test_replace_null_does_not_modify_the_given_value(): |
| 58 | + params = {"a": NULL, "b": [NULL]} |
| 59 | + replace_null(params) |
| 60 | + |
| 61 | + assert params == {"a": NULL, "b": [NULL]} |
| 62 | + |
| 63 | + |
| 64 | +def test_replace_null_normalizes_mappings_to_dicts(): |
| 65 | + result = replace_null(OrderedDict([("a", NULL)])) |
| 66 | + |
| 67 | + assert result == {"a": None} |
| 68 | + |
| 69 | + |
| 70 | +class StubResponse: |
| 71 | + status_code = 200 |
| 72 | + headers = {"content-type": "application/json"} |
| 73 | + |
| 74 | + def json(self): |
| 75 | + return {} |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture(name="sent_payloads") |
| 79 | +def sent_payloads_fixture(monkeypatch): |
| 80 | + payloads = [] |
| 81 | + |
| 82 | + # pylint: disable=unused-argument |
| 83 | + def request(self, method, url, *args, **kwargs): |
| 84 | + payloads.append(kwargs.get("json")) |
| 85 | + return StubResponse() |
| 86 | + |
| 87 | + monkeypatch.setattr(niquests.Session, "request", request) |
| 88 | + |
| 89 | + return payloads |
| 90 | + |
| 91 | + |
| 92 | +def test_client_sends_null_params_as_json_null(sent_payloads): |
| 93 | + client = SeamHttpClient(base_url="https://example.com", auth_headers={}) |
| 94 | + client.post("/devices/update", json={"device_id": "a", "name": NULL}) |
| 95 | + |
| 96 | + assert sent_payloads == [{"device_id": "a", "name": None}] |
| 97 | + |
| 98 | + |
| 99 | +def test_client_sends_nested_null_params_as_json_null(sent_payloads): |
| 100 | + client = SeamHttpClient(base_url="https://example.com", auth_headers={}) |
| 101 | + client.post("/spaces/update", json={"customer_data": {"check_in": NULL}}) |
| 102 | + |
| 103 | + assert sent_payloads == [{"customer_data": {"check_in": None}}] |
| 104 | + |
| 105 | + |
| 106 | +def test_client_passes_through_payloads_without_null_params(sent_payloads): |
| 107 | + client = SeamHttpClient(base_url="https://example.com", auth_headers={}) |
| 108 | + client.post("/devices/update", json={"device_id": "a", "name": "Front Door"}) |
| 109 | + |
| 110 | + assert sent_payloads == [{"device_id": "a", "name": "Front Door"}] |
0 commit comments