Completed
Push — master ( 67eae9...f0f1ec )
by
unknown
01:23
created

ChartsTest.test_make_discrete_bar_chart()   A

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 1
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
from omaha.settings import KEY_PREFIX, KEY_LAST_ID
25
from omaha.utils import (
26
    get_sec_since_midnight,
27
    redis,
28
    get_id,
29
    create_id,
30
    make_piechart,
31
)
32
33
34
class UtilsTest(TestCase):
35
    def test_get_seconds_since_midnight(self):
36
        self.assertEqual(43,
37
                         get_sec_since_midnight(datetime(year=2014,
38
                                                         month=1,
39
                                                         day=1,
40
                                                         second=43)))
41
42
        self.assertEqual(0,
43
                         get_sec_since_midnight(datetime(year=2014,
44
                                                         month=1,
45
                                                         day=1)))
46
47
        self.assertEqual(3600,
48
                         get_sec_since_midnight(datetime(year=2014,
49
                                                         month=1,
50
                                                         day=1,
51
                                                         hour=1)))
52
53
        self.assertEqual(56508,
54
                         get_sec_since_midnight(datetime(year=2014,
55
                                                         month=1,
56
                                                         day=1,
57
                                                         hour=15,
58
                                                         minute=41,
59
                                                         second=48)))
60
61
        self.assertEqual((16 * 3600) + (23 * 60) + 17,
62
                         get_sec_since_midnight(datetime(year=2014,
63
                                                         month=1,
64
                                                         day=1,
65
                                                         hour=16,
66
                                                         minute=23,
67
                                                         second=17)))
68
69
70
class GetIdTest(TestCase):
71
    def setUp(self):
72
        self.uid = '{8C65E04C-0383-4AE2-893F-4EC7C58F70DC}'
73
        self.redis = redis
74
        self.redis.flushdb()
75
76
    def tearDown(self):
77
        self.redis.flushdb()
78
79
    def test_get_id_new(self):
80
        id = get_id(self.uid)
81
82
        self.assertIsInstance(id, int)
83
84
        _id = self.redis.get('{}:{}'.format(KEY_PREFIX, self.uid))
85
        self.assertEqual(id, int(_id))
86
        self.assertEqual(id, int(self.redis.get(KEY_LAST_ID)))
87
88
        get_id('new_uid')
89
        self.assertEqual(id + 1, int(self.redis.get(KEY_LAST_ID)))
90
91
    def test_get_id_exist(self):
92
        id = 123
93
        self.redis.set('{}:{}'.format(KEY_PREFIX, self.uid), 123)
94
        self.assertEqual(id, get_id(self.uid))
95
96
    def test_cteate_id(self):
97
        id = create_id(self.uid)
98
99
        self.assertIsInstance(id, int)
100
101
        _id = self.redis.get('{}:{}'.format(KEY_PREFIX, self.uid))
102
        self.assertEqual(id, int(_id))
103
        self.assertEqual(id, int(self.redis.get(KEY_LAST_ID)))
104
105
106
class ChartsTest(TestCase):
107
    def test_make_piechart(self):
108
        data = [('apple', 10), ('orange', 3)]
109
        self.assertDictEqual(make_piechart('test', data),
110
                             {'chartcontainer': 'chart_container_test',
111
                              'chartdata': {
112
                                  'extra1': {'tooltip': {'y_end': ' users', 'y_start': ''}},
113
                                  'x': ['apple', 'orange'],
114
                                  'y1': [10, 3]},
115
                              'charttype': 'pieChart',
116
                              'extra': {'jquery_on_ready': False,
117
                                        'tag_script_js': True,
118
                                        'x_axis_format': '',
119
                                        'x_is_date': False}})
120
121