-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediator_test.go
56 lines (41 loc) · 963 Bytes
/
mediator_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
package headwater
import (
"context"
"fmt"
"testing"
)
type User struct {
Id string
Name string
}
type GetUserQuery struct {
Id string
}
type GetUserReceiver = Receiver[*GetUserQuery, *User]
type FullMediator struct {
GetUser GetUserReceiver
}
var id = "1"
var user = User{Id: id}
func CreateGetUserHandler(mediator *FullMediator) {
var users = map[string]*User{id: &user}
mediator.GetUser.SetHandler(func(context context.Context, request *GetUserQuery) (*User, error) {
user := users[request.Id]
if user == nil {
return nil, fmt.Errorf("no user for id: %v", request.Id)
}
return user, nil
})
}
func TestMediator(t *testing.T) {
mediator := FullMediator{}
CreateGetUserHandler(&mediator)
result, error := mediator.GetUser.Send(context.TODO(), &GetUserQuery{Id: id})
if error != nil {
t.Errorf("Received error: %v", error)
}
want := &user
if result != want {
t.Errorf("Received: %v, Wanted: %v", result, want)
}
}