~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/jobs/tests/test_cstat.py

[r=abentley][bug=][author=adeuring] show downloads in past 7, 30, 180 days on the charm details page.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2012, 2013 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
 
4
from datetime import datetime
4
5
import logging
5
6
 
6
7
from pyelasticsearch import ElasticSearch
7
8
from requests import Response
8
 
 
 
9
from urlparse import (
 
10
    parse_qsl,
 
11
    urlparse,
 
12
)
9
13
from charmworld.charmstore import CharmStore
10
14
from charmworld.jobs.cstat import update_counts
11
15
from charmworld.models import CharmSource
32
36
            @staticmethod
33
37
            def send(request):
34
38
                response = Response()
35
 
                if ('start' in request.url):
36
 
                    response._content = ('[["2010-12-24", 4],'
37
 
                                         ' ["2010-12-25", 1]]')
 
39
                params = dict(parse_qsl(urlparse(request.url).query))
 
40
                if ('start' in params):
 
41
                    start = datetime.strptime(params['start'], '%Y-%m-%d')
 
42
                    end = datetime.strptime(params['end'], '%Y-%m-%d')
 
43
                    days = (end - start).days
 
44
                    if days == 7:
 
45
                        response._content = ('[["2010-12-24", 4],'
 
46
                                             ' ["2010-12-25", 1]]')
 
47
                    elif days == 30:
 
48
                        response._content = ('[["2010-12-24", 10],'
 
49
                                             ' ["2010-12-24", 4],'
 
50
                                             ' ["2010-12-25", 1]]')
 
51
                    elif days == 182:
 
52
                        response._content = ('[["2010-12-23", 10],'
 
53
                                             ' ["2010-12-24", 4],'
 
54
                                             ' ["2010-12-25", 1]]')
 
55
                        response._content = ('[["2010-12-22", 100],'
 
56
                                             ' ["2010-12-23", 10],'
 
57
                                             ' ["2010-12-24", 4],'
 
58
                                             ' ["2010-12-25", 1]]')
38
59
                else:
39
60
                    response._content = '[[25]]'
40
61
                return response
42
63
        store.session = FakeSession
43
64
        update_counts(CharmSource(self.db, index_client), store, log)
44
65
        db_charm = self.db.charms.find_one({'_id': charm_id})
45
 
        self.assertEqual(5, db_charm['downloads_in_past_30_days'])
 
66
        self.assertEqual(5, db_charm['downloads_in_past_7_days'])
 
67
        self.assertEqual(15, db_charm['downloads_in_past_30_days'])
 
68
        self.assertEqual(115, db_charm['downloads_in_past_half_year'])
46
69
        self.assertEqual(25, db_charm['downloads'])
47
70
        index_charm = index_client.get(charm_id)
48
 
        self.assertEqual(5, index_charm['downloads_in_past_30_days'])
 
71
        self.assertEqual(5, index_charm['downloads_in_past_7_days'])
 
72
        self.assertEqual(15, index_charm['downloads_in_past_30_days'])
 
73
        self.assertEqual(115, index_charm['downloads_in_past_half_year'])
49
74
        self.assertEqual(25, index_charm['downloads'])
50
75
 
51
76
    def test_update_no_index(self):