-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.go
70 lines (56 loc) · 1.74 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
68
69
70
package tests
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/code-payments/code-server/pkg/code/data/rendezvous"
)
func RunTests(t *testing.T, s rendezvous.Store, teardown func()) {
for _, tf := range []func(t *testing.T, s rendezvous.Store){
testHappyPath,
} {
tf(t, s)
teardown()
}
}
func testHappyPath(t *testing.T, s rendezvous.Store) {
t.Run("testHappyPath", func(t *testing.T) {
ctx := context.Background()
start := time.Now()
time.Sleep(time.Millisecond)
record := &rendezvous.Record{
Key: "key",
Location: "localhost:1234",
}
cloned := record.Clone()
require.NoError(t, s.Delete(ctx, record.Key))
_, err := s.Get(ctx, record.Key)
assert.Equal(t, rendezvous.ErrNotFound, err)
require.NoError(t, s.Save(ctx, record))
actual, err := s.Get(ctx, record.Key)
require.NoError(t, err)
assert.True(t, actual.Id > 0)
assert.True(t, actual.CreatedAt.After(start))
assert.True(t, actual.LastUpdatedAt.After(start))
assertEquivalentRecords(t, &cloned, actual)
updateTime := time.Now()
time.Sleep(time.Millisecond)
record.Location = "localhost:5678"
cloned = record.Clone()
require.NoError(t, s.Save(ctx, record))
actual, err = s.Get(ctx, record.Key)
require.NoError(t, err)
assert.True(t, actual.CreatedAt.Before(updateTime))
assert.True(t, actual.LastUpdatedAt.After(updateTime))
assertEquivalentRecords(t, &cloned, actual)
require.NoError(t, s.Delete(ctx, record.Key))
_, err = s.Get(ctx, record.Key)
assert.Equal(t, rendezvous.ErrNotFound, err)
})
}
func assertEquivalentRecords(t *testing.T, obj1, obj2 *rendezvous.Record) {
assert.Equal(t, obj1.Key, obj2.Key)
assert.Equal(t, obj1.Location, obj2.Location)
}