test_compress.py 1.3 KB
Newer Older
Arthur Lutz's avatar
Arthur Lutz committed
1
2
3
from collections import defaultdict
from datetime import datetime, timedelta
import random
Arthur Lutz's avatar
Arthur Lutz committed
4
5
6
7

from cubicweb.devtools.testlib import CubicWebTC
from logilab.common import date

Arthur Lutz's avatar
Arthur Lutz committed
8
from utils import compress_old_hits
Arthur Lutz's avatar
Arthur Lutz committed
9
10
11
12
13
14


class CompressTest(CubicWebTC):

    def test_compress(self):
        req = self.request()
Arthur Lutz's avatar
Arthur Lutz committed
15
16
        update_stats=defaultdict(int)
        content = req.create_entity('Content', title=u'test')
Arthur Lutz's avatar
Arthur Lutz committed
17
18
19
20
21
22
23
        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)
Arthur Lutz's avatar
Arthur Lutz committed
24
25
26
27
        compress_old_hits(req, update_stats)
        # XXX 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.assertNotEqual(len(req.execute('Any X WHERE X is Hits')), 200)
Arthur Lutz's avatar
Arthur Lutz committed
28
29
30
31
32


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