-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtests.py
105 lines (79 loc) · 4.09 KB
/
tests.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import unittest
from datetime import datetime
from dateutil import tz
from suntime import Sun, SunTimeException
_SF_LAT = 37.7749
_SF_LON = -122.4194
_TOKYO_LAT = 35.6895
_TOKYO_LON = 139.6917
_SYDNEY_LAT = -33.8688
_SYDNEY_LON = 151.2093
_NORTH_POLE_LAT = 90
_NORTH_POLE_LON = 0
class TestWestSun(unittest.TestCase):
""" Test on a location where the sun always rises and sets (i.e. San Francisco) """
def setUp(self):
self.sun = Sun(_SF_LAT, _SF_LON) # Coordinates for San Francisco
def test_get_sunrise_time(self):
# Sunrise in San Francisco (winter time)
expected_sunrise = datetime(2024, 3, 11, 14, 25, 48, tzinfo=tz.UTC) # 6:26 AM local time
utc_sunrise = self.sun.get_sunrise_time(datetime(2024, 3, 11))
local_sunrise = self.sun.get_local_sunrise_time(datetime(2024, 3, 11), tz.gettz('America/Los_Angeles'))
# Assert time matches 14:40 UTC
self.assertEqual(utc_sunrise, expected_sunrise)
self.assertEqual(local_sunrise, expected_sunrise)
# Sunrise in San Francisco (summer time)
expected_sunrise = datetime(2024, 6, 20, 12, 48, 0, tzinfo=tz.UTC)
utc_sunrise = self.sun.get_sunrise_time(datetime(2024, 6, 20))
local_sunrise = self.sun.get_local_sunrise_time(datetime(2024, 6, 20), tz.gettz('America/Los_Angeles'))
# Assert time matches 13:25 UTC
self.assertEqual(utc_sunrise, expected_sunrise)
self.assertEqual(local_sunrise, expected_sunrise)
def test_get_sunset_time(self):
# Test sunset in San Francisco
expected_sunset = datetime(2024, 3, 12, 2, 13, 48, tzinfo=tz.tzutc())
utc_sunset = self.sun.get_sunset_time(datetime(2024, 3, 11))
local_sunset = self.sun.get_local_sunset_time(datetime(2024, 3, 11), tz.gettz('America/Los_Angeles'))
self.assertEqual(utc_sunset, expected_sunset)
self.assertEqual(local_sunset, expected_sunset)
# Check with no params
utc_default_sunrise = self.sun.get_sunrise_time()
local_default_sunrise = self.sun.get_local_sunrise_time()
self.assertEqual(utc_default_sunrise.date(), datetime.now().date())
self.assertEqual(local_default_sunrise.date(), datetime.now().date())
class TestEastSun(unittest.TestCase):
def setUp(self):
self.sun = Sun(_TOKYO_LAT, _TOKYO_LON)
def test_get_sunrise_time(self):
# Sunrise in Tokyo
expected_sunrise = datetime(2024, 3, 11, 20, 57, 36, tzinfo=tz.UTC)
utc_sunrise = self.sun.get_sunrise_time(datetime(2024, 3, 11))
self.assertEqual(utc_sunrise, expected_sunrise)
class TestSouthSun(unittest.TestCase):
""" Test south hemisphere location where the sun rises and sets (i.e. Sydney)"""
def setUp(self):
self.sun = Sun(_SYDNEY_LAT, _SYDNEY_LON)
def test_get_sunrise_time(self):
# Sunrise in Sydney
expected_sunrise = datetime(2024, 3, 11, 6, 51, 36, tzinfo=tz.gettz('Australia/Sydney'))
local_sunrise = self.sun.get_sunrise_time(datetime(2024, 3, 11), tz.gettz('Australia/Sydney'))
self.assertEqual(expected_sunrise, local_sunrise)
def test_get_sunset_time(self):
# Test sunset in Sydney
expected_sunset = datetime(2024, 3, 11, 19, 18, 0, tzinfo=tz.gettz('Australia/Sydney'))
local_sunset = self.sun.get_sunset_time(datetime(2024, 3, 11), tz.gettz('Australia/Sydney'))
self.assertEqual(expected_sunset, local_sunset)
class TestNoSun(unittest.TestCase):
""" Test on a location where the sun never rises or sets (i.e. North Pole) """
def setUp(self):
self.sun = Sun(_NORTH_POLE_LAT, _NORTH_POLE_LON) # Coordinates for North Pole
def test_get_sunrise_time(self):
# Test for no sunrise
with self.assertRaises(SunTimeException):
self.sun.get_sunrise_time(datetime(2024, 12, 21)) # Winter solstice in the northern hemisphere
def test_get_sunset_time(self):
# Test for no sunset
with self.assertRaises(SunTimeException):
self.sun.get_sunset_time(datetime(2024, 6, 21)) # Summer solstice in the northern hemisphere
if __name__ == '__main__':
unittest.main()