@@ -6,13 +6,19 @@ import (
6
6
"strings"
7
7
"testing"
8
8
9
- "github.com/arduino/arduino-cloud-cli/internal/iot/mocks"
10
9
iotclient "github.com/arduino/iot-client-go"
11
- "github.com/stretchr/testify/mock"
12
10
)
13
11
14
12
const testFilename = "testdata/empty.bin"
15
13
14
+ type deviceUploaderTest struct {
15
+ deviceOTA func (id string , file * os.File , expireMins int ) error
16
+ }
17
+
18
+ func (d * deviceUploaderTest ) DeviceOTA (id string , file * os.File , expireMins int ) error {
19
+ return d .deviceOTA (id , file , expireMins )
20
+ }
21
+
16
22
func TestRun (t * testing.T ) {
17
23
var (
18
24
failPrefix = "00000000"
@@ -23,14 +29,14 @@ func TestRun(t *testing.T) {
23
29
okID2 = okPrefix + "-003f-42f9-a80c-85a1de36753b"
24
30
okID3 = okPrefix + "-dac4-4a6a-80a4-698062fe2af5"
25
31
)
26
- mockClient := & mocks.Client {}
27
- mockDeviceOTA := func (id string , file * os.File , expireMins int ) error {
28
- if strings .Split (id , "-" )[0 ] == failPrefix {
29
- return errors .New ("err" )
30
- }
31
- return nil
32
+ mockClient := & deviceUploaderTest {
33
+ deviceOTA : func (id string , file * os.File , expireMins int ) error {
34
+ if strings .Split (id , "-" )[0 ] == failPrefix {
35
+ return errors .New ("err" )
36
+ }
37
+ return nil
38
+ },
32
39
}
33
- mockClient .On ("DeviceOTA" , mock .Anything , mock .Anything , mock .Anything ).Return (mockDeviceOTA , nil )
34
40
35
41
devs := []string {okID1 , failID1 , okID2 , failID2 , okID3 }
36
42
res := run (mockClient , devs , testFilename , 0 )
@@ -49,6 +55,14 @@ func TestRun(t *testing.T) {
49
55
}
50
56
}
51
57
58
+ type deviceListerTest struct {
59
+ list []iotclient.ArduinoDevicev2
60
+ }
61
+
62
+ func (d * deviceListerTest ) DeviceList (tags map [string ]string ) ([]iotclient.ArduinoDevicev2 , error ) {
63
+ return d .list , nil
64
+ }
65
+
52
66
func TestValidateDevices (t * testing.T ) {
53
67
var (
54
68
correctFQBN = "arduino:samd:nano_33_iot"
@@ -60,23 +74,21 @@ func TestValidateDevices(t *testing.T) {
60
74
idNotFound = "deb17b7f-b39d-47a2-adf3-d26cdf474707"
61
75
)
62
76
63
- mockClient := & mocks.Client {}
64
- mockDeviceList := func (tags map [string ]string ) []iotclient.ArduinoDevicev2 {
65
- return []iotclient.ArduinoDevicev2 {
77
+ mockDeviceList := deviceListerTest {
78
+ list : []iotclient.ArduinoDevicev2 {
66
79
{Id : idCorrect1 , Fqbn : correctFQBN },
67
80
{Id : idCorrect2 , Fqbn : correctFQBN },
68
81
{Id : idNotValid , Fqbn : wrongFQBN },
69
- }
82
+ },
70
83
}
71
- mockClient .On ("DeviceList" , mock .Anything ).Return (mockDeviceList , nil )
72
84
73
85
ids := []string {
74
86
idCorrect1 ,
75
87
idNotFound ,
76
88
idCorrect2 ,
77
89
idNotValid ,
78
90
}
79
- v , i , err := validateDevices (mockClient , ids , correctFQBN )
91
+ v , i , err := validateDevices (& mockDeviceList , ids , correctFQBN )
80
92
if err != nil {
81
93
t .Errorf ("unexpected error: %s" , err .Error ())
82
94
}
0 commit comments