-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_test.go
104 lines (86 loc) · 2.26 KB
/
service_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
package user_test
import (
"testing"
"app/adapter/logger"
"app/adapter/postgres"
"app/adapter/time"
"app/adapter/uuid"
"app/adapter/validation"
"app/adapter/watermill"
"app/config"
"app/internal/appcontext"
"app/test"
. "app/test/matchers"
"app/user"
"app/user/user_module"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
"gorm.io/gorm"
)
func TestUserService(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "user service suite")
}
var _ = Describe("user service", Ordered, func() {
var (
app *fxtest.App
userService *user.Service
db *gorm.DB
)
BeforeAll(func() {
app = fxtest.New(
GinkgoT(),
logger.NopLoggerProvider,
test.Queue,
appcontext.Module,
watermill.Module,
validation.Module,
config.Module,
postgres.Module,
user_module.ServiceModule,
uuid.Module,
time.Module,
fx.Populate(&userService, &db),
)
app.RequireStart()
})
BeforeEach(func() {
Must(db.Exec("BEGIN").Error)
})
AfterEach(func() {
Must(db.Exec("ROLLBACK").Error)
})
AfterAll(func() {
app.RequireStop()
})
Describe("DeleteAll", func() {
It("removes all users", func(ctx SpecContext) {
Must2(userService.CreateUser(ctx, "[email protected]"))
Expect(userService.List(ctx)).NotTo(BeEmpty())
Must(userService.DeleteAll(ctx))
Expect(userService.List(ctx)).To(BeEmpty())
})
})
It("created users can be found by id", func(ctx SpecContext) {
user := Must2(userService.CreateUser(ctx, "[email protected]"))
found := Must2(userService.FindByID(ctx, user.ID))
Expect(found).To(Equal(user))
})
It("created users can be found by email", func(ctx SpecContext) {
user := Must2(userService.CreateUser(ctx, "[email protected]"))
found := Must2(userService.FindByEmail(ctx, user.Email))
Expect(found).To(Equal(user))
})
It("lists created users", func(ctx SpecContext) {
user := Must2(userService.CreateUser(ctx, "[email protected]"))
Expect(userService.List(ctx)).To(ContainElement(user))
})
It("removed users are not listed", func(ctx SpecContext) {
user := Must2(userService.CreateUser(ctx, "[email protected]"))
Must(userService.Remove(ctx, user))
users := Must2(userService.List(ctx))
Expect(users).NotTo(ContainElement(user))
})
})