datasource_test.gno

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