-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_test.go
151 lines (143 loc) · 2.81 KB
/
point_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
package domain
import (
"encoding/json"
"testing"
)
func TestNewPoint(t *testing.T) {
tests := []struct {
name string
lat float64
lng float64
expected Point
isError bool
}{
{
name: "Valid Point",
lat: 40.7128,
lng: -74.0060,
expected: MustPoint(40.7128, -74.0060),
isError: false,
},
{
name: "Invalid Latitude",
lat: 100.0,
lng: -74.0060,
expected: Point{},
isError: true,
},
{
name: "Invalid Longitude",
lat: 40.7128,
lng: -200.0,
expected: Point{},
isError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p, err := NewPoint(tt.lat, tt.lng)
if tt.isError && err == nil {
t.Fatalf("error expected")
}
if !tt.isError && err != nil {
t.Fatalf("no error expected, got %v", err)
}
if err != nil {
return
}
if p.String() != tt.expected.String() {
t.Errorf("Expected point %v, but got %v", tt.expected, p)
}
})
}
}
func TestPoint_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input string
expected Point
isError bool
}{
{
name: "Valid JSON",
input: `{"lat": 40.7128, "lon": -74.0060}`,
expected: MustPoint(40.7128, -74.0060),
isError: false,
},
{
name: "Zero Latitude and Longitude is Valid JSON",
input: `{"lat": 0, "lon": 0}`,
expected: MustPoint(0, 0),
isError: false,
},
{
name: "Invalid JSON",
input: `{"lat": "invalid", "lon": -74.0060}`,
expected: Point{},
isError: true,
},
{
name: "Missing Latitude",
input: `{"lon": -74.0060}`,
expected: Point{},
isError: true,
},
{
name: "Missing Longitude",
input: `{"lat": 40.7128}`,
expected: Point{},
isError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var p Point
var err = json.Unmarshal([]byte(tt.input), &p)
if tt.isError && err == nil {
t.Fatalf("error expected")
}
if !tt.isError && err != nil {
t.Fatalf("no error expected, got %v", err)
}
})
}
}
func TestPoint_Scan(t *testing.T) {
tests := []struct {
name string
input interface{}
expected Point
isError bool
}{
{
name: "Valid Point",
input: "(40.7128,-74.0060)",
expected: MustPoint(40.7128, -74.0060),
isError: false,
},
{
name: "Invalid Point",
input: "invalid",
expected: Point{},
isError: true,
},
{
name: "Nil",
input: nil,
expected: Point{},
isError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var p Point
var err = p.Scan(tt.input)
if tt.isError && err == nil {
t.Fatalf("error expected")
}
if !tt.isError && err != nil {
t.Fatalf("no error expected, got %v", err)
}
})
}
}