|
1
|
|
|
package domain_test |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"testing" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/memnix/memnix-rest/domain" |
|
7
|
|
|
) |
|
8
|
|
|
|
|
9
|
|
|
func TestDeck_TableName(t *testing.T) { |
|
10
|
|
|
deck := &domain.Deck{} |
|
11
|
|
|
|
|
12
|
|
|
expected := "decks" |
|
13
|
|
|
tableName := deck.TableName() |
|
14
|
|
|
|
|
15
|
|
|
if tableName != expected { |
|
16
|
|
|
t.Errorf("Expected table name to be %s, but got %s", expected, tableName) |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
func TestDeck_ToPublicDeck(t *testing.T) { |
|
21
|
|
|
deck := &domain.Deck{ |
|
22
|
|
|
Name: "Test Deck", |
|
23
|
|
|
Description: "Test Description", |
|
24
|
|
|
Lang: "English", |
|
25
|
|
|
Banner: "Test Banner", |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
deck.ID = 1 |
|
29
|
|
|
|
|
30
|
|
|
expected := domain.PublicDeck{ |
|
31
|
|
|
ID: 1, |
|
32
|
|
|
Name: "Test Deck", |
|
33
|
|
|
Description: "Test Description", |
|
34
|
|
|
Lang: "English", |
|
35
|
|
|
Banner: "Test Banner", |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
publicDeck := deck.ToPublicDeck() |
|
39
|
|
|
|
|
40
|
|
|
if publicDeck != expected { |
|
41
|
|
|
t.Errorf("Expected public deck to be %v, but got %v", expected, publicDeck) |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
func TestDeck_IsOwner(t *testing.T) { |
|
46
|
|
|
deck := &domain.Deck{ |
|
47
|
|
|
OwnerID: 1, |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Test case 1: Owner ID matches |
|
51
|
|
|
id := uint(1) |
|
52
|
|
|
expected := true |
|
53
|
|
|
result := deck.IsOwner(id) |
|
54
|
|
|
if result != expected { |
|
55
|
|
|
t.Errorf("Expected IsOwner(%d) to be %v, but got %v", id, expected, result) |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Test case 2: Owner ID does not match |
|
59
|
|
|
id = uint(2) |
|
60
|
|
|
expected = false |
|
61
|
|
|
result = deck.IsOwner(id) |
|
62
|
|
|
if result != expected { |
|
63
|
|
|
t.Errorf("Expected IsOwner(%d) to be %v, but got %v", id, expected, result) |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
func TestCreateDeck_ToDeck(t *testing.T) { |
|
68
|
|
|
createDeck := &domain.CreateDeck{ |
|
69
|
|
|
Name: "Test Name", |
|
70
|
|
|
Description: "Test Description", |
|
71
|
|
|
Lang: "English", |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
expected := domain.Deck{ |
|
75
|
|
|
Name: "Test Name", |
|
76
|
|
|
Description: "Test Description", |
|
77
|
|
|
Lang: "English", |
|
78
|
|
|
Status: domain.DeckStatusPrivate, |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
deck := createDeck.ToDeck() |
|
82
|
|
|
|
|
83
|
|
|
if deck.Name != expected.Name { |
|
84
|
|
|
t.Errorf("Expected deck name to be %s, but got %s", expected.Name, deck.Name) |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if deck.Description != expected.Description { |
|
88
|
|
|
t.Errorf("Expected deck description to be %s, but got %s", expected.Description, deck.Description) |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if deck.Lang != expected.Lang { |
|
92
|
|
|
t.Errorf("Expected deck language to be %s, but got %s", expected.Lang, deck.Lang) |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if deck.Status != expected.Status { |
|
96
|
|
|
t.Errorf("Expected deck status to be %s, but got %s", expected.Status, deck.Status) |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
func TestDeckStatus_String(t *testing.T) { |
|
101
|
|
|
tests := []struct { |
|
102
|
|
|
expected string |
|
103
|
|
|
status domain.DeckStatus |
|
104
|
|
|
}{ |
|
105
|
|
|
{status: domain.DeckStatusPrivate, expected: "private"}, |
|
106
|
|
|
{status: domain.DeckStatusToReview, expected: "to review"}, |
|
107
|
|
|
{status: domain.DeckStatusPublic, expected: "public"}, |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
for _, test := range tests { |
|
111
|
|
|
result := test.status.String() |
|
112
|
|
|
if result != test.expected { |
|
113
|
|
|
t.Errorf("Expected DeckStatus.String() to return %s, but got %s", test.expected, result) |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
func TestCreateDeck_Validate(t *testing.T) { |
|
119
|
|
|
testCases := []struct { |
|
120
|
|
|
createDeck *domain.CreateDeck |
|
121
|
|
|
name string |
|
122
|
|
|
shouldErr bool |
|
123
|
|
|
}{ |
|
124
|
|
|
{ |
|
125
|
|
|
name: "Valid CreateDeck", |
|
126
|
|
|
createDeck: &domain.CreateDeck{ |
|
127
|
|
|
Name: "Test Name", |
|
128
|
|
|
Description: "Test Description", |
|
129
|
|
|
Lang: "EN", |
|
130
|
|
|
Banner: "https://test.com/banner.png", |
|
131
|
|
|
}, |
|
132
|
|
|
shouldErr: false, |
|
133
|
|
|
}, |
|
134
|
|
|
{ |
|
135
|
|
|
name: "No Name CreateDeck", |
|
136
|
|
|
createDeck: &domain.CreateDeck{ |
|
137
|
|
|
Name: "", |
|
138
|
|
|
Description: "Test Description", |
|
139
|
|
|
Lang: "EN", |
|
140
|
|
|
Banner: "https://test.com/banner.png", |
|
141
|
|
|
}, |
|
142
|
|
|
shouldErr: true, |
|
143
|
|
|
}, |
|
144
|
|
|
{ |
|
145
|
|
|
name: "No Description CreateDeck", |
|
146
|
|
|
createDeck: &domain.CreateDeck{ |
|
147
|
|
|
Name: "Test Name", |
|
148
|
|
|
Description: "", |
|
149
|
|
|
Lang: "EN", |
|
150
|
|
|
Banner: "https://test.com/banner.png", |
|
151
|
|
|
}, |
|
152
|
|
|
shouldErr: true, |
|
153
|
|
|
}, |
|
154
|
|
|
{ |
|
155
|
|
|
name: "No Lang CreateDeck", |
|
156
|
|
|
createDeck: &domain.CreateDeck{ |
|
157
|
|
|
Name: "Test Name", |
|
158
|
|
|
Description: "Test Description", |
|
159
|
|
|
Lang: "", |
|
160
|
|
|
Banner: "https://test.com/banner.png", |
|
161
|
|
|
}, |
|
162
|
|
|
shouldErr: true, |
|
163
|
|
|
}, |
|
164
|
|
|
{ |
|
165
|
|
|
name: "No Banner CreateDeck", |
|
166
|
|
|
createDeck: &domain.CreateDeck{ |
|
167
|
|
|
Name: "Test Name", |
|
168
|
|
|
Description: "Test Description", |
|
169
|
|
|
Lang: "EN", |
|
170
|
|
|
Banner: "", |
|
171
|
|
|
}, |
|
172
|
|
|
shouldErr: true, |
|
173
|
|
|
}, |
|
174
|
|
|
{ |
|
175
|
|
|
name: "Invalid Lang CreateDeck", |
|
176
|
|
|
createDeck: &domain.CreateDeck{ |
|
177
|
|
|
Name: "Test Name", |
|
178
|
|
|
Description: "Test Description", |
|
179
|
|
|
Lang: "ENGLISH", |
|
180
|
|
|
Banner: "https://test.com/banner.png", |
|
181
|
|
|
}, |
|
182
|
|
|
shouldErr: true, |
|
183
|
|
|
}, |
|
184
|
|
|
{ |
|
185
|
|
|
name: "Invalid Banner CreateDeck", |
|
186
|
|
|
createDeck: &domain.CreateDeck{ |
|
187
|
|
|
Name: "Test Name", |
|
188
|
|
|
Description: "Test Description", |
|
189
|
|
|
Lang: "EN", |
|
190
|
|
|
Banner: "test.com/banner.png", |
|
191
|
|
|
}, |
|
192
|
|
|
shouldErr: true, |
|
193
|
|
|
}, |
|
194
|
|
|
{ |
|
195
|
|
|
name: "Invalid Language CreateDeck", |
|
196
|
|
|
createDeck: &domain.CreateDeck{ |
|
197
|
|
|
Name: "Test Name", |
|
198
|
|
|
Description: "Test Description", |
|
199
|
|
|
Lang: "INVALID", |
|
200
|
|
|
Banner: "https://test.com/banner.png", |
|
201
|
|
|
}, |
|
202
|
|
|
shouldErr: true, |
|
203
|
|
|
}, |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
for _, tc := range testCases { |
|
207
|
|
|
t.Run(tc.name, func(t *testing.T) { |
|
208
|
|
|
err := tc.createDeck.Validate() |
|
209
|
|
|
if tc.shouldErr && err == nil { |
|
210
|
|
|
t.Errorf("Expected error, but got nil") |
|
211
|
|
|
} |
|
212
|
|
|
if !tc.shouldErr && err != nil { |
|
213
|
|
|
t.Errorf("Expected no error, but got %v", err) |
|
214
|
|
|
} |
|
215
|
|
|
}) |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|