This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_mock.go
81 lines (68 loc) · 1.7 KB
/
repository_mock.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
package roles
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/golang-tire/auth/internal/entity"
"github.com/google/uuid"
)
var errCRUD = errors.New("error crud")
func NewMockRepository() *mockRepository {
return &mockRepository{}
}
type mockRepository struct {
items []entity.Role
}
func (m mockRepository) GetByTitle(ctx context.Context, title string) (entity.Role, error) {
for _, item := range m.items {
if item.Title == title {
return item, nil
}
}
return entity.Role{}, gorm.ErrRecordNotFound
}
func (m mockRepository) Get(ctx context.Context, id string) (entity.Role, error) {
for _, item := range m.items {
if item.UUID == id {
return item, nil
}
}
return entity.Role{}, gorm.ErrRecordNotFound
}
func (m mockRepository) Count(ctx context.Context) (int64, error) {
return int64(len(m.items)), nil
}
func (m mockRepository) Query(ctx context.Context, query string, offset, limit int64) ([]entity.Role, int, error) {
return m.items, len(m.items), nil
}
func (m *mockRepository) Create(ctx context.Context, role entity.Role) (string, error) {
Uuid := uuid.New().String()
role.UUID = Uuid
if role.Title == "error" {
return Uuid, errCRUD
}
m.items = append(m.items, role)
return Uuid, nil
}
func (m *mockRepository) Update(ctx context.Context, role entity.Role) error {
if role.Title == "error" {
return errCRUD
}
for i, item := range m.items {
if item.UUID == role.UUID {
m.items[i] = role
break
}
}
return nil
}
func (m *mockRepository) Delete(ctx context.Context, role entity.Role) error {
for i, item := range m.items {
if item.UUID == role.UUID {
m.items[i] = m.items[len(m.items)-1]
m.items = m.items[:len(m.items)-1]
break
}
}
return nil
}