Exemple #1
0
    def test_index_info(self):
        db = self.db

        yield db.test.drop_indexes()
        yield db.test.remove({})

        db.test.save({})  # create collection
        ix_info = yield db.test.index_information()
        self.assertEqual(len(ix_info), 1)
        self.assertEqual(ix_info["_id_"]["name"], "_id_")

        yield db.test.create_index(qf.sort(qf.ASCENDING("hello")))
        ix_info = yield db.test.index_information()
        self.assertEqual(len(ix_info), 2)
        self.assertEqual(ix_info["hello_1"]["name"], "hello_1")

        yield db.test.create_index(
            qf.sort(qf.DESCENDING("hello") + qf.ASCENDING("world")),
            unique=True,
            sparse=True)
        ix_info = yield db.test.index_information()
        self.assertEqual(ix_info["hello_1"]["name"], "hello_1")
        self.assertEqual(len(ix_info), 3)
        self.assertEqual({
            "hello": -1,
            "world": 1
        }, ix_info["hello_-1_world_1"]["key"])
        self.assertEqual(True, ix_info["hello_-1_world_1"]["unique"])
        self.assertEqual(True, ix_info["hello_-1_world_1"]["sparse"])

        yield db.test.drop_indexes()
        yield db.test.remove({})
Exemple #2
0
def example():
    mongo = yield txmongo.MongoConnection()

    foo = mongo.foo  # `foo` database
    test = foo.test  # `test` collection

    idx = filter.sort(filter.ASCENDING("something") + filter.DESCENDING("else"))
    print "IDX:", idx

    result = yield test.create_index(idx)
    print "create_index:", result

    result = yield test.index_information()
    print "index_information:", result

    result = yield test.drop_index(idx)
    print "drop_index:", result

    # Geohaystack example
    geoh_idx = filter.sort(filter.GEOHAYSTACK("loc") + filter.ASCENDING("type"))
    print "IDX:", geoh_idx
    result = yield test.create_index(geoh_idx, **{'bucketSize':1})
    print "index_information:", result

    result = yield test.drop_index(geoh_idx)
    print "drop_index:", result

    # 2D geospatial index
    geo_idx = filter.sort(filter.GEO2D("pos"))
    print "IDX:", geo_idx
    result = yield test.create_index(geo_idx, **{ 'min':-100, 'max':100 })
    print "index_information:", result

    result = yield test.drop_index(geo_idx)
    print "drop_index:", result
Exemple #3
0
    def test_index_info(self):
        db = self.db

        yield db.test.drop_indexes()
        yield db.test.remove({})

        db.test.save({})  # create collection
        ix_info = yield db.test.index_information()
        self.assertEqual(len(ix_info), 1)

        self.assert_("_id_" in ix_info)

        yield db.test.create_index(filter.sort(filter.ASCENDING("hello")))
        ix_info = yield db.test.index_information()
        self.assertEqual(len(ix_info), 2)

        self.assertEqual(ix_info["hello_1"], [("hello", 1)])

        yield db.test.create_index(filter.sort(
            filter.DESCENDING("hello") + filter.ASCENDING("world")),
                                   unique=True)
        ix_info = yield db.test.index_information()

        self.assertEqual(ix_info["hello_1"], [("hello", 1)])
        self.assertEqual(len(ix_info), 3)
        self.assertEqual([("world", 1), ("hello", -1)],
                         ix_info["hello_-1_world_1"])
        # Unique key will not show until index_information is updated with changes introduced in version 1.7
        #self.assertEqual(True, ix_info["hello_-1_world_1"]["unique"])

        yield db.test.drop_indexes()
        yield db.test.remove({})
Exemple #4
0
    def test_hint(self):
        yield self.coll.create_index(qf.sort(qf.ASCENDING('x')))

        yield self.db.command("profile", 2)
        cnt = yield self.coll.count(hint=qf.hint(qf.ASCENDING('x')))
        self.assertEqual(cnt, 3)
        yield self.db.command("profile", 0)

        cmds = yield self.db.system.profile.count({"command.hint": {"x": 1}})
        self.assertEqual(cmds, 1)

        self.assertRaises(TypeError, self.coll.count, hint={'x': 1})
        self.assertRaises(TypeError, self.coll.count, hint=[('x', 1)])
