-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
219 lines (178 loc) · 5.28 KB
/
example_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
package mongodb_test
import (
"errors"
"github.com/mbretter/go-mongodb"
"github.com/mbretter/go-mongodb/types"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"log"
"os"
"testing"
)
// User represents a user entity with an ID, username, and personal details such as firstname and lastname.
type User struct {
Id types.ObjectId `bson:"_id"`
Username string `bson:"username,omitempty"`
Firstname string `bson:"firstname,omitempty"`
Lastname string `bson:"lastname,omitempty"`
}
// UserDbInterface defines the methods required to interact with the User entity in the database.
// Insert adds a new User to the database and returns an error if the operation fails.
// Update modifies an existing User in the database and returns an UpdateResult and an error if the operation fails.
// Read retrieves a User by their ObjectId and returns the User and an error if the operation fails.
// Delete removes a User by their ObjectId and returns a DeleteResult and an error if the operation fails.
type UserDbInterface interface {
Insert(*User) error
Update(user *User) (*mongo.UpdateResult, error)
Read(id types.ObjectId) (*User, error)
Delete(id types.ObjectId) (*mongo.DeleteResult, error)
}
// UserModel provides methods for managing user data by integrating with the UserDbInterface.
type UserModel struct {
db UserDbInterface
}
// ProvideModel initializes a User instance with the provided UserDbInterface.
func ProvideModel(db UserDbInterface) UserModel {
return UserModel{db: db}
}
func (m UserModel) Create(user User) (User, error) {
user.Id = types.NewObjectId()
err := m.db.Insert(&user)
if err != nil {
return user, err
}
return user, nil
}
func (m UserModel) ReadById(id types.ObjectId) (*User, error) {
return m.db.Read(id)
}
func (m UserModel) Update(user User) error {
_, err := m.db.Update(&user)
return err
}
func (m UserModel) DeleteById(id types.ObjectId) error {
_, err := m.db.Delete(id)
return err
}
// UserDb provides methods to interact with the user collection in the database using a Connector interface.
type UserDb struct {
conn mongodb.Connector
}
func (d *UserDb) Insert(user *User) error {
_, err := d.conn.InsertOne(user)
if err != nil {
return err
}
return nil
}
func (d *UserDb) Update(user *User) (*mongo.UpdateResult, error) {
res, err := d.conn.UpdateById(user.Id, bson.D{{"$set", user}})
return res, err
}
func (d *UserDb) Read(id types.ObjectId) (*User, error) {
var ret User
err := d.conn.FindOne(bson.D{{"_id", id}}).Decode(&ret)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, nil
}
return nil, err
}
return &ret, nil
}
func (d *UserDb) Delete(id types.ObjectId) (*mongo.DeleteResult, error) {
return d.conn.DeleteOne(bson.D{{"_id", id}})
}
func ProviderUserDb(conn mongodb.Connector) *UserDb {
return &UserDb{
conn: conn.WithCollection("user"),
}
}
// Example demonstrates the process of creating, reading, updating, and deleting a user in a MongoDB database.
func Example() {
conn, err := mongodb.NewConnector(mongodb.NewParams{
Uri: os.Getenv("MONGODB_URI"),
Database: os.Getenv("MONGODB_DB"),
})
if err != nil {
log.Fatalf("failed to connect to db: %v\n", err)
}
userDb := ProviderUserDb(conn)
userModel := ProvideModel(userDb)
user := User{
Username: "foo@bar.com",
Firstname: "John",
Lastname: "Doe",
}
user, err = userModel.Create(user)
if err != nil {
log.Fatalf("failed to create user: %v", err)
}
log.Printf("created user: %v", user)
existingUser, err := userModel.ReadById(user.Id)
if err != nil {
log.Fatalf("failed to read user: %v", err)
}
if existingUser == nil {
log.Fatalf("user not found")
}
updateUser := User{
Id: existingUser.Id,
Firstname: "Jane",
}
err = userModel.Update(updateUser)
if err != nil {
log.Fatalf("failed to update user: %v", err)
}
err = userModel.DeleteById(user.Id)
if err != nil {
log.Fatalf("failed to delete user: %v", err)
}
}
// Example_Test tests the scenario for user creation, ensuring correct insertion and data consistency.
// The Connector is mocked using the auto-generated mock by mockery.
// The ObjectId generator function is stubbed, to get reproducable results.
func Example_TestCreate(t *testing.T) {
newUserId := "66cc9ca8c042f7a732b7fc2a"
types.SetObjectIdGenerator(func() string { return newUserId })
user := User{
Id: types.ObjectId(newUserId),
Username: "foo@bar.com",
Firstname: "John",
Lastname: "Doe",
}
tests := []struct {
name string
err error
}{
{
"Success",
nil,
},
{
"Failed",
errors.New("some database error occurred"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
conn := NewConnectorMock(t)
conn.EXPECT().WithCollection("user").Return(conn)
userDb := ProviderUserDb(conn)
userModel := ProvideModel(userDb)
res := mongo.InsertOneResult{}
conn.EXPECT().InsertOne(&user).Return(&res, test.err)
user, err := userModel.Create(user)
if test.err == nil {
assert.Nil(t, err)
assert.Equal(t, user.Id, types.ObjectId(newUserId))
assert.Equal(t, user.Username, "foo@bar.com")
assert.Equal(t, user.Firstname, "John")
assert.Equal(t, user.Lastname, "Doe")
} else {
assert.NotNil(t, err)
}
})
}
}