forked from Broham/suncalcPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
77 lines (63 loc) · 3.13 KB
/
test.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
import unittest
import suncalcPy2
from datetime import datetime
import pytz
class SunCalcTestCases(unittest.TestCase):
"""Tests for `suncalc.py`."""
def near(self, val1, val2):
return abs(val1 - val2) < (1E-15)
date = datetime.strptime('2013-03-04 16','%Y-%m-%d %H')
lat = 50.5
lng = -30.5
sunTimes = dict(
# solarNoon = '2013-03-05 10:10:57Z',
# nadir = '2013-03-04T22:10:57Z',
sunrise = '2013-03-05 04:34:56',
sunset = '2013-03-05 15:46:57',
sunriseEnd = '2013-03-05 04:38:19',
sunsetStart = '2013-03-05 15:43:34',
dawn = '2013-03-05 04:02:17',
dusk = '2013-03-05 16:19:36',
nauticalDawn = '2013-03-05 03:24:31',
nauticalDusk = '2013-03-05 16:57:22',
nightEnd = '2013-03-05 02:46:17',
night = '2013-03-05 17:35:36',
goldenHourEnd = '2013-03-05 05:19:01',
goldenHour = '2013-03-05 15:02:52')
def test_getPositions(self):
"""Get sun positions correctly"""
sunPos = suncalcPy2.getPosition(self.date, self.lat, self.lng)
self.assertTrue(self.near(sunPos["azimuth"], -2.5003175907168385) )
self.assertTrue( self.near(sunPos["altitude"], -0.7000406838781611) )
def test_getTimes(self):
"""Get sun times correctly"""
# Weirdness to handle PST to UTC date/time comparisons. Consider moving everything to PST instead of having this conversion?
local = pytz.timezone ("America/Los_Angeles")
naive = datetime.strptime ('2013-03-04 16','%Y-%m-%d %H')
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone (pytz.utc)
times = suncalcPy2.getTimes(utc_dt, self.lat, self.lng)
for i in self.sunTimes:
naive = datetime.strptime (times[i],'%Y-%m-%d %H:%M:%S')
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone (pytz.utc)
self.assertEqual(self.sunTimes[i],utc_dt.strftime('%Y-%m-%d %H:%M:%S'))
def test_getMoonPosition(self):
"""Get Moon position correctly"""
date = datetime.strptime('2013-03-04 16','%Y-%m-%d %H')
moonPos = suncalcPy2.getMoonPosition(self.date, self.lat, self.lng)
self.assertTrue(self.near(moonPos["azimuth"], -0.9783999522438226))
self.assertTrue(self.near(moonPos["altitude"], 0.006969727754891917))
self.assertTrue(self.near(moonPos["distance"], 364121.37256256194))
def test_getMoonIllumination(self):
"""Get moon illumination correctly"""
moonIllum = suncalcPy2.getMoonIllumination(self.date)
self.assertTrue(self.near(moonIllum["fraction"], 0.4848068202456373))
self.assertTrue(self.near(moonIllum["phase"], 0.7548368838538762))
self.assertTrue(self.near(moonIllum["angle"], 1.6732942678578346))
def test_getMoonTimes(self):
moonTimes = suncalcPy2.getMoonTimes(self.date, self.lat, self.lng)
self.assertEqual(moonTimes["rise"].strftime('%Y-%m-%d %H:%M:%S'), '2013-03-04 15:57:55')
# self.assertEqual(moonTimes["rise"].strftime('%Y-%m-%d %H:%M:%S'), '2013-03-04 23:57:55')
if __name__ == '__main__':
unittest.main()