This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaction_xpub_test.go
221 lines (176 loc) · 7.01 KB
/
action_xpub_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package bux
import (
"testing"
"github.com/BuxOrg/bux/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestClient_NewXpub will test the method NewXpub()
func (ts *EmbeddedDBTestSuite) TestClient_NewXpub() {
for _, testCase := range dbTestCases {
ts.T().Run(testCase.name+" - valid", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
xPub, err := tc.client.NewXpub(tc.ctx, testXPub, tc.client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Equal(t, testXPubID, xPub.ID)
xPub2, err2 := tc.client.GetXpub(tc.ctx, testXPub)
require.NoError(t, err2)
assert.Equal(t, testXPubID, xPub2.ID)
})
ts.T().Run(testCase.name+" - valid with metadata (key->val)", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
opts := append(tc.client.DefaultModelOptions(), WithMetadata(testMetadataKey, testMetadataValue))
xPub, err := tc.client.NewXpub(tc.ctx, testXPub, opts...)
require.NoError(t, err)
assert.Equal(t, testXPubID, xPub.ID)
assert.Equal(t, Metadata{testMetadataKey: testMetadataValue}, xPub.Metadata)
xPub2, err2 := tc.client.GetXpub(tc.ctx, testXPub)
require.NoError(t, err2)
assert.Equal(t, testXPubID, xPub2.ID)
assert.Equal(t, Metadata{testMetadataKey: testMetadataValue}, xPub2.Metadata)
})
ts.T().Run(testCase.name+" - valid with metadatas", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
opts := append(
tc.client.DefaultModelOptions(),
WithMetadatas(map[string]interface{}{
testMetadataKey: testMetadataValue,
}),
)
xPub, err := tc.client.NewXpub(tc.ctx, testXPub, opts...)
require.NoError(t, err)
assert.Equal(t, testXPubID, xPub.ID)
assert.Equal(t, Metadata{testMetadataKey: testMetadataValue}, xPub.Metadata)
xPub2, err2 := tc.client.GetXpub(tc.ctx, testXPub)
require.NoError(t, err2)
assert.Equal(t, testXPubID, xPub2.ID)
assert.Equal(t, Metadata{testMetadataKey: testMetadataValue}, xPub2.Metadata)
})
ts.T().Run(testCase.name+" - errors", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
_, err := tc.client.NewXpub(tc.ctx, "test", tc.client.DefaultModelOptions()...)
assert.ErrorIs(t, err, utils.ErrXpubInvalidLength)
_, err = tc.client.NewXpub(tc.ctx, "", tc.client.DefaultModelOptions()...)
assert.ErrorIs(t, err, utils.ErrXpubInvalidLength)
})
ts.T().Run(testCase.name+" - duplicate xPub", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
xPub, err := tc.client.NewXpub(tc.ctx, testXPub, tc.client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Equal(t, testXPubID, xPub.ID)
_, err2 := tc.client.NewXpub(tc.ctx, testXPub, tc.client.DefaultModelOptions()...)
require.Error(t, err2)
})
}
}
// TestClient_GetXpub will test the method GetXpub()
func (ts *EmbeddedDBTestSuite) TestClient_GetXpub() {
for _, testCase := range dbTestCases {
ts.T().Run(testCase.name+" - valid", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
xPub, err := tc.client.NewXpub(tc.ctx, testXPub, tc.client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Equal(t, testXPubID, xPub.ID)
xPub2, err2 := tc.client.GetXpub(tc.ctx, testXPub)
require.NoError(t, err2)
assert.Equal(t, testXPubID, xPub2.ID)
xPub3, err3 := tc.client.GetXpubByID(tc.ctx, xPub2.ID)
require.NoError(t, err3)
assert.Equal(t, testXPubID, xPub3.ID)
})
ts.T().Run(testCase.name+" - error - invalid xpub", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
xPub, err := tc.client.GetXpub(tc.ctx, "test")
require.Error(t, err)
require.Nil(t, xPub)
assert.ErrorIs(t, err, ErrMissingXpub)
})
ts.T().Run(testCase.name+" - error - missing xpub", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
xPub, err := tc.client.GetXpub(tc.ctx, testXPub)
require.Error(t, err)
require.Nil(t, xPub)
assert.ErrorIs(t, err, ErrMissingXpub)
})
}
}
// TestClient_GetXpubByID will test the method GetXpubByID()
func (ts *EmbeddedDBTestSuite) TestClient_GetXpubByID() {
for _, testCase := range dbTestCases {
ts.T().Run(testCase.name+" - valid", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
xPub, err := tc.client.NewXpub(tc.ctx, testXPub, tc.client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Equal(t, testXPubID, xPub.ID)
xPub2, err2 := tc.client.GetXpubByID(tc.ctx, xPub.ID)
require.NoError(t, err2)
assert.Equal(t, testXPubID, xPub2.ID)
})
ts.T().Run(testCase.name+" - error - invalid xpub", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
xPub, err := tc.client.GetXpubByID(tc.ctx, "test")
require.Error(t, err)
require.Nil(t, xPub)
})
ts.T().Run(testCase.name+" - error - missing xpub", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
xPub, err := tc.client.GetXpubByID(tc.ctx, testXPub)
require.Error(t, err)
require.Nil(t, xPub)
assert.ErrorIs(t, err, ErrMissingXpub)
})
}
}
// TestClient_UpdateXpubMetadata will test the method UpdateXpubMetadata()
func (ts *EmbeddedDBTestSuite) TestClient_UpdateXpubMetadata() {
for _, testCase := range dbTestCases {
ts.T().Run(testCase.name+" - valid", func(t *testing.T) {
tc := ts.genericDBClient(t, testCase.database, false)
defer tc.Close(tc.ctx)
metadata := Metadata{
"test-key-1": "test-value-1",
"test-key-2": "test-value-2",
"test-key-3": "test-value-3",
}
opts := tc.client.DefaultModelOptions()
opts = append(opts, WithMetadatas(metadata))
xPub, err := tc.client.NewXpub(tc.ctx, testXPub, opts...)
require.NoError(t, err)
assert.Equal(t, testXPubID, xPub.ID)
assert.Equal(t, metadata, xPub.Metadata)
xPub, err = tc.client.UpdateXpubMetadata(tc.ctx, xPub.ID, Metadata{"test-key-new": "new-value"})
require.NoError(t, err)
assert.Len(t, xPub.Metadata, 4)
assert.Equal(t, "new-value", xPub.Metadata["test-key-new"])
xPub, err = tc.client.UpdateXpubMetadata(tc.ctx, xPub.ID, Metadata{
"test-key-new-2": "new-value-2",
"test-key-1": nil,
"test-key-2": nil,
"test-key-3": nil,
})
require.NoError(t, err)
assert.Len(t, xPub.Metadata, 2)
assert.Equal(t, "new-value", xPub.Metadata["test-key-new"])
assert.Equal(t, "new-value-2", xPub.Metadata["test-key-new-2"])
err = xPub.Save(tc.ctx)
require.NoError(t, err)
// make sure it was saved
xPub2, err2 := tc.client.GetXpubByID(tc.ctx, xPub.ID)
require.NoError(t, err2)
assert.Len(t, xPub2.Metadata, 2)
assert.Equal(t, "new-value", xPub2.Metadata["test-key-new"])
assert.Equal(t, "new-value-2", xPub2.Metadata["test-key-new-2"])
})
}
}