Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit 5e348d3

Browse files
committed
Add test
1 parent 45eb337 commit 5e348d3

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

app/di/test_store_component.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"log"
77
"strings"
88

9+
"github.com/volatiletech/sqlboiler/boil"
10+
911
"github.com/ProgrammingLab/prolab-accounts/app/config"
1012
"github.com/ProgrammingLab/prolab-accounts/infra/record"
1113
)
@@ -14,6 +16,8 @@ import (
1416
func MustCreateTestStoreComponent(cfg *config.Config) *TestStoreComponent {
1517
db := mustConnectTestRDB(cfg)
1618

19+
boil.SetDB(db)
20+
1721
return &TestStoreComponent{
1822
storeComponentImpl: &storeComponentImpl{
1923
db: db,

app/server/users_server_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,94 @@
11
package server
2+
3+
import (
4+
"context"
5+
"log"
6+
"testing"
7+
8+
api_pb "github.com/ProgrammingLab/prolab-accounts/api"
9+
"github.com/ProgrammingLab/prolab-accounts/app/config"
10+
"github.com/ProgrammingLab/prolab-accounts/app/di"
11+
"github.com/ProgrammingLab/prolab-accounts/model"
12+
)
13+
14+
func Test_UsersServer_GetUser(t *testing.T) {
15+
cfg, err := config.LoadConfig("../../.env")
16+
if err != nil {
17+
log.Fatalf("%+v", err)
18+
}
19+
20+
store := di.MustCreateTestStoreComponent(cfg)
21+
defer store.MustClose()
22+
ctx := context.Background()
23+
users, err := CreateTestUsers(ctx, store)
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
t.Run("Get public user", func(t *testing.T) {
29+
var req *api_pb.GetUserRequest
30+
for _, u := range users {
31+
if userProfileScope(model.UserID(u.ID)) != model.Public {
32+
continue
33+
}
34+
35+
req = &api_pb.GetUserRequest{
36+
UserName: u.Name,
37+
}
38+
break
39+
}
40+
41+
srv := NewUserServiceServer(store, cfg)
42+
resp, err := srv.GetUser(ctx, req)
43+
44+
if err != nil {
45+
t.Errorf("returned an error %v", err)
46+
}
47+
48+
if resp == nil {
49+
t.Error("response should not nil")
50+
}
51+
52+
if got, want := resp.Email, ""; got != want {
53+
t.Errorf("Email is %v, want %v", got, want)
54+
}
55+
if got, want := resp.FullName, ""; got != want {
56+
t.Errorf("FullName is %v, want %v", got, want)
57+
}
58+
})
59+
60+
t.Run("Get members only user", func(t *testing.T) {
61+
var req *api_pb.GetUserRequest
62+
for _, u := range users {
63+
if userProfileScope(model.UserID(u.ID)) != model.MembersOnly {
64+
continue
65+
}
66+
67+
req = &api_pb.GetUserRequest{
68+
UserName: u.Name,
69+
}
70+
break
71+
}
72+
73+
srv := NewUserServiceServer(store, cfg)
74+
resp, err := srv.GetUser(ctx, req)
75+
76+
if err != nil {
77+
t.Errorf("returned an error %v", err)
78+
}
79+
80+
if resp == nil {
81+
t.Error("response should not nil")
82+
}
83+
84+
if got, want := resp.Email, ""; got != want {
85+
t.Errorf("Email is %v, want %v", got, want)
86+
}
87+
if got, want := resp.FullName, ""; got != want {
88+
t.Errorf("FullName is %v, want %v", got, want)
89+
}
90+
if got, want := resp.Description, ""; got != want {
91+
t.Errorf("Description is %v, want %v", got, want)
92+
}
93+
})
94+
}

0 commit comments

Comments
 (0)