Пример #1
0
    def test_disconnect(self):
        Connection.disconnect()

        connection_name_list = Connection.get_connection_name_list()
        connection_name = Connection.get_default_connection_name()

        self.assertEqual(['con'], connection_name_list)
        self.assertEqual(connection_name, 'con')
Пример #2
0
def test():
    Connection.connect('test')

    yield UserDocument.remove()
    
    bob_id = yield UserDocument.insert({
        'name': 'Bob',
        'email': '*****@*****.**',
        'age': 19
    })

    alice_id = yield UserDocument.insert({
        'name': 'Alice',
        'email': '*****@*****.**',
        'sex': 'female',
        'age': 18
    })

    post_id = yield PostDocument.insert({
        'author': DBRef(UserDocument.meta['collection'], bob_id),
        'publish_time': datetime.now(),
        'title': 'title',
    })

    comment = {
        'commentor': DBRef(UserDocument.meta['collection'], alice_id),
        'contents': 'I am comments.'
    }

    yield PostDocument.update({'_id': post_id}, 
                              {'$push': {'comments': comment}})

    user = yield UserDocument.find_one({'name': 'Bob'})
    posts = yield PostDocument.find().to_list(5)
    user_list = yield UserDocument.get_user_list_1()
    print user_list
    user_list = yield UserDocument.get_user_list_2()
    print user_list
    user_list = yield UserDocument.get_user_list_3()
    print user_list
    user_list = UserDocument.get_user_list_4()
    print user_list
    user_list = UserDocument.get_user_list_5()
    print user_list
    user_list = UserDocument.get_user_list_6()
    print user_list
Пример #3
0
def runserver():
    '''启动服务器'''

    signal(SIGINT, suicide)

    # for sleekxmpp
    reload(sys)
    sys.setdefaultencoding('utf8')

    define('port', default=8000, help='run on the given port', type=int)
    define("database", default="Young", help="database to use", type=str)
    define("debug", default=False, help="whether is debug mode", type=bool)

    parse_command_line()

    register_message_writers()
    register_message_readers()

    Connection.connect(options.database)

    setting.APPLICATION_SETTINGS.update({
        "debug": options.debug,
        "template_path": os.path.join(
            setting.ROOT_LOCATION,
            "app" if options.debug else "templates"
        ),
        "es": Elasticsearch(),
        "session_manager": SessionManager(
            Connection.get_database(pymongo=True).session
        )
    })
    application = Application(
        handlers=urlmap.urlpattern,
        **setting.APPLICATION_SETTINGS
    )

    http_server = HTTPServer(application, xheaders=True)
    http_server.listen(options.port)

    IOLoop.instance().start()
Пример #4
0
def runserver():
    '''启动服务器'''

    signal(SIGINT, suicide)

    # for sleekxmpp
    reload(sys)
    sys.setdefaultencoding('utf8')

    define('port', default=8000, help='run on the given port', type=int)
    define("database", default="Young", help="database to use", type=str)
    define("debug", default=False, help="whether is debug mode", type=bool)

    parse_command_line()

    register_message_writers()
    register_message_readers()

    Connection.connect(options.database)

    setting.APPLICATION_SETTINGS.update({
        "debug": options.debug,
        "template_path": os.path.join(
            setting.ROOT_LOCATION,
            "app" if options.debug else "templates"
        ),
        "es": Elasticsearch(),
        "session_manager": SessionManager(
            Connection.get_database(pymongo=True).session
        )
    })
    application = Application(
        handlers=urlmap.urlpattern,
        **setting.APPLICATION_SETTINGS
    )

    http_server = HTTPServer(application, xheaders=True)
    http_server.listen(options.port)

    IOLoop.instance().start()
Пример #5
0
 def tearDown(self):
     super(DocumentTest, self).tearDown()
     Connection.disconnect()
Пример #6
0
 def setUp(self):
     super(DocumentTest, self).setUp()
     Connection.connect('monguo_test')
Пример #7
0
 def tearDown(self):
     super(DocumentTest, self).tearDown()
     Connection.disconnect()
Пример #8
0
 def setUp(self):
     super(DocumentTest, self).setUp()
     Connection.connect('monguo_test')
Пример #9
0
    def test_connection(self):
        # test connect(), get_connection(), get_database()...
        Connection.connect('monguo_test1')
        connection = Connection.get_connection()
        database = Connection.get_database()
        db_name = Connection.get_default_database_name()
        connection_name = Connection.get_default_connection_name()

        self.assertIsInstance(connection, MotorClient)
        self.assertIsInstance(database, MotorDatabase)
        self.assertEqual(db_name, 'monguo_test1')
        self.assertEqual(connection_name, Connection.DEFAULT_CONNECTION_NAME)

        Connection.connect('monguo_test2', 'con')
        db_name = Connection.get_default_database_name()
        connection_name = Connection.get_default_connection_name()

        self.assertEqual(db_name, 'monguo_test2')
        self.assertEqual(connection_name, 'con')

        # test get_connection_name_list()
        connection_name_list = Connection.get_connection_name_list()
        self.assertIn(Connection.DEFAULT_CONNECTION_NAME, connection_name_list)
        self.assertIn('con', connection_name_list)
        self.assertEqual(sorted([Connection.DEFAULT_CONNECTION_NAME, 'con']), 
                         sorted(connection_name_list))

        # test switch_connection()
        Connection.switch_connection(Connection.DEFAULT_CONNECTION_NAME)
        connection_name = Connection.get_default_connection_name()
        self.assertEqual(connection_name, Connection.DEFAULT_CONNECTION_NAME)

        # test switch_database()
        Connection.switch_database('monguo_test3')
        db_name = Connection.get_default_database_name()
        self.assertEqual(db_name, 'monguo_test3')