forked from pool/python-suntime
- Remove LICENSE now in upstream tarball - Added tests.py and activated test suite - Update to v1.3.0 OBS-URL: https://build.opensuse.org/request/show/1155378 OBS-URL: https://build.opensuse.org/package/show/devel:languages:python/python-suntime?expand=0&rev=3
106 lines
4.0 KiB
Python
106 lines
4.0 KiB
Python
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)
|
|
|
|
|
|
class TestEastSun(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.sun = Sun(_TOKYO_LAT, _TOKYO_LON)
|
|
|
|
def test_get_sunrise_time(self):
|
|
# Sunrise in Tokyo
|
|
print("TOKYO")
|
|
expected_sunrise = datetime(2024, 3, 11, 20, 57, 36, tzinfo=tz.UTC)
|
|
utc_sunrise = self.sun.get_sunrise_time(datetime(2024, 3, 11))
|
|
print(utc_sunrise)
|
|
local_sunrise = self.sun.get_local_sunrise_time(datetime(2024, 3, 11), tz.gettz('Asia/Tokyo'))
|
|
print(local_sunrise)
|
|
self.assertEqual(utc_sunrise, expected_sunrise)
|
|
# self.assertEqual(local_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()
|