Example #1
0
    def test_patch_set(self):
        objects = [
            PatchingDumbDocument.objects.create(name="dumb1",
                                                int_fld=1,
                                                lst_fld=['a', 'b', 'c'],
                                                emb=DumbEmbedded(name="emb1")),
            PatchingDumbDocument.objects.create(name="dumb2",
                                                int_fld=2,
                                                lst_fld=['b', 'c', 'd'],
                                                emb=DumbEmbedded(name="emb2")),
            PatchingDumbDocument.objects.create(name="dumb3",
                                                int_fld=3,
                                                lst_fld=['d', 'e', 'f'],
                                                emb=DumbEmbedded(name="emb3"))
        ]

        patch = Patch(data=[{
            'path': '/int_fld',
            'op': 'inc',
            'value': 100
        }, {
            'path': '/lst_fld',
            'op': 'push',
            'value': 'z'
        }, {
            'path': '/emb/name',
            'op': 'set',
            'value': "Foo"
        }])

        assert patch.is_valid(), patch.errors

        queryset = PatchingDumbDocument.objects.all()
        patch.update_queryset(queryset)

        for o in objects:
            o.reload()
        assert [o.int_fld for o in objects] == [101, 102, 103]
        assert [o.lst_fld for o in objects] == [['a', 'b', 'c', 'z'],
                                                ['b', 'c', 'd', 'z'],
                                                ['d', 'e', 'f', 'z']]
        assert [o.emb.name for o in objects] == ["Foo", "Foo", "Foo"]
    def test_patch_set(self):
        objects = [
            PatchingDumbDocument.objects.create(name="dumb1", int_fld=1, lst_fld=['a', 'b', 'c'], emb=DumbEmbedded(name="emb1")),
            PatchingDumbDocument.objects.create(name="dumb2", int_fld=2, lst_fld=['b', 'c', 'd'], emb=DumbEmbedded(name="emb2")),
            PatchingDumbDocument.objects.create(name="dumb3", int_fld=3, lst_fld=['d', 'e', 'f'], emb=DumbEmbedded(name="emb3"))
        ]

        patch = Patch(data=[{'path': '/int_fld', 'op': 'inc', 'value': 100},
                            {'path': '/lst_fld', 'op': 'push', 'value': 'z'},
                            {'path': '/emb/name', 'op': 'set', 'value': "Foo"}])

        assert patch.is_valid(), patch.errors

        queryset = PatchingDumbDocument.objects.all()
        patch.update_queryset(queryset)

        for o in objects:
            o.reload()
        assert [o.int_fld for o in objects] == [101, 102, 103]
        assert [o.lst_fld for o in objects] == [['a', 'b', 'c', 'z'], ['b', 'c', 'd', 'z'], ['d', 'e', 'f', 'z']]
        assert [o.emb.name for o in objects] == ["Foo", "Foo", "Foo"]
Example #3
0
    def test_patch_matched(self):
        objects = [
            PatchingDumbDocument.objects.create(
                name="dumb1",
                emb_lst=[
                    DumbEmbedded(name="dumb1emb1", foo=11),
                    DumbEmbedded(name="dumb1emb2", foo=12),
                    DumbEmbedded(name="dumb1emb3", foo=13)
                ]),
            PatchingDumbDocument.objects.create(
                name="dumb2",
                emb_lst=[
                    DumbEmbedded(name="dumb2emb1", foo=21),
                    DumbEmbedded(name="dumb2emb2", foo=22),
                    DumbEmbedded(name="dumb2emb3", foo=23)
                ]),
            PatchingDumbDocument.objects.create(
                name="dumb3",
                emb_lst=[
                    DumbEmbedded(name="dumb3emb1", foo=31),
                    DumbEmbedded(name="dumb3emb2", foo=32),
                    DumbEmbedded(name="dumb3emb3", foo=33)
                ]),
        ]

        patch = Patch(data=[{
            'path': "/emb_lst/S/name",
            'op': 'set',
            'value': "winner"
        }])
        assert patch.is_valid(), patch.errors

        queryset = PatchingDumbDocument.objects.filter(emb_lst__foo=22)
        patch.update_queryset(queryset)
        for o in objects:
            o.reload()

        for o in objects:
            for e in o.emb_lst:
                assert e.foo != 22 or e.name == "winner"
    def test_patch_matched(self):
        objects = [
            PatchingDumbDocument.objects.create(
                name="dumb1",
                emb_lst=[
                    DumbEmbedded(name="dumb1emb1", foo=11),
                    DumbEmbedded(name="dumb1emb2", foo=12),
                    DumbEmbedded(name="dumb1emb3", foo=13)
                ]
            ),
            PatchingDumbDocument.objects.create(
                name="dumb2",
                emb_lst=[
                    DumbEmbedded(name="dumb2emb1", foo=21),
                    DumbEmbedded(name="dumb2emb2", foo=22),
                    DumbEmbedded(name="dumb2emb3", foo=23)
                ]
            ),
            PatchingDumbDocument.objects.create(
                name="dumb3",
                emb_lst=[
                    DumbEmbedded(name="dumb3emb1", foo=31),
                    DumbEmbedded(name="dumb3emb2", foo=32),
                    DumbEmbedded(name="dumb3emb3", foo=33)
                ]
            ),
        ]

        patch = Patch(data=[{'path': "/emb_lst/S/name", 'op': 'set', 'value': "winner"}])
        assert patch.is_valid(), patch.errors

        queryset = PatchingDumbDocument.objects.filter(emb_lst__foo=22)
        patch.update_queryset(queryset)
        for o in objects:
            o.reload()

        for o in objects:
            for e in o.emb_lst:
                assert e.foo != 22 or e.name == "winner"
    def test_patch_obj(self):
        objects = [
            PatchingDumbDocument.objects.create(name="dumb1", int_fld=1, lst_fld=['a', 'b', 'c'], emb=DumbEmbedded(name="emb1")),
            PatchingDumbDocument.objects.create(name="dumb2", int_fld=2, lst_fld=['b', 'c', 'd'], emb=DumbEmbedded(name="emb2")),
            PatchingDumbDocument.objects.create(name="dumb3", int_fld=3, lst_fld=['d', 'e', 'f'], emb=DumbEmbedded(name="emb3"))
        ]

        patch = Patch(data=[{'path': '/int_fld', 'op': 'inc', 'value': 100},
                            {'path': '/lst_fld', 'op': 'push', 'value': 'z'},
                            {'path': '/dct_fld/foo', 'op': 'set', 'value': "f"},
                            {'path': '/dct_fld/bar', 'op': 'set', 'value': "b"},
                            {'path': '/emb/name', 'op': 'set', 'value': "Foo"}])

        assert patch.is_valid(), patch.errors

        obj = PatchingDumbDocument.objects.get(name="dumb2")
        patch.update_queryset(obj)

        for o in objects:
            o.reload()
        assert [o.int_fld for o in objects] == [1, 102, 3]
        assert [o.lst_fld for o in objects] == [['a', 'b', 'c'], ['b', 'c', 'd', 'z'], ['d', 'e', 'f']]
        assert [o.dct_fld for o in objects] == [{}, {'foo': 'f', 'bar': 'b'}, {}]
        assert [o.emb.name for o in objects] == ["emb1", "Foo", "emb3"]