Ejemplo n.º 1
0
    def test_transform_update(self):
        class LisDoc(Document):
            foo = ListField(StringField())

        class DicDoc(Document):
            dictField = DictField()

        class Doc(Document):
            pass

        LisDoc.drop_collection()
        DicDoc.drop_collection()
        Doc.drop_collection()

        DicDoc().save()
        doc = Doc().save()

        for k, v in (("set", "$set"), ("set_on_insert", "$setOnInsert"), ("push", "$push")):
            update = transform.update(DicDoc, **{"%s__dictField__test" % k: doc})
            self.assertIsInstance(update[v]["dictField.test"], dict)

        # Update special cases
        update = transform.update(DicDoc, unset__dictField__test=doc)
        self.assertEqual(update["$unset"]["dictField.test"], 1)

        update = transform.update(DicDoc, pull__dictField__test=doc)
        self.assertIsInstance(update["$pull"]["dictField"]["test"], dict)

        update = transform.update(LisDoc, pull__foo__in=['a'])
        self.assertEqual(update, {'$pull': {'foo': {'$in': ['a']}}})
Ejemplo n.º 2
0
    def test_geojson_PolygonField(self):
        class Location(Document):
            poly = PolygonField()

        update = transform.update(Location,
                                  set__poly=[[[40, 5], [40, 6], [41, 6],
                                              [40, 5]]])
        assert update == {
            "$set": {
                "poly": {
                    "type": "Polygon",
                    "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]],
                }
            }
        }

        update = transform.update(
            Location,
            set__poly={
                "type": "Polygon",
                "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]],
            },
        )
        assert update == {
            "$set": {
                "poly": {
                    "type": "Polygon",
                    "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]],
                }
            }
        }
Ejemplo n.º 3
0
    def test_geojson_LineStringField(self):
        class Location(Document):
            line = LineStringField()

        update = transform.update(Location, set__line=[[1, 2], [2, 2]])
        assert update == {
            "$set": {
                "line": {
                    "type": "LineString",
                    "coordinates": [[1, 2], [2, 2]]
                }
            }
        }

        update = transform.update(Location,
                                  set__line={
                                      "type": "LineString",
                                      "coordinates": [[1, 2], [2, 2]]
                                  })
        assert update == {
            "$set": {
                "line": {
                    "type": "LineString",
                    "coordinates": [[1, 2], [2, 2]]
                }
            }
        }
Ejemplo n.º 4
0
    def test_transform_update(self):
        class DicDoc(Document):
            dictField = DictField()

        class Doc(Document):
            pass

        DicDoc.drop_collection()
        Doc.drop_collection()

        doc = Doc().save()
        dic_doc = DicDoc().save()

        for k, v in (("set", "$set"), ("set_on_insert", "$setOnInsert"),
                     ("push", "$push")):
            update = transform.update(DicDoc,
                                      **{"%s__dictField__test" % k: doc})
            self.assertTrue(isinstance(update[v]["dictField.test"], dict))

        # Update special cases
        update = transform.update(DicDoc, unset__dictField__test=doc)
        self.assertEqual(update["$unset"]["dictField.test"], 1)

        update = transform.update(DicDoc, pull__dictField__test=doc)
        self.assertTrue(isinstance(update["$pull"]["dictField"]["test"], dict))
Ejemplo n.º 5
0
    def test_update_pull_for_list_fields(self):
        """
        Test added to check pull operation in update for
        EmbeddedDocumentListField which is inside a EmbeddedDocumentField
        """

        class Word(EmbeddedDocument):
            word = StringField()
            index = IntField()

        class SubDoc(EmbeddedDocument):
            heading = ListField(StringField())
            text = EmbeddedDocumentListField(Word)

        class MainDoc(Document):
            title = StringField()
            content = EmbeddedDocumentField(SubDoc)

        word = Word(word="abc", index=1)
        update = transform.update(MainDoc, pull__content__text=word)
        assert update == {
            "$pull": {"content.text": SON([("word", "abc"), ("index", 1)])}
        }

        update = transform.update(MainDoc, pull__content__heading="xyz")
        assert update == {"$pull": {"content.heading": "xyz"}}

        update = transform.update(MainDoc, pull__content__text__word__in=["foo", "bar"])
        assert update == {"$pull": {"content.text": {"word": {"$in": ["foo", "bar"]}}}}

        update = transform.update(
            MainDoc, pull__content__text__word__nin=["foo", "bar"]
        )
        assert update == {"$pull": {"content.text": {"word": {"$nin": ["foo", "bar"]}}}}
