datasource_test.gno
4.01 Kb ยท 164 lines
1package hor
2
3import (
4 "testing"
5
6 "gno.land/p/demo/avl"
7 "gno.land/p/demo/uassert"
8 "gno.land/p/demo/urequire"
9 "gno.land/p/jeronimoalbi/datasource"
10 "gno.land/p/moul/addrset"
11)
12
13var (
14 _ datasource.Datasource = (*Datasource)(nil)
15 _ datasource.Record = (*record)(nil)
16 _ datasource.ContentRecord = (*record)(nil)
17 _ datasource.Iterator = (*iterator)(nil)
18)
19
20func TestDatasourceRecords(t *testing.T) {
21 cases := []struct {
22 name string
23 items []*Item
24 recordIDs []string
25 options []datasource.QueryOption
26 }{
27 {
28 name: "all items",
29 items: []*Item{{id: 1}, {id: 2}, {id: 3}},
30 recordIDs: []string{"0000001", "0000002", "0000003"},
31 },
32 {
33 name: "with offset",
34 items: []*Item{{id: 1}, {id: 2}, {id: 3}},
35 recordIDs: []string{"0000002", "0000003"},
36 options: []datasource.QueryOption{datasource.WithOffset(1)},
37 },
38 {
39 name: "with count",
40 items: []*Item{{id: 1}, {id: 2}, {id: 3}},
41 recordIDs: []string{"0000001", "0000002"},
42 options: []datasource.QueryOption{datasource.WithCount(2)},
43 },
44 {
45 name: "with offset and count",
46 items: []*Item{{id: 1}, {id: 2}, {id: 3}},
47 recordIDs: []string{"0000002"},
48 options: []datasource.QueryOption{
49 datasource.WithOffset(1),
50 datasource.WithCount(1),
51 },
52 },
53 }
54
55 for _, tc := range cases {
56 t.Run(tc.name, func(t *testing.T) {
57 // Initialize a local instance of exhibition
58 exhibition := &Exhibition{items: avl.NewTree()}
59 for _, item := range tc.items {
60 exhibition.items.Set(item.id.String(), item)
61 }
62
63 // Get a records iterator
64 ds := Datasource{exhibition}
65 query := datasource.NewQuery(tc.options...)
66 iter := ds.Records(query)
67
68 // Start asserting
69 urequire.Equal(t, len(tc.items), ds.Size(), "datasource size")
70
71 var records []datasource.Record
72 for iter.Next() {
73 records = append(records, iter.Record())
74 }
75 urequire.Equal(t, len(tc.recordIDs), len(records), "record count")
76
77 for i, r := range records {
78 uassert.Equal(t, tc.recordIDs[i], r.ID())
79 }
80 })
81 }
82}
83
84func TestDatasourceRecord(t *testing.T) {
85 cases := []struct {
86 name string
87 items []*Item
88 id string
89 err string
90 }{
91 {
92 name: "found",
93 items: []*Item{{id: 1}, {id: 2}, {id: 3}},
94 id: "0000001",
95 },
96 {
97 name: "no found",
98 items: []*Item{{id: 1}, {id: 2}, {id: 3}},
99 id: "42",
100 err: "realm submission not found",
101 },
102 }
103
104 for _, tc := range cases {
105 t.Run(tc.name, func(t *testing.T) {
106 // Initialize a local instance of exhibition
107 exhibition := &Exhibition{items: avl.NewTree()}
108 for _, item := range tc.items {
109 exhibition.items.Set(item.id.String(), item)
110 }
111
112 // Get a single record
113 ds := Datasource{exhibition}
114 r, err := ds.Record(tc.id)
115
116 // Start asserting
117 if tc.err != "" {
118 uassert.ErrorContains(t, err, tc.err)
119 return
120 }
121
122 urequire.NoError(t, err, "no error")
123 urequire.NotEqual(t, nil, r, "record not nil")
124 uassert.Equal(t, tc.id, r.ID())
125 })
126 }
127}
128
129func TestItemRecord(t *testing.T) {
130 pkgpath := "gno.land/r/demo/test"
131 item := Item{
132 id: 1,
133 pkgpath: pkgpath,
134 title: "Test Realm",
135 description: "This is a test realm in the Hall of Fame",
136 blockNum: 42,
137 upvote: &addrset.Set{},
138 downvote: &addrset.Set{},
139 }
140 item.downvote.Add("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
141 item.upvote.Add("g1w4ek2u33ta047h6lta047h6lta047h6ldvdwpn")
142 item.upvote.Add("g1w4ek2u3jta047h6lta047h6lta047h6l9huexc")
143
144 r := record{&item}
145
146 uassert.Equal(t, "0000001", r.ID())
147 uassert.Equal(t, pkgpath, r.String())
148
149 fields, _ := r.Fields()
150 details, found := fields.Get("details")
151 urequire.True(t, found, "details field")
152 uassert.Equal(t, "Votes: โถ 2 - โท 1", details)
153
154 content, _ := r.Content()
155 wantContent := `### [Test Realm](/r/demo/test)
156This is a test realm in the Hall of Fame
157
158by demo
159
160Submitted at Block #42
161
162**[2๐](/r/leon/hor$help&func=Upvote&pkgpath=gno.land%2Fr%2Fdemo%2Ftest) - [1๐](/r/leon/hor$help&func=Downvote&pkgpath=gno.land%2Fr%2Fdemo%2Ftest)**`
163 uassert.Equal(t, wantContent, content)
164}