diff options
Diffstat (limited to 'tests/unit/mixins/test_mixin_methods.py')
| -rw-r--r-- | tests/unit/mixins/test_mixin_methods.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/unit/mixins/test_mixin_methods.py b/tests/unit/mixins/test_mixin_methods.py index c40ccbd..9121453 100644 --- a/tests/unit/mixins/test_mixin_methods.py +++ b/tests/unit/mixins/test_mixin_methods.py @@ -45,9 +45,52 @@ def test_get_mixin(gl): assert isinstance(obj, FakeObject) assert obj.foo == "bar" assert obj.id == 42 + assert obj._lazy is False assert responses.assert_call_count(url, 1) is True +def test_get_mixin_lazy(gl): + class M(GetMixin, FakeManager): + pass + + url = "http://localhost/api/v4/tests/42" + + mgr = M(gl) + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: + rsps.add( + method=responses.GET, + url=url, + json={"id": 42, "foo": "bar"}, + status=200, + match=[responses.matchers.query_param_matcher({})], + ) + obj = mgr.get(42, lazy=True) + assert isinstance(obj, FakeObject) + assert not hasattr(obj, "foo") + assert obj.id == 42 + assert obj._lazy is True + # a `lazy` get does not make a network request + assert not rsps.calls + + +def test_get_mixin_lazy_missing_attribute(gl): + class FakeGetManager(GetMixin, FakeManager): + pass + + manager = FakeGetManager(gl) + obj = manager.get(1, lazy=True) + assert obj.id == 1 + with pytest.raises(AttributeError) as exc: + obj.missing_attribute + # undo `textwrap.fill()` + message = str(exc.value).replace("\n", " ") + assert "'FakeObject' object has no attribute 'missing_attribute'" in message + assert ( + "note that <class 'tests.unit.mixins.test_mixin_methods.FakeObject'> was " + "created as a `lazy` object and was not initialized with any data." + ) in message + + @responses.activate def test_head_mixin(gl): class M(GetMixin, FakeManager): |