Exemple #5
0
    def process_message(self, msg):
        headers = 'headers' in msg.content.properties and \
            msg.content.properties['headers'] or None

        data = msg.content.body

        log.msg('RECEIVED MSG: {}'.format(str(headers)))
        log.msg(data)

        part = {}
        for key, value in self.run(data, headers):
            ap = part.get(key, None)
            if ap is None:
                ap = []
                part[key] = ap
            ap.append((key, value))

        data = dict(**headers)
        data['result'] = json.dumps(part)
        yield self.results.insert(SON(data))

        # TODO: I think this is not right place for this code. @german
        idx = filter.sort(filter.ASCENDING("task"))
        self.results.ensure_index(idx)
        headers['worker'] = 'map'
        self.publisher.sendMessage('OK',
                                   routing_key=ns.MASTER_QUEUE,
                                   headers=headers)
        returnValue(msg)
Exemple #6
0
    def test_create_index_nodup(self):
        coll = self.coll

        yield coll.insert({'b': 1})
        yield coll.insert({'b': 1})

        ix = coll.create_index(qf.sort(qf.ASCENDING("b")), unique=True)
        yield self.assertFailure(ix, errors.DuplicateKeyError)
Exemple #7
0
    def test_Failures(self):
        # can't alter _id
        yield self.assertFailure(self.coll.update_many({}, {"$set": {"_id": 1}}), WriteError)
        # invalid field name
        yield self.assertFailure(self.coll.update_many({}, {"$set": {'$': 1}}), WriteError)

        yield self.coll.create_index(qf.sort(qf.ASCENDING('x')), unique=True)
        yield self.assertFailure(self.coll.update_many({'x': 2}, {"$set": {'x': 1}}), DuplicateKeyError)
Exemple #8
0
    def test_ensure_index(self):
        db = self.db
        coll = self.coll

        yield coll.ensure_index(qf.sort(qf.ASCENDING("hello")))
        indices = yield db.system.indexes.find({"ns": u"mydb.mycol"})
        self.assert_(u"hello_1" in [a["name"] for a in indices])

        yield coll.drop_indexes()
Exemple #9
0
    def test_Failures(self):
        yield self.assertFailure(
            self.coll.replace_one({'x': 1}, {'x': {
                '$': 5
            }}), WriteError)

        yield self.coll.create_index(qf.sort(qf.ASCENDING('x')), unique=True)
        yield self.assertFailure(self.coll.replace_one({'x': 1}, {'x': 2}),
                                 DuplicateKeyError)
Exemple #10
0
    def test_ensure_index(self):
        db = self.db
        coll = self.coll

        yield coll.ensure_index(qf.sort(qf.ASCENDING("hello")))
        indices = yield coll.index_information()
        self.assert_(u"hello_1" in indices)

        yield coll.drop_indexes()
Exemple #11
0
    def test_FilterMerge(self):
        self.assertEqual(
            qf.sort(qf.ASCENDING('x') + qf.DESCENDING('y')),
            qf.sort(qf.ASCENDING('x')) + qf.sort(qf.DESCENDING('y')))

        comment = "hello world"

        yield self.db.command("profile", 2)
        yield self.coll.find({},
                             filter=qf.sort(qf.ASCENDING('x')) +
                             qf.comment(comment))
        yield self.db.command("profile", 0)

        cnt = yield self.db.system.profile.count({
            "query.$orderby.x": 1,
            "query.$comment": comment
        })
        self.assertEqual(cnt, 1)
    def test_create_index_nodup(self):
        coll = self.coll

        coll.drop()
        coll.insert({'b': 1})
        coll.insert({'b': 1})

        ix = coll.create_index(filter.sort(filter.ASCENDING("b")), unique=True)
        return self.assertFailure(ix, errors.DuplicateKeyError)