Ejemplo n.º 6
0
    def test_geojson_LineStringField(self):
        class Location(Document):
            line = LineStringField()

        update = transform.update(Location, set__line=[[1, 2], [2, 2]])
        self.assertEqual(
            update, {
                '$set': {
                    'line': {
                        "type": "LineString",
                        "coordinates": [[1, 2], [2, 2]]
                    }
                }
            })

        update = transform.update(Location,
                                  set__line={
                                      "type": "LineString",
                                      "coordinates": [[1, 2], [2, 2]]
                                  })
        self.assertEqual(
            update, {
                '$set': {
                    'line': {
                        "type": "LineString",
                        "coordinates": [[1, 2], [2, 2]]
                    }
                }
            })
Ejemplo n.º 7
0
    def test_geojson_PolygonField(self):
        class Location(Document):
            poly = PolygonField()

        update = transform.update(Location,
                                  set__poly=[[[40, 5], [40, 6], [41, 6],
                                              [40, 5]]])
        self.assertEqual(
            update, {
                '$set': {
                    'poly': {
                        "type": "Polygon",
                        "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]]
                    }
                }
            })

        update = transform.update(Location,
                                  set__poly={
                                      "type":
                                      "Polygon",
                                      "coordinates": [[[40, 5], [40, 6],
                                                       [41, 6], [40, 5]]]
                                  })
        self.assertEqual(
            update, {
                '$set': {
                    'poly': {
                        "type": "Polygon",
                        "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]]
                    }
                }
            })
Ejemplo n.º 8
0
    def test_geojson_PointField(self):
        class Location(Document):
            loc = PointField()

        update = transform.update(Location, set__loc=[1, 2])
        self.assertEqual(
            update,
            {'$set': {
                'loc': {
                    "type": "Point",
                    "coordinates": [1, 2]
                }
            }})

        update = transform.update(Location,
                                  set__loc={
                                      "type": "Point",
                                      "coordinates": [1, 2]
                                  })
        self.assertEqual(
            update,
            {'$set': {
                'loc': {
                    "type": "Point",
                    "coordinates": [1, 2]
                }
            }})
Ejemplo n.º 9
0
    def test_transform_update(self):
        class LisDoc(Document):
            foo = ListField(StringField())

        class DicDoc(Document):
            dictField = DictField()

        class Doc(Document):
            pass

        LisDoc.drop_collection()
        DicDoc.drop_collection()
        Doc.drop_collection()

        DicDoc().save()
        doc = Doc().save()

        for k, v in (("set", "$set"), ("set_on_insert", "$setOnInsert"),
                     ("push", "$push")):
            update = transform.update(DicDoc,
                                      **{"%s__dictField__test" % k: doc})
            self.assertIsInstance(update[v]["dictField.test"], dict)

        # Update special cases
        update = transform.update(DicDoc, unset__dictField__test=doc)
        self.assertEqual(update["$unset"]["dictField.test"], 1)

        update = transform.update(DicDoc, pull__dictField__test=doc)
        self.assertIsInstance(update["$pull"]["dictField"]["test"], dict)

        update = transform.update(LisDoc, pull__foo__in=['a'])
        self.assertEqual(update, {'$pull': {'foo': {'$in': ['a']}}})
