-
Notifications
You must be signed in to change notification settings - Fork 7
/
decks_test.go
121 lines (97 loc) · 2.62 KB
/
decks_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
package ankiconnect
import (
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestDecksManager_GetAll(t *testing.T) {
getAllRequest := []byte(`{
"action": "deckNames",
"version": 6
}`)
getAllResult := []byte(`{
"result": [
"Default",
"Deck01",
"Deck02"
],
"error": null
}`)
t.Run("success", func(t *testing.T) {
defer httpmock.Reset()
registerVerifiedPayload(t, getAllRequest, getAllResult)
decks, restErr := client.Decks.GetAll()
assert.NotNil(t, decks)
assert.Nil(t, restErr)
assert.Equal(t, 3, len(*decks))
})
t.Run("error", func(t *testing.T) {
defer httpmock.Reset()
registerErrorResponse(t)
decks, restErr := client.Decks.GetAll()
assert.Nil(t, decks)
assert.NotNil(t, restErr)
assert.Equal(t, http.StatusBadRequest, restErr.StatusCode)
assert.Equal(t, "some error message", restErr.Message)
})
t.Run("http request error", func(t *testing.T) {
defer httpmock.Reset()
decks, restErr := client.Decks.GetAll()
assert.Nil(t, decks)
assert.NotNil(t, restErr)
assert.Equal(t, http.StatusInternalServerError, restErr.StatusCode)
assert.Equal(t, "Internal Server Error", restErr.Message)
})
}
func TestDecksManager_Create(t *testing.T) {
createRequest := []byte(`{
"action": "createDeck",
"version": 6,
"params": {
"deck": "Japanese::Tokyo"
}
}`)
createResponse := []byte(`{
"result": 1659294179522,
"error": null
}`)
t.Run("success", func(t *testing.T) {
defer httpmock.Reset()
registerVerifiedPayload(t, createRequest, createResponse)
restErr := client.Decks.Create("Japanese::Tokyo")
assert.Nil(t, restErr)
})
t.Run("error", func(t *testing.T) {
defer httpmock.Reset()
registerErrorResponse(t)
restErr := client.Decks.Create("test")
assert.NotNil(t, restErr)
assert.Equal(t, http.StatusBadRequest, restErr.StatusCode)
assert.Equal(t, "some error message", restErr.Message)
})
}
func TestDecksManagerDelete(t *testing.T) {
deleteDeckRequest := []byte(`{
"action": "deleteDecks",
"version": 6,
"params": {
"decks": ["test"],
"cardsToo": true
}
}`)
t.Run("success", func(t *testing.T) {
defer httpmock.Reset()
registerVerifiedPayload(t, deleteDeckRequest, genericSuccessJson)
restErr := client.Decks.Delete("test")
assert.Nil(t, restErr)
})
t.Run("error", func(t *testing.T) {
defer httpmock.Reset()
registerErrorResponse(t)
restErr := client.Decks.Delete("test")
assert.NotNil(t, restErr)
assert.Equal(t, http.StatusBadRequest, restErr.StatusCode)
assert.Equal(t, "some error message", restErr.Message)
})
}