Exemple #13
0
    def test_FilterMerge(self):
        self.assertEqual(
            qf.sort(qf.ASCENDING('x') + qf.DESCENDING('y')),
            qf.sort(qf.ASCENDING('x')) + qf.sort(qf.DESCENDING('y')))

        comment = "hello world"

        yield self.db.command("profile", 2)
        yield self.coll.find({},
                             filter=qf.sort(qf.ASCENDING('x')) +
                             qf.comment(comment))
        yield self.db.command("profile", 0)

        if (yield self.__3_2_or_higher()):
            profile_filter = {"query.sort.x": 1, "query.comment": comment}
        else:
            profile_filter = {"query.$orderby.x": 1, "query.$comment": comment}
        cnt = yield self.db.system.profile.count(profile_filter)
        self.assertEqual(cnt, 1)
Exemple #14
0
    def test_create_index(self):
        db = self.db
        coll = self.coll

        self.assertRaises(TypeError, coll.create_index, 5)
        self.assertRaises(TypeError, coll.create_index, {"hello": 1})

        yield coll.insert({'c': 1})  # make sure collection exists.

        yield coll.drop_indexes()
        count = len((yield coll.index_information()))
        self.assertEqual(count, 1)
        self.assertIsInstance(count, int)

        yield coll.create_index(qf.sort(qf.ASCENDING("hello")))
        yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")))

        count = len((yield coll.index_information()))
        self.assertEqual(count, 3)

        yield coll.drop_indexes()
        ix = yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")),
            name="hello_world")
        self.assertEquals(ix, "hello_world")

        yield coll.drop_indexes()
        count = len((yield coll.index_information()))
        self.assertEqual(count, 1)

        yield coll.create_index(qf.sort(qf.ASCENDING("hello")))
        indices = yield coll.index_information()
        self.assert_(u"hello_1" in indices)

        yield coll.drop_indexes()
        count = len((yield coll.index_information()))
        self.assertEqual(count, 1)

        ix = yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")))
        self.assertEquals(ix, "hello_1_world_-1")
