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
|
|
|
|
21
|
|
|
from datetime import datetime |
22
|
|
|
from unittest import TestCase |
23
|
|
|
|
24
|
|
|
import django |
25
|
|
|
|
26
|
|
|
from omaha.factories import ( |
27
|
|
|
PlatformFactory, VersionFactory, |
28
|
|
|
ApplicationFactory, |
29
|
|
|
) |
30
|
|
|
from omaha.models import Version, Platform, Application |
31
|
|
|
from omaha.settings import KEY_PREFIX, KEY_LAST_ID |
32
|
|
|
from omaha.utils import ( |
33
|
|
|
get_sec_since_midnight, |
34
|
|
|
redis, |
35
|
|
|
get_id, |
36
|
|
|
create_id, |
37
|
|
|
make_piechart, |
38
|
|
|
get_platforms_by_appid, |
39
|
|
|
) |
40
|
|
|
from sparkle.factories import SparkleVersionFactory |
41
|
|
|
from sparkle.models import SparkleVersion |
42
|
|
|
|
43
|
|
|
class UtilsTest(TestCase): |
44
|
|
|
def test_get_seconds_since_midnight(self): |
45
|
|
|
self.assertEqual(43, |
46
|
|
|
get_sec_since_midnight(datetime(year=2014, |
47
|
|
|
month=1, |
48
|
|
|
day=1, |
49
|
|
|
second=43))) |
50
|
|
|
|
51
|
|
|
self.assertEqual(0, |
52
|
|
|
get_sec_since_midnight(datetime(year=2014, |
53
|
|
|
month=1, |
54
|
|
|
day=1))) |
55
|
|
|
|
56
|
|
|
self.assertEqual(3600, |
57
|
|
|
get_sec_since_midnight(datetime(year=2014, |
58
|
|
|
month=1, |
59
|
|
|
day=1, |
60
|
|
|
hour=1))) |
61
|
|
|
|
62
|
|
|
self.assertEqual(56508, |
63
|
|
|
get_sec_since_midnight(datetime(year=2014, |
64
|
|
|
month=1, |
65
|
|
|
day=1, |
66
|
|
|
hour=15, |
67
|
|
|
minute=41, |
68
|
|
|
second=48))) |
69
|
|
|
|
70
|
|
|
self.assertEqual((16 * 3600) + (23 * 60) + 17, |
71
|
|
|
get_sec_since_midnight(datetime(year=2014, |
72
|
|
|
month=1, |
73
|
|
|
day=1, |
74
|
|
|
hour=16, |
75
|
|
|
minute=23, |
76
|
|
|
second=17))) |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
class GetIdTest(TestCase): |
80
|
|
|
def setUp(self): |
81
|
|
|
self.uid = '{8C65E04C-0383-4AE2-893F-4EC7C58F70DC}' |
82
|
|
|
self.redis = redis |
83
|
|
|
self.redis.flushdb() |
84
|
|
|
|
85
|
|
|
def tearDown(self): |
86
|
|
|
self.redis.flushdb() |
87
|
|
|
|
88
|
|
|
def test_get_id_new(self): |
89
|
|
|
id = get_id(self.uid) |
90
|
|
|
|
91
|
|
|
self.assertIsInstance(id, int) |
92
|
|
|
|
93
|
|
|
_id = self.redis.get('{}:{}'.format(KEY_PREFIX, self.uid)) |
94
|
|
|
self.assertEqual(id, int(_id)) |
95
|
|
|
self.assertEqual(id, int(self.redis.get(KEY_LAST_ID))) |
96
|
|
|
|
97
|
|
|
get_id('new_uid') |
98
|
|
|
self.assertEqual(id + 1, int(self.redis.get(KEY_LAST_ID))) |
99
|
|
|
|
100
|
|
|
def test_get_id_exist(self): |
101
|
|
|
id = 123 |
102
|
|
|
self.redis.set('{}:{}'.format(KEY_PREFIX, self.uid), 123) |
103
|
|
|
self.assertEqual(id, get_id(self.uid)) |
104
|
|
|
|
105
|
|
|
def test_cteate_id(self): |
106
|
|
|
id = create_id(self.uid) |
107
|
|
|
|
108
|
|
|
self.assertIsInstance(id, int) |
109
|
|
|
|
110
|
|
|
_id = self.redis.get('{}:{}'.format(KEY_PREFIX, self.uid)) |
111
|
|
|
self.assertEqual(id, int(_id)) |
112
|
|
|
self.assertEqual(id, int(self.redis.get(KEY_LAST_ID))) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
class ChartsTest(TestCase): |
116
|
|
|
def test_make_piechart(self): |
117
|
|
|
data = [('apple', 10), ('orange', 3)] |
118
|
|
|
self.assertDictEqual(make_piechart('test', data), |
119
|
|
|
{'chartcontainer': 'chart_container_test', |
120
|
|
|
'chartdata': { |
121
|
|
|
'extra1': {'tooltip': {'y_end': ' users', 'y_start': ''}}, |
122
|
|
|
'x': ['apple', 'orange'], |
123
|
|
|
'y1': [10, 3]}, |
124
|
|
|
'charttype': 'pieChart', |
125
|
|
|
'extra': {'jquery_on_ready': False, |
126
|
|
|
'tag_script_js': True, |
127
|
|
|
'x_axis_format': '', |
128
|
|
|
'x_is_date': False}}) |
129
|
|
|
|
130
|
|
|
class GetPlatformTest(django.test.TestCase): |
131
|
|
|
def setUp(self): |
132
|
|
|
self.app = ApplicationFactory() |
133
|
|
|
self.a_platform = PlatformFactory(name='a') |
134
|
|
|
self.b_platform = PlatformFactory(name='b') |
135
|
|
|
self.empty_platform = PlatformFactory(name='empty') |
136
|
|
|
self.mac_platform = PlatformFactory(name='mac') |
137
|
|
|
VersionFactory(platform=self.a_platform, app=self.app) |
138
|
|
|
VersionFactory(platform=self.b_platform, app=self.app) |
139
|
|
|
|
140
|
|
View Code Duplication |
def test_without_mac_versions(self): |
|
|
|
|
141
|
|
|
self.assertEqual(Platform.objects.count(), 4) |
142
|
|
|
self.assertEqual(Version.objects.count(), 2) |
143
|
|
|
self.assertEqual(SparkleVersion.objects.filter(app=self.app).count(), 0) |
144
|
|
|
platforms = get_platforms_by_appid(self.app) |
145
|
|
|
self.assertEqual(len(platforms), 2) |
146
|
|
|
assert self.a_platform in platforms |
147
|
|
|
assert self.b_platform in platforms |
148
|
|
|
assert self.mac_platform not in platforms |
149
|
|
|
assert self.empty_platform not in platforms |
150
|
|
|
|
151
|
|
View Code Duplication |
def test_with_mac_versions(self): |
|
|
|
|
152
|
|
|
self.assertEqual(Platform.objects.count(), 4) |
153
|
|
|
self.assertEqual(Version.objects.count(), 2) |
154
|
|
|
mac_version = SparkleVersionFactory(app=self.app) |
155
|
|
|
self.assertEqual(SparkleVersion.objects.filter(app=self.app).count(), 1) |
156
|
|
|
platforms = get_platforms_by_appid(self.app) |
157
|
|
|
self.assertEqual(len(platforms), 3) |
158
|
|
|
assert self.a_platform in platforms |
159
|
|
|
assert self.b_platform in platforms |
160
|
|
|
assert self.mac_platform in platforms |
161
|
|
|
assert self.empty_platform not in platforms |