def StoreMetaData(zone, data, meta=False): db = client.TestingData if not meta: col = Collection(db, zone) else: col = Collection(db, zone+'_meta') dead = data['dead'] infected = data['infected'] recovered = data['recovered'] countDead = yield col.count(spec={'dead': {'$exists': True}}) countInf = yield col.count(spec={'infected': {'$exists': True}}) countRec = yield col.count(spec={'recovered': {'$exists': True}}) if dead is not None and countDead: docs = yield col.find(spec={'dead': {'$exists': True}}) try: docs[0]['dead'].index(dead) except: yield col.update(spec={'dead': {'$exists': True}}, document={'$push': {'dead': dead}}) d = yield col.find(spec={'countD': {'$exists': True}}) count = int(d[0]['countD'])+1 yield col.update(spec={'countD': {'$exists': True}}, document={'$set': {'countD': count}}) elif not countDead and dead is not None: count = 1 d = {'dead': [dead], 'countD': 1} yield col.insert_one(d) if infected is not None and countInf: docs = yield col.find(spec={'infected': {'$exists': True}}) try: docs[0]['infected'].index(infected) except: yield col.update(spec={'infected': {'$exists': True}}, document={'$push': {'infected': infected}}) d = yield col.find(spec={'countI': {'$exists': True}}) count = int(d[0]['countI'])+1 yield col.update(spec={'countI': {'$exists': True}}, document={'$set': {'countI': count}}) elif not countInf and infected is not None: count = 1 d = {'infected': [infected], 'countI': 1} yield col.insert_one(d) if recovered is not None and countRec: docs = yield col.find(spec={'recovered': {'$exists': True}}) try: docs[0]['recovered'].index(recovered) except: yield col.update(spec={'recovered': {'$exists': True}}, document={'$push': {'recovered': recovered}}) d = yield col.find(spec={'countR': {'$exists': True}}) count = int(d[0]['countR'])+1 yield col.update(spec={'countR': {'$exists': True}}, document={'$set': {'countR': count}}) elif not countRec and recovered is not None: count = 1 d = {'recovered': [recovered], 'countR': 1} yield col.insert_one(d) yield sleep(0)
def test_dbref(self): self.assertRaises(TypeError, DBRef, 5, "test_id") self.assertRaises(TypeError, DBRef, "test", "test_id", 5) oid = ObjectId() ref = DBRef("testc", oid, "testdb") self.assertEqual(ref.collection, "testc") self.assertEqual(ref.id, oid) self.assertEqual(ref.database, "testdb") collection = Collection("testdb", "testcoll") ref = DBRef(collection, oid) self.assertEqual(ref.collection, "testcoll") ref_son = SON([("$ref", "testcoll"), ("$id", oid)]) self.assertEqual(ref.as_doc(), ref_son) self.assertEqual(repr(ref), "DBRef(testcoll, %r)" % oid) ref = DBRef(collection, oid, "testdb") ref_son = SON([("$ref", "testcoll"), ("$id", oid), ("$db", "testdb")]) self.assertEqual(ref.as_doc(), ref_son) self.assertEqual(repr(ref), "DBRef(testcoll, %r, testdb)" % oid) ref1 = DBRef('a', oid) ref2 = DBRef('b', oid) self.assertEqual(cmp(ref1, ref2), -1) self.assertEqual(cmp(ref1, 0), -1) ref1 = DBRef('a', oid) ref2 = DBRef('a', oid) self.assertEqual(hash(ref1), hash(ref2))
def test_collection(self): self.assertRaises(TypeError, Collection, self.db, 5) def make_col(base, name): return base[name] self.assertRaises(errors.InvalidName, make_col, self.db, "") self.assertRaises(errors.InvalidName, make_col, self.db, "te$t") self.assertRaises(errors.InvalidName, make_col, self.db, ".test") self.assertRaises(errors.InvalidName, make_col, self.db, "test.") self.assertRaises(errors.InvalidName, make_col, self.db, "tes..t") self.assertRaises(errors.InvalidName, make_col, self.db.test, "") self.assertRaises(errors.InvalidName, make_col, self.db.test, "te$t") self.assertRaises(errors.InvalidName, make_col, self.db.test, ".test") self.assertRaises(errors.InvalidName, make_col, self.db.test, "test.") self.assertRaises(errors.InvalidName, make_col, self.db.test, "tes..t") self.assertRaises(errors.InvalidName, make_col, self.db.test, "tes\x00t") self.assert_(isinstance(self.db.test, Collection)) self.assertEqual(self.db.test, Collection(self.db, "test")) self.assertEqual(self.db.test.mike, self.db["test.mike"]) self.assertEqual(self.db.test["mike"], self.db["test.mike"]) yield self.db.drop_collection('test') collection_names = yield self.db.collection_names() self.assertFalse('test' in collection_names)
def test_Priority(self): """ Check that connection-level, database-level, collection-level and query-level write concerns are respected with correct priority """ conn = MongoConnection(mongo_host, mongo_port, w=1, wtimeout=500) try: with self.mock_gle() as mock: yield conn.mydb.mycol.insert({'x': 42}) mock.assert_called_once_with("mydb", w=1, wtimeout=500) db_w0 = Database(conn, "mydb", write_concern=WriteConcern(w=0)) with self.mock_gle() as mock: yield db_w0.mycol.insert({'x': 42}) self.assertFalse(mock.called) coll = Collection(db_w0, "mycol", write_concern=WriteConcern(w=2)) with self.mock_gle() as mock: yield coll.insert({'x': 42}) mock.assert_called_once_with("mydb", w=2) with self.mock_gle() as mock: yield coll.insert({'x': 42}, j=True) mock.assert_called_once_with("mydb", j=True) finally: yield conn.mydb.mycol.drop() yield conn.disconnect()
def create_collection(self, name, options=None, **kwargs): collection = Collection(self, name) if options: if "size" in options: options["size"] = float(options["size"]) options.update(kwargs) yield self.command("create", name, **options) defer.returnValue(collection)
def create_collection(self, name, options=None, **kwargs): collection = Collection(self, name) if options: if "size" in options: options["size"] = float(options["size"]) options.update(kwargs) return self.command("create", name, **options)\ .addCallback(lambda _: collection) return collection
def __getattr__(self, key): if key in self._registered_documents: if key not in self._documents: self._documents[key] = self._registered_documents[key]( collection=self) return self._documents[key] else: newkey = u"{}.{}".format(self.name, key) if newkey not in self._collections: self._collections[newkey] = Collection(self.database, newkey) return self._collections[newkey]
def _authenticate_with_nonce(self, result, name, password, d): nonce = result['nonce'] key = helpers._auth_key(nonce, name, password) # hacky because order matters auth_command = SON(authenticate=1) auth_command['user'] = unicode(name) auth_command['nonce'] = nonce auth_command['key'] = key # Now actually authenticate Collection(self.database, "$cmd").find_one(auth_command, _proto=self).addCallback( self._authenticated, d).addErrback(self._auth_error, d)
def test_collection(self): self.assertRaises(TypeError, Collection, self.db, 5) def make_col(base, name): return base[name] self.assertRaises(errors.InvalidName, make_col, self.db, "") self.assertRaises(errors.InvalidName, make_col, self.db, "te$t") self.assertRaises(errors.InvalidName, make_col, self.db, ".test") self.assertRaises(errors.InvalidName, make_col, self.db, "test.") self.assertRaises(errors.InvalidName, make_col, self.db, "tes..t") self.assertRaises(errors.InvalidName, make_col, self.db.test, "") self.assertRaises(errors.InvalidName, make_col, self.db.test, "te$t") self.assertRaises(errors.InvalidName, make_col, self.db.test, ".test") self.assertRaises(errors.InvalidName, make_col, self.db.test, "test.") self.assertRaises(errors.InvalidName, make_col, self.db.test, "tes..t") self.assertRaises(errors.InvalidName, make_col, self.db.test, "tes\x00t") self.assertRaises(TypeError, self.coll.save, 'test') self.assertRaises(ValueError, self.coll.filemd5, 'test') self.assertFailure(self.db.test.find(spec="test"), TypeError) self.assertFailure(self.db.test.find(fields="test"), TypeError) self.assertFailure(self.db.test.find(skip="test"), TypeError) self.assertFailure(self.db.test.find(limit="test"), TypeError) self.assertFailure(self.db.test.insert([1]), TypeError) self.assertFailure(self.db.test.insert(1), TypeError) self.assertFailure(self.db.test.update(1, 1), TypeError) self.assertFailure(self.db.test.update({}, 1), TypeError) self.assertFailure(self.db.test.update({}, {}, 'a'), TypeError) self.assert_(isinstance(self.db.test, Collection)) self.assertEqual(NotImplemented, self.db.test.__cmp__(7)) self.assertNotEqual(cmp(self.db.test, 7), 0) self.assertEqual(self.db.test, Collection(self.db, "test")) self.assertEqual(self.db.test.mike, self.db["test.mike"]) self.assertEqual(self.db.test["mike"], self.db["test.mike"]) self.assertEqual(repr(self.db.test), 'Collection(mydb, test)') self.assertEqual(self.db.test.test, self.db.test('test')) options = yield self.db.test.options() print options self.assertIsInstance(options, dict) yield self.db.drop_collection('test') collection_names = yield self.db.collection_names() self.assertFalse('test' in collection_names)
def test_collection(self): self.assertRaises(TypeError, Collection, self.db, 5) def make_col(base, name): return base[name] self.assertRaises(errors.InvalidName, make_col, self.db, "") self.assertRaises(errors.InvalidName, make_col, self.db, "te$t") self.assertRaises(errors.InvalidName, make_col, self.db, ".test") self.assertRaises(errors.InvalidName, make_col, self.db, "test.") self.assertRaises(errors.InvalidName, make_col, self.db, "tes..t") self.assertRaises(errors.InvalidName, make_col, self.db.test, "") self.assertRaises(errors.InvalidName, make_col, self.db.test, "te$t") self.assertRaises(errors.InvalidName, make_col, self.db.test, ".test") self.assertRaises(errors.InvalidName, make_col, self.db.test, "test.") self.assertRaises(errors.InvalidName, make_col, self.db.test, "tes..t") self.assertRaises(errors.InvalidName, make_col, self.db.test, "tes\x00t") self.assertRaises(TypeError, self.coll.save, "test") self.assertRaises(ValueError, self.coll.filemd5, "test") self.assertRaises(TypeError, self.db.test.find, spec="test") self.assertRaises(TypeError, self.db.test.find, fields="test") self.assertRaises(TypeError, self.db.test.find, skip="test") self.assertRaises(TypeError, self.db.test.find, limit="test") self.assertRaises(TypeError, self.db.test.insert, [1]) self.assertRaises(TypeError, self.db.test.insert, 1) self.assertRaises(TypeError, self.db.test.update, 1, 1) self.assertRaises(TypeError, self.db.test.update, {}, 1) self.assertRaises(TypeError, self.db.test.update, {}, {}, 'a') self.assert_(isinstance(self.db.test, Collection)) self.assertEqual(NotImplemented, self.db.test.__cmp__(7)) self.assertNotEqual(cmp(self.db.test, 7), 0) self.assertEqual(self.db.test, Collection(self.db, "test")) self.assertEqual(self.db.test.mike, self.db["test.mike"]) self.assertEqual(self.db.test["mike"], self.db["test.mike"]) self.assertEqual(repr(self.db.test), "Collection(mydb, test)") self.assertEqual(self.db.test.test, self.db.test("test")) options = yield self.db.test.options() self.assertTrue(isinstance(options, dict)) yield self.db.drop_collection("test") collection_names = yield self.db.collection_names() self.assertFalse("test" in collection_names)
def _authenticate(self, name, password): """ Send an authentication command for this database. mostly stolen from pymongo """ if not isinstance(name, basestring): raise TypeError("name must be an instance of basestring") if not isinstance(password, basestring): raise TypeError("password must be an instance of basestring") d = defer.Deferred() # First get the nonce Collection(self.database, "$cmd").find_one({ "getnonce": 1 }, _proto=self).addCallback(self._authenticate_with_nonce, name, password, d).addErrback( self._auth_error, d) return d
def create_collection(self, name, options={}): def wrapper(result, deferred, collection): if result.get("ok", 0.0): deferred.callback(collection) else: deferred.errback(RuntimeError(result.get("errmsg", "unknown error"))) deferred = defer.Deferred() collection = Collection(self, name) if options: if "size" in options: options["size"] = float(options["size"]) command = SON({"create": name}) command.update(options) d = self["$cmd"].find_one(command) d.addCallback(wrapper, deferred, collection) else: deferred.callback(collection) return deferred
def create_collection(self, name, options=None, write_concern=None, codec_options=None, **kwargs): collection = Collection(self, name, write_concern=write_concern, codec_options=codec_options) if options is None and kwargs: options = kwargs kwargs = {} if options: if "size" in options: options["size"] = float(options["size"]) options.update(kwargs) return self.command("create", name, **options)\ .addCallback(lambda _: collection) return collection
def __getitem__(self, collection_name): return Collection(self, collection_name)
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)])}, )
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)]) })