forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo_test.go
76 lines (67 loc) · 2.11 KB
/
repo_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
package hooks
import (
"context"
"fmt"
"testing"
"github.com/prebid/prebid-server/v2/hooks/hookstage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewHookRepository(t *testing.T) {
id := "foobar"
testCases := map[string]struct {
isFound bool
providedHook interface{}
expectedHook interface{}
expectedErr error
getHookFn func(HookRepository) (interface{}, bool)
}{
"Added hook returns": {
isFound: true,
providedHook: hook{},
expectedHook: hook{},
expectedErr: nil,
getHookFn: func(repo HookRepository) (interface{}, bool) {
return repo.GetEntrypointHook(id)
},
},
"Not found hook": {
isFound: false,
providedHook: hook{},
expectedHook: nil,
expectedErr: nil,
getHookFn: func(repo HookRepository) (interface{}, bool) {
return repo.GetRawAuctionHook(id) // ask for not implemented hook
},
},
"Fails to add type that does not implement any hook interface": {
providedHook: struct{}{},
expectedErr: fmt.Errorf(`hook "%s" does not implement any supported hook interface`, id),
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
repo, err := NewHookRepository(map[string]interface{}{id: test.providedHook})
assert.Equal(t, test.expectedErr, err)
if test.expectedErr == nil {
hook, found := test.getHookFn(repo)
assert.Equal(t, test.isFound, found)
assert.Equal(t, test.expectedHook, hook)
}
})
}
}
func TestAddHook_FailsToAddHookOfSameTypeAndIdTwice(t *testing.T) {
id := "foobar"
hook := hook{}
repo := hookRepository{}
expectedErr := fmt.Errorf(`hook of type "%T" with id "%s" already registered`, new(hookstage.Entrypoint), id)
err := repo.add(id, hook)
require.NoError(t, err, "failed to add hook")
err = repo.add(id, hook)
assert.Equal(t, expectedErr, err)
}
type hook struct{}
func (h hook) HandleEntrypointHook(ctx context.Context, context hookstage.ModuleInvocationContext, payload hookstage.EntrypointPayload) (hookstage.HookResult[hookstage.EntrypointPayload], error) {
return hookstage.HookResult[hookstage.EntrypointPayload]{}, nil
}