forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_tests.go
150 lines (137 loc) · 4.4 KB
/
common_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
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
// Copyright (c) 2014 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testutil
import (
"context"
"reflect"
"testing"
"time"
eh "github.com/looplab/eventhorizon"
"github.com/looplab/eventhorizon/mocks"
)
// RepoCommonTests are test cases that are common to all
// implementations of projector drivers.
func RepoCommonTests(t *testing.T, ctx context.Context, repo eh.ReadWriteRepo) {
// Find non-existing item.
entity, err := repo.Find(ctx, eh.NewUUID())
if rrErr, ok := err.(eh.RepoError); !ok || rrErr.Err != eh.ErrEntityNotFound {
t.Error("there should be a ErrEntityNotFound error:", err)
}
if entity != nil {
t.Error("there should be no entity:", entity)
}
// FindAll with no items.
result, err := repo.FindAll(ctx)
if err != nil {
t.Error("there should be no error:", err)
}
if len(result) != 0 {
t.Error("there should be no items:", len(result))
}
// Save model without ID.
entityMissingID := &mocks.Model{
Content: "entity1",
CreatedAt: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
}
err = repo.Save(ctx, entityMissingID)
if rrErr, ok := err.(eh.RepoError); !ok || rrErr.BaseErr != eh.ErrMissingEntityID {
t.Error("there should be a ErrMissingEntityID error:", err)
}
// Save and find one item.
entity1 := &mocks.Model{
ID: eh.NewUUID(),
Content: "entity1",
CreatedAt: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
}
if err = repo.Save(ctx, entity1); err != nil {
t.Error("there should be no error:", err)
}
entity, err = repo.Find(ctx, entity1.ID)
if err != nil {
t.Error("there should be no error:", err)
}
if !reflect.DeepEqual(entity, entity1) {
t.Error("the item should be correct:", entity)
}
// FindAll with one item.
result, err = repo.FindAll(ctx)
if err != nil {
t.Error("there should be no error:", err)
}
if len(result) != 1 {
t.Error("there should be one item:", len(result))
}
if !reflect.DeepEqual(result, []eh.Entity{entity1}) {
t.Error("the item should be correct:", entity1)
}
// Save and overwrite with same ID.
entity1Alt := &mocks.Model{
ID: entity1.ID,
Content: "entity1Alt",
CreatedAt: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
}
if err = repo.Save(ctx, entity1Alt); err != nil {
t.Error("there should be no error:", err)
}
entity, err = repo.Find(ctx, entity1Alt.ID)
if err != nil {
t.Error("there should be no error:", err)
}
if !reflect.DeepEqual(entity, entity1Alt) {
t.Error("the item should be correct:", entity)
}
// Save with another ID.
entity2 := &mocks.Model{
ID: eh.NewUUID(),
Content: "entity2",
CreatedAt: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
}
if err = repo.Save(ctx, entity2); err != nil {
t.Error("there should be no error:", err)
}
entity, err = repo.Find(ctx, entity2.ID)
if err != nil {
t.Error("there should be no error:", err)
}
if !reflect.DeepEqual(entity, entity2) {
t.Error("the item should be correct:", entity)
}
// FindAll with two items, order should be preserved from insert.
result, err = repo.FindAll(ctx)
if err != nil {
t.Error("there should be no error:", err)
}
if len(result) != 2 {
t.Error("there should be two items:", len(result))
}
if !reflect.DeepEqual(result, []eh.Entity{entity1Alt, entity2}) {
t.Error("the items should be correct:", result)
}
// Remove item.
if err := repo.Remove(ctx, entity1Alt.ID); err != nil {
t.Error("there should be no error:", err)
}
entity, err = repo.Find(ctx, entity1Alt.ID)
if rrErr, ok := err.(eh.RepoError); !ok || rrErr.Err != eh.ErrEntityNotFound {
t.Error("there should be a ErrEntityNotFound error:", err)
}
if entity != nil {
t.Error("there should be no entity:", entity)
}
// Remove non-existing item.
err = repo.Remove(ctx, entity1Alt.ID)
if rrErr, ok := err.(eh.RepoError); !ok || rrErr.Err != eh.ErrEntityNotFound {
t.Error("there should be a ErrEntityNotFound error:", err)
}
}