Exemple #15
0
    def test_create_index(self):
        db = self.db
        coll = self.coll

        self.assertFailure(coll.create_index(5), TypeError)
        self.assertFailure(coll.create_index({"hello": 1}), TypeError)

        yield coll.insert({'c': 1})  # make sure collection exists.

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)
        self.assertIsInstance(count, int)

        yield coll.create_index(qf.sort(qf.ASCENDING("hello")))
        yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")))

        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 3)

        yield coll.drop_indexes()
        ix = yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")),
            name="hello_world")
        self.assertEquals(ix, "hello_world")

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        yield coll.create_index(qf.sort(qf.ASCENDING("hello")))
        indices = yield db.system.indexes.find({"ns": u"mydb.mycol"})
        self.assert_(u"hello_1" in [a["name"] for a in indices])

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        ix = yield coll.create_index(
            qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world")))
        self.assertEquals(ix, "hello_1_world_-1")
Exemple #16
0
    def test_Sort(self):
        doc = yield self.coll.find_one_and_delete({'x': {"$exists": True}},
                                                  sort=qf.sort(qf.ASCENDING('y')))
        self.assertEqual(doc['x'], 1)

        doc = yield self.coll.find_one_and_delete({'x': {"$exists": True}},
                                                  sort=qf.sort(qf.DESCENDING('y')))
        self.assertEqual(doc['x'], 3)

        cnt = yield self.coll.count()
        self.assertEqual(cnt, 1)
Exemple #17
0
    def test_index_haystack(self):
        db = self.db
        coll = self.coll
        yield coll.drop_indexes()

        _id = yield coll.insert({
            "pos": {
                "long": 34.2,
                "lat": 33.3
            },
            "type": "restaurant"
        })
        yield coll.insert({
            "pos": {
                "long": 34.2,
                "lat": 37.3
            },
            "type": "restaurant"
        })
        yield coll.insert({
            "pos": {
                "long": 59.1,
                "lat": 87.2
            },
            "type": "office"
        })

        yield coll.create_index(
            filter.sort(filter.GEOHAYSTACK("pos") + filter.ASCENDING("type")),
            **{'bucket_size': 1})

        # TODO: A db.command method has not been implemented yet.
        # Sending command directly
        command = SON([
            ("geoSearch", "mycol"),
            ("near", [33, 33]),
            ("maxDistance", 6),
            ("search", {
                "type": "restaurant"
            }),
            ("limit", 30),
        ])

        results = yield db["$cmd"].find_one(command)
        self.assertEqual(2, len(results['results']))
        self.assertEqual(
            {
                "_id": _id,
                "pos": {
                    "long": 34.2,
                    "lat": 33.3
                },
                "type": "restaurant"
            }, results["results"][0])
Exemple #18
0
    def test_Hint(self):
        # find() should fail with 'bad hint' if hint specifier works correctly
        self.assertFailure(self.coll.find({}, filter=qf.hint([('x', 1)])),
                           OperationFailure)

        # create index and test it is honoured
        yield self.coll.create_index(qf.sort(qf.ASCENDING("x")),
                                     name="test_index")
        found_1 = yield self.coll.find({}, filter=qf.hint([('x', 1)]))
        found_2 = yield self.coll.find({}, filter=qf.hint(qf.ASCENDING("x")))
        found_3 = yield self.coll.find({}, filter=qf.hint("test_index"))
        self.assertTrue(found_1 == found_2 == found_3)

        # find() should fail with 'bad hint' if hint specifier works correctly
        self.assertFailure(
            self.coll.find({}, filter=qf.hint(["test_index", 1])),
            OperationFailure)
        self.assertFailure(
            self.coll.find({}, filter=qf.hint(qf.ASCENDING("test_index"))),
            OperationFailure)
Exemple #19
0
    def test_create_index(self):
        db = self.db
        coll = self.coll

        self.assertRaises(TypeError, coll.create_index, 5)
        self.assertRaises(TypeError, coll.create_index, {"hello": 1})

        yield coll.insert({'c': 1})  # make sure collection exists.

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        result1 = yield coll.create_index(
            filter.sort(filter.ASCENDING("hello")))
        result2 = yield coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                          filter.DESCENDING("world")))

        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 3)

        yield coll.drop_indexes()
        ix = yield coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                   filter.DESCENDING("world")), name="hello_world")
        self.assertEquals(ix, "hello_world")

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        yield coll.create_index(filter.sort(filter.ASCENDING("hello")))
        indices = yield db.system.indexes.find({"ns": u"mydb.mycol"})
        self.assert_(u"hello_1" in [a["name"] for a in indices])

        yield coll.drop_indexes()
        count = yield db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        ix = yield coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                   filter.DESCENDING("world")))
        self.assertEquals(ix, "hello_1_world_-1")
Exemple #20
0
    def test_drop_index(self):
        index = qf.sort(qf.ASCENDING("hello") + qf.DESCENDING("world"))

        yield self.coll.create_index(index, name="myindex")
        res = yield self.coll.drop_index("myindex")
        self.assertEqual(res["ok"], 1)

        yield self.coll.create_index(index)
        res = yield self.coll.drop_index(index)
        self.assertEqual(res["ok"], 1)

        self.assertRaises(TypeError, self.coll.drop_index, 123)
Exemple #21
0
    def test_Sort(self):
        doc = yield self.coll.find_one_and_update({}, {"$set": {'y': 5}},
                                                  projection={"_id": 0},
                                                  sort=qf.sort(qf.ASCENDING('y')))
        self.assertEqual(doc, {'x': 10, 'y': 10})

        doc = yield self.coll.find_one_and_update({}, {"$set": {'y': 35}},
                                                  projection={"_id": 0},
                                                  sort=qf.sort(qf.DESCENDING('y')))
        self.assertEqual(doc, {'x': 30, 'y': 30})

        ys = yield self.coll.distinct('y')
        self.assertEqual(set(ys), {5, 20, 35})
