def test_add_resources(self): r = redis.StrictRedis() r.flushdb() db = motor.MotorClient().test_fbt cache = FBTUserResourceManager(db=db, cache=r) yield cache.get_collection().remove({}) uid = 1234 res_list = yield cache.get_resource_of_user(uid) self.assertEqual(res_list, []) file_id1 = "1234_4321" file_id2 = "4321_1234" file_id3 = "5678_8765" yield cache.add_file_in_dir_to_my_resource_list( uid, [file_id1, file_id2, file_id3]) res_list = yield cache.get_resource_of_user(uid) self.assertListEqual(res_list, [file_id3, file_id2, file_id1]) # test rebuild resources from db r.flushdb() res_list = yield cache.get_resource_of_user(uid) self.assertListEqual(res_list, [file_id3, file_id2, file_id1])
def test_ipv6(self): assert host in ('localhost', '127.0.0.1'), ( "This unittest isn't written to test IPv6 with host %s" % repr(host) ) try: MongoClient("[::1]") except ConnectionFailure: # Either mongod was started without --ipv6 # or the OS doesn't support it (or both). raise SkipTest("No IPV6") if test.env.auth: cx_string = 'mongodb://%s:%s@[::1]:%d' % ( test.db_user, test.db_password, port) else: cx_string = 'mongodb://[::1]:%d' % port cx = motor.MotorClient(cx_string, io_loop=self.io_loop) collection = cx.motor_test.test_collection yield collection.insert({"dummy": "object"}) self.assertTrue((yield collection.find_one({"dummy": "object"})))
def test_cert_ssl_validation_optional(self): # Expects the server to be running with the server.pem, ca.pem # and crl.pem provided in mongodb and the server tests e.g.: # # --sslPEMKeyFile=jstests/libs/server.pem # --sslCAFile=jstests/libs/ca.pem # --sslCRLFile=jstests/libs/crl.pem # # Also requires an /etc/hosts entry where "server" is resolvable. if not test.env.mongod_validates_client_cert: raise SkipTest("No mongod available over SSL with certs") if not test.env.server_is_resolvable: raise SkipTest("No hosts entry for 'server'. Cannot validate " "hostname in the certificate") client = motor.MotorClient( 'server', ssl_certfile=CLIENT_PEM, ssl_cert_reqs=ssl.CERT_OPTIONAL, ssl_ca_certs=CA_PEM, io_loop=self.io_loop) response = yield client.admin.command('ismaster') if 'setName' in response: if response['primary'].split(":")[0] != 'server': raise SkipTest("No hosts in the replicaset for 'server'. " "Cannot validate hostname in the certificate") client = motor.MotorReplicaSetClient( 'server', replicaSet=response['setName'], ssl_certfile=CLIENT_PEM, ssl_cert_reqs=ssl.CERT_OPTIONAL, ssl_ca_certs=CA_PEM, io_loop=self.io_loop) yield client.db.collection.find_one()
def __init__(self, crawler): try: import motor except ImportError: logger.info( "MotorPipeline is disabled because motor Python package " "is not available") raise NotConfigured self.crawler = crawler opts = self.crawler.settings if not opts.getbool('MOTOR_PIPELINE_ENABLED', False): raise NotConfigured self.job_id_key = opts.get('MOTOR_PIPELINE_JOBID_KEY') self.db_uri = opts.get('MOTOR_PIPELINE_URI', 'mongodb://localhost:27017') db_name = opts.get('MOTOR_PIPELINE_DB_NAME', 'motor_exporter') self.client = motor.MotorClient(self.db_uri) self.items_table = self.client[db_name][self.ITEMS_COLLECTION] self.jobs_table = self.client[db_name][self.JOBS_COLLECTION] self.connected = False
def after_start(cls, application, io_loop=None, *args, **kw): host = application.config.get('MONGOHOST') port = application.config.get('MONGOPORT') db = application.config.get('MONGODATABASE') user = application.config.get('MONGOUSER') password = application.config.get('MONGOPASS') if not db: raise RuntimeError("MONGODATABASE configuration is required.") conn_str = "%s:%d/%s" % (host, port, db) user_str = "" if user is not None: user_str = user if password is not None: user_str = "%s:%s" % (user, password) conn = "mongodb://%s%s" % (user_str, conn_str) logging.info("Connecting to mongodb at %s" % (conn_str)) application.mongoserver = motor.MotorClient(conn, io_loop=io_loop) application.mongo = application.mongoserver[db]
async def test_handshake(self): server = self.server() client = motor.MotorClient(server.uri, connectTimeoutMS=100, serverSelectionTimeoutMS=100) # Trigger connection. future = client.db.command("ping") ismaster = await self.run_thread(server.receives, "ismaster") meta = ismaster.doc["client"] self.assertEqual("PyMongo|Motor", meta["driver"]["name"]) self.assertIn("Tornado", meta["platform"]) self.assertTrue( meta["driver"]["version"].endswith(motor.version), "Version in handshake [%s] doesn't end with Motor version [%s]" % (meta["driver"]["version"], motor.version), ) ismaster.hangs_up() server.stop() client.close() try: await future except Exception: pass
def test_authenticate(self): # self.db is logged in as root. yield self.db.add_user("mike", "password") client = motor.MotorClient(host, port, **self.get_client_kwargs()) db = client.motor_test try: # Authenticate many times at once to test concurrency. yield [db.authenticate("mike", "password") for _ in range(10)] # Just make sure there are no exceptions here. yield db.remove_user("mike") yield db.logout() if (yield version.at_least(self.cx, (2, 5, 4))): info = yield self.db.command("usersInfo", "mike") users = info.get('users', []) else: users = yield self.db.system.users.find().to_list(length=10) self.assertFalse("mike" in [u['user'] for u in users]) finally: yield remove_all_users(self.db) test.env.sync_cx.disconnect()
def main(): global http_server try: signal(SIGTERM, on_signal) parse_command_line() if options.config != None: parse_config_file(options.config) path = join(dirname(__file__), "templates") application = Application([(r'/', IndexHandler), (r'/stock', StockHandler)], template_path=path, static_path=join(dirname(__file__), "static")) application.db = motor.MotorClient( options.db_host, options.db_port).open_sync()[options.db_name] http_server = HTTPServer(application) http_server.listen(options.port, options.address) log().info("server listening on port %s:%d" % (options.address, options.port)) if log().isEnabledFor(DEBUG): log().debug("autoreload enabled") tornado.autoreload.start() IOLoop.instance().start() except KeyboardInterrupt: log().info("exiting...") except BaseException as ex: log().error("exiting due: [%s][%s]" % (str(ex), str(format_exc().splitlines()))) exit(1)
def test_mongos_ha(self): dbname = 'pymongo_mongos_ha' c = yield motor.MotorClient(self.seed_list).open() yield c.drop_database(dbname) coll = c[dbname].test yield coll.insert({'foo': 'bar'}) first = '%s:%d' % (c.host, c.port) ha_tools.kill_mongos(first) # Fail first attempt with assert_raises(AutoReconnect): yield coll.count() # Find new mongos self.assertEqual(1, (yield coll.count())) second = '%s:%d' % (c.host, c.port) self.assertNotEqual(first, second) ha_tools.kill_mongos(second) # Fail first attempt with assert_raises(AutoReconnect): yield coll.count() # Find new mongos self.assertEqual(1, (yield coll.count())) third = '%s:%d' % (c.host, c.port) self.assertNotEqual(second, third) ha_tools.kill_mongos(third) # Fail first attempt with assert_raises(AutoReconnect): yield coll.count() # We've killed all three, restart one. ha_tools.restart_mongos(first) # Find new mongos self.assertEqual(1, (yield coll.count()))
def test_handshake(self): server = self.server() client = motor.MotorClient(server.uri, connectTimeoutMS=100, serverSelectionTimeoutMS=100) # Trigger connection. future = client.db.command('ping') ismaster = yield self.run_thread(server.receives, "ismaster") meta = ismaster.doc['client'] self.assertEqual('PyMongo|Motor', meta['driver']['name']) self.assertIn('Tornado', meta['platform']) self.assertTrue( meta['driver']['version'].endswith(motor.version), "Version in handshake [%s] doesn't end with Motor version [%s]" % ( meta['driver']['version'], motor.version)) ismaster.hangs_up() server.stop() client.close() try: yield future except Exception: pass
def connect_mongo(mongo_settings, **kwargs): mongo_addr = kwargs.get('mongo_addr', { 'host': mongo_settings['host'], 'port': mongo_settings['port'] }) mongo_db = kwargs.get('mongo_db', mongo_settings['db_name']) db = None for i in range(mongo_settings['reconnect_tries']): try: db = motor.MotorClient(**mongo_addr)[mongo_db] except ConnectionFailure: if i >= mongo_settings['reconnect_tries']: raise else: timeout = mongo_settings['reconnect_timeout'] l.warning( 'ConnectionFailure #{0} during server start, waiting {1} seconds' .format(i + 1, timeout)) time.sleep(timeout) else: break return db
def setup_db(self): log.debug('connect database at {}:{}'.format(options.dbhost, options.dbport)) self.conn = motor.MotorClient(host=options.dbhost, port=options.dbport) self.db = self.conn[options.dbname] self.mongo_conn = pymongo.MongoClient(host=options.dbhost, port=options.dbport) self.mongo_db = self.mongo_conn[options.dbname] # ensure index # posts # posts_idx = pymongo.IndexModel([('post_name',)], unique=True) # self.mongo_db.posts.create_indexes([posts_idx]) self.mongo_db.posts.ensure_index([('post_name', pymongo.ASCENDING)], unique=True) # PyMongo2.8 # users # account_idx = pymongo.IndexModel([('account',)], unique=True) # username_idx = pymongo.IndexModel([('username',)], unique=True) # self.mongo_db.users.create_indexes([account_idx, username_idx]) self.mongo_db.users.ensure_index([('account', pymongo.ASCENDING)], unique=True) # PyMongo2.8 self.mongo_db.users.ensure_index([('username', pymongo.ASCENDING)], unique=True) # PyMongo2.8
def test_authenticate(self): # self.db is logged in as root. test.env.create_user(self.db.name, "mike", "password", roles=['userAdmin', 'readWrite']) client = motor.MotorClient(env.host, env.port, **self.get_client_kwargs()) db = client.motor_test try: # Authenticate many times at once to test concurrency. yield [db.authenticate("mike", "password") for _ in range(10)] # Just make sure there are no exceptions here. test.env.drop_user(db.name, 'mike') yield db.logout() info = yield self.db.command("usersInfo", "mike") users = info.get('users', []) self.assertFalse("mike" in [u['user'] for u in users]) finally: yield remove_all_users(self.db) test.env.sync_cx.close()
def test_unix_socket(self): if env.mongod_started_with_ssl: raise SkipTest("Server started with SSL") mongodb_socket = '/tmp/mongodb-%d.sock' % env.port if not os.access(mongodb_socket, os.R_OK): raise SkipTest("Socket file is not accessible") encoded_socket = '%2Ftmp%2Fmongodb-' + str(env.port) + '.sock' uri = 'mongodb://%s' % encoded_socket client = self.motor_client(uri) if test.env.auth: yield client.admin.authenticate(db_user, db_password) yield client.motor_test.test.insert_one({"dummy": "object"}) # Confirm it fails with a missing socket. client = motor.MotorClient("mongodb://%2Ftmp%2Fnon-existent.sock", io_loop=self.io_loop, serverSelectionTimeoutMS=100) with self.assertRaises(ConnectionFailure): yield client.admin.command('ismaster')
async def _no_access(access): counter = 0 with pytest.raises(pymongo.errors.OperationFailure): while True: try: counter += 1 mongo = motor.MotorClient( "mongodb://*****:*****@testmongo:27017") _ = await mongo.server_info() time.sleep(1) break except pymongo.errors.OperationFailure as ops_fail: print(ops_fail.details['codeName']) time.sleep(1) if counter == 5: break continue except Exception as E: print("something really strange happen: ", E.details['codeName']) break _ = await mongo["core4test"].list_collection_names()
def _test_exhaust_query_network_error(self, rs): # When doing an exhaust query, the socket stays checked out on success # but must be checked in on error to avoid counter leak. server = self.primary_or_standalone(rs=rs) client = motor.MotorClient(server.uri, maxPoolSize=1) yield client.admin.command('ismaster') pool = get_primary_pool(client) pool._check_interval_seconds = None # Never check. sock_info = one(pool.sockets) cursor = client.db.collection.find(cursor_type=CursorType.EXHAUST) # With Tornado, simply accessing fetch_next starts the fetch. fetch_next = cursor.fetch_next request = yield self.run_thread(server.receives, OpQuery) request.hangs_up() with self.assertRaises(pymongo.errors.ConnectionFailure): yield fetch_next self.assertTrue(sock_info.closed) del cursor self.assertNotIn(sock_info, pool.sockets)
def run(self): settings = { "title": u"SoundSlash backend Web API", "xsrf_cookies": False, "debug": True, "db": motor.MotorClient("mongodb://*****:*****@127.0.0.1:27017/pipeline")["pipeline"], "facebook_api_key": "486579808100437", "facebook_secret": "5841eb1027ed61718249f034d503e612", "executor": ThreadPoolExecutor(max_workers=4), "streams": self.streams, "icecast": self.icecast } application = tornado.web.Application([ (r"/mount_add", MountAddHandler), (r"/mount_remove", MountRemoveHandler), (r"/listener_add", ListenerAddHandler), (r"/listener_remove", ListenerRemoveHandler), ], **settings) application.listen(self.port) logging.getLogger('pipeline_api').debug("Starting Auth Web API") tornado.ioloop.IOLoop.instance().start()
class StockDaily: db = motor.MotorClient(options.dbhost).ss_test def __init__(self): self.kk=42.36 @classmethod def loaddailystockfromdb(self, start,end ,stockcode): stock_dict = {} find_query = { "_id.d": {"$gte": start}, "_id.d": {"$lte":end}, "_id.c": stockcode } sort_query = [("_id.d", pymongo.ASCENDING)] cursor = self.db.stocks_daily.find(find_query,sort = sort_query) return cursor @classmethod def getRSV(self, stock_list , param1): rsv = [] times = [] timestr = [] closelist = [] begin = int(0) while begin+param1 < len(stock_list): high = max(map(lambda x: x['e'][2], stock_list[begin:begin+param1])) low = min(map(lambda x: x['e'][3], stock_list[begin:begin+param1])) close = stock_list[begin+param1-1]['e'][4] volume = stock_list[begin+param1-1]['e'][5] if 0 == high-low: high = high+0.1 elif 0 == volume: begin += 1 continue value = (close-low)/(high-low)*100 rsv.append( value ) closelist.append(close) print(begin) print(stock_list[begin:begin+param1]) print(high) print(low) print(close) print(value) print(stock_list[begin+param1-1]["_id"]["d"]) t = int(time.mktime(stock_list[begin+param1-1]["_id"]["d"].timetuple())) * 1000 times.append(t) timestr.append(stock_list[begin+param1-1]["_id"]["d"]) begin += 1 return times,rsv,closelist,timestr @classmethod def _avg(self, a): length = len(a) return sum(a) / length @classmethod def getK(self, values, window): array =[] begin = 0 global kk while begin+window < len(values): #average = self._avg(values[begin:begin+window]) rsv = values[begin] print("rsv%f :"%rsv) print("kk%f:"%kk) print("window:%d"%window) curmb = (rsv+(window-1)*kk)/window print("curmb:%f"%curmb) kk = curmb array.append(curmb ) begin += 1 return array @classmethod def getD(self, values, window): array =[] begin = 0 global dd while begin+window < len(values): #average = self._avg(values[begin:begin+window]) rsv = values[begin] print("rsv%f :"%rsv) print("kk%f:"%dd) print("window:%d"%window) curmb = (rsv+(window-1)*dd)/window print("curmb:%f"%curmb) dd = curmb array.append(curmb ) begin += 1 return array @classmethod def is_match(self, prev_k, prev_d, curr_k, curr_d): if prev_d <= prev_k and curr_d > curr_k: return 'sell' if prev_k <= prev_d and curr_k > curr_d: return 'buy' return '' @classmethod def cacl(self,k,d,c,timestr): begin = 4 global cur_stocknum,beginmoney,cur_totalmoney while begin < len(d): lenk = len(k) lenb = len(d) print("begin:%d"%begin) print("lenk:%d"%lenk) print("lenb:%d"%lenb) curr_k = k[begin] curr_d = d[begin] prev_k = k[begin-1] prev_d = d[begin-1] cur_price = c[begin] match = self.is_match(prev_k, prev_d, curr_k, curr_d) if 'buy'== match: buynum = int(cur_totalmoney/cur_price) cur_stocknum = cur_stocknum + buynum print("buy%d"%buynum) cur_totalmoney = cur_totalmoney - cur_stocknum*cur_price elif 'sell' == match: gainmoney = cur_stocknum*cur_price prev_totalmoney = cur_totalmoney cur_totalmoney = cur_totalmoney +gainmoney cur_stocknum = 0 print("buy%d"%cur_stocknum) rate2 = gainmoney/prev_totalmoney rate1 = cur_totalmoney/beginmoney-1 print("sell%d:"%cur_stocknum) print("cur_totalmoney%d"%cur_totalmoney) print("prev_totalmoney%d"%prev_totalmoney) print("gainmoney%d"%gainmoney) print("rate: %f "%rate1) print("rate: %f "%rate2) print(timestr[begin+3]) begin = begin +1 return @classmethod @gen.coroutine def getkdj(self,starttime,endtime,stock_id,n,m,m1): cursor = self.loaddailystockfromdb(starttime ,endtime,stock_id) stocklist = yield cursor.to_list(1000) i = 0 length = len(stocklist) while i < length: if stocklist[i]['e'][5] == 0: print("volume is 0") del stocklist[i] i=0 length = len(stocklist) i+=1 times ,rsv,c,timestr = self.getRSV(stocklist,n) k = self.getK(rsv,m) d = self.getD(k,m1) j = list(map(lambda x: 3*x[0]-2*x[1], zip(k[3:], d))) #k.extend([7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]) #d.extend([-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]) print(list(zip(timestr,k))) print("\n") print(list(zip(timestr,d))) print("\n") print(j) print("\n") self.cacl(k,d,c,timestr) raise gen.Return([list(zip(times,k)),list(zip(times,d)),list(zip(times,j))]) @classmethod def _ema(self, s, n): """ returns an n period exponential moving average for the time series s s is a list ordered from oldest (index 0) to most recent (index -1) n is an integer returns a numeric array of the exponential moving average """ if len(s) <= n: return "No enough item in %s" % s ema = [] j = 1 #get n sma first and calculate the next n period ema sma = sum(s[:n]) / n multiplier = 2 / float(1 + n) ema.append(sma) #EMA(current) = ( (Price(current) - EMA(prev) ) x Multiplier) + EMA(prev) ema.append(( (s[n] - sma) * multiplier) + sma) #now calculate the rest of the values for i in s[n+1:]: tmp = ( (i - ema[j]) * multiplier) + ema[j] j = j + 1 ema.append(tmp) return ema @classmethod @gen.coroutine def getmacd(self, starttime,endtime,stock_id,n,m,m1): cursor = self.loaddailystockfromdb(starttime ,endtime,stock_id) array = yield cursor.to_list(1000) for i in range(len(array)): if array[i]['e'][5] == 0: print("volume is 0") del array[i] prices = list(map(lambda x: x['e'][0], array)) #t = list(map(lambda x: int(time.mktime(x['e'][1].timetuple())) * 1000, array)) short_ema = self._ema(prices, 12) long_ema = self._ema(prices, 26) diff = list(map(lambda x: x[0]-x[1], zip(short_ema[::-1], long_ema[::-1]))) diff.reverse() dea = self._ema(diff, 9) bar = list(map(lambda x: 2*(x[0]-x[1]), zip(diff[::-1], dea[::-1]))) bar.reverse() raise gen.Return([ diff[8:] , dea , bar ])
with open(setting["config_filename"], "r") as fin: config = yaml.load(fin) for k, v in config["global"].items(): setting[k] = v if "session" in config: setting["session"]["driver_settings"] = config["session"] except: print "cannot found config.yaml file" sys.exit(0) # mongodb connection # format: mongodb://user:pass@host:port/ # database name: minos try: client = motor.MotorClient(config["database"]["config"]) database = client[config["database"]["db"]] setting["database"] = database except: print "cannot connect mongodb, check the config.yaml" sys.exit(0) application = tornado.web.Application( [(r"^/public/post/([a-f0-9]{24})", "controller.open.PostHandler"), (r"^/public/list(/(\d*))?", "controller.open.ListHandler"), (r"^/(page/(\d+))?", "controller.main.HomeHandler"), (r"^/login", "controller.auth.LoginHandler"), (r"^/register", "controller.auth.RegisterHandler"), (r"^/nologin/([a-z]+)", "controller.auth.AjaxHandler"), (r"^/forgetpwd", "controller.auth.ForgetpwdHandler"), (r"^/captcha\.png", "controller.auth.CaptchaHanlder"),
def test_io_loop(self): with self.assertRaises(TypeError): motor.MotorClient(test.env.uri, io_loop='foo')
async def test_grant(core4api): async def _access(access): """ Check if accessing collection ons mongodb is possible, try/except is caused by async, otherwise tests fail randomly :param access: :return: """ counter = 0 while True: try: counter += 1 mongo = motor.MotorClient("mongodb://*****:*****@testmongo:27017") _ = await mongo.server_info() _ = await mongo["core4test"].list_collection_names() time.sleep(1) break except pymongo.errors.OperationFailure as ops_fail: print(ops_fail.details['codeName']) time.sleep(1) if counter == 5: break continue except Exception as E: print("something really strange happen: ", E.details['codeName']) break assert await mongo.core4test.sys.role.count_documents({}) > 0 async def _no_access(access): counter = 0 with pytest.raises(pymongo.errors.OperationFailure): while True: try: counter += 1 mongo = motor.MotorClient( "mongodb://*****:*****@testmongo:27017") _ = await mongo.server_info() time.sleep(1) break except pymongo.errors.OperationFailure as ops_fail: print(ops_fail.details['codeName']) time.sleep(1) if counter == 5: break continue except Exception as E: print("something really strange happen: ", E.details['codeName']) break _ = await mongo["core4test"].list_collection_names() await core4api.login() data = { "name": "test_reg_test_role1", "realname": "test role1", "passwd": "123456", "email": "*****@*****.**", "role": ["standard_user"], "perm": [] } rv = await core4api.post("/core4/api/v1/roles", json=data) assert rv.code == 200 id = rv.json()["data"]["_id"] etag = rv.json()["data"]["etag"] await core4api.login("test_reg_test_role1", "123456") rv = await core4api.get("/core4/api/v1/profile") assert rv.code == 200 rv = await core4api.post("/core4/api/v1/access") assert rv.code == 200 access = rv.json()["data"]["mongodb"] # 1 await _no_access(access) core4api.set_admin() data = {"etag": etag, "perm": ["mongodb://core4test"]} rv = await core4api.put("/core4/api/v1/roles/" + id, json=data) assert rv.code == 200 etag = rv.json()["data"]["etag"] # 2 await _access(access) await core4api.login("test_reg_test_role1", "123456") rv = await core4api.post("/core4/api/v1/access") assert rv.code == 200 access = rv.json()["data"]["mongodb"] # 3 await _access(access) data = {"etag": etag, "realname": "no change"} core4api.set_admin() rv = await core4api.put("/core4/api/v1/roles/" + id, json=data) assert rv.code == 200 etag = rv.json()["data"]["etag"] # 4 await _access(access) data = {"etag": etag, "perm": ["mongodb://core4test", "mongodb://other"]} rv = await core4api.put("/core4/api/v1/roles/" + id, json=data) assert rv.code == 200 etag = rv.json()["data"]["etag"] # 5 await _access(access) await core4api.login("test_reg_test_role1", "123456") rv = await core4api.post("/core4/api/v1/access/mongodb") assert rv.code == 200 access = rv.json()["data"] # 6 await _access(access) data = {"etag": etag, "perm": ["mongodb://other"]} core4api.set_admin() rv = await core4api.put("/core4/api/v1/roles/" + id, json=data) assert rv.code == 200 # 7 await _no_access(access) await core4api.login("test_reg_test_role1", "123456") rv = await core4api.post("/core4/api/v1/access/mongodb") assert rv.code == 200 access = rv.json()["data"] # 8 await _no_access(access) core4api.set_admin() rv = await core4api.get("/core4/api/v1/roles/" + id) # 9 assert rv.code == 200 etag = rv.json()["data"]["etag"] data = {"etag": etag, "perm": ["mongodb://core4test"]} rv = await core4api.put("/core4/api/v1/roles/" + id, json=data) # 10 assert rv.code == 200 etag = rv.json()["data"]["etag"] await _access(access) rv = await core4api.delete("/core4/api/v1/roles/" + id + "/" + etag) # 11 assert rv.code == 200 counter = 0 while True: try: mongo = motor.MotorClient("mongodb://*****:*****@testmongo:27017") _ = await mongo.server_info() time.sleep(1) break except pymongo.errors.OperationFailure as aha: print(aha.details['codeName']) time.sleep(1) counter += 1 if counter == 5: break assert counter == 5 # 12 await _no_access(access) await core4api.login("test_reg_test_role1", "123456", 401) rv = await core4api.get("/core4/api/v1/profile") assert rv.code == 401
def test_secondary_connection(self): self.c = yield motor.MotorReplicaSetClient( self.seed, replicaSet=self.name).open() self.assertTrue(bool(len(self.c.secondaries))) db = self.c.motor_test yield db.test.remove({}, w=len(self.c.secondaries)) # Wait for replication... w = len(self.c.secondaries) + 1 yield db.test.insert({'foo': 'bar'}, w=w) # Test direct connection to a primary or secondary primary_host, primary_port = ha_tools.get_primary().split(':') primary_port = int(primary_port) (secondary_host, secondary_port) = ha_tools.get_secondaries()[0].split(':') secondary_port = int(secondary_port) arbiter_host, arbiter_port = ha_tools.get_arbiters()[0].split(':') arbiter_port = int(arbiter_port) # A connection succeeds no matter the read preference for kwargs in [ { 'read_preference': PRIMARY }, { 'read_preference': PRIMARY_PREFERRED }, { 'read_preference': SECONDARY }, { 'read_preference': SECONDARY_PREFERRED }, { 'read_preference': NEAREST }, ]: client = yield motor.MotorClient(primary_host, primary_port, **kwargs).open() self.assertEqual(primary_host, client.host) self.assertEqual(primary_port, client.port) self.assertTrue(client.is_primary) # Direct connection to primary can be queried with any read pref self.assertTrue((yield client.motor_test.test.find_one())) client = yield motor.MotorClient(secondary_host, secondary_port, **kwargs).open() self.assertEqual(secondary_host, client.host) self.assertEqual(secondary_port, client.port) self.assertFalse(client.is_primary) # Direct connection to secondary can be queried with any read pref # but PRIMARY if kwargs.get('read_preference') != PRIMARY: self.assertTrue((yield client.motor_test.test.find_one())) else: with assert_raises(AutoReconnect): yield client.motor_test.test.find_one() # Since an attempt at an acknowledged write to a secondary from a # direct connection raises AutoReconnect('not master'), MotorClient # should do the same for unacknowledged writes. try: yield client.motor_test.test.insert({}, w=0) except AutoReconnect as e: self.assertEqual('not master', e.args[0]) else: self.fail( 'Unacknowledged insert into secondary client %s should' 'have raised exception' % client) # Test direct connection to an arbiter client = yield motor.MotorClient(arbiter_host, arbiter_port, **kwargs).open() self.assertEqual(arbiter_host, client.host) self.assertEqual(arbiter_port, client.port) self.assertFalse(client.is_primary) # See explanation above try: yield client.motor_test.test.insert({}, w=0) except AutoReconnect as e: self.assertEqual('not master', e.args[0]) else: self.fail( 'Unacknowledged insert into arbiter connection %s should' 'have raised exception' % client)
def get_db(self): client = motor.MotorClient(io_loop=self.io_loop) return client[self.database_name]
else: return uri.rsplit('/')[-1] class QueryDataHandler: def prepare(self, request): pprint.pprint(request) def post(self, request): pprint.pprint(request) # data_json = tornado.escape.json_encode(self.request.arguments) pprint.pprint(self.request.body) if __name__ == "__main__": db = motor.MotorClient() application = tornado.web.Application( handlers=[ (r"/lodex2c", MainHandler), (r'/lodex2c/static/(.*)', tornado.web.StaticFileHandler, { 'path': './static' }), (r"/lodex2c/([0-9]+)", GraphHandler), (r"/lodex2c/getData/([0-9]+)", DataHandler), (r"/lodex2c/intensional(.*)", IntensionalHandler), (r"/lodex2c/int/Data(.*)", IntensionalDataHandler), # (r"/lodex2/query", QueryDataHandler) ], static_path=os.path.join(os.path.dirname(__file__), "static"), db=db)
def get_db(): return motor.MotorClient(CONF.mongo_url)[CONF.mongo_dbname]
class HomeHandler(BaseHandler): @tornado.web.authenticated def get(self): self.render('home.html', user=self.current_user) class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", HomeHandler), (r"/signup", SignupHandler), (r"/login", LoginHandler), (r"/logout", LogoutHandler), ] settings = { 'template_path': os.path.join(os.path.dirname(__file__), 'tpl'), 'static_path': os.path.join(os.path.dirname(__file__), 'static'), 'site_title': u"yutori", 'xsrf_cookies': True, 'debug': True } tornado.web.Application.__init__(self, handlers, **settings) if __name__ == "__main__": db = motor.MotorClient().yutori tornado.options.parse_command_line() http_server = tornado.httpserver.HTTPServer(Application()) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
def test_connection_failure(self): # Assuming there isn't anything actually running on this port client = motor.MotorClient('localhost', 8765, io_loop=self.io_loop) with assert_raises(ConnectionFailure): yield client.open()
def test_open_sync(self): loop = IOLoop() cx = loop.run_sync(motor.MotorClient(host, port, io_loop=loop).open) self.assertTrue(isinstance(cx, motor.MotorClient))
def test_io_loop(self): with assert_raises(TypeError): motor.MotorClient(host, port, io_loop='foo')
def db(self): if not self._db_connection: self._db_connection = motor.MotorClient().open_sync() return self._db_connection[settings.DATABASE_NAME]