Debian Codegraph @

collection_page_test.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "errors"
5 "reflect"
6 "testing"
7
8 "github.com/google/go-cmp/cmp"
9 )
10
11 func mockCollectionPage(items ...Item) CollectionPage {
12 cc := CollectionPage{
13 ID: IRIf("https://example.com", Inbox),
14 Type: CollectionPageType,
15 }
16 if len(items) == 0 {
17 cc.Items = make(ItemCollection, 0)
18 } else {
19 cc.Items = items
20 cc.TotalItems = uint(len(items))
21 }
22 return cc
23 }
24
25 func TestCollectionPageNew(t *testing.T) {
26 testValue := ID("test")
27
28 c := CollectionNew(testValue)
29 p := CollectionPageNew(c)
30 if reflect.DeepEqual(p.Collection, c) {
31 t.Errorf("Invalid collection parent '%v'", p.PartOf)
32 }
33 if p.PartOf != c.GetLink() {
34 t.Errorf("Invalid collection '%v'", p.PartOf)
35 }
36 }
37
38 func TestCollectionPage_Append(t *testing.T) {
39 tests := []struct {
40 name string
41 col CollectionPage
42 it []Item
43 wantErr error
44 }{
45 {
46 name: "empty",
47 col: mockCollectionPage(),
48 it: ItemCollection{},
49 },
50 {
51 name: "add one item",
52 col: mockCollectionPage(),
53 it: ItemCollection{
54 Object{ID: ID("grrr")},
55 },
56 },
57 }
58 for _, tt := range tests {
59 t.Run(tt.name, func(t *testing.T) {
60 defer func() {
61 tt.col.Items = tt.col.Items[:0]
62 tt.col.TotalItems = 0
63 }()
64 if err := tt.col.Append(tt.it...); (err != nil) && errors.Is(err, tt.wantErr) {
65 t.Errorf("Append() error = %v, wantErr %v", err, tt.wantErr)
66 }
67 if tt.col.TotalItems != uint(len(tt.it)) {
68 t.Errorf("Post Append() %T TotalItems %d different than added count %d", tt.col, tt.col.TotalItems, len(tt.it))
69 }
70 for _, it := range tt.it {
71 if !tt.col.Items.Contains(it) {
72 t.Errorf("Post Append() unable to find %s in %T Items %v", tt.col, it.GetLink(), tt.col.Items)
73 }
74 }
75 })
76 }
77 }
78
79 func TestCollectionPage_UnmarshalJSON(t *testing.T) {
80 p := CollectionPage{}
81
82 dataEmpty := []byte("{}")
83 p.UnmarshalJSON(dataEmpty)
84 if p.ID != "" {
85 t.Errorf("Unmarshaled object should have empty ID, received %q", p.ID)
86 }
87 if HasTypes(p) {
88 t.Errorf("Unmarshaled object should have empty Type, received %q", p.GetType())
89 }
90 if p.AttributedTo != nil {
91 t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", p.AttributedTo)
92 }
93 if len(p.Name) != 0 {
94 t.Errorf("Unmarshaled object should have empty Name, received %q", p.Name)
95 }
96 if len(p.Summary) != 0 {
97 t.Errorf("Unmarshaled object should have empty Summary, received %q", p.Summary)
98 }
99 if len(p.Content) != 0 {
100 t.Errorf("Unmarshaled object should have empty Content, received %q", p.Content)
101 }
102 if p.TotalItems != 0 {
103 t.Errorf("Unmarshaled object should have empty TotalItems, received %d", p.TotalItems)
104 }
105 if len(p.Items) > 0 {
106 t.Errorf("Unmarshaled object should have empty Items, received %v", p.Items)
107 }
108 if p.URL != nil {
109 t.Errorf("Unmarshaled object should have empty URL, received %v", p.URL)
110 }
111 if !p.Published.IsZero() {
112 t.Errorf("Unmarshaled object should have empty Published, received %q", p.Published)
113 }
114 if !p.StartTime.IsZero() {
115 t.Errorf("Unmarshaled object should have empty StartTime, received %q", p.StartTime)
116 }
117 if !p.Updated.IsZero() {
118 t.Errorf("Unmarshaled object should have empty Updated, received %q", p.Updated)
119 }
120 if p.PartOf != nil {
121 t.Errorf("Unmarshaled object should have empty PartOf, received %q", p.PartOf)
122 }
123 if p.Current != nil {
124 t.Errorf("Unmarshaled object should have empty Current, received %q", p.Current)
125 }
126 if p.First != nil {
127 t.Errorf("Unmarshaled object should have empty First, received %q", p.First)
128 }
129 if p.Last != nil {
130 t.Errorf("Unmarshaled object should have empty Last, received %q", p.Last)
131 }
132 if p.Next != nil {
133 t.Errorf("Unmarshaled object should have empty Next, received %q", p.Next)
134 }
135 if p.Prev != nil {
136 t.Errorf("Unmarshaled object should have empty Prev, received %q", p.Prev)
137 }
138 }
139
140 func TestCollectionPage_Collection(t *testing.T) {
141 id := ID("test")
142
143 c := CollectionNew(id)
144 p := CollectionPageNew(c)
145
146 if !reflect.DeepEqual(p.Collection(), p.Items) {
147 t.Errorf("Collection items should be equal %v %v", p.Collection(), p.Items)
148 }
149 }
150
151 func TestCollectionPage_Count(t *testing.T) {
152 id := ID("test")
153
154 c := CollectionNew(id)
155 p := CollectionPageNew(c)
156
157 if p.TotalItems != 0 {
158 t.Errorf("Object should have empty TotalItems, received %d", p.TotalItems)
159 }
160 if len(p.Items) > 0 {
161 t.Errorf("Empty object should have empty Items, received %v", p.Items)
162 }
163 if p.Count() != uint(len(p.Items)) {
164 t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.Items))
165 }
166
167 _ = p.Append(IRI("test"))
168 if p.TotalItems != 1 {
169 t.Errorf("Empty object should have %d TotalItems, received %d", 1, p.TotalItems)
170 }
171 if p.Count() != uint(len(p.Items)) {
172 t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.Items))
173 }
174 }
175
176 func TestToCollectionPage(t *testing.T) {
177 tests := []struct {
178 name string
179 it LinkOrIRI
180 want *CollectionPage
181 wantErr error
182 }{
183 {
184 name: "empty",
185 },
186 {
187 name: "Valid CollectionPage",
188 it: CollectionPage{ID: "test", Type: CollectionPageType},
189 want: &CollectionPage{ID: "test", Type: CollectionPageType},
190 },
191 {
192 name: "Valid *CollectionPage",
193 it: &CollectionPage{ID: "test", Type: CollectionPageType},
194 want: &CollectionPage{ID: "test", Type: CollectionPageType},
195 },
196 {
197 name: "Valid Collection",
198 it: Collection{ID: "test", Type: CollectionType},
199 wantErr: ErrorInvalidType[CollectionPage](Collection{}),
200 },
201 {
202 name: "Valid *Collection",
203 it: &Collection{ID: "test", Type: CollectionType},
204 wantErr: ErrorInvalidType[CollectionPage](new(Collection)),
205 },
206 {
207 name: "Valid OrderedCollection",
208 it: OrderedCollection{ID: "test", Type: OrderedCollectionType},
209 wantErr: ErrorInvalidType[CollectionPage](OrderedCollection{}),
210 },
211 {
212 name: "Valid *OrderedCollection",
213 it: &OrderedCollection{ID: "test", Type: OrderedCollectionType},
214 wantErr: ErrorInvalidType[CollectionPage](new(OrderedCollection)),
215 },
216 {
217 name: "Valid OrderedCollectionPage",
218 it: OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType},
219 want: &CollectionPage{ID: "test", Type: OrderedCollectionPageType},
220 },
221 {
222 name: "Valid *OrderedCollectionPage",
223 it: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType},
224 want: &CollectionPage{ID: "test", Type: OrderedCollectionPageType},
225 },
226 {
227 name: "IRI",
228 it: IRI("https://example.com"),
229 wantErr: ErrorInvalidType[CollectionPage](IRI("")),
230 },
231 {
232 name: "IRIs",
233 it: IRIs{IRI("https://example.com")},
234 wantErr: ErrorInvalidType[CollectionPage](IRIs{}),
235 },
236 {
237 name: "ItemCollection",
238 it: ItemCollection{},
239 wantErr: ErrorInvalidType[CollectionPage](ItemCollection{}),
240 },
241 {
242 name: "Object",
243 it: &Object{ID: "test", Type: ArticleType},
244 wantErr: ErrorInvalidType[CollectionPage](&Object{}),
245 },
246 {
247 name: "Activity",
248 it: &Activity{ID: "test", Type: CreateType},
249 wantErr: ErrorInvalidType[CollectionPage](&Activity{}),
250 },
251 {
252 name: "IntransitiveActivity",
253 it: &IntransitiveActivity{ID: "test", Type: ArriveType},
254 wantErr: ErrorInvalidType[CollectionPage](&IntransitiveActivity{}),
255 },
256 }
257 for _, tt := range tests {
258 t.Run(tt.name, func(t *testing.T) {
259 got, err := ToCollectionPage(tt.it)
260 if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) {
261 t.Errorf("ToCollectionPage() error = %v, wantErr %v", err, tt.wantErr)
262 return
263 }
264 if !cmp.Equal(got, tt.want) {
265 t.Errorf("ToCollectionPage() got = %s", cmp.Diff(tt.want, got))
266 }
267 })
268 }
269 }
270
271 func TestCollectionPage_Contains(t *testing.T) {
272 t.Skipf("TODO")
273 }
274
275 func TestCollectionPage_GetID(t *testing.T) {
276 t.Skipf("TODO")
277 }
278
279 func TestCollectionPage_GetLink(t *testing.T) {
280 t.Skipf("TODO")
281 }
282
283 func TestCollectionPage_GetType(t *testing.T) {
284 t.Skipf("TODO")
285 }
286
287 func TestCollectionPage_IsCollection(t *testing.T) {
288 t.Skipf("TODO")
289 }
290
291 func TestCollectionPage_IsLink(t *testing.T) {
292 t.Skipf("TODO")
293 }
294
295 func TestCollectionPage_IsObject(t *testing.T) {
296 t.Skipf("TODO")
297 }
298
299 func TestCollectionPage_MarshalJSON(t *testing.T) {
300 t.Skipf("TODO")
301 }
302
303 func TestCollectionPage_ItemMatches(t *testing.T) {
304 t.Skipf("TODO")
305 }
306
307 func TestCollectionPage_Remove(t *testing.T) {
308 tests := []struct {
309 name string
310 col CollectionPage
311 toRemove ItemCollection
312 remaining ItemCollection
313 }{
314 {
315 name: "empty",
316 col: mockCollectionPage(),
317 },
318 {
319 name: "remove one item from empty collection",
320 col: mockCollectionPage(),
321 toRemove: ItemCollection{Object{ID: ID("grrr")}},
322 },
323 {
324 name: "remove all from collection",
325 col: mockCollectionPage(
326 Object{ID: ID("grrr")},
327 Activity{ID: ID("one")},
328 Actor{ID: ID("jdoe")},
329 ),
330 toRemove: ItemCollection{
331 Object{ID: ID("grrr")},
332 Activity{ID: ID("one")},
333 Actor{ID: ID("jdoe")},
334 },
335 remaining: ItemCollection{},
336 },
337 {
338 name: "empty_collection_non_nil_item",
339 col: mockCollectionPage(),
340 toRemove: ItemCollection{&Object{}},
341 remaining: ItemCollection{},
342 },
343 {
344 name: "non_empty_collection_nil_item",
345 col: mockCollectionPage(&Object{ID: "test"}),
346 toRemove: nil,
347 remaining: ItemCollection{&Object{ID: "test"}},
348 },
349 {
350 name: "non_empty_collection_non_contained_item_empty_ID",
351 col: mockCollectionPage(&Object{ID: "test"}),
352 toRemove: ItemCollection{&Object{}},
353 remaining: ItemCollection{
354 &Object{ID: "test"},
355 },
356 },
357 {
358 name: "non_empty_collection_non_contained_item",
359 col: mockCollectionPage(&Object{ID: "test"}),
360 toRemove: ItemCollection{&Object{ID: "test123"}},
361 remaining: ItemCollection{
362 &Object{ID: "test"},
363 },
364 },
365 {
366 name: "non_empty_collection_just_contained_item",
367 col: mockCollectionPage(&Object{ID: "test"}),
368 toRemove: ItemCollection{&Object{ID: "test"}},
369 remaining: ItemCollection{},
370 },
371 {
372 name: "non_empty_collection_contained_item_first_pos",
373 col: mockCollectionPage(
374 &Object{ID: "test"},
375 &Object{ID: "test123"},
376 ),
377 toRemove: ItemCollection{&Object{ID: "test"}},
378 remaining: ItemCollection{
379 &Object{ID: "test123"},
380 },
381 },
382 {
383 name: "non_empty_collection_contained_item_not_first_pos",
384 col: mockCollectionPage(
385 &Object{ID: "test123"},
386 &Object{ID: "test"},
387 &Object{ID: "test321"},
388 ),
389 toRemove: ItemCollection{&Object{ID: "test"}},
390 remaining: ItemCollection{
391 &Object{ID: "test123"},
392 &Object{ID: "test321"},
393 },
394 },
395 {
396 name: "non_empty_collection_contained_item_last_pos",
397 col: mockCollectionPage(
398 &Object{ID: "test123"},
399 &Object{ID: "test"},
400 ),
401 toRemove: ItemCollection{&Object{ID: "test"}},
402 remaining: ItemCollection{
403 &Object{ID: "test123"},
404 },
405 },
406 }
407 for _, tt := range tests {
408 t.Run(tt.name, func(t *testing.T) {
409 tt.col.Remove(tt.toRemove...)
410
411 if tt.col.TotalItems != uint(len(tt.remaining)) {
412 t.Errorf("Post Remove() %T TotalItems %d different than expected %d", tt.col, tt.col.TotalItems, len(tt.remaining))
413 }
414 for _, it := range tt.remaining {
415 if !tt.col.Items.Contains(it) {
416 t.Errorf("Post Remove() unable to find %s in %T Items %v", it.GetLink(), tt.col, tt.col.Items)
417 }
418 }
419 for _, it := range tt.toRemove {
420 if tt.col.Items.Contains(it) {
421 t.Errorf("Post Remove() was still able to find %s in %T Items %v", it.GetLink(), tt.col, tt.col.Items)
422 }
423 }
424 })
425 }
426 }