Completed
Push — master ( 589bde...f684af )
by Egor
12s
created

StatisticsTest.test_userid_counting()   A

Complexity

Conditions 1

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 66
rs 9.3191
cc 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
from django.test import TestCase, RequestFactory
21
22
import mock
23
from django_redis import get_redis_connection
24
from bitmapist import DayEvents, HourEvents
25
26
from omaha.utils import get_id
27
from sparkle.statistics import update_live_statistics, collect_statistics
28
from omaha.statistics import add_app_statistics
29
30
redis = get_redis_connection('statistics')
31
32
class StatisticsTest(TestCase):
33
    request_factory = RequestFactory()
34
35
    def setUp(self):
36
        redis.flushdb()
37
38
    def tearDown(self):
39
        redis.flushdb()
40
41
    def test_userid_counting(self):
42
        userid1 = '{F07B3878-CD6F-4B96-B52F-95C4D23077E0}'
43
        user1_id = get_id(userid1)
44
45
        userid2 = '{EC4C5647-F798-4BCA-83DA-926CD448A1D5}'
46
        user2_id = get_id(userid2)
47
48
        appid = '{F97917B1-19AB-48C1-9802-CEF305B10804}'
49
        version = '0.0.0.1'
50
        channel = 'test'
51
        test_app = dict(appid=appid, version=version, tag=channel)
52
53
        request_app_events = DayEvents('request:%s' % appid)
54
        request_version_events = DayEvents('request:{}:{}'.format(appid, version))
55
        request_platform_events = DayEvents('request:{}:{}'.format(appid, 'mac'))
56
        request_channel_events = DayEvents('request:{}:{}'.format(appid, channel))
57
        request_platform_version_events = DayEvents('request:{}:{}:{}'.format(appid, 'mac', version))
58
59
        self.assertFalse(user1_id in request_app_events)
60
        self.assertEqual(len(request_app_events), 0)
61
        self.assertFalse(user1_id in request_version_events)
62
        self.assertEqual(len(request_version_events), 0)
63
        self.assertFalse(user1_id in request_platform_events)
64
        self.assertEqual(len(request_platform_events), 0)
65
        self.assertFalse(user1_id in request_channel_events)
66
        self.assertEqual(len(request_channel_events), 0)
67
        self.assertFalse(user1_id in request_platform_version_events)
68
        self.assertEqual(len(request_platform_version_events), 0)
69
70
        add_app_statistics(user1_id, 'mac', test_app)
71
        self.assertTrue(user1_id in request_app_events)
72
        self.assertEqual(len(request_app_events), 1)
73
        self.assertTrue(user1_id in request_version_events)
74
        self.assertEqual(len(request_version_events), 1)
75
        self.assertTrue(user1_id in request_platform_events)
76
        self.assertEqual(len(request_platform_events), 1)
77
        self.assertTrue(user1_id in request_channel_events)
78
        self.assertEqual(len(request_channel_events), 1)
79
        self.assertTrue(user1_id in request_platform_version_events)
80
        self.assertEqual(len(request_platform_version_events), 1)
81
82
        add_app_statistics(user1_id, 'mac', test_app)
83
84
        self.assertTrue(user1_id in request_app_events)
85
        self.assertEqual(len(request_app_events), 1)
86
        self.assertTrue(user1_id in request_version_events)
87
        self.assertEqual(len(request_version_events), 1)
88
        self.assertTrue(user1_id in request_platform_events)
89
        self.assertEqual(len(request_platform_events), 1)
90
        self.assertTrue(user1_id in request_channel_events)
91
        self.assertEqual(len(request_channel_events), 1)
92
        self.assertTrue(user1_id in request_platform_version_events)
93
        self.assertEqual(len(request_platform_version_events), 1)
94
95
        add_app_statistics(user2_id, 'mac', test_app)
96
97
        self.assertTrue(user2_id in request_app_events)
98
        self.assertEqual(len(request_app_events), 2)
99
        self.assertTrue(user2_id in request_version_events)
100
        self.assertEqual(len(request_version_events), 2)
101
        self.assertTrue(user2_id in request_platform_events)
102
        self.assertEqual(len(request_platform_events), 2)
103
        self.assertTrue(user2_id in request_channel_events)
104
        self.assertEqual(len(request_channel_events), 2)
105
        self.assertTrue(user2_id in request_platform_version_events)
106
        self.assertEqual(len(request_platform_version_events), 2)
107
108
    def test_update_live_statistics(self):
109
        userid1 = '{F07B3878-CD6F-4B96-B52F-95C4D23077E0}'
110
        user1_id = get_id(userid1)
111
112
        userid2 = '{EC4C5647-F798-4BCA-83DA-926CD448A1D5}'
113
        user2_id = get_id(userid2)
114
115
        appid = '{F97917B1-19AB-48C1-9802-CEF305B10804}'
116
        version = '0.0.0.1'
117
118
        request_version_events = HourEvents('online:{}:{}'.format(appid, version))
119
        request_platform_version_events = HourEvents('online:{}:{}:{}'.format(appid, 'mac', version))
120
121
        self.assertFalse(user1_id in request_version_events)
122
        self.assertEqual(len(request_version_events), 0)
123
        self.assertFalse(user1_id in request_platform_version_events)
124
        self.assertEqual(len(request_platform_version_events), 0)
125
126
        update_live_statistics(user1_id, appid, version)
127
128
        self.assertTrue(user1_id in request_version_events)
129
        self.assertEqual(len(request_version_events), 1)
130
        self.assertTrue(user1_id in request_platform_version_events)
131
        self.assertEqual(len(request_platform_version_events), 1)
132
133
        update_live_statistics(user1_id, appid, version)
134
135
        self.assertTrue(user1_id in request_version_events)
136
        self.assertEqual(len(request_version_events), 1)
137
        self.assertTrue(user1_id in request_platform_version_events)
138
        self.assertEqual(len(request_platform_version_events), 1)
139
140
        update_live_statistics(user2_id, appid, version)
141
142
        self.assertTrue(user2_id in request_version_events)
143
        self.assertEqual(len(request_version_events), 2)
144
        self.assertTrue(user2_id in request_platform_version_events)
145
        self.assertEqual(len(request_platform_version_events), 2)
146
147
    @mock.patch('sparkle.statistics.update_live_statistics')
148
    def test_collect_statistics(self, mock_update_live_statistics):
149
        app_id = '00000000-0000-0000-0000-000000000001'
150
        app_name = "Sparrow"
151
        channel = "test"
152
        version = "0.0.0.1"
153
        deviceID = "A37152BF-A3CB-5FEC-8230-FACF43BDCDDD"
154
        request = RequestFactory().get('/sparkle/%s/%s/appcast.xml?appVersionShort=%s&deviceID=%s' %
155
                                       (app_name, channel, version, deviceID))
156
157
        collect_statistics(request, app_id, channel)
158
159
        user_id = get_id(deviceID)
160
        mock_update_live_statistics.assert_called_with(user_id, app_id, version)
161
162