-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.go
67 lines (51 loc) · 1.68 KB
/
tests.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
package tests
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/language"
"github.com/code-payments/code-server/pkg/code/data/preferences"
"github.com/code-payments/code-server/pkg/code/data/user"
)
func RunTests(t *testing.T, s preferences.Store, teardown func()) {
for _, tf := range []func(t *testing.T, s preferences.Store){
testRoundTrip,
} {
tf(t, s)
teardown()
}
}
func testRoundTrip(t *testing.T, s preferences.Store) {
t.Run("testRoundTrip", func(t *testing.T) {
ctx := context.Background()
containerId := user.NewDataContainerID()
_, err := s.Get(ctx, containerId)
assert.Equal(t, preferences.ErrPreferencesNotFound, err)
expected := preferences.GetDefaultPreferences(containerId)
cloned := expected.Clone()
start := time.Now()
time.Sleep(time.Millisecond)
require.NoError(t, s.Save(ctx, expected))
assert.True(t, expected.Id > 0)
assert.True(t, expected.LastUpdatedAt.After(start))
actual, err := s.Get(ctx, containerId)
require.NoError(t, err)
assertEquivalentRecords(t, &cloned, actual)
expected.Locale = language.CanadianFrench
cloned = expected.Clone()
start = time.Now()
time.Sleep(time.Millisecond)
require.NoError(t, s.Save(ctx, expected))
assert.Equal(t, cloned.Id, expected.Id)
assert.True(t, expected.LastUpdatedAt.After(start))
actual, err = s.Get(ctx, containerId)
require.NoError(t, err)
assertEquivalentRecords(t, &cloned, actual)
})
}
func assertEquivalentRecords(t *testing.T, obj1, obj2 *preferences.Record) {
assert.Equal(t, obj1.DataContainerId, obj2.DataContainerId)
assert.Equal(t, obj1.Locale.String(), obj2.Locale.String())
}