@@ -45,9 +45,52 @@ class M(GetMixin, FakeManager):
4545 assert isinstance (obj , FakeObject )
4646 assert obj .foo == "bar"
4747 assert obj .id == 42
48+ assert obj ._lazy is False
4849 assert responses .assert_call_count (url , 1 ) is True
4950
5051
52+ def test_get_mixin_lazy (gl ):
53+ class M (GetMixin , FakeManager ):
54+ pass
55+
56+ url = "http://localhost/api/v4/tests/42"
57+
58+ mgr = M (gl )
59+ with responses .RequestsMock (assert_all_requests_are_fired = False ) as rsps :
60+ rsps .add (
61+ method = responses .GET ,
62+ url = url ,
63+ json = {"id" : 42 , "foo" : "bar" },
64+ status = 200 ,
65+ match = [responses .matchers .query_param_matcher ({})],
66+ )
67+ obj = mgr .get (42 , lazy = True )
68+ assert isinstance (obj , FakeObject )
69+ assert not hasattr (obj , "foo" )
70+ assert obj .id == 42
71+ assert obj ._lazy is True
72+ # a `lazy` get does not make a network request
73+ assert not rsps .calls
74+
75+
76+ def test_get_mixin_lazy_missing_attribute (gl ):
77+ class FakeGetManager (GetMixin , FakeManager ):
78+ pass
79+
80+ manager = FakeGetManager (gl )
81+ obj = manager .get (1 , lazy = True )
82+ assert obj .id == 1
83+ with pytest .raises (AttributeError ) as exc :
84+ obj .missing_attribute
85+ # undo `textwrap.fill()`
86+ message = str (exc .value ).replace ("\n " , " " )
87+ assert "'FakeObject' object has no attribute 'missing_attribute'" in message
88+ assert (
89+ "note that <class 'tests.unit.mixins.test_mixin_methods.FakeObject'> was "
90+ "created as a `lazy` object and was not initialized with any data."
91+ ) in message
92+
93+
5194@responses .activate
5295def test_head_mixin (gl ):
5396 class M (GetMixin , FakeManager ):
0 commit comments