Ejemplo n.º 10
0
    def test_update_pull_for_list_fields(self):
        """
        Test added to check pull operation in update for
        EmbeddedDocumentListField which is inside a EmbeddedDocumentField
        """
        class Word(EmbeddedDocument):
            word = StringField()
            index = IntField()

        class SubDoc(EmbeddedDocument):
            heading = ListField(StringField())
            text = EmbeddedDocumentListField(Word)

        class MainDoc(Document):
            title = StringField()
            content = EmbeddedDocumentField(SubDoc)

        word = Word(word='abc', index=1)
        update = transform.update(MainDoc, pull__content__text=word)
        self.assertEqual(
            update,
            {'$pull': {
                'content.text': SON([('word', u'abc'), ('index', 1)])
            }})

        update = transform.update(MainDoc, pull__content__heading='xyz')
        self.assertEqual(update, {'$pull': {'content.heading': 'xyz'}})
Ejemplo n.º 11
0
    def test_geojson_PointField(self):
        class Location(Document):
            loc = PointField()

        update = transform.update(Location, set__loc=[1, 2])
        assert update == {
            "$set": {
                "loc": {
                    "type": "Point",
                    "coordinates": [1, 2]
                }
            }
        }

        update = transform.update(Location,
                                  set__loc={
                                      "type": "Point",
                                      "coordinates": [1, 2]
                                  })
        assert update == {
            "$set": {
                "loc": {
                    "type": "Point",
                    "coordinates": [1, 2]
                }
            }
        }
Ejemplo n.º 12
0
    def test_transform_update(self):
        class LisDoc(Document):
            foo = ListField(StringField())

        class DicDoc(Document):
            dictField = DictField()

        class Doc(Document):
            pass

        LisDoc.drop_collection()
        DicDoc.drop_collection()
        Doc.drop_collection()

        DicDoc().save()
        doc = Doc().save()

        for k, v in (
            ("set", "$set"),
            ("set_on_insert", "$setOnInsert"),
            ("push", "$push"),
        ):
            update = transform.update(DicDoc,
                                      **{"%s__dictField__test" % k: doc})
            assert isinstance(update[v]["dictField.test"], dict)

        # Update special cases
        update = transform.update(DicDoc, unset__dictField__test=doc)
        assert update["$unset"]["dictField.test"] == 1

        update = transform.update(DicDoc, pull__dictField__test=doc)
        assert isinstance(update["$pull"]["dictField"]["test"], dict)

        update = transform.update(LisDoc, pull__foo__in=["a"])
        assert update == {"$pull": {"foo": {"$in": ["a"]}}}
Ejemplo n.º 13
0
    def test_geojson_LineStringField(self):
        class Location(Document):
            line = LineStringField()

        update = transform.update(Location, set__line=[[1, 2], [2, 2]])
        self.assertEqual(update, {'$set': {'line': {"type": "LineString", "coordinates": [[1, 2], [2, 2]]}}})

        update = transform.update(Location, set__line={"type": "LineString", "coordinates": [[1, 2], [2, 2]]})
        self.assertEqual(update, {'$set': {'line': {"type": "LineString", "coordinates": [[1, 2], [2, 2]]}}})
Ejemplo n.º 14
0
    def test_geojson_PointField(self):
        class Location(Document):
            loc = PointField()

        update = transform.update(Location, set__loc=[1, 2])
        self.assertEqual(update, {'$set': {'loc': {"type": "Point", "coordinates": [1, 2]}}})

        update = transform.update(Location, set__loc={"type": "Point", "coordinates": [1, 2]})
        self.assertEqual(update, {'$set': {'loc': {"type": "Point", "coordinates": [1, 2]}}})
