-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.mocks.go
114 lines (92 loc) · 3.52 KB
/
tests.mocks.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
110
111
112
113
114
package main
import (
"context"
"time"
)
// This file contains mocks definitions needed to perform unit tests.
type MockBookStorage struct {
AddFunc func(ctx context.Context, id string, book Book) error
GetOneFunc func(ctx context.Context, id string) (Book, error)
DeleteFunc func(ctx context.Context, id string) error
UpdateFunc func(ctx context.Context, id string, book Book) (Book, error)
GetAllFunc func(ctx context.Context) ([]Book, error)
DeleteAllFunc func(ctx context.Context) error
}
// Add mocks the behavior of book creation by the repository.
func (m *MockBookStorage) Add(ctx context.Context, id string, book Book) error {
return m.AddFunc(ctx, id, book)
}
// GetOne mocks the behavior of retrieving a book by the repository.
func (m *MockBookStorage) GetOne(ctx context.Context, id string) (Book, error) {
return m.GetOneFunc(ctx, id)
}
// Delete mocks the behavior of deleting a book by the repository.
func (m *MockBookStorage) Delete(ctx context.Context, id string) error {
return m.DeleteFunc(ctx, id)
}
// Update mocks the behavior of updating a book by the repository.
func (m *MockBookStorage) Update(ctx context.Context, id string, book Book) (Book, error) {
return m.UpdateFunc(ctx, id, book)
}
// GetAll mocks the behavior of retrieving all books by the repository.
func (m *MockBookStorage) GetAll(ctx context.Context) ([]Book, error) {
return m.GetAllFunc(ctx)
}
// DeleteAll mocks the behavior of deleting all books by the repository.
func (m *MockBookStorage) DeleteAll(ctx context.Context) error {
return m.DeleteAllFunc(ctx)
}
// MockClocker implements a fake Clocker.
type MockClocker struct {
MockNow time.Time
MockZero time.Time
}
// NewMockClocker returns a mocked instance with fixed time.
func NewMockClocker() *MockClocker {
return &MockClocker{time.Date(2023, 0o7, 0o2, 0o0, 0o0, 0o0, 0o00000000, time.UTC), time.Time{}}
}
// Now returns an already defined time to be used as mock. This
// equals to `Sun, 02 Jul 2023 00:00:00 UTC` in time.RFC1123 format.
// equals to `2023-07-02 00:00:00 +0000 UTC` in String format.
func (mck *MockClocker) Now() time.Time {
return mck.MockNow
}
// Zero returns zero time.Time to be used as mock.
func (mck *MockClocker) Zero() time.Time {
return mck.MockZero
}
// MockUIDHandler implements a fake UIDHandler.
type MockUIDHandler struct {
MockedUID string
Valid bool
}
// NewMockUIDHandler returns a mocked instance with predictable id.
func NewMockUIDHandler(id string, valid bool) *MockUIDHandler {
return &MockUIDHandler{MockedUID: id, Valid: valid}
}
// Generate constructs a predictable id to be used as mock.
func (muid *MockUIDHandler) Generate(prefix string) string {
return prefix + ":" + muid.MockedUID
}
// IsValid mocks IsValid behavior by providing configured status.
func (muid *MockUIDHandler) IsValid(_, _ string) bool {
return muid.Valid
}
type MockQueuer struct {
PushFunc func(ctx context.Context, qid string, book Book) error
PopFunc func(ctx context.Context, qids ...string) (string, Book, error)
}
// Push mocks the behavior of book enqueuing into the queue.
func (m *MockQueuer) Push(ctx context.Context, qid string, book Book) error {
return m.PushFunc(ctx, qid, book)
}
// Pop mocks the behavior of deuqueing a book from the queue.
func (m *MockQueuer) Pop(ctx context.Context, qids ...string) (string, Book, error) {
return m.PopFunc(ctx, qids...)
}
type MockConsumer struct {
ConsumeFunc func(ctx context.Context, qids ...string)
}
func (m *MockConsumer) Consume(ctx context.Context, qids ...string) {
m.ConsumeFunc(ctx, qids...)
}