-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_user_interactor_test.go
109 lines (83 loc) · 3.02 KB
/
create_user_interactor_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
package usecase_test
import (
"context"
"testing"
"user-api/entity/user"
"user-api/test/double"
"user-api/usecase"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
const (
id_user_creation string = "1234"
name_user_creation string = "Name"
email_user_creation string = "Email"
age_user_creation int = 42
)
var userExpected user.User
var request usecase.CreateUserRequest
func init() {
request, _ = usecase.
NewCreateUserRequest(name_user_creation, email_user_creation, age_user_creation)
userExpected, _ = user.
NewBuilder().
Id("123455").
Name("name").
Email("email").
Age(123).
Build()
}
func TestUserCreationWithSucess(t *testing.T) {
var repository *usecase.UserRepository = double.NewUserRepositoryDouble(
func(ctx context.Context, user user.User) (user.User, error) {
return userExpected, nil
},
func(ctx context.Context, name string) (user.User, error) {
return nil, usecase.NewUserDontExistError("")
},
nil,
)
useCase, _ := usecase.NewCreateUser(repository)
response, err := useCase.Create(context.Background(), request)
assert.Nil(t, err, "Unexpected error to the create user")
assert.Equal(t, response.Id(), userExpected.Id(), "Invalid Id from userCreation")
}
func TestUserCreationAlreadyCreated(t *testing.T) {
var repository *usecase.UserRepository = double.NewUserRepositoryDouble(
nil,
func(ctx context.Context, name string) (user.User, error) { return userExpected, nil },
nil,
)
useCase, _ := usecase.NewCreateUser(repository)
response, err := useCase.Create(context.Background(), request)
assert.Error(t, err, "User created even that another user have already created")
assert.Nil(t, response, "Response created even that another user exist in repository")
}
func TestCreateWithRepositoryReturningErrorToTryCreateUser(t *testing.T) {
var repository *usecase.UserRepository = double.NewUserRepositoryDouble(
func(ctx context.Context, user user.User) (user.User, error) { return nil, errors.New("Error") },
func(ctx context.Context, name string) (user.User, error) { return nil, nil },
nil,
)
useCase, _ := usecase.NewCreateUser(repository)
response, err := useCase.Create(context.Background(), request)
assert.Error(t, err, "Create method working without requested param")
assert.Nil(t, response, "Reponse create even without requested param")
}
func TestCreateErrorWhenCreateUserWithRepositoryReturn(t *testing.T) {
usrReturnedFromRepo, _ := user.
NewBuilder().
Name("name").
Email("email").
Age(123).
Build()
var repository *usecase.UserRepository = double.NewUserRepositoryDouble(
func(ctx context.Context, user user.User) (user.User, error) { return usrReturnedFromRepo, nil },
func(ctx context.Context, name string) (user.User, error) { return nil, nil },
nil,
)
useCase, _ := usecase.NewCreateUser(repository)
response, err := useCase.Create(context.Background(), request)
assert.Error(t, err, "Repository returning User without id after creation")
assert.Nil(t, response, "Reponse create even without requested param Id")
}