Ejemplo n.º 15
0
    def test_geojson_PolygonField(self):
        class Location(Document):
            poly = PolygonField()

        update = transform.update(Location, set__poly=[[[40, 5], [40, 6], [41, 6], [40, 5]]])
        self.assertEqual(update, {'$set': {'poly': {"type": "Polygon", "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]]}}})

        update = transform.update(Location, set__poly={"type": "Polygon", "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]]})
        self.assertEqual(update, {'$set': {'poly': {"type": "Polygon", "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]]}}})
Ejemplo n.º 16
0
    def test_transform_update_push(self):
        """Ensure the differences in behvaior between 'push' and 'push_all'"""
        class BlogPost(Document):
            tags = ListField(StringField())

        update = transform.update(BlogPost, push__tags=['mongo', 'db'])
        self.assertEqual(update, {'$push': {'tags': ['mongo', 'db']}})

        update = transform.update(BlogPost, push_all__tags=['mongo', 'db'])
        self.assertEqual(update, {'$push': {'tags': {'$each': ['mongo', 'db']}}})
Ejemplo n.º 17
0
    def test_transform_update_push(self):
        """Ensure the differences in behvaior between 'push' and 'push_all'"""
        class BlogPost(Document):
            tags = ListField(StringField())

        update = transform.update(BlogPost, push__tags=['mongo', 'db'])
        self.assertEqual(update, {'$push': {'tags': ['mongo', 'db']}})

        update = transform.update(BlogPost, push_all__tags=['mongo', 'db'])
        self.assertEqual(update, {'$push': {'tags': {'$each': ['mongo', 'db']}}})
Ejemplo n.º 18
0
    def test_transform_update_push(self):
        """Ensure the differences in behvaior between 'push' and 'push_all'"""
        class BlogPost(Document):
            tags = ListField(StringField())

        update = transform.update(BlogPost, push__tags=["mongo", "db"])
        assert update == {"$push": {"tags": ["mongo", "db"]}}

        update = transform.update(BlogPost, push_all__tags=["mongo", "db"])
        assert update == {"$push": {"tags": {"$each": ["mongo", "db"]}}}
Ejemplo n.º 19
0
    def test_transform_update_no_operator_default_to_set(self):
        """Ensure the differences in behvaior between 'push' and 'push_all'"""
        class BlogPost(Document):
            tags = ListField(StringField())

        update = transform.update(BlogPost, tags=['mongo', 'db'])
        self.assertEqual(update, {'$set': {'tags': ['mongo', 'db']}})
Ejemplo n.º 20
0
    def test_transform_update_no_operator_default_to_set(self):
        """Ensure the differences in behvaior between 'push' and 'push_all'"""
        class BlogPost(Document):
            tags = ListField(StringField())

        update = transform.update(BlogPost, tags=["mongo", "db"])
        assert update == {"$set": {"tags": ["mongo", "db"]}}
Ejemplo n.º 21
0
    def test_update_pull_for_list_fields(self):
        """
        Test added to check pull operation in update for
        EmbeddedDocumentListField which is inside a EmbeddedDocumentField
        """
        class Word(EmbeddedDocument):
            word = StringField()
            index = IntField()

        class SubDoc(EmbeddedDocument):
            heading = ListField(StringField())
            text = EmbeddedDocumentListField(Word)

        class MainDoc(Document):
            title = StringField()
            content = EmbeddedDocumentField(SubDoc)

        word = Word(word='abc', index=1)
        update = transform.update(MainDoc, pull__content__text=word)
        self.assertEqual(update, {'$pull': {'content.text': SON([('word', u'abc'), ('index', 1)])}})

        update = transform.update(MainDoc, pull__content__heading='xyz')
        self.assertEqual(update, {'$pull': {'content.heading': 'xyz'}})
Ejemplo n.º 22
0
    def test_transform_update(self):
        class DicDoc(Document):
            dictField = DictField()

        class Doc(Document):
            pass

        DicDoc.drop_collection()
        Doc.drop_collection()

        doc = Doc().save()
        dic_doc = DicDoc().save()

        for k, v in (("set", "$set"), ("set_on_insert", "$setOnInsert"), ("push", "$push")):
            update = transform.update(DicDoc, **{"%s__dictField__test" % k: doc})
            self.assertTrue(isinstance(update[v]["dictField.test"], dict))

        # Update special cases
        update = transform.update(DicDoc, unset__dictField__test=doc)
        self.assertEqual(update["$unset"]["dictField.test"], 1)

        update = transform.update(DicDoc, pull__dictField__test=doc)
        self.assertTrue(isinstance(update["$pull"]["dictField"]["test"], dict))