Search Apps Documentation Source Content File Folder Download Copy Actions Download

datasource_test.gno

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