-
Notifications
You must be signed in to change notification settings - Fork 7
/
service_test.go
92 lines (87 loc) · 3.24 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
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package callhome_test
import (
"context"
"fmt"
"testing"
"github.com/absmach/callhome"
"github.com/absmach/callhome/mocks"
"github.com/absmach/callhome/timescale"
repoMocks "github.com/absmach/callhome/timescale/mocks"
"github.com/ip2location/ip2location-go/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestRetrieve(t *testing.T) {
ctx := context.TODO()
t.Run("failed repo save", func(t *testing.T) {
timescaleRepo := repoMocks.NewTelemetryRepo(t)
svc := callhome.New(timescaleRepo, nil)
timescaleRepo.On("RetrieveAll", ctx, callhome.PageMetadata{}).Return(callhome.TelemetryPage{}, timescale.ErrSaveEvent)
_, err := svc.Retrieve(ctx, callhome.PageMetadata{}, callhome.TelemetryFilters{})
assert.NotNil(t, err)
assert.Equal(t, timescale.ErrSaveEvent, err)
})
t.Run("success", func(t *testing.T) {
timescaleRepo := repoMocks.NewTelemetryRepo(t)
svc := callhome.New(timescaleRepo, nil)
timescaleRepo.On("RetrieveAll", ctx, callhome.PageMetadata{}).Return(callhome.TelemetryPage{}, nil)
_, err := svc.Retrieve(ctx, callhome.PageMetadata{}, callhome.TelemetryFilters{})
assert.Nil(t, err)
})
}
func TestSave(t *testing.T) {
ctx := context.TODO()
t.Run("error obtaining location", func(t *testing.T) {
timescaleRepo := repoMocks.NewTelemetryRepo(t)
locMock := mocks.NewLocationService(t)
locMock.On("GetLocation", "").Return(ip2location.IP2Locationrecord{}, fmt.Errorf("error getting loc"))
svc := callhome.New(timescaleRepo, locMock)
err := svc.Save(ctx, callhome.Telemetry{})
assert.NotNil(t, err)
})
t.Run("error saving to timescale", func(t *testing.T) {
timescaleRepo := repoMocks.NewTelemetryRepo(t)
locMock := mocks.NewLocationService(t)
locMock.On("GetLocation", "").Return(ip2location.IP2Locationrecord{
Latitude: 1.2,
Longitude: 30,
Country_long: "SomeCountry",
City: "someCity",
}, nil)
timescaleRepo.On("Save", ctx, mock.AnythingOfType("callhome.Telemetry")).Return(timescale.ErrSaveEvent)
svc := callhome.New(timescaleRepo, locMock)
err := svc.Save(ctx, callhome.Telemetry{})
assert.NotNil(t, err)
assert.Equal(t, timescale.ErrSaveEvent, err)
})
t.Run("successful save", func(t *testing.T) {
timescaleRepo := repoMocks.NewTelemetryRepo(t)
locMock := mocks.NewLocationService(t)
locMock.On("GetLocation", "").Return(ip2location.IP2Locationrecord{
Latitude: 1.2,
Longitude: 30,
Country_long: "SomeCountry",
City: "someCity",
}, nil)
timescaleRepo.On("Save", ctx, mock.AnythingOfType("callhome.Telemetry")).Return(nil)
svc := callhome.New(timescaleRepo, locMock)
err := svc.Save(ctx, callhome.Telemetry{})
assert.Nil(t, err)
})
t.Run("successful update", func(t *testing.T) {
timescaleRepo := repoMocks.NewTelemetryRepo(t)
locMock := mocks.NewLocationService(t)
locMock.On("GetLocation", "").Return(ip2location.IP2Locationrecord{
Latitude: 1.2,
Longitude: 30,
Country_long: "SomeCountry",
City: "someCity",
}, nil)
timescaleRepo.On("Save", ctx, mock.AnythingOfType("callhome.Telemetry")).Return(nil)
svc := callhome.New(timescaleRepo, locMock)
err := svc.Save(ctx, callhome.Telemetry{})
assert.Nil(t, err)
})
}