Skip to content

Commit c06ded2

Browse files
committed
feat(functional): add _parent_ref_attr support for nested managers
Add _parent_ref_attr class attribute to RESTManager that allows nested managers
1 parent 2d5475e commit c06ded2

1 file changed

Lines changed: 98 additions & 1 deletion

File tree

tests/unit/mixins/test_mixin_methods.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ class TestClass(UploadMixin, FakeObject):
584584
url=url,
585585
json={"id": 42, "file_name": "test.txt", "file_content": "testing contents"},
586586
status=200,
587-
match=[responses.matchers.query_param_matcher({})],
588587
)
589588

590589
mgr = FakeManager(gl)
@@ -596,3 +595,101 @@ class TestClass(UploadMixin, FakeObject):
596595
assert res_only_path["file_name"] == "test.txt"
597596
assert res_only_path["file_content"] == "testing contents"
598597
assert responses.assert_call_count(url, 1) is True
598+
599+
600+
class MockParentRefWithIID:
601+
def __init__(self, iid):
602+
self.iid = iid
603+
604+
605+
class MockParentWithRef:
606+
def __init__(self, parent_ref):
607+
self.parent_ref = parent_ref
608+
609+
610+
class MockManagerWithRefAttr(base.RESTManager):
611+
_path = "/tests/{test_id}/refs"
612+
_obj_cls = FakeObject
613+
_from_parent_attrs = {"test_id": "id"}
614+
_parent_ref_attr = "parent_ref"
615+
616+
617+
def test_get_parent_ref_id_no_parent(gl):
618+
class M(MockManagerWithRefAttr):
619+
pass
620+
621+
mgr = M(gl)
622+
assert mgr._get_parent_ref_id() is None
623+
624+
625+
def test_get_parent_ref_id_no_parent_ref_attr(gl):
626+
class M(FakeManager):
627+
pass
628+
629+
mgr = M(gl)
630+
assert mgr._get_parent_ref_id() is None
631+
632+
633+
def test_get_parent_ref_id_parent_has_no_ref_attr(gl):
634+
class M(FakeManager):
635+
_parent_ref_attr = "nonexistent"
636+
637+
parent = MockParentWithRef(None)
638+
mgr = M(gl, parent=parent)
639+
assert mgr._get_parent_ref_id() is None
640+
641+
642+
def test_get_parent_ref_id_parent_ref_is_none(gl):
643+
class M(MockManagerWithRefAttr):
644+
pass
645+
646+
parent = MockParentWithRef(None)
647+
mgr = M(gl, parent=parent)
648+
assert mgr._get_parent_ref_id() is None
649+
650+
651+
def test_get_parent_ref_id_success(gl):
652+
class M(MockManagerWithRefAttr):
653+
pass
654+
655+
parent_ref = MockParentRefWithIID(42)
656+
parent = MockParentWithRef(parent_ref)
657+
mgr = M(gl, parent=parent)
658+
assert mgr._get_parent_ref_id() == 42
659+
660+
661+
def test_get_parent_ref_id_no_iid_attribute(gl):
662+
class MockParentRefNoIID:
663+
pass
664+
665+
class M(MockManagerWithRefAttr):
666+
pass
667+
668+
parent_ref = MockParentRefNoIID()
669+
parent = MockParentWithRef(parent_ref)
670+
mgr = M(gl, parent=parent)
671+
assert mgr._get_parent_ref_id() is None
672+
673+
674+
def test_get_mixin_without_id_raises_error_when_no_parent_ref(gl):
675+
class M(GetMixin, MockManagerWithRefAttr):
676+
pass
677+
678+
mgr = M(gl)
679+
with pytest.raises(ValueError, match="id is required"):
680+
mgr.get()
681+
682+
683+
@responses.activate
684+
def test_update_mixin_without_id_no_parent_ref(gl):
685+
class M(UpdateMixin, FakeManager):
686+
_update_method = UpdateMethod.POST
687+
_obj_cls = FakeObject
688+
689+
url = "http://localhost/api/v4/tests"
690+
responses.add(method=responses.POST, url=url, json={}, status=200)
691+
692+
mgr = M(gl)
693+
result = mgr.update(new_data={"foo": "bar"})
694+
assert isinstance(result, dict)
695+
assert responses.assert_call_count(url, 1) is True

0 commit comments

Comments
 (0)