Пример #1
0
def create_application(url_generator=AsyncRandomURLGenerator(),
                       db=AsyncMomokoDBQueryExecutor(),
                       cursor_parser=AsyncSQLDataParser(),
                       logger=log,
                       executor=executors,
                       url_shortener_path=router_settings.url_shortener_path):
    return tornado.web.Application(
        [
            # Map the "/" url to main handler
            (r"/url_gen", URLGenHandler,
             dict(url_generator=url_generator,
                  db=db,
                  cursor_parser=cursor_parser,
                  url_shortener=url_shortener_path,
                  logger=logger)),
            (r'^%s\w+' % url_shortener_path, URLRedirectHandler,
             dict(db=db, cursor_parser=cursor_parser, logger=logger)),
            (r'^/url_latest_100', URLLatest100Handler,
             dict(db=db,
                  cursor_parser=cursor_parser,
                  executor=executor,
                  logger=logger)),
            (r'^/url_top_10_domain_30_days', URLTop10Domain30Days,
             dict(db=db, cursor_parser=cursor_parser, logger=logger)),
            (r'^/url_info', URLGetURLInfo,
             dict(db=db, cursor_parser=cursor_parser, logger=logger))
        ],
        **settings)
Пример #2
0
    def setUp(self):
        # Usage:
        #       Constructor for TestDB, primarly for setting up the momoko pool, and cursor parser
        #       so later in other unit tests, we can use them
        # Arguments:
        #       None

        # Need to call parent's setup
        super(TestDB, self).setUp()

        # The reason that we cannot use the same momoko_pool in momoko_pool.py is that
        # the unit tests generates it's own io_loop in the self.io_loop variable, hence
        # if we don't use it, momoko.pool isn't going to run on it's io_loop.
        self.momoko_db_connection = momoko.Pool(dsn='dbname=%s user=%s password=%s host=%s port=%s'
                                                    % (momoko_settings.dbname, momoko_settings.user,
                                                       momoko_settings.password, momoko_settings.host,
                                                       momoko_settings.port),
                                                size=momoko_settings.num_connections,
                                                ioloop=self.io_loop)

        # Call connect to establish connection with the momoko pool
        self.momoko_db_connection.connect()

        # Create an AsyncMomokoDBQueryExecutor so that we can use it's read/write functions
        self.db = AsyncMomokoDBQueryExecutor(momoko_pool=self.momoko_db_connection)

        # Create a cursor parser to get data from cursor
        self.cursor_parser = AsyncSQLDataParser()

        # A few dummy variables to test our database with
        self.dummy_shortened_url = "dummy_shortened_url"
        self.dummy_original_url = "dummy_original_url"
        self.dummy_domain = "dummy_domain"
        self.dummy_time = datetime.datetime.now().isoformat()
Пример #3
0
    def get_app(self):
        # Usage:
        #       Constructor for TestURLGenHandler, we will need to return the
        #       application in order for the http server that starts up in this
        #       class to be called. We also need to setup a momoko_db_connection using
        #       the class's own io_loop like when we tested momoko pool in tests_database.py.
        # Arguments:
        #       None

        # The reason that we cannot use the same momoko_pool in momoko_pool.py is that
        # the unit tests generates it's own io_loop in the self.io_loop variable, hence
        # if we don't use it, momoko.pool isn't going to run on it's io_loop.
        self.momoko_db_connection = momoko.Pool(dsn='dbname=%s user=%s password=%s host=%s port=%s'
                                                    % (momoko_settings.dbname, momoko_settings.user,
                                                       momoko_settings.password, momoko_settings.host,
                                                       momoko_settings.port),
                                                size=momoko_settings.num_connections,
                                                ioloop=self.io_loop)

        # Call connect to establish connection with the momoko pool
        self.momoko_db_connection.connect()

        # Create an AsyncMomokoDBQueryExecutor so that we can use it's read/write functions
        self.db = AsyncMomokoDBQueryExecutor(momoko_pool=self.momoko_db_connection)

        # Create a cursor parser to get data from cursor
        self.cursor_parser = AsyncSQLDataParser()

        # Return the application to our HTTP server.
        return router.create_application(db=self.db,
                                         cursor_parser=self.cursor_parser)