Exemple #1
0
 def patch_test(new_value: Any):
     event1 = ModelChangedEvent(d, root1, 'foo', new_value)
     patch1 = patch_doc.create([event1]).content
     d.apply_json_patch(patch1)
     if isinstance(new_value, dict):
         return root1.lookup('foo').get_value(root1)
     else:
         return root1.foo
Exemple #2
0
    def test_patch_reference_property(self) -> None:
        d = document.Document()
        assert not d.roots
        assert len(d.models) == 0
        root1 = SomeModelInTestDocument(foo=42)
        root2 = SomeModelInTestDocument(foo=43)
        child1 = SomeModelInTestDocument(foo=44)
        child2 = SomeModelInTestDocument(foo=45)
        child3 = SomeModelInTestDocument(foo=46, child=child2)
        root1.child = child1
        root2.child = child1
        d.add_root(root1)
        d.add_root(root2)
        assert len(d.roots) == 2

        assert child1.id in d.models
        assert child2.id not in d.models
        assert child3.id not in d.models

        event1 = ModelChangedEvent(d, root1, 'child', child3)
        patch1 = patch_doc.create([event1]).content
        d.apply_json_patch(patch1)

        assert root1.child.id == child3.id
        assert root1.child.child.id == child2.id
        assert child1.id in d.models
        assert child2.id in d.models
        assert child3.id in d.models

        # put it back how it was before
        event2 = ModelChangedEvent(d, root1, 'child', child1)
        patch2 = patch_doc.create([event2]).content
        d.apply_json_patch(patch2)

        assert root1.child.id == child1.id
        assert root1.child.child is None

        assert child1.id in d.models
        assert child2.id not in d.models
        assert child3.id not in d.models
Exemple #3
0
    def test_patch_integer_property(self) -> None:
        d = document.Document()
        assert not d.roots
        assert len(d.models) == 0
        root1 = SomeModelInTestDocument(foo=42)
        root2 = SomeModelInTestDocument(foo=43)
        child1 = SomeModelInTestDocument(foo=44)
        root1.child = child1
        root2.child = child1
        d.add_root(root1)
        d.add_root(root2)
        assert len(d.roots) == 2

        event1 = ModelChangedEvent(d, root1, 'foo', 57)
        patch1 = patch_doc.create([event1]).content
        d.apply_json_patch(patch1)

        assert root1.foo == 57

        event2 = ModelChangedEvent(d, child1, 'foo', 67)
        patch2 = patch_doc.create([event2]).content
        d.apply_json_patch(patch2)

        assert child1.foo == 67
Exemple #4
0
    def test_patch_two_properties_at_once(self) -> None:
        d = document.Document()
        assert not d.roots
        assert len(d.models) == 0
        root1 = SomeModelInTestDocument(foo=42)
        child1 = SomeModelInTestDocument(foo=43)
        root1.child = child1
        d.add_root(root1)
        assert len(d.roots) == 1
        assert root1.child == child1
        assert root1.foo == 42
        assert root1.child.foo == 43

        child2 = SomeModelInTestDocument(foo=44)

        event1 = ModelChangedEvent(d, root1, 'foo', 57)
        event2 = ModelChangedEvent(d, root1, 'child', child2)
        patch1 = patch_doc.create([event1, event2]).content
        d.apply_json_patch(patch1)

        assert root1.foo == 57
        assert root1.child.foo == 44