-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastroinfo_test.go
221 lines (216 loc) · 8.13 KB
/
astroinfo_test.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
//
// SPDX-License-Identifier: MIT
package meteologix
import (
"testing"
"time"
)
func TestClient_AstronomicalInfoByCoordinates(t *testing.T) {
la := 52.5067296
lo := 13.2599306
loc, err := time.LoadLocation("Europe/Berlin")
if err != nil {
t.Errorf("failed to load time location data for Europe/Berlin: %s", err)
return
}
rt := time.Date(2023, 5, 28, 15, 8, 33, 0, loc)
nfmt := time.Date(2023, 6, 4, 5, 43, 56, 0, loc)
nnmt := time.Date(2023, 6, 18, 6, 39, 10, 0, loc)
c := New(withMockAPI())
ai, err := c.AstronomicalInfoByCoordinates(la, lo)
if err != nil {
t.Errorf("failed to fetch astronomical information: %s", err)
return
}
if ai.Latitude != la {
t.Errorf("AstronomicalInfoByCoordinates failed, expected lat: %f, got: %f", la,
ai.Latitude)
}
if ai.Longitude != lo {
t.Errorf("AstronomicalInfoByCoordinates failed, expected lon: %f, got: %f", lo,
ai.Longitude)
}
if ai.Run.UnixMilli() != rt.UnixMilli() {
t.Errorf("AstronomicalInfoByCoordinates failed, expected run time: %s, got: %s",
rt.String(), ai.Run.String())
}
if ai.TimeZone != loc.String() {
t.Errorf("AstronomicalInfoByCoordinates failed, expected time zone: %s, got: %s",
loc.String(), ai.TimeZone)
}
if ai.NextFullMoon.UnixMilli() != nfmt.UnixMilli() {
t.Errorf("AstronomicalInfoByCoordinates failed, expected next full moon: %s, got: %s",
nfmt.String(), ai.NextFullMoon.String())
}
if ai.NextNewMoon.UnixMilli() != nnmt.UnixMilli() {
t.Errorf("AstronomicalInfoByCoordinates failed, expected next new moon: %s, got: %s",
nnmt.String(), ai.NextNewMoon.String())
}
}
func TestClient_AstronomicalInfoByLocation(t *testing.T) {
la := 52.5067296
lo := 13.2599306
loc, err := time.LoadLocation("Europe/Berlin")
if err != nil {
t.Errorf("failed to load time location data for Europe/Berlin: %s", err)
return
}
rt := time.Date(2023, 5, 28, 15, 8, 33, 0, loc)
nfmt := time.Date(2023, 6, 4, 5, 43, 56, 0, loc)
nnmt := time.Date(2023, 6, 18, 6, 39, 10, 0, loc)
c := New(withMockAPI())
ai, err := c.AstronomicalInfoByLocation("Berlin, Germany")
if err != nil {
t.Errorf("failed to fetch astronomical information: %s", err)
return
}
if ai.Latitude != la {
t.Errorf("AstronomicalInfoByCoordinates failed, expected lat: %f, got: %f", la,
ai.Latitude)
}
if ai.Longitude != lo {
t.Errorf("AstronomicalInfoByCoordinates failed, expected lon: %f, got: %f", lo,
ai.Longitude)
}
if ai.Run.UnixMilli() != rt.UnixMilli() {
t.Errorf("AstronomicalInfoByCoordinates failed, expected run time: %s, got: %s",
rt.String(), ai.Run.String())
}
if ai.TimeZone != loc.String() {
t.Errorf("AstronomicalInfoByCoordinates failed, expected time zone: %s, got: %s",
loc.String(), ai.TimeZone)
}
if ai.NextFullMoon.UnixMilli() != nfmt.UnixMilli() {
t.Errorf("AstronomicalInfoByCoordinates failed, expected next full moon: %s, got: %s",
nfmt.String(), ai.NextFullMoon.String())
}
if ai.NextNewMoon.UnixMilli() != nnmt.UnixMilli() {
t.Errorf("AstronomicalInfoByCoordinates failed, expected next new moon: %s, got: %s",
nnmt.String(), ai.NextNewMoon.String())
}
}
func TestAstronomicalInfo_SunsetByDateString(t *testing.T) {
la := 52.5067296
lo := 13.2599306
loc, err := time.LoadLocation("Europe/Berlin")
if err != nil {
t.Errorf("failed to load time location data for Europe/Berlin: %s", err)
return
}
ti := time.Date(2023, 5, 28, 21, 16, 37, 0, loc)
ddt := time.Date(2023, 5, 28, 0, 0, 0, 0, time.UTC)
c := New(withMockAPI())
ai, err := c.AstronomicalInfoByCoordinates(la, lo)
if err != nil {
t.Errorf("failed to fetch astronomical information: %s", err)
return
}
if !ai.SunsetByTime(ti).IsAvailable() {
t.Errorf("SunsetByTime failed, expected entry, but got 'not available'")
return
}
if ai.SunsetByTime(ti).Value().UnixMilli() != ti.UnixMilli() {
t.Errorf("SunsetByTime failed, expected sunset: %s, got: %s",
ti.String(), ai.SunsetByTime(ti).Value().String())
}
if !ai.SunsetByDateString(ti.Format(DateFormat)).IsAvailable() {
t.Errorf("SunsetByDateString failed, expected entry, but got 'not available'")
return
}
if ai.SunsetByTime(ti).String() != ti.Format(time.RFC3339) {
t.Errorf("SunsetByTime failed, expected sunset: %s, got: %s",
ti.Format(time.RFC3339), ai.SunsetByTime(ti).String())
}
if ai.SunsetByTime(ti).DateTime().Format(time.RFC3339) != ddt.Format(time.RFC3339) {
t.Errorf("SunsetByTime failed, expected sunset: %s, got: %s",
ddt.Format(time.RFC3339), ai.SunsetByTime(ti).DateTime().Format(time.RFC3339))
}
if ai.SunsetByDateString(ti.Format(DateFormat)).Value().UnixMilli() != ti.UnixMilli() {
t.Errorf("SunsetByDateString failed, expected sunset: %s, got: %s",
ti.String(), ai.SunsetByDateString(ti.Format(DateFormat)).Value().String())
}
ti = time.Time{}
if ai.SunsetByTime(ti).IsAvailable() {
t.Errorf("SunsetByTime failed, expected no entry, but got: %s",
ai.SunsetByTime(ti).Value().String())
}
if !ai.SunsetByTime(ti).Value().IsZero() {
t.Errorf("SunsetByTime failed, expected no entry, but got: %s",
ai.SunsetByTime(ti).Value().String())
}
if len(ai.SunsetAll()) != 14 {
t.Errorf("SunsetByTime failed, expected 14 entired, but got: %d", len(ai.SunsetAll()))
return
}
if ai.SunsetAll()[0].DateTime().Format("2006-01-02") != "2023-05-28" {
t.Errorf("SunsetByTime failed, expected first entry to be: %s, got: %s", "2023-05-28",
ai.SunsetAll()[0].DateTime().Format("2006-01-02"))
}
if ai.SunsetAll()[13].DateTime().Format("2006-01-02") != "2023-06-10" {
t.Errorf("SunsetByTime failed, expected first entry to be: %s, got: %s", "2023-06-10",
ai.SunsetAll()[13].DateTime().Format("2006-01-02"))
}
}
func TestAstronomicalInfo_SunriseByDateString(t *testing.T) {
la := 52.5067296
lo := 13.2599306
loc, err := time.LoadLocation("Europe/Berlin")
if err != nil {
t.Errorf("failed to load time location data for Europe/Berlin: %s", err)
return
}
ti := time.Date(2023, 5, 28, 4, 51, 48, 0, loc)
ddt := time.Date(2023, 5, 28, 0, 0, 0, 0, time.UTC)
c := New(withMockAPI())
ai, err := c.AstronomicalInfoByCoordinates(la, lo)
if err != nil {
t.Errorf("failed to fetch astronomical information: %s", err)
return
}
if !ai.SunriseByTime(ti).IsAvailable() {
t.Errorf("SunriseByTime failed, expected entry, but got 'not available'")
return
}
if ai.SunriseByTime(ti).Value().UnixMilli() != ti.UnixMilli() {
t.Errorf("SunriseByTime failed, expected sunrise: %s, got: %s",
ti.String(), ai.SunriseByTime(ti).Value().String())
}
if !ai.SunriseByDateString(ti.Format(DateFormat)).IsAvailable() {
t.Errorf("SunriseByDateString failed, expected entry, but got 'not available'")
return
}
if ai.SunriseByTime(ti).String() != ti.Format(time.RFC3339) {
t.Errorf("SunriseByTime failed, expected sunrise: %s, got: %s",
ti.Format(time.RFC3339), ai.SunriseByTime(ti).String())
}
if ai.SunriseByTime(ti).DateTime().Format(time.RFC3339) != ddt.Format(time.RFC3339) {
t.Errorf("SunriseByTime failed, expected sunrise: %s, got: %s",
ddt.Format(time.RFC3339), ai.SunriseByTime(ti).DateTime().Format(time.RFC3339))
}
if ai.SunriseByDateString(ti.Format(DateFormat)).Value().UnixMilli() != ti.UnixMilli() {
t.Errorf("SunriseByDateString failed, expected sunrise: %s, got: %s",
ti.String(), ai.SunriseByDateString(ti.Format(DateFormat)).Value().String())
}
ti = time.Time{}
if ai.SunriseByTime(ti).IsAvailable() {
t.Errorf("SunriseByTime failed, expected no entry, but got: %s",
ai.SunriseByTime(ti).Value().String())
}
if !ai.SunriseByTime(ti).Value().IsZero() {
t.Errorf("SunriseByTime failed, expected no entry, but got: %s",
ai.SunriseByTime(ti).Value().String())
}
if len(ai.SunriseAll()) != 14 {
t.Errorf("SunriseByTime failed, expected 14 entired, but got: %d", len(ai.SunriseAll()))
return
}
if ai.SunriseAll()[0].DateTime().Format("2006-01-02") != "2023-05-28" {
t.Errorf("SunriseByTime failed, expected first entry to be: %s, got: %s", "2023-05-28",
ai.SunriseAll()[0].DateTime().Format("2006-01-02"))
}
if ai.SunriseAll()[13].DateTime().Format("2006-01-02") != "2023-06-10" {
t.Errorf("SunriseByTime failed, expected first entry to be: %s, got: %s", "2023-06-10",
ai.SunriseAll()[13].DateTime().Format("2006-01-02"))
}
}