def setUp(self): """ Before each test, set up a environment. """ self.memcached_key = ('get_data', '()', '{}') utils.memcached_data = {} utils.get_data()
def test_group_by_weekday(self): """ Test of group_by_weekday """ self.assertDictEqual( utils.group_by_weekday(utils.get_data()[10]), { 0: [], 1: [30047], 2: [24465], 3: [23705], 4: [], 5: [], 6: [] } ) self.assertDictEqual( utils.group_by_weekday(utils.get_data()[11]), { 0: [24123], 1: [16564], 2: [25321], 3: [22969, 22999], 4: [6426], 5: [], 6: [] } )
def test_cache(self): """ Test if cache works. """ data_uncached = utils.get_data() os.rename(main.app.config['DATA_CSV'], main.app.config['DATA_CSV']+"_bckp") try: data_cached = utils.get_data() except: data_cached.clear() # check if cached data is retrieved corretly self.assertEqual(data_uncached, data_cached) time.sleep(20) try: data_cached = utils.get_data() except: data_cached.clear() #check if unable to retrieve data after cache timeout self.assertNotEqual(data_uncached, data_cached) os.rename(main.app.config['DATA_CSV']+"_bckp", main.app.config['DATA_CSV'])
def test_group_by_weekday_start_end(self): """ Test of group_by_weekday_start_end """ self.assertDictEqual( utils.group_by_weekday_start_end(utils.get_data()[10]), { 0: {'start': [], 'end': []}, 1: {'start': [34745], 'end': [64792]}, 2: {'start': [33592], 'end': [58057]}, 3: {'start': [38926], 'end': [62631]}, 4: {'start': [], 'end': []}, 5: {'start': [], 'end': []}, 6: {'start': [], 'end': []} } ) self.assertDictEqual( utils.group_by_weekday_start_end(utils.get_data()[11]), { 0: {'start': [33134], 'end': [57257]}, 1: {'start': [33590], 'end': [50154]}, 2: {'start': [33206], 'end': [58527]}, 3: {'start': [37116, 34088], 'end': [60085, 57087]}, 4: {'start': [47816], 'end': [54242]}, 5: {'start': [], 'end': []}, 6: {'start': [], 'end': []} } )
def test_get_data_cache(self): """ Test caching function for parsing of CSV file. Checking data is identical. """ data = utils.get_data() time.sleep(3) data2 = utils.get_data() self.assertEqual(data, data2)
def test_cache(self): """ Test caching decorator. """ utils.get_data() timeout1 = utils.cache['((t(dp0\ntp1\n.']['timeout'] self.assertEqual( utils.cache['((t(dp0\ntp1\n.']['value'], { 10: { datetime.date(2013, 9, 10): { 'start': datetime.time(9, 39, 5), 'end': datetime.time(17, 59, 52) }, datetime.date(2013, 9, 12): { 'start': datetime.time(10, 48, 46), 'end': datetime.time(17, 23, 51) }, datetime.date(2013, 9, 11): { 'start': datetime.time(9, 19, 52), 'end': datetime.time(16, 7, 37) } }, 11: { datetime.date(2013, 9, 13): { 'start': datetime.time(13, 16, 56), 'end': datetime.time(15, 4, 2) }, datetime.date(2013, 9, 12): { 'start': datetime.time(10, 18, 36), 'end': datetime.time(16, 41, 25) }, datetime.date(2013, 9, 11): { 'start': datetime.time(9, 13, 26), 'end': datetime.time(16, 15, 27) }, datetime.date(2013, 9, 10): { 'start': datetime.time(9, 19, 50), 'end': datetime.time(13, 55, 54) }, datetime.date(2013, 9, 9): { 'start': datetime.time(9, 12, 14), 'end': datetime.time(15, 54, 17) }, datetime.date(2013, 9, 5): { 'start': datetime.time(9, 28, 8), 'end': datetime.time(15, 51, 27) } } }) self.assertNotEqual(utils.cache, {}) utils.get_data() timeout2 = utils.cache['((t(dp0\ntp1\n.']['timeout'] self.assertEqual(timeout1, timeout2)
def test_caching(self): """ Check of caching decorator works correctly. """ data = utils.get_data() main.app.config.update({ 'DATA_CSV': TEST_DATA_CSV_2, }) new_data = utils.get_data() self.assertDictEqual(data, new_data)
def test_get_data_caching(self): """ Test caching of get_data method. """ data = utils.get_data() main.app.config.update({ 'DATA_CSV': TEST_DATA_CSV_2, }) data_cached = utils.get_data() self.assertDictEqual(data, data_cached)
def test_cache(self): """ Test cache. """ utils.CACHE = {} data1 = utils.get_data() data2 = {} self.assertNotEqual(data1, data2) data2 = utils.get_data() self.assertEqual(data1, data2) self.assertIsInstance(data1, dict)
def test_invalid_data_handled(self, mocked_log): """ On introduction of invalid data logger with message 'Problem with line 0' should be called. """ main.app.config.update({'DATA_CSV': INVALID_FORMAT_TEST_DATA}) with self.assertRaises(UnboundLocalError): utils.get_data() self.assertEqual(mocked_log.debug.call_count, 1) self.assertEqual(mocked_log.debug.call_args[0], ('Problem with line %d: ', 0))
def test_get_data_cache(self): data = utils.get_data() self.assertDictEqual(data, utils.CACHE[0]['data']) main.app.config.update({'DATA_CSV': TEST_CACHE_DATA_CSV1}) data = utils.get_data() self.assertDictEqual(data, utils.CACHE[0]['data']) utils.CACHE = {} data = utils.get_data() self.assertDictEqual(data, utils.CACHE[0]['data'])
def test_cache(self): """ Cache test. """ first_data = utils.get_data() main.app.config.update({'DATA_CSV': TEST_CACHE_DATA_CSV}) second_data = utils.get_data() self.assertDictEqual(first_data, second_data) utils.CACHE = {} second_data = utils.get_data() self.assertNotEqual(first_data, second_data) utils.CACHE = {}
def test_cache_get_data(self): """ Test Caching decorator to global variable. """ data = utils.get_data() main.app.config.update({'DATA_CSV': TEST_CACHE_DATA}) cache_data = utils.get_data() self.assertEqual(data, cache_data) utils.CACHE = {} utils.TIMESTAMPS = {} new_data = utils.get_data() self.assertNotEqual(data, new_data)
def test_caching(self): """ Check of caching decorator works correctly. """ data = utils.get_data() main.app.config.update( { 'DATA_CSV': TEST_DATA_CSV_2, } ) new_data = utils.get_data() self.assertDictEqual(data, new_data)
def test_get_data(self): """ Test parsing of CSV file. """ self.assertIsInstance(utils.get_data(), dict) self.assertItemsEqual(utils.get_data().keys(), [10, 11]) sample_date = datetime.date(2013, 9, 10) self.assertIn(sample_date, utils.get_data()[10]) self.assertItemsEqual(utils.get_data()[10][sample_date].keys(), ['start', 'end']) self.assertEqual(utils.get_data()[10][sample_date]['start'], datetime.time(9, 39, 5))
def test_cache(self): """ Test cache function """ data1 = utils.get_data() main.app.config.update({'DATA_CSV': TEST_CACHE_CSV}) data2 = utils.get_data() self.assertEqual(data1, data2) utils.CACHE = {} data2 = utils.get_data() self.assertNotEqual(data1, data2) self.assertIsInstance(data1, dict) self.assertIsInstance(data2, dict) utils.CACHE = {}
def test_recaching_get_data(self): """ Test re-caching of get_data(). """ then = datetime.datetime.now() - datetime.timedelta(seconds=600) utils.memcached_data[self.memcached_key]['exp_date'] = then utils.get_data() self.assertNotEqual( utils.memcached_data[self.memcached_key]['exp_date'], then) now = utils.memcached_data[self.memcached_key]['exp_date'] utils.get_data() self.assertEqual(utils.memcached_data[self.memcached_key]['exp_date'], now)
def test_cache(self): """ Test caching of CSV file """ result = utils.get_data() main.app.config.update({'DATA_CSV': TEST_CACHE_CSV}) result_cached = utils.get_data() self.assertEqual(result_cached, result) utils.TIME = {} utils.CACHE = {} new_result = utils.get_data() self.assertNotEqual(new_result, result) main.app.config.update({'DATA_CSV': TEST_DATA_CSV}) utils.TIME = {} utils.CACHE = {}
def test_get_data_broken_datasource(self): """ Test parsing of CSV file - testing broken data (bad types). """ main.app.config.update({'DATA_CSV': TEST_BROKEN_DATA_CSV}) data_broken = utils.get_data() self.assertEqual(len(data_broken[11]), 5)
def users_view(): """ Users listing for dropdown consisting of user_id, name and image url. If user data can be found in USERS_XML_FILE, user's actual name is used and image_url is added. Otherwise, id is used and avatar_url is set to null. """ data = get_data() users_data = get_user_data() named_users = [] unnamed_users = [] for i in data.keys(): try: named_users.append({ 'user_id': i, 'name': users_data[i]['name'], }) except KeyError: unnamed_users.append({ 'user_id': i, 'name': 'User {}'.format(str(i)), }) named_users.sort(key=lambda user_name: user_name['name'], cmp=locale.strcoll) # Users without names should be sorted by ID which is part of their 'names' like User 10. unnamed_users.sort(key=lambda user_id: int(user_id['name'].split()[1])) return named_users + unnamed_users
def test_group_by_weekday(self): """ Test grouping dates by weekdays. """ data = utils.get_data() weekdays = utils.group_by_weekday(data[10]) self.assertEqual(weekdays[0][0], 30927)
def test_group_start_end_weekday(self): data = utils.get_data() start_end_data = utils.group_start_end_weekday(data[10]) self.assertListEqual(start_end_data, [{ 'start': [], 'end': [] }, { 'start': [34745], 'end': [64792] }, { 'start': [33592], 'end': [58057] }, { 'start': [38926], 'end': [62631] }, { 'start': [], 'end': [] }, { 'start': [], 'end': [] }, { 'start': [], 'end': [] }])
def users_view(): """Users listing for dropdown.""" data = get_data() return [{ 'user_id': i, 'name': 'User {0}'.format(str(i)) } for i in data.keys()]
def users_view(): """ Users listing for dropdown. """ data = get_data() return [{'user_id': i, 'name': 'User {0}'.format(str(i))} for i in data.keys()]
def presence_top_5_users_monthly_view(month): # pylint: disable=invalid-name """ Return 5 users from top of mean presence by month. """ months = calendar.month_name[1:] if month not in months: abort(404) data_xml = get_data_xml() data = get_data() result = [] for user_id in data_xml: result.append( { 'avatar': data_xml[user_id]['avatar'], 'user_id': user_id, 'name': data_xml[user_id]['name'], 'mean': mean_by_month( data.get(user_id, []) )[months.index(month)], } ) return sorted( result, key=lambda sort_by: sort_by['mean'], reverse=True )[:5]
def test_group_start_end_by_weekday(self): """ Test grouping starts and ends by weekday """ data = utils.get_data() result = utils.group_start_end_by_weekday(data[10]) self.assertDictContainsSubset(result, { 0: {'end': [], 'start': []}, 1: { 'end': [datetime.time(17, 59, 52)], 'start': [datetime.time(9, 39, 5)] }, 2: { 'end': [datetime.time(16, 7, 37)], 'start': [datetime.time(9, 19, 52)] }, 3: { 'end': [datetime.time(17, 23, 51)], 'start': [datetime.time(10, 48, 46)] }, 4: {'end': [], 'start': []}, 5: {'end': [], 'start': []}, 6: {'end': [], 'start': []}} )
def monthly_hours_view(user_id): data = get_data() if user_id not in data: log.debug('User %s not found!', user_id) return 404 return monthly_hours(data[user_id])
def test_group_by_weekday(self): """Test groups entries by weekday""" data = utils.get_data() result = utils.group_by_weekday(data[10]) self.assertEqual(len(result), 7) self.assertIsInstance(result, dict) self.assertIsInstance(result[2], list) self.assertDictEqual(result, { 0: [], 1: [utils.interval (data[10][datetime.date(2013, 9, 10)]['start'], data[10][datetime.date(2013, 9, 10)]['end'] ) ], 2: [utils.interval (data[10][datetime.date(2013, 9, 11)]['start'], data[10][datetime.date(2013, 9, 11)]['end'] ) ], 3: [utils.interval (data[10][datetime.date(2013, 9, 12)]['start'], data[10][datetime.date(2013, 9, 12)]['end'] ) ], 4: [], 5: [], 6: [], } ) self.assertEqual(len(result[3]), 1)
def test_get_data_broken_datasource2(self): """ Test parsing of CSV file - testing broken data (additional column). """ main.app.config.update({'DATA_CSV': TEST_BROKEN_DATA2_CSV}) data_broken2 = utils.get_data() self.assertEqual(len(data_broken2), 0)
def test_count_avg_group_by_weekday(self): """ Testing returned presence starts, ends by weekday. """ sample_data = utils.get_data() test_data = utils.count_avg_group_by_weekday( sample_data[10], ) self.assertEqual( test_data, { 0: {'start': [], 'end': []}, 1: {'start': [34745], 'end': [64792]}, 2: {'start': [33592], 'end': [58057]}, 3: {'start': [38926], 'end': [62631]}, 4: {'start': [], 'end': []}, 5: {'start': [], 'end': []}, 6: {'start': [], 'end': []}, } ) test_data = utils.count_avg_group_by_weekday( sample_data[11], ) self.assertAlmostEqual( test_data, { 0: {'start': [33134], 'end': [57257]}, 1: {'start': [33590], 'end': [50154]}, 2: {'start': [33206], 'end': [58527]}, 3: {'start': [37116, 34088], 'end': [60085, 57087]}, 4: {'start': [47816], 'end': [54242]}, 5: {'start': [], 'end': []}, 6: {'start': [], 'end': []}, } )
def test_group_by_weekday(self): """ Testing groups presence entries by weekday """ import_data = utils.get_data() # user_10 user_10 = utils.group_by_weekday(import_data[10]) self.assertEqual(user_10.keys(), range(7)) self.assertDictEqual(user_10, { 0: [], 1: [30047], 2: [24465], 3: [23705], 4: [], 5: [], 6: [], }) # user_11 user_11 = utils.group_by_weekday(import_data[11]) self.assertEqual(user_11.keys(), range(7)) self.assertDictEqual(user_11, { 0: [24123], 1: [16564], 2: [25321], 3: [22969, 22999], 4: [6426], 5: [], 6: [], })
def test_get_data_corrupted_id_as_list(self, csv_reader): """ Test if method log problem for corrupted id """ csv_reader.return_value = [ [[1, 2], '2013-09-13', '13:16:56', '13:16:56']] self.assertEqual(utils.get_data(), {})
def test_mean_group_by_weekday_seconds(self): """ Testing groups presence entries by weekday with seconds """ import_data = utils.get_data() # user_10 user_10 = utils.mean_group_by_weekday_seconds(import_data[10]) self.assertEqual(user_10.keys(), range(7)) self.assertDictEqual(user_10, { 0: {'end': [], 'start': []}, 1: {'end': [64792], 'start': [34745]}, 2: {'end': [58057], 'start': [33592]}, 3: {'end': [62631], 'start': [38926]}, 4: {'end': [], 'start': []}, 5: {'end': [], 'start': []}, 6: {'end': [], 'start': []} }) # user_11 user_11 = utils.mean_group_by_weekday_seconds(import_data[11]) self.assertEqual(user_11.keys(), range(7)) self.assertDictEqual(user_11, { 0: {'end': [57257], 'start': [33134]}, 1: {'end': [50154], 'start': [33590]}, 2: {'end': [58527], 'start': [33206]}, 3: {'end': [60085, 57087], 'start': [37116, 34088]}, 4: {'end': [54242], 'start': [47816]}, 5: {'end': [], 'start': []}, 6: {'end': [], 'start': []} })
def test_group_by_weekday(self): """ Test if function correctly groups by weekdays. """ data = utils.get_data() li = [[], [30047], [24465], [23705], [], [], []] self.assertEqual(utils.group_by_weekday(data[10]), li)
def test_group_by_start_end(self): """Test group by start-end""" data = utils.get_data() self.assertIsNotNone(data) result = utils.group_by_start_end(data[11]) self.assertIsNotNone(result) self.assertIsInstance(data, dict) self.assertEqual(len(result), 7) self.assertDictEqual(result, { 0: {'start': [33134], 'end': [57257]}, 1: {'start': [33590], 'end': [50154]}, 2: {'start': [33206], 'end': [58527]}, 3: {'start': [37116, 34088], 'end': [60085, 57087]}, 4: {'start': [47816], 'end': [54242]}, 5: {'start': [], 'end': []}, 6: {'start': [], 'end': []} } ) result = utils.group_by_start_end(data[10]) self.assertIsNotNone(result) self.assertIsInstance(data, dict) self.assertEqual(len(result), 7) self.assertDictEqual(result, { 0: {'start': [], 'end': []}, 1: {'start': [34745], 'end': [64792]}, 2: {'start': [33592], 'end': [58057]}, 3: {'start': [38926], 'end': [62631]}, 4: {'start': [], 'end': []}, 5: {'start': [], 'end': []}, 6: {'start': [], 'end': []} } )
def test_group_by_weekday(self): """ Test grouping presences by weekday. """ test_data = { datetime.date(2017, 4, 18): { 'start': datetime.time(8, 15, 0), 'end': datetime.time(16, 0, 0), }, datetime.date(2017, 4, 19): { 'start': datetime.time(13, 21, 30), 'end': datetime.time(15, 4, 2), }, } self.assertEqual([[], [27900], [6152], [], [], [], []], utils.group_by_weekday(test_data)) # more dates for weekday 1 to test if they are counted test_data[datetime.date(2017, 4, 25)] = { 'start': datetime.time(0, 0, 0), 'end': datetime.time(0, 0, 0), } test_data[datetime.date(2017, 4, 11)] = { 'start': datetime.time(8, 0, 0), 'end': datetime.time(16, 0, 0), } self.assertEqual(3, len(utils.group_by_weekday(test_data)[1])) data = utils.get_data() self.assertEqual([[], [30047], [24465], [23705], [], [], []], utils.group_by_weekday(data[10])) self.assertEqual( [[24123], [16564], [25321], [22969, 22999], [6426], [], []], utils.group_by_weekday(data[11]))
def test_group_by_weekday(self): """ Test groups precence entries by weekday """ box = utils.get_data() data = utils.group_by_weekday(box[10]) self.assertIsInstance(data, dict) sample_data = { 0: [], 1: [30047], 2: [24465], 3: [23705], 4: [], 5: [], 6: [], } self.assertDictEqual(data, sample_data) data = utils.group_by_weekday(box[11]) self.assertIsInstance(data, dict) sample_data = { 0: [24123], 1: [16564], 2: [25321], 3: [22969, 22999], 4: [6426], 5: [], 6: [], } self.assertDictEqual(data, sample_data)
def test_group_by_weekday(self): """ Test if function is grouping by weekdays. """ data = utils.get_data() expected = { 0: [], 1: [30047], 2: [24465], 3: [23705], 4: [], 5: [], 6: [], } expected_2 = { 0: [24123], 1: [16564], 2: [25321], 3: [22969, 22999], 4: [6426], 5: [], 6: [], } self.assertDictEqual(utils.group_by_weekday(data[10]), expected) self.assertDictEqual(utils.group_by_weekday(data[11]), expected_2)
def test_group_start_end_by_weekday(self): """ Test grouping start and end time by weekday """ expected_result = { 0: { 'starts': [], 'ends': [] }, 1: { 'starts': [34745], 'ends': [64792] }, 2: { 'starts': [33592], 'ends': [58057] }, 3: { 'starts': [38926], 'ends': [62631] }, 4: { 'starts': [], 'ends': [] }, 5: { 'starts': [], 'ends': [] }, 6: { 'starts': [], 'ends': [] } } data = utils.get_data() sample_data = utils.group_start_end_by_weekday(data[10]) self.assertIsInstance(sample_data, dict) self.assertEqual(len(sample_data), 7) self.assertEqual(sample_data, expected_result)
def test_start_end_presence(self): """ Test calculations of start/end presence. """ data = utils.get_data() expected = { 0: {'start': [], 'end': []}, 1: {'start': [34745], 'end': [64792]}, 2: {'start': [33592], 'end': [58057]}, 3: {'start': [38926], 'end': [62631]}, 4: {'start': [], 'end': []}, 5: {'start': [], 'end': []}, 6: {'start': [], 'end': []}, } expected_2 = { 0: {'start': [33134], 'end': [57257]}, 1: {'start': [33590], 'end': [50154]}, 2: {'start': [33206], 'end': [58527]}, 3: {'start': [37116, 34088], 'end': [60085, 57087]}, 4: {'start': [47816], 'end': [54242]}, 5: {'start': [], 'end': []}, 6: {'start': [], 'end': []}, } self.assertDictEqual(utils.start_end_presence(data[10]), expected) self.assertDictEqual(utils.start_end_presence(data[11]), expected_2)
def test_group_by_weekday(self): """ Test groups presence entries by weeekday. """ sample_data = utils.get_data() result = utils.group_by_weekday(sample_data[10]) self.assertDictEqual({ 0: [], 1: [30047], 2: [24465], 3: [23705], 4: [], 5: [], 6: [], }, result) self.assertIsInstance(utils.group_by_weekday(sample_data[11]), dict) self.assertDictEqual({ 0: [24123], 1: [16564], 2: [25321], 3: [22969, 22999], 4: [6426], 5: [], 6: [], }, result)
def test_group_by_month(self): """ Test group_by_month. """ testData1 = utils.group_by_months(utils.get_data()[11]) testData2 = utils.group_by_months(utils.get_data()[10]) self.assertDictEqual( testData1, { '2013.09': 118402 } ) self.assertDictEqual( testData2, { '2013.09': 78217 } )
def test_group_by_weekday(self): """ test if grouping is right """ user_id = 11 data = utils.get_data() groups = [[24123], [16564], [25321], [22969, 22999], [6426], [], []] self.assertListEqual(groups, utils.group_by_weekday(data[user_id]))
def test_caching(self): """ After calling get_data data should appear in cache. """ self.assertEqual(utils._storage, {}) result = utils.get_data() key = utils.get_key_hash(utils.get_data.__name__) self.assertEqual(result, utils._storage[key]['result'])
def test_group_by_start_end_time(self): """Test groups start and end time entries by weekday.""" proper_data = [[0, 0], [34745, 64792], [33592, 58057], [38926, 62631], [0, 0], [0, 0], [0, 0]] data = utils.get_data() self.assertEqual(proper_data, utils.group_by_start_end_time(data[10]))
def test_group_by_weekday(self): """ Test group presence result by weekday """ weekdays = utils.group_by_weekday(utils.get_data()[10]) self.assertIsInstance(weekdays, list) self.assertEqual(len(weekdays), 7) self.assertEqual(weekdays[3], [23705])
def test_group_by_weekday(self): sample_data = utils.get_data() group_data = utils.group_by_weekday(sample_data[10]) self.assertEqual(group_data, [[], [30047], [24465], [23705], [], [], []]) group_data = utils.group_by_weekday(sample_data[11]) self.assertEqual( group_data, [[24123], [16564], [25321], [22969, 22999], [6426], [], []])
def users_view(): """ Users listing for dropdown. """ data = utils.get_data() return [{ 'user_id': user, 'name': u'User {0}'.format(user) } for user in data]
def test_mean(self): """ Test calculating arithmetic mean. """ data = utils.get_data() weekdays = utils.group_by_weekday(data[10]) self.assertAlmostEqual(utils.mean(weekdays[0]), 29934) self.assertEqual(utils.mean(weekdays[7]), 0) self.assertEqual(utils.mean([]), 0) self.assertEqual(utils.mean([0]), 0)
def presence_start_end_time(user_id): """Returns start and end time of given user grouped by weekday.""" data = get_data() if user_id not in data: log.debug('User %s not found!', user_id) abort(404) weekdays = group_by_start_end_time(data[user_id]) return [(calendar.day_abbr[weekday], start, end) for weekday, (start, end) in enumerate(weekdays)]
def test_group_by_start_end(self): """ Test group_by_start_end. """ testResult = utils.group_by_start_end(utils.get_data()[10]) testResult2 = utils.group_by_start_end(utils.get_data()[11]) testResult3 = utils.group_by_start_end([]) self.assertListEqual( testResult, [ [[], []], [[34745], [64792]], [[33592], [58057]], [[38926], [62631]], [[], []], [[], []], [[], []] ] ) self.assertListEqual( testResult2, [ [[33134], [57257]], [[33590], [50154]], [[33206], [58527]], [[37116, 34088], [60085, 57087]], [[47816], [54242]], [[], []], [[], []], ] ) self.assertListEqual( testResult3, [ [[], []], [[], []], [[], []], [[], []], [[], []], [[], []], [[], []] ] )
def test_seconds_since_midnight(self): """ Test calculating correct amount of seconds since midnight. """ data = utils.get_data() time = data[10][datetime.date(2012, 7, 5)]['start'] midnight = datetime.time(hour=0, minute=0, second=0) midday = datetime.time(hour=12, minute=0, second=0) self.assertEqual(utils.seconds_since_midnight(time), 32917) self.assertEqual(utils.seconds_since_midnight(midnight), 0) self.assertEqual(utils.seconds_since_midnight(midday), 43200)
def mean_time_weekday_view(user_id): """Returns mean presence time of given user grouped by weekday.""" data = get_data() if user_id not in data: log.debug('User %s not found!', user_id) abort(404) weekdays = group_by_weekday(data[user_id]) result = [(calendar.day_abbr[weekday], mean(intervals)) for weekday, intervals in enumerate(weekdays)] return result
def test_group_quarters(self): """ Test grouping quarters by year and numeral. """ data = utils.get_data() result = { 0: { 'numeral': 3, 'year': 2013, } } self.assertEqual(result, utils.group_quarters(data))
def test_interval(self): """ Test calculate intervals in seconds between two time objects. """ data = utils.get_data() start = data[10][datetime.date(2012, 7, 5)]['start'] end = data[10][datetime.date(2012, 7, 5)]['end'] self.assertEqual(utils.intervals(start, end), 32907) start = datetime.time(hour=12, minute=0, second=0) end = datetime.time(hour=12, minute=0, second=0) self.assertEqual(utils.interval(start, end), 0)
def month_and_year_presence(user_id): """ Returns total presence time of give user grouped by month and year. """ data = get_data() if user_id not in data: log.debug('User %s not found!', user_id) abort(404) year_months = group_by_month_and_year(data[user_id]) result = [(year_month, sum(intervals)) for year_month, intervals in year_months.items()] return result
def top5_weeks_view(user_id): """ Returns top 5 weeks in work. """ data = get_data() if user_id not in data: log.debug('User %s not found!', user_id) abort(404) weekdays = group_top5_weeks(data[user_id]) result = sorted(weekdays.items(), key=lambda x: x[1], reverse=True) return result[0:5]
def presence_weekday_view(user_id): """Returns total presence time of given user grouped by weekday.""" data = get_data() if user_id not in data: log.debug('User %s not found!', user_id) abort(404) weekdays = group_by_weekday(data[user_id]) result = [(calendar.day_abbr[weekday], sum(intervals)) for weekday, intervals in enumerate(weekdays)] result.insert(0, ('Weekday', 'Presence (s)')) return result