Skip to content

Commit 2a95a72

Browse files
committed
PR FIXUP - Add LensID test substitution
1 parent d0cfa3c commit 2a95a72

5 files changed

Lines changed: 68 additions & 4 deletions

File tree

tests/action/add.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var _ Action = (*Add)(nil)
1919
var _ Stateful = (*Add)(nil)
2020

2121
func (a *Add) Execute() {
22-
_, err := a.s.Store.Add(a.s.Ctx, a.Config)
22+
id, err := a.s.Store.Add(a.s.Ctx, a.Config)
2323
require.NoError(a.s.T, err)
24+
25+
a.s.LensIDs = append(a.s.LensIDs, id)
2426
}

tests/action/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ var _ Action = (*List)(nil)
2121
var _ Stateful = (*List)(nil)
2222

2323
func (a *List) Execute() {
24+
for key, model := range a.Expected {
25+
delete(a.Expected, key)
26+
a.Expected[replace(a.s, key)] = model
27+
}
28+
2429
result, err := a.s.Store.List(a.s.Ctx)
2530
require.NoError(a.s.T, err)
2631

tests/action/util_replace.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
package action
6+
7+
import (
8+
"bytes"
9+
"maps"
10+
"strconv"
11+
"strings"
12+
"text/template"
13+
14+
"github.com/stretchr/testify/require"
15+
16+
"github.com/sourcenetwork/lens/tests/state"
17+
)
18+
19+
// templateDataGenerators contains a set of data generators by their template prefix.
20+
//
21+
// Supporting action properties will replace any templated elements with data drawn from these
22+
// sets.
23+
var templateDataGenerators = map[string]func(*state.State) map[string]string{
24+
"LensIDs": func(s *state.State) map[string]string {
25+
res := map[string]string{}
26+
for i, ID := range s.LensIDs {
27+
res["LensIDs"+strconv.Itoa(i)] = ID.String()
28+
}
29+
30+
return res
31+
},
32+
}
33+
34+
// replace returns a new string with any templating placholders (see "text/template") with data drawn
35+
// from `state`.
36+
func replace(s *state.State, input string) string {
37+
if !strings.Contains(input, "{{") {
38+
// If the input doesn't contain any templating elements we can return early
39+
return input
40+
}
41+
42+
templateData := map[string]string{}
43+
for _, datasetGenerator := range templateDataGenerators {
44+
// Having to regenerate the full dataset for every node-action is horribly inefficient, but
45+
// it is tolerable for now.
46+
maps.Copy(templateData, datasetGenerator(s))
47+
}
48+
49+
tmpl := template.Must(template.New("").Parse(input))
50+
var buf bytes.Buffer
51+
err := tmpl.Execute(&buf, templateData)
52+
require.NoError(s.T, err)
53+
54+
return buf.String()
55+
}

tests/integration/node/add_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAddList(t *testing.T) {
3636
},
3737
&action.List{
3838
Expected: map[string]model.Lens{
39-
"bafyreibtjagnf2pgjgteonxcwigifwfylqgbrrn7f5zcx6tsqi7v3evgqq": {
39+
"{{.LensIDs0}}": {
4040
Lenses: []model.LensModule{
4141
{
4242
Path: "data://" + string(wasm),

tests/state/state.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"testing"
1010

11+
"github.com/ipfs/go-cid"
1112
"github.com/sourcenetwork/lens/host-go/node"
1213
"github.com/sourcenetwork/lens/host-go/store"
1314
)
@@ -19,6 +20,7 @@ type State struct {
1920
Node *node.Node
2021
Path string
2122

22-
Store store.Store
23-
Txns []store.Txn
23+
Store store.Store
24+
Txns []store.Txn
25+
LensIDs []cid.Cid
2426
}

0 commit comments

Comments
 (0)