-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.repo.redis_test.go
155 lines (133 loc) · 4.59 KB
/
tests.repo.redis_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
package main
import (
"context"
"net"
"reflect"
"testing"
"github.com/ory/dockertest/v3"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
func startRedisDockerContainer(t *testing.T) (string, func()) {
t.Helper()
pool, err := dockertest.NewPool("")
if err != nil {
t.Fatalf("Failed to start Dockertest: %+v", err)
}
err = pool.Client.Ping()
if err != nil {
t.Skipf("Skipped. Could not connect to Docker: %+v", err)
}
resource, err := pool.Run("redis", "7.0.10-alpine", nil)
if err != nil {
t.Fatalf("Failed to start redis: %+v", err)
}
// build address the container is listening on
addr := net.JoinHostPort("localhost", resource.GetPort("6379/tcp"))
// ensure to wait for the container to be ready
err = pool.Retry(func() error {
var e error
client := redis.NewClient(&redis.Options{Addr: addr})
defer client.Close()
e = client.Ping(context.Background()).Err()
return e
})
if err != nil {
t.Fatalf("failed to ping redis: %+v", err)
}
destroyFunc := func() {
if err := pool.Purge(resource); err != nil {
t.Logf("failed to purge resource: %+v", err)
}
}
return addr, destroyFunc
}
// Ensure concrete type redisBookStorage satisfies BookStorage interface.
func TestRedisBookStorageImplementsBookStorageInterface(t *testing.T) {
var i interface{} = new(redisBookStorage)
if _, ok := i.(BookStorage); !ok {
t.Fatalf("expected %T to implement BookStorage", i)
}
}
func TestRedisStore(t *testing.T) {
t.Skip("github actions failing to pull container. Failed to start redis: API error (500): Get https://registry-1.docker.io/v2/library/redis/manifests/sha256:0859ed47321d2d26a3f53bca47b76fb7970ea2512ca3a379926dc965880e442e: EOF")
addr, destroyFunc := startRedisDockerContainer(t)
defer destroyFunc()
rs := NewRedisBookStorage(zap.NewNop(), redis.NewClient(&redis.Options{Addr: addr}))
testBook0ID, testBook1ID := "b:0", "b:1"
testBook := Book{
ID: testBook0ID,
Title: "Redis test book title",
Description: "Redis test book desc",
Author: "Jerome Amon",
Price: "10$",
CreatedAt: "2023-07-01 20:19:10.7604632 +0000 UTC",
UpdatedAt: "2023-07-01 20:19:10.7604632 +0000 UTC",
}
t.Run("Add Book", func(t *testing.T) {
// ensures we can insert new book record.
err := rs.Add(context.Background(), testBook0ID, testBook)
assert.NoError(t, err)
})
t.Run("Get Existent Book", func(t *testing.T) {
// ensures we can fetch specific book.
book, err := rs.GetOne(context.Background(), testBook0ID)
assert.NoError(t, err)
if !reflect.DeepEqual(testBook, book) {
t.Errorf("Got %v but Expected %v.", book, testBook)
}
})
t.Run("Get NonExistent Book", func(t *testing.T) {
// ensures fetching non-existent book fails.
book, err := rs.GetOne(context.Background(), testBook1ID)
assert.Equal(t, ErrBookNotFound, err)
assert.Equal(t, Book{}, book)
})
t.Run("Delete Existent Book", func(t *testing.T) {
// ensures deleting existent book succeed.
err := rs.Delete(context.Background(), testBook0ID)
assert.NoError(t, err)
book, err := rs.GetOne(context.Background(), testBook0ID)
assert.Equal(t, ErrBookNotFound, err)
assert.Equal(t, Book{}, book)
})
t.Run("Delete NonExistent Book", func(t *testing.T) {
// ensures deleting non existent book returns an error.
err := rs.Delete(context.Background(), testBook1ID)
assert.Equal(t, ErrBookNotFound, err)
})
t.Run("Update NonExistent Book", func(t *testing.T) {
// ensures updating non-existing book create that book.
book, err := rs.Update(context.Background(), testBook0ID, testBook)
assert.NoError(t, err)
if !reflect.DeepEqual(testBook, book) {
t.Errorf("Got %v but Expected %v.", book, testBook)
}
book, err = rs.GetOne(context.Background(), testBook0ID)
assert.NoError(t, err)
if !reflect.DeepEqual(testBook, book) {
t.Errorf("Got %v but Expected %v.", book, testBook)
}
})
t.Run("Update Existent Book", func(t *testing.T) {
// ensures we can update an existent book record.
testBook.Price = "20$"
book, err := rs.Update(context.Background(), testBook0ID, testBook)
assert.NoError(t, err)
if !reflect.DeepEqual(testBook, book) {
t.Errorf("Got %v but Expected %v.", book, testBook)
}
book, err = rs.GetOne(context.Background(), testBook0ID)
assert.NoError(t, err)
assert.Equal(t, testBook.Price, book.Price)
})
t.Run("Get All Books", func(t *testing.T) {
// ensures we get exact number of stored books.
err := rs.Add(context.Background(), testBook1ID, testBook)
assert.NoError(t, err)
books, err := rs.GetAll(context.Background())
assert.NoError(t, err)
assert.Equal(t, 2, len(books))
})
}