Exemple #22
0
    def test_create_index_dropdups(self):
        # dropDups was removed from MongoDB v3.0
        ismaster = yield self.db.command("ismaster")
        if ismaster["maxWireVersion"] >= 3:
            raise unittest.SkipTest("dropDups was removed from MongoDB 3")

        yield self.coll.insert([{'b': 1}, {'b': 1}])

        yield self.coll.create_index(qf.sort(qf.ASCENDING('b')),
                                     unique=True,
                                     drop_dups=True)
        docs = yield self.coll.find(fields={"_id": 0})
        self.assertEqual(docs, [{'b': 1}])
Exemple #23
0
    def test_index_haystack(self):
        db = self.db
        coll = self.coll
        yield coll.drop_indexes()

        _id = yield coll.insert({
            "pos": {
                "long": 34.2,
                "lat": 33.3
            },
            "type": "restaurant"
        })
        yield coll.insert({
            "pos": {
                "long": 34.2,
                "lat": 37.3
            },
            "type": "restaurant"
        })
        yield coll.insert({
            "pos": {
                "long": 59.1,
                "lat": 87.2
            },
            "type": "office"
        })

        yield coll.create_index(
            qf.sort(qf.GEOHAYSTACK("pos") + qf.ASCENDING("type")),
            **{"bucket_size": 1})

        results = yield db.command("geoSearch",
                                   "mycol",
                                   near=[33, 33],
                                   maxDistance=6,
                                   search={"type": "restaurant"},
                                   limit=30)

        self.assertEqual(2, len(results["results"]))
        self.assertEqual(
            {
                "_id": _id,
                "pos": {
                    "long": 34.2,
                    "lat": 33.3
                },
                "type": "restaurant"
            }, results["results"][0])
    def test_Timestamps(self):
        """Tests mongo operations with Timestamps"""
        conn = yield txmongo.MongoConnection(mongo_host, mongo_port)
        test = conn.foo.test_ts

        test.drop()

        # insert with specific timestamp
        doc1 = {'_id':objectid.ObjectId(),
                'ts':timestamp.Timestamp(1, 2)}
        yield test.insert(doc1, safe=True)

        result = yield test.find_one(doc1)
        self.assertEqual(result.get('ts').time, 1)
        self.assertEqual(result.get('ts').inc, 2)

        # insert with specific timestamp
        doc2 = {'_id':objectid.ObjectId(),
                'ts':timestamp.Timestamp(2, 1)}
        yield test.insert(doc2, safe=True)

        # the objects come back sorted by ts correctly.
        # (test that we stored inc/time in the right fields)
        result = yield test.find(filter=qf.sort(qf.ASCENDING('ts')))
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0]['_id'], doc1['_id'])
        self.assertEqual(result[1]['_id'], doc2['_id'])

        # insert with null timestamp
        doc3 = {'_id':objectid.ObjectId(),
                'ts':timestamp.Timestamp(0, 0)}
        yield test.insert(doc3, safe=True)

        # time field loaded correctly
        result = yield test.find_one(doc3['_id'])
        now = time.time()
        self.assertTrue(now - 2 <= result['ts'].time <= now)

        # delete
        yield test.remove(doc1["_id"], safe=True)
        yield test.remove(doc2["_id"], safe=True)
        yield test.remove(doc3["_id"], safe=True)

        # disconnect
        yield conn.disconnect()
Exemple #25
0
def example():
    mongo = yield txmongo.MongoConnection()

    foo = mongo.foo  # `foo` database
    test = foo.test  # `test` collection

    idx = filter.sort(
        filter.ASCENDING("something") + filter.DESCENDING("else"))
    print "IDX:", idx

    result = yield test.create_index(idx)
    print "create_index:", result

    result = yield test.index_information()
    print "index_information:", result

    result = yield test.drop_index(idx)
    print "drop_index:", result
Exemple #26
0
 def test_SortAscendingMultipleFields(self):
     self.assertEqual(qf.sort(qf.ASCENDING(['x', 'y'])),
                      qf.sort(qf.ASCENDING('x') + qf.ASCENDING('y')))
Exemple #27
0
 def test_convert(self):
     self.assertEqual(
         Collection._find_args_compat(spec={'x': 42}), {
             "filter": {
                 'x': 42
             },
             "projection": None,
             "skip": 0,
             "limit": 0,
             "sort": None,
             "cursor": False
         })
     self.assertEqual(
         Collection._find_args_compat(filter={'x': 42}), {
             "filter": {
                 'x': 42
             },
             "projection": None,
             "skip": 0,
             "limit": 0,
             "sort": None
         })
     self.assertEqual(
         Collection._find_args_compat(filter=qf.sort(qf.ASCENDING('x'))), {
             "filter": None,
             "projection": None,
             "skip": 0,
             "limit": 0,
             "sort": qf.sort(qf.ASCENDING('x')),
             "cursor": False
         })
     self.assertEqual(
         Collection._find_args_compat(sort=qf.sort(qf.ASCENDING('x'))), {
             "filter": None,
             "projection": None,
             "skip": 0,
             "limit": 0,
             "sort": qf.sort(qf.ASCENDING('x'))
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}), {
             "filter": {
                 'x': 42
             },
             "projection": None,
             "skip": 0,
             "limit": 0,
             "sort": None
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, unknown_arg=123), {
             "filter": {
                 'x': 42
             },
             "projection": None,
             "skip": 0,
             "limit": 0,
             "sort": None,
             "unknown_arg": 123
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, {'a': 1}), {
             "filter": {
                 'x': 42
             },
             "projection": {
                 'a': 1
             },
             "skip": 0,
             "limit": 0,
             "sort": None
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, projection={'a': 1}), {
             "filter": {
                 'x': 42
             },
             "projection": {
                 'a': 1
             },
             "skip": 0,
             "limit": 0,
             "sort": None
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, fields={'a': 1}), {
             "filter": {
                 'x': 42
             },
             "projection": {
                 'a': 1
             },
             "skip": 0,
             "limit": 0,
             "sort": None,
             "cursor": False
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, 5), {
             "filter": {
                 'x': 42
             },
             "projection": None,
             "skip": 5,
             "limit": 0,
             "sort": None,
             "cursor": False
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, {'a': 1}, 5), {
             "filter": {
                 'x': 42
             },
             "projection": {
                 'a': 1
             },
             "skip": 5,
             "limit": 0,
             "sort": None
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, {'a': 1}, 5, 6), {
             "filter": {
                 'x': 42
             },
             "projection": {
                 'a': 1
             },
             "skip": 5,
             "limit": 6,
             "sort": None
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, 5, 6, {'a': 1}), {
             "filter": {
                 'x': 42
             },
             "projection": {
                 'a': 1
             },
             "skip": 5,
             "limit": 6,
             "sort": None,
             "cursor": False
         })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, {'a': 1}, 5, 6,
                                      qf.sort([('s', 1)])), {
                                          "filter": {
                                              'x': 42
                                          },
                                          "projection": {
                                              'a': 1
                                          },
                                          "skip": 5,
                                          "limit": 6,
                                          "sort": qf.sort([('s', 1)])
                                      })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, 5, 6, {'a': 1},
                                      qf.sort([('s', 1)])), {
                                          "filter": {
                                              'x': 42
                                          },
                                          "projection": {
                                              'a': 1
                                          },
                                          "skip": 5,
                                          "limit": 6,
                                          "sort": qf.sort([('s', 1)]),
                                          "cursor": False
                                      })
     self.assertEqual(
         Collection._find_args_compat({'x': 42}, 5, 6, {'a': 1},
                                      qf.sort([('s', 1)]), True), {
                                          "filter": {
                                              'x': 42
                                          },
                                          "projection": {
                                              'a': 1
                                          },
                                          "skip": 5,
                                          "limit": 6,
                                          "sort": qf.sort([('s', 1)]),
                                          "cursor": True
                                      })
     self.assertEqual(
         Collection._find_args_compat(spec={'x': 42},
                                      filter=qf.sort([('s', 1)]),
                                      limit=6,
                                      fields={'a': 1},
                                      skip=5), {
                                          "filter": {
                                              'x': 42
                                          },
                                          "projection": {
                                              'a': 1
                                          },
                                          "skip": 5,
                                          "limit": 6,
                                          "sort": qf.sort([('s', 1)]),
                                          "cursor": False
                                      })
     self.assertEqual(
         Collection._find_args_compat(filter={'x': 42},
                                      sort=qf.sort([('s', 1)]),
                                      limit=6,
                                      projection={'a': 1},
                                      skip=5), {
                                          "filter": {
                                              'x': 42
                                          },
                                          "projection": {
                                              'a': 1
                                          },
                                          "skip": 5,
                                          "limit": 6,
                                          "sort": qf.sort([('s', 1)])
                                      })
Exemple #28
0
def configure_db():
    """
    Initialize the database, configure indexes.

    Add a dummy campaigns for events without campaigns.

    :return:
    """
    logger = logging.getLogger(__name__)
    yield get_mongo_db()

    campaign_idx = filter.sort(filter.ASCENDING("campaign_id"))
    banner_idx = filter.sort(filter.ASCENDING("banner_id"))
    timestamp_idx = filter.sort(filter.ASCENDING("timestamp"))
    event_idx = filter.sort(filter.ASCENDING("event_id"))
    user_idx = filter.sort(filter.ASCENDING("user_id"))
    keyword_idx = filter.sort(filter.ASCENDING("keyword"))
    updated_idx = filter.sort(filter.ASCENDING("updated"))
    campaign_event_idx = filter.sort(
        filter.ASCENDING("campaign_id") + filter.ASCENDING("event_id"))

    campaign_collection = yield get_campaign_collection()
    yield campaign_collection.create_index(campaign_idx, unique=True)

    banner_collection = yield get_banner_collection()
    yield banner_collection.create_index(banner_idx, unique=True)
    yield banner_collection.create_index(campaign_idx)

    event_collection = yield get_event_collection()
    yield event_collection.create_index(event_idx, unique=True)
    yield event_collection.create_index(timestamp_idx)
    yield event_collection.create_index(banner_idx)
    yield event_collection.create_index(user_idx)

    payment_collection = yield get_payment_collection()
    yield payment_collection.create_index(timestamp_idx)
    yield payment_collection.create_index(campaign_event_idx)

    payment_round_collection = yield get_payment_rounds_collection()
    yield payment_round_collection.create_index(timestamp_idx, unique=True)

    user_value_collection = yield get_user_value_collection()
    yield user_value_collection.create_index(user_idx)
    yield user_value_collection.create_index(campaign_idx)

    user_score_collection = yield get_user_score_collection()
    yield user_score_collection.create_index(timestamp_idx)
    yield user_value_collection.create_index(campaign_idx)
    yield user_value_collection.create_index(user_idx)

    user_keyword_frequency_collection = yield get_user_keyword_frequency_collection(
    )
    yield user_keyword_frequency_collection.create_index(user_idx)
    yield user_keyword_frequency_collection.create_index(keyword_idx)

    user_profile_collection = yield get_user_profile_collection()
    yield user_profile_collection.create_index(user_idx)

    keyword_frequency_collection = yield get_keyword_frequency_collection()
    yield keyword_frequency_collection.create_index(updated_idx)
    yield keyword_frequency_collection.create_index(keyword_idx)
    yield logger.debug('Database configured successfully.')

    # Add default campaign
    yield campaign_collection.replace_one({'campaign_id': 'not_found'}, {
        'campaign_id': 'not_found',
        'time_start': 0,
        'time_end': 2147483646,
        'filters': {
            'require': {},
            'exclude': {}
        },
        'keywords': {},
        'banners': [],
        'max_cpc': 0,
        'max_cpm': 0,
        'budget': 0
    },
                                          upsert=True)
Exemple #29
0
 def test_Repr(self):
     self.assertEqual(repr(qf.sort(qf.ASCENDING('x'))),
                      "<mongodb QueryFilter: {'orderby': (('x', 1),)}>")