query_test.gno
1.98 Kb ยท 104 lines
1package datasource
2
3import (
4 "fmt"
5 "testing"
6
7 "gno.land/p/demo/uassert"
8)
9
10func TestNewQuery(t *testing.T) {
11 cases := []struct {
12 name string
13 options []QueryOption
14 setup func() Query
15 }{
16 {
17 name: "default",
18 setup: func() Query {
19 return Query{Count: DefaultQueryRecords}
20 },
21 },
22 {
23 name: "with offset",
24 options: []QueryOption{WithOffset(100)},
25 setup: func() Query {
26 return Query{
27 Offset: 100,
28 Count: DefaultQueryRecords,
29 }
30 },
31 },
32 {
33 name: "with count",
34 options: []QueryOption{WithCount(10)},
35 setup: func() Query {
36 return Query{Count: 10}
37 },
38 },
39 {
40 name: "with invalid count",
41 options: []QueryOption{WithCount(0)},
42 setup: func() Query {
43 return Query{Count: DefaultQueryRecords}
44 },
45 },
46 {
47 name: "by tag",
48 options: []QueryOption{ByTag("foo")},
49 setup: func() Query {
50 return Query{
51 Tag: "foo",
52 Count: DefaultQueryRecords,
53 }
54 },
55 },
56 {
57 name: "with filter",
58 options: []QueryOption{WithFilter("foo", 42)},
59 setup: func() Query {
60 q := Query{Count: DefaultQueryRecords}
61 q.Filters.Set("foo", 42)
62 return q
63 },
64 },
65 {
66 name: "with multiple filters",
67 options: []QueryOption{
68 WithFilter("foo", 42),
69 WithFilter("bar", "baz"),
70 },
71 setup: func() Query {
72 q := Query{Count: DefaultQueryRecords}
73 q.Filters.Set("foo", 42)
74 q.Filters.Set("bar", "baz")
75 return q
76 },
77 },
78 }
79
80 for _, tc := range cases {
81 t.Run(tc.name, func(t *testing.T) {
82 // Arrange
83 want := tc.setup()
84
85 // Act
86 q := NewQuery(tc.options...)
87
88 // Assert
89 uassert.Equal(t, want.Offset, q.Offset)
90 uassert.Equal(t, want.Count, q.Count)
91 uassert.Equal(t, want.Tag, q.Tag)
92 uassert.Equal(t, want.Filters.Size(), q.Filters.Size())
93
94 want.Filters.Iterate("", "", func(k string, v any) bool {
95 got, exists := q.Filters.Get(k)
96 uassert.True(t, exists)
97 if exists {
98 uassert.Equal(t, fmt.Sprint(v), fmt.Sprint(got))
99 }
100 return false
101 })
102 })
103 }
104}