示例#1
0
    def test_init_4(self):
        from shortening.service.models import db, User, Shortening
        from peewee import IntegrityError

        # Given...
        db = SqliteDatabase(TEST_DB, check_same_thread=False)
        db.connect()

        # remove all records
        Shortening.delete().execute()

        # Test...
        total = 1000
        t0 = time.time()
        for x in range(total):
            try:
                s = Shortening(user=User.get(login="******"), short_url=Shortening.generate_uniqie_shortening(),
                               long_url="http://google.com/")
                s.save()
            except IntegrityError:
                pass
                # Shortening already exists

        t1 = time.time()

        diff = t1 - t0
        diff_per_row = diff / total
        print("Insert records with random name performance test, method 1, set size: %s, time: %s s, per row: %s s" %
              (total, diff, diff_per_row))
        db.close()
示例#2
0
    def test_init_2(self):
        """
        Test form checking constructor parameters, unequal lists length
        """
        # Given...
        from shortening.service.models import db, User, Logs, Shortening
        from peewee import IntegrityError

        db.connect()
        if not User.table_exists():
            User.create_table()
        if not Logs.table_exists():
            Logs.create_table()
        if not Shortening.table_exists():
            Shortening.create_table()

        User.proxy = 1

        try:
            u = User(login='******', password='******')
            u.save()
        except IntegrityError:
            print("User already exists")

        users = User.select()
        for user in users:
            print('> %s' % (str(user),))

        db.close()
示例#3
0
    def test_init_1(self):
        db.connect()
        username = '******'
        password = '******'

        result = BaseHandler.check_credentials(username, password)
        self.assertIsNotNone(result)
        db.close()
示例#4
0
    def test_init_2(self):
        """
        Test form checking constructor parameters, unequal lists length
        """
        # Given...
        from shortening.service.models import db, User, Logs, Shortening
        from peewee import IntegrityError, DoesNotExist

        db.connect()
        if not User.table_exists():
            User.create_table()
        if not Logs.table_exists():
            Logs.create_table()
        if not Shortening.table_exists():
            Shortening.create_table()

        try:
            u = User(login='******', password='******')
            u.save()
        except IntegrityError:
            print("User already exists")

        users = User.select()
        for user in users:
            print('> %s' % (str(user),))

        try:
            s = Shortening(user=User.get(login="******"), short_url="0002", long_url="http://google.com/")
            s.save()
        except IntegrityError:
            print("Shortening already exists")

        shortenings = Shortening.select()
        for shortening in shortenings:
            print('>> %s' % (str(shortening),))

        try:
            s = Shortening.get(Shortening.short_url == "80001")
            print(":" + str(s))
        except DoesNotExist:
            print("Shortening not exists")

        db.close()
示例#5
0
    def test_service_performance(self):
        from shortening.service.models import db, Shortening

        def handle_request(response):
            if response.error:
                # should not be here !!!
                self.assertTrue(False)
            else:
                self.assertTrue(self.check_header(response))
                cookies = self.get_cookies(response)
                # RUN TEST
                url = BASE_URL + "/generate?long_url="
                self.asynchronous_test(cookies, url)

        # Given...
        db = SqliteDatabase(TEST_DB, check_same_thread=False)
        db.connect()

        # remove all records
        Shortening.delete().execute()

        # Test...
        app = Application()
        http_server = tornado.httpserver.HTTPServer(app)
        http_server.listen(options.port)
        print("Step1: /login")
        request = tornado.httpclient.HTTPRequest(BASE_URL + "/login?username=user1&password=pass1",
                                                 follow_redirects=False,
                                                 method='POST',
                                                 body="",
                                                 validate_cert=False)

        http_client = httpclient.AsyncHTTPClient()
        http_client.fetch(request, handle_request)
        self.wait(timeout=90)
        print("Finished")
示例#6
0
    def test_insert_existing_performance(self):
        from shortening.service.models import db, User, Shortening
        from peewee import IntegrityError, DoesNotExist

        # Given...
        db = SqliteDatabase(TEST_DB, check_same_thread=False)
        db.connect()

        # remove all records
        Shortening.delete().execute()

        # Test...
        total = 10000
        t0 = time.time()
        for x in range(total):
            try:
                s = Shortening(user=User.get(login="******"), short_url="0002", long_url="http://long_url/")
                s.save()
            except IntegrityError:
                pass
                # Shortening already exists

        t1 = time.time()
        diff = t1 - t0
        diff_per_row = diff / total
        print("Insert of existing record performance test, method 1, set size: %s, time: %s, per row: %s s" %
              (total, diff, diff_per_row))

        # THIS IS FASTEST METHOD
        # remove all records
        Shortening.delete().execute()
        t0 = time.time()
        for x in range(total):
            query = Shortening.select().where(Shortening.short_url == "80001")
            if not query.exists():
                # short_url does not exists exists
                pass

        t1 = time.time()
        diff = t1 - t0
        diff_per_row = diff / total
        print("Insert of existing record performance test, method 2, set size: %s, time: %s, per row: %s s" %
              (total, diff, diff_per_row))

        # remove all records
        Shortening.delete().execute()
        t0 = time.time()
        for x in range(total):
            try:
                s = Shortening.get(Shortening.short_url == "80001")
                print(":" + str(s))
            except DoesNotExist:
                # Shortening not exists
                pass

        t1 = time.time()
        diff = t1 - t0
        diff_per_row = diff / total
        print("Insert of existing record performance test, method 3, set size: %s, time: %s, per row: %s s" %
              (total, diff, diff_per_row))

        db.close()