summaryrefslogtreecommitdiffstats
path: root/omaha_server/sparkle/tests/test_statistics.py
blob: 18febb8681fc7aee8c4010828974c789be6e88bd (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# coding: utf8

"""
This software is licensed under the Apache 2 license, quoted below.

Copyright 2014 Crystalnix Limited

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
"""
from django.test import TestCase, RequestFactory

from datetime import datetime

import mock
from django_redis import get_redis_connection
from bitmapist import DayEvents, HourEvents
from freezegun import freeze_time

from omaha.utils import get_id
from sparkle.statistics import add_app_statistics, userid_counting

redis = get_redis_connection('statistics')


class StatisticsTest(TestCase):
    request_factory = RequestFactory()

    def setUp(self):
        redis.flushdb()

    def tearDown(self):
        redis.flushdb()

    @freeze_time('2016-1-1')
    def test_add_app_statistics(self):
        now = datetime.utcnow()
        next_month = now.replace(month=now.month + 1)
        userid = 1
        platform = 'mac'
        appid = '{F97917B1-19AB-48C1-9802-CEF305B10804}'
        version = '0.0.0.1'
        channel = 'test'
        test_app = dict(appid=appid, version=version, tag=channel)

        events_request_appid = lambda date=now: DayEvents.from_date('request:%s' % appid, date)
        events_new_appid = lambda date=now: DayEvents.from_date('new_install:%s' % appid, date)
        events_request_appid_version = lambda date=now: DayEvents.from_date('request:{}:{}'.format(appid, version),
                                                                            date)
        events_request_appid_platform = lambda date=now: DayEvents.from_date('request:{}:{}'.format(appid, platform),
                                                                             date)
        events_new_appid_platform = lambda date=now: DayEvents.from_date('new_install:{}:{}'.format(appid, platform),
                                                                         date)
        events_request_appid_channel = lambda date=now: DayEvents.from_date('request:{}:{}'.format(appid, channel),
                                                                            date)
        events_request_appid_platform_version = lambda date=now: DayEvents.from_date(
            'request:{}:{}:{}'.format(appid, platform, version), date)

        self.assertEqual(len(events_new_appid()), 0)
        self.assertEqual(len(events_request_appid()), 0)
        self.assertEqual(len(events_request_appid_version()), 0)
        self.assertEqual(len(events_request_appid_platform()), 0)
        self.assertEqual(len(events_new_appid_platform()), 0)
        self.assertEqual(len(events_request_appid_channel()), 0)
        self.assertEqual(len(events_request_appid_platform_version()), 0)

        add_app_statistics(userid, platform, test_app)
        self.assertEqual(len(events_new_appid()), 1)
        self.assertEqual(len(events_request_appid()), 0)
        self.assertEqual(len(events_request_appid_version()), 1)
        self.assertEqual(len(events_new_appid_platform()), 1)
        self.assertEqual(len(events_request_appid_platform()), 0)
        self.assertEqual(len(events_request_appid_channel()), 1)
        self.assertEqual(len(events_request_appid_platform_version()), 1)

        self.assertIn(userid, events_new_appid())
        self.assertIn(userid, events_request_appid_version())
        self.assertIn(userid, events_new_appid_platform())
        self.assertIn(userid, events_request_appid_channel())
        self.assertIn(userid, events_request_appid_platform_version())

        add_app_statistics(userid, platform, test_app)
        self.assertEqual(len(events_new_appid()), 1)
        self.assertEqual(len(events_request_appid()), 0)
        self.assertEqual(len(events_request_appid_version()), 1)
        self.assertEqual(len(events_new_appid_platform()), 1)
        self.assertEqual(len(events_request_appid_platform()), 0)
        self.assertEqual(len(events_request_appid_channel()), 1)
        self.assertEqual(len(events_request_appid_platform_version()), 1)

        with freeze_time(next_month):
            add_app_statistics(userid, platform, test_app)

        self.assertEqual(len(events_request_appid(next_month)), 1)
        self.assertEqual(len(events_request_appid_platform(next_month)), 1)
        self.assertEqual(len(events_new_appid(next_month)), 0)
        self.assertEqual(len(events_request_appid_version(next_month)), 1)
        self.assertEqual(len(events_new_appid_platform(next_month)), 0)
        self.assertEqual(len(events_request_appid_channel()), 1)
        self.assertEqual(len(events_request_appid_platform_version()), 1)

        self.assertIn(userid, events_request_appid(next_month))
        self.assertIn(userid, events_request_appid_platform(next_month))

    def test_update_live_statistics(self):
        userid1 = '{F07B3878-CD6F-4B96-B52F-95C4D23077E0}'
        user1_id = get_id(userid1)

        userid2 = '{EC4C5647-F798-4BCA-83DA-926CD448A1D5}'
        user2_id = get_id(userid2)

        appid = '{F97917B1-19AB-48C1-9802-CEF305B10804}'
        version = '0.0.0.1'
        test_app = dict(appid=appid, version=version)
        platform = 'mac'

        request_version_events = HourEvents('request:{}:{}'.format(appid, version))
        request_platform_version_events = HourEvents('request:{}:{}:{}'.format(appid, 'mac', version))

        self.assertFalse(user1_id in request_version_events)
        self.assertEqual(len(request_version_events), 0)
        self.assertFalse(user1_id in request_platform_version_events)
        self.assertEqual(len(request_platform_version_events), 0)

        userid_counting(userid1, test_app, platform)

        self.assertTrue(user1_id in request_version_events)
        self.assertEqual(len(request_version_events), 1)
        self.assertTrue(user1_id in request_platform_version_events)
        self.assertEqual(len(request_platform_version_events), 1)

        userid_counting(userid1, test_app, platform)

        self.assertTrue(user1_id in request_version_events)
        self.assertEqual(len(request_version_events), 1)
        self.assertTrue(user1_id in request_platform_version_events)
        self.assertEqual(len(request_platform_version_events), 1)

        userid_counting(userid2, test_app, platform)

        self.assertTrue(user2_id in request_version_events)
        self.assertEqual(len(request_version_events), 2)
        self.assertTrue(user2_id in request_platform_version_events)
        self.assertEqual(len(request_platform_version_events), 2)