-
Notifications
You must be signed in to change notification settings - Fork 0
/
worldtimeapiunittest.py
57 lines (44 loc) · 2.19 KB
/
worldtimeapiunittest.py
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
import application
import unittest
from worldtimeapi import WorldTimeApi
import httpgetter
class HttpGetterMock:
def __init__(self, failedReds : int = 0) -> None:
self.failedReds = failedReds
with open('etc/test_response.txt','rb') as rf:
self.mockResponse = rf.read()
self.mockResponse = self.mockResponse.decode('utf-8')
def http_get(self, url):
if self.failedReds > 0:
self.failedReds -= 1
return ""
else:
return self.mockResponse
def get_body(self, response):
return httpgetter.get_body(response)
class WorldTimeApiUnitTest(unittest.TestCase):
def test__parseDateTime(self):
worldTimeApi = WorldTimeApi(HttpGetterMock())
testString = '{"abbreviation":"CEST","client_ip":"178.192.81.185","datetime":"2022-09-07T16:12:43.772110+02:00","day_of_week":3,"day_of_year":250,"dst":true,"dst_from":"2022-03-27T01:00:00+00:00","dst_offset":3600,"dst_until":"2022-10-30T01:00:00+00:00","raw_offset":3600,"timezone":"Europe/Zurich","unixtime":1662559963,"utc_datetime":"2022-09-07T14:12:43.772110+00:00","utc_offset":"+02:00","week_number":36}'
actualDateTime = worldTimeApi._parseDateTime(testString)
expectedDateTime=(2022, 9, 7, 3, 16, 12, 43, 0)
self.assertEqual(actualDateTime, expectedDateTime)
def test_getInternetTime(self):
worldTimeApi = WorldTimeApi(HttpGetterMock())
actualDateTime = worldTimeApi.getInternetTime()
expectedDateTime = (2022, 9, 24, 6, 20, 59, 10, 0)
self.assertEqual(actualDateTime, expectedDateTime)
def test_getInternetTime_two_fails(self):
worldTimeApi = WorldTimeApi(HttpGetterMock(2))
actualDateTime = worldTimeApi.getInternetTime()
expectedDateTime = (2022, 9, 24, 6, 20, 59, 10, 0)
self.assertEqual(actualDateTime, expectedDateTime)
def test_getInternetTime_complete_fail(self):
try:
worldTimeApi = WorldTimeApi(HttpGetterMock(11))
actualDateTime = worldTimeApi.getInternetTime()
self.fail('Expected ValueError')
except ValueError:
pass
if __name__ == '__main__': # type: ignore
unittest.main()