This repository was archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
192 lines (182 loc) · 4.33 KB
/
service_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
package vio_test
import (
"context"
"errors"
"flag"
"log"
"log/slog"
"net"
"os"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/henvic/pgtools/sqltest"
"github.com/henvic/vio"
"github.com/henvic/vio/internal/mock"
"go.uber.org/mock/gomock"
)
var force = flag.Bool("force", false, "Force cleaning the database before starting")
func TestMain(m *testing.M) {
if os.Getenv("INTEGRATION_TESTDB") != "true" {
log.Printf("Skipping tests that require database connection")
return
}
os.Exit(m.Run())
}
func TestServiceLookupLocation(t *testing.T) {
t.Parallel()
migration := sqltest.New(t, sqltest.Options{
Force: *force,
Files: os.DirFS("migrations"),
})
pool := migration.Setup(context.Background(), "")
service := vio.NewService(vio.NewPostgres(pool, slog.Default()))
file, err := os.Open("testdata/example.csv")
if err != nil {
t.Fatal(err)
}
defer file.Close()
stats, err := vio.NewImporter(3, slog.Default(), pool).Stream(context.Background(), file)
if stats == nil {
t.Error("stats should not be nil")
}
if err != nil {
t.Errorf("cannot import location data: %v", err)
}
type args struct {
ctx context.Context
ip string
}
tests := []struct {
name string
args args
mock func(t testing.TB) *mock.MockDB // Leave as nil for using a real database implementation.
want *vio.Geolocation
wantErr string
}{
{
name: "empty_ip",
args: args{
ctx: context.Background(),
ip: "",
},
wantErr: "invalid IP address format",
},
{
name: "invalid_ip",
args: args{
ctx: context.Background(),
ip: "123",
},
wantErr: "invalid IP address format",
},
{
name: "ip1",
args: args{
ctx: context.Background(),
ip: "160.103.7.140",
},
want: &vio.Geolocation{
IPAddress: net.ParseIP("160.103.7.140"),
CountryCode: "CZ",
Country: "Nicaragua",
City: "New Neva",
Latitude: "-68.31023296602508",
Longitude: "-37.62435199624531",
UpdatedAt: time.Now(),
},
},
{
name: "ip2",
args: args{
ctx: context.Background(),
ip: "125.159.20.54",
},
want: &vio.Geolocation{
IPAddress: net.ParseIP("125.159.20.54"),
CountryCode: "LI",
Country: "Guyana",
City: "Port Karson",
Latitude: "-78.2274228596799",
Longitude: "-163.26218895343357",
UpdatedAt: time.Now(),
},
},
{
name: "ip2v6",
args: args{
ctx: context.Background(),
ip: "::ffff:125.159.20.54",
},
want: &vio.Geolocation{
IPAddress: net.ParseIP("125.159.20.54"),
CountryCode: "LI",
Country: "Guyana",
City: "Port Karson",
Latitude: "-78.2274228596799",
Longitude: "-163.26218895343357",
UpdatedAt: time.Now(),
},
},
{
name: "not_found",
args: args{
ctx: context.Background(),
ip: "127.0.0.1",
},
want: nil,
},
{
name: "canceled_ctx",
args: args{
ctx: canceledContext(),
ip: "127.0.0.1",
},
wantErr: "context canceled",
},
{
name: "deadline_exceeded_ctx",
args: args{
ctx: deadlineExceededContext(),
ip: "127.0.0.1",
},
wantErr: "context deadline exceeded",
},
{
name: "database_error",
args: args{
ctx: context.Background(),
ip: "127.0.0.1",
},
mock: func(t testing.TB) *mock.MockDB {
ctrl := gomock.NewController(t)
m := mock.NewMockDB(ctrl)
m.EXPECT().LookupLocation(gomock.Not(gomock.Nil()), net.ParseIP("127.0.0.1")).Return(nil, errors.New("unexpected error"))
return m
},
wantErr: "unexpected error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// If tt.mock is nil, use real database implementation if available. Otherwise, skip the test.
var s = service
if tt.mock != nil {
s = vio.NewService(tt.mock(t))
} else if s == nil {
t.Skip("required database not found, skipping test")
}
got, err := s.LookupLocation(tt.args.ctx, tt.args.ip)
if err == nil && tt.wantErr != "" || err != nil && tt.wantErr != err.Error() {
t.Errorf("Service.LookupLocation() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
return
}
if !cmp.Equal(tt.want, got, cmpopts.EquateApproxTime(time.Minute)) {
t.Errorf("value returned by Service.LookupLocation() doesn't match: %v", cmp.Diff(tt.want, got))
}
})
}
}