示例#1
0
    def test_redis_queue_lpush_rpop_with_encoder_prefix(self):
        queue = RedisQueue(RedisMock(), prefix='pre::', encoder=json)

        data1 = {"test": [1, 2, 3]}
        data2 = [1, 2, 3]
        queue.lpush("q1", 111111)
        queue.lpush("q2", 222222)
        queue.lpush("q1", data1)
        queue.lpush("q2", data2)
        queue.lpush("q1", 555555)
        queue.lpush("q2", 666666)

        assert queue.rpop("q1") == 111111
        assert queue.rpop("q1") == data1
        assert queue.rpop("q1") == 555555

        assert queue.rpop("q2") == 222222
        assert queue.rpop("q2") == data2
        assert queue.rpop("q2") == 666666
示例#2
0
    def test_redis_queue_lpush_rpop_with_encoder_prefix(self):
        queue = RedisQueue(RedisMock(), prefix='pre::', encoder=json)

        data1 = {"test": [1, 2, 3]}
        data2 = [1, 2, 3]
        queue.lpush("q1", 111111)
        queue.lpush("q2", 222222)
        queue.lpush("q1", data1)
        queue.lpush("q2", data2)
        queue.lpush("q1", 555555)
        queue.lpush("q2", 666666)

        assert queue.rpop("q1") == 111111
        assert queue.rpop("q1") == data1
        assert queue.rpop("q1") == 555555

        assert queue.rpop("q2") == 222222
        assert queue.rpop("q2") == data2
        assert queue.rpop("q2") == 666666
示例#3
0
    def test_lpush_and_rpop(self):
        queue = RedisQueue(RedisMock())
        #  Fill queue One and Two
        for i in range(0, 12):
            queue.lpush("One", i)
            queue.lpush("Two", i+30)

        # Check
        for i in range(0, 12):
            assert queue.rpop("One") == i
            assert queue.rpop("Two") == i+30
示例#4
0
    def test_lpush_and_rpop(self):
        queue = RedisQueue(RedisMock())
        #  Fill queue One and Two
        for i in range(0, 12):
            queue.lpush("One", i)
            queue.lpush("Two", i + 30)

        # Check
        for i in range(0, 12):
            assert queue.rpop("One") == i
            assert queue.rpop("Two") == i + 30
示例#5
0
class TestObelixLogging(unittest.TestCase):

    def setUp(self):
        """ setup any state tied to the execution of the given method in a
        class.  setup_method is invoked for every test method of a class.
        """
        self.cache = RedisStorage(RedisMock(), prefix='pre::', encoder=json)
        self.recommendations = RedisStorage(RedisMock(), 'recommendations::')
        self.queues = RedisQueue(RedisMock(), encoder=json)
        self.obelix = Obelix(self.cache, self.recommendations, self.queues)

    def test_log_search_result(self):
        obelix = self.obelix

        # def test_log_search(self):
        user_info = {'uid': 1, 'remote_ip': "127.0.0.1", "uri": "testuri"}
        record_ids = [[1, 88], [1, 2]]
        results_final_colls_scores = [[0.3, 0.5], [0.5, 0.2]]
        cols_in_result_ordered = ["Thesis", "Another"]
        seconds_to_rank_and_print = 2

        jrec, rg, rm, cc = 0, 10, "recommendations", "obelix"

        obelix.log("search_result", user_info, record_ids, record_ids,
                   results_final_colls_scores,
                   cols_in_result_ordered,
                   seconds_to_rank_and_print,
                   jrec, rg, rm, cc)

        storage_key = "{0}::{1}".format("last-search-result", user_info['uid'])
        log_storage = self.cache.get(storage_key)
        log_queue = self.queues.rpop("statistics-search-result")

        assert record_ids == log_storage['record_ids']
        assert results_final_colls_scores == \
            log_queue['results_final_colls_scores']

    def test_log_search_analytics(self):
        obelix = self.obelix

        user_info = {'uid': 1, 'remote_ip': "127.0.0.1", "uri": "testuri"}
        record_ids = [[1, 88], [1, 2]]
        results_final_colls_scores = [[0.3, 0.5], [0.5, 0.2]]
        cols_in_result_ordered = ["Thesis", "Another"]
        seconds_to_rank_and_print = 2

        jrec, rg, rm, cc = 0, 10, "recommendations", "obelix"

        obelix.log('search_result', user_info, record_ids, record_ids,
                   results_final_colls_scores,
                   cols_in_result_ordered,
                   seconds_to_rank_and_print,
                   jrec, rg, rm, cc)

        # TODO: Check if own queue are needed
        # log_queue = queues.lpop("statistics-search-result::1")
        log_queue = self.queues.lpop("statistics-search-result")

        assert str(user_info['uid']) == str(log_queue['uid'])
        assert user_info['remote_ip'] == log_queue['remote_ip']

    def test_log_page_view(self):
        obelix = self.obelix

        user_info = {'uid': 1, 'remote_ip': "127.0.0.1", "uri": "testuri"}
        record_ids = [[1, 88], [1, 2]]
        results_final_colls_scores = [[0.3, 0.5], [0.5, 0.2]]
        cols_in_result_ordered = ["Thesis", "Another"]
        seconds_to_rank_and_print = 2

        jrec, rg, rm, cc = 0, 10, "recommendations", "obelix"

        obelix.log('search_result', user_info, record_ids, record_ids,
                   results_final_colls_scores,
                   cols_in_result_ordered,
                   seconds_to_rank_and_print,
                   jrec, rg, rm, cc)
        obelix.log('page_view', user_info, 1)

        logged = self.queues.lpop("statistics-page-view")
        assert str(logged['uid']) == '1'
        # TODO: check cache "last-search-result"
        logged = self.queues.lpop("logentries")
        assert logged['type'] == "events.pageviews"
        assert str(logged['user']) == '1'

    def test_log_page_view_after_search_error(self):
        """ There should be no statistics
            because there was no search before
        """
        # TODO: Improve test
        obelix = self.obelix

        user_info = {'uid': 1, 'remote_ip': "127.0.0.1", "uri": "testuri"}
        record_ids = [[1, 88], [1, 2]]

        obelix.log('page_view_after_search', user_info, record_ids)

        logged = self.queues.lpop("statistics-page-view")
        assert logged is None
        # TODO: check cache "last-search-result"
        logged = self.queues.lpop("logentries")
        assert logged['type'] == "events.pageviews"
        assert str(logged['user']) == '1'

    def test_log_download_after_search(self):
        obelix = self.obelix

        user_info = {'uid': 5, 'remote_ip': "127.0.0.1", "uri": "testuri.pdf"}
        record_ids = [[1, 88], [1, 2]]
        results_final_colls_scores = [[0.3, 0.5], [0.5, 0.2]]
        cols_in_result_ordered = ["Thesis", "Another"]
        seconds_to_rank_and_print = 2
        downloaded_record = 88

        jrec, rg, rm, cc = 0, 10, "recommendations", "obelix"

        # log search
        obelix.log('search_result', user_info, record_ids, record_ids,
                   results_final_colls_scores,
                   cols_in_result_ordered,
                   seconds_to_rank_and_print,
                   jrec, rg, rm, cc)
        obelix.log('download_after_search', user_info, downloaded_record)
        # 'last-search-result::5'
        logged = self.queues.lpop("statistics-search-result")
        assert str(logged['uid']) == '5'
        # TODO: check cache "last-search-result"
        logged = self.queues.lpop("logentries")
        assert logged['type'] == "events.downloads"
        assert str(logged['user']) == '5'