test_compress.py 1.53 KB
Newer Older
Arthur Lutz's avatar
Arthur Lutz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

from cubicweb.devtools.testlib import CubicWebTC
from logilab.common import date
from datetime import datetime, timedelta
import random

from cubes.awstats.utils import compress_old_hits

#TODO fake schema addition with stats_about

class CompressTest(CubicWebTC):

#     def setUp(self):
#         super(CompressTest, self).setUp()
#         self.schema.add_entity_type(EntityType('Content'))
#         self.schema.add_relation_type(RelationType('stats_about'))
#         self.schema.add_relation_def(RelationDefinition('Hits', 'stats_about', 'Content'))

    def test_compress(self):
        req = self.request()
        content = req.create_entity('TestContent', title=u'test')
        for day in date.date_range(datetime.now()-timedelta(200), datetime.now()):
            stp = req.create_entity('StatPeriod', start=day, stop=day+timedelta(1))
            hit = req.create_entity('Hits', hit_type=u'normal',
                                    count=random.choice(range(2000)),
                                    period=stp, stats_about=content)
        self.assertEqual(len(req.execute('Any X WHERE X is Hits')), 200)
        self.assertEqual(len(req.execute('Any P WHERE P is StatPeriod, P start S, P stop E HAVING E-S >= 27')), 0)
        compress(req)
        #SQLITE bug self.assertEqual(len(req.execute('Any P WHERE P is StatPeriod, P start S, P stop E HAVING E-S >= 27')), 9)
        self.assertNotEquals(len(req.execute('Any X WHERE X is Hits')), 200)


if __name__ == '__main__':
    from logilab.common.testlib import unittest_main
    unittest_main()