aboutsummaryrefslogtreecommitdiffstats
path: root/http/types_test.go
blob: 9550001ede7a9d1767af223fb2929f2f3003e5df (plain) (blame)
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
package http

import (
	"reflect"
	"testing"

	"github.com/mpolden/atb/atb"
)

func TestConvertBusStop(t *testing.T) {
	stop := atb.BusStop{
		StopID:      100633,
		NodeID:      "16011376",
		Description: "Prof. Brochs gt",
		Longitude:   "1157514",
		Latitude:    9202874,
		MobileCode:  "16011376 (Prof.)",
		MobileName:  "Prof. (16011376)",
	}
	expected := BusStop{
		StopID:      100633,
		NodeID:      16011376,
		Description: "Prof. Brochs gt",
		Longitude:   10.398126,
		Latitude:    63.415535,
		MobileCode:  "16011376 (Prof.)",
		MobileName:  "Prof. (16011376)",
	}
	actual, err := convertBusStop(stop)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(actual, expected) {
		t.Fatalf("Expected %+v, got %+v", expected, actual)
	}
}

func TestConvertBusStops(t *testing.T) {
	stops := atb.BusStops{
		Stops: []atb.BusStop{{
			NodeID:    "16011376",
			Longitude: "1157514",
			Latitude:  9202874,
		}}}
	expected := BusStops{
		Stops: []BusStop{{
			NodeID:    16011376,
			Longitude: 10.398126,
			Latitude:  63.415535,
		}}}
	actual, err := convertBusStops(stops)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(actual, expected) {
		t.Fatalf("Expected %+v, got %+v", expected, actual)
	}
}

func TestConvertTime(t *testing.T) {
	time, err := ConvertTime("26.02.2015 18:38")
	if err != nil {
		t.Fatal(err)
	}
	expected := "2015-02-26T18:38:00.000"
	if time != expected {
		t.Fatalf("Expected %s, got %s", expected, time)
	}
}

func TestIsRealtime(t *testing.T) {
	if !IsRealtime("prev") {
		t.Fatal("Expected true")
	}
	if !IsRealtime("Prev") {
		t.Fatal("Expected true")
	}
	if IsRealtime("foo") {
		t.Fatal("Expected false")
	}
}

func TestConvertForecast(t *testing.T) {
	forecast := atb.Forecast{
		LineID:                  "6",
		LineDescription:         "6",
		RegisteredDepartureTime: "26.02.2015 18:38",
		ScheduledDepartureTime:  "26.02.2015 18:01",
		StationForecast:         "Prev",
		Destination:             "Munkegata M5",
	}
	expected := Departure{
		LineID:                  "6",
		Destination:             "Munkegata M5",
		RegisteredDepartureTime: "2015-02-26T18:38:00.000",
		ScheduledDepartureTime:  "2015-02-26T18:01:00.000",
		IsRealtimeData:          true,
	}
	actual, err := convertForecast(forecast)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(actual, expected) {
		t.Fatalf("Expected %+v, got %+v", expected, actual)
	}
}

func TestIsTowardsCentrum(t *testing.T) {
	if !IsTowardsCentrum(16011376) {
		t.Fatal("Expected true")
	}
	if IsTowardsCentrum(16010376) {
		t.Fatal("Expected false")
	}
}

func TestConvertForecasts(t *testing.T) {
	forecasts := atb.Forecasts{
		Nodes: []atb.NodeInfo{{NodeID: "16011376"}},
		Forecasts: []atb.Forecast{{
			RegisteredDepartureTime: "26.02.2015 18:38",
			ScheduledDepartureTime:  "26.02.2015 18:01",
		}}}
	b := true
	expected := Departures{TowardsCentrum: &b,
		Departures: []Departure{{
			RegisteredDepartureTime: "2015-02-26T18:38:00.000",
			ScheduledDepartureTime:  "2015-02-26T18:01:00.000",
			IsRealtimeData:          false,
		}}}
	actual, err := convertForecasts(forecasts)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(actual, expected) {
		t.Fatalf("Expected %+v, got %+v", expected, actual)
	}
}

func TestConvertCoordinates(t *testing.T) {
	// Prof. Brochs gate
	latitude, longitude := 9202565, 1157522
	lat, lon := ConvertCoordinates(latitude, longitude)
	if expected := 63.41429265308724; lat != expected {
		t.Fatalf("Expected %f, got %f", expected, lat)
	}
	if expected := 10.398197043045966; lon != expected {
		t.Fatalf("Expected %f, got %f", expected, lon)
	}

	// Ilsvika
	latitude, longitude = 9206756, 1152920
	lat, lon = ConvertCoordinates(latitude, longitude)
	if expected := 63.43113671582598; lat != expected {
		t.Fatalf("Expected %f, got %f", expected, lat)
	}
	if expected := 10.356856573670786; lon != expected {
		t.Fatalf("Expected %f, got %f", expected, lon)
	}
}

func TestCeilN(t *testing.T) {
	expected := 1.234567
	actual := ceilN(1.2345661, 6)
	if actual != expected {
		t.Fatalf("Expected %f, got %f", expected, actual)
	}
	expected = 1.234567
	actual = ceilN(1.2345665, 6)
	if actual != expected {
		t.Fatalf("Expected %f, got %f", expected, actual)
	}
}