File: sunrisesunset_test.go

package info (click to toggle)
golang-github-kelvins-sunrisesunset 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 92 kB
  • sloc: makefile: 2
file content (82 lines) | stat: -rw-r--r-- 2,469 bytes parent folder | download | duplicates (2)
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
package sunrisesunset

import (
	"testing"
	"time"
)

func TestGetSunriseSunset(t *testing.T) {

  date := time.Date(2017, 3, 23, 0, 0, 0, 0, time.UTC)

	// Test invalid parameters

	// Table tests
	var invalidParameters = []struct {
		latitude      float64
		longitude     float64
		utcOffset     float64
		date          time.Time
		expectedError string
	}{
		{     -95.0, -46.704082,  -3.0, date, "Latitude invalid"},
		{     100.0, -46.704082,  -3.0, date, "Latitude invalid"},
		{-23.545570,     -185.0,  -3.0, date, "Longitude invalid"},
		{-23.545570,      190.0,  -3.0, date, "Longitude invalid"},
		{-23.545570, -46.704082, -15.0, date, "UTC offset invalid"},
		{-23.545570, -46.704082,  18.0, date, "UTC offset invalid"},
		{-23.545570, -46.704082,  -3.0, time.Date(1000, 1, 1, 0, 0, 0, 0, time.UTC), "Date invalid"},
		{-23.545570, -46.704082,  -3.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC), "Date invalid"},
	}

	// Test with all values in the table
	for _, pair := range invalidParameters {
		_, _, err := GetSunriseSunset(pair.latitude, pair.longitude, pair.utcOffset, pair.date)
		if err == nil {
			t.Error(
				"Expect an error",
			)
		}
	}

	// Test with valid values

	// Table tests
	var tTests = []struct {
		latitude  float64
		longitude float64
		utcOffset float64
		date      time.Time
		sunrise   time.Time
		sunset    time.Time
	}{
		{-23.545570, -46.704082, -3.0, date, time.Date(1, 1, 1, 6, 11, 44, 0, time.UTC), time.Date(1, 1, 1, 18, 14, 27, 0, time.UTC)}, // Sao Paulo - Brazil
		{36.7201600, -4.4203400,  1.0, date, time.Date(1, 1, 1, 7, 16, 45, 0, time.UTC), time.Date(1, 1, 1, 19, 32, 10, 0, time.UTC)}, // Málaga - Spain
		{ 28.613084,  77.209168,  5.5, date, time.Date(1, 1, 1, 6, 21, 45, 0, time.UTC), time.Date(1, 1, 1, 18, 34, 07, 0, time.UTC)}, // Nova Delhi - India
    { 32.755701, -96.797296, -5.0, date, time.Date(1, 1, 1, 7, 26, 34, 0, time.UTC), time.Date(1, 1, 1, 19, 41, 07, 0, time.UTC)}, // Dallas - United States of America
	}

	// Test with all values in the table
	for _, pair := range tTests {
		sunrise, sunset, err := GetSunriseSunset(pair.latitude, pair.longitude, pair.utcOffset, pair.date)

		if err != nil {
			t.Error(
				"Expect: nil",
				"Received: ", err,
			)
		}
		if !sunrise.Equal(pair.sunrise) {
			t.Error(
				"Expected: ", pair.sunrise,
				"Received: ", sunrise,
			)
		}
		if !sunset.Equal(pair.sunset) {
			t.Error(
				"Expected: ", pair.sunset,
				"Received: ", sunset,
			)
		}
	}
}