Skip to content

Commit

Permalink
Revert encode round-trip to leave unencoded node test intact
Browse files Browse the repository at this point in the history
As per review, remove the special re-encode and codec for test fixtures so
we have test coverage of the case where we're traversing a node that's not been
through a codec that messes with field ordering.
  • Loading branch information
rvagg committed Sep 30, 2021
1 parent 1ba5ccf commit 47336e2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
41 changes: 13 additions & 28 deletions traversal/focus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package traversal_test

import (
"fmt"
"io"
"testing"

. "github.com/warpfork/go-wish"

"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/codec"
"github.com/ipld/go-ipld-prime/codec/dagjson"
_ "github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/fluent"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/multicodec"
"github.com/ipld/go-ipld-prime/must"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/storage"
Expand All @@ -27,11 +23,11 @@ import (

var store = storage.Memory{}
var (
// bagaibqabcmclve3gs4
// baguqeeyexkjwnfy
leafAlpha, leafAlphaLnk = encode(basicnode.NewString("alpha"))
// bagaibqabcmcikrpz5q
// baguqeeyeqvc7t3a
leafBeta, leafBetaLnk = encode(basicnode.NewString("beta"))
// bagaibqabcmcaymeikm
// baguqeeyezhlahvq
middleMapNode, middleMapNodeLnk = encode(fluent.MustBuildMap(basicnode.Prototype.Map, 3, func(na fluent.MapAssembler) {
na.AssembleEntry("foo").AssignBool(true)
na.AssembleEntry("bar").AssignBool(false)
Expand All @@ -40,14 +36,19 @@ var (
na.AssembleEntry("nonlink").AssignString("zoo")
})
}))
// bagaibqabcmcdgcuooe
// baguqeeyehfkkfwa
middleListNode, middleListNodeLnk = encode(fluent.MustBuildList(basicnode.Prototype.List, 4, func(na fluent.ListAssembler) {
na.AssembleValue().AssignLink(leafAlphaLnk)
na.AssembleValue().AssignLink(leafAlphaLnk)
na.AssembleValue().AssignLink(leafBetaLnk)
na.AssembleValue().AssignLink(leafAlphaLnk)
}))
// bagaibqabcmcbiztlhi
// note that using `rootNode` directly will have a different field ordering than
// the encoded form if you were to load `rootNodeLnk` due to dag-json field
// reordering on encode, beware the difference for traversal order between
// created, in-memory nodes and those that have passed through a codec with
// field ordering rules
// baguqeeyeie4ajfy
rootNode, rootNodeLnk = encode(fluent.MustBuildMap(basicnode.Prototype.Map, 4, func(na fluent.MapAssembler) {
na.AssembleEntry("plain").AssignString("olde string")
na.AssembleEntry("linkedString").AssignLink(leafAlphaLnk)
Expand All @@ -56,40 +57,24 @@ var (
}))
)

func UnorderedDagJsonEncode(n datamodel.Node, w io.Writer) error {
return dagjson.EncodeOptions{
EncodeLinks: true,
EncodeBytes: true,
MapSortMode: codec.MapSortMode_None,
}.Encode(n, w)
}

// encode hardcodes some encoding choices for ease of use in fixture generation;
// just gimme a link and stuff the bytes in a map.
// (also return the node again for convenient assignment.)
func encode(n datamodel.Node) (datamodel.Node, datamodel.Link) {
multicodec.RegisterEncoder(0x300000, UnorderedDagJsonEncode)
multicodec.RegisterDecoder(0x300000, dagjson.Decode)

lp := cidlink.LinkPrototype{Prefix: cid.Prefix{
Version: 1,
Codec: 0x300000,
Codec: 0x0129,
MhType: 0x13,
MhLength: 4,
}}
lsys := cidlink.DefaultLinkSystem()
lsys.StorageWriteOpener = (&store).OpenWrite
lsys.StorageReadOpener = (&store).OpenRead

lnk, err := lsys.Store(linking.LinkContext{}, lp, n)
if err != nil {
panic(err)
}
loaded, err := lsys.Load(linking.LinkContext{}, lnk, basicnode.Prototype.Any)
if err != nil {
panic(err)
}
return loaded, lnk
return n, lnk
}

// covers Focus used on one already-loaded Node; no link-loading exercised.
Expand Down Expand Up @@ -344,7 +329,7 @@ func TestFocusedTransformWithLinks(t *testing.T) {
Wish(t, progress.Path.String(), ShouldEqual, "linkedMap/nested/nonlink")
Wish(t, must.String(prev), ShouldEqual, "zoo")
Wish(t, progress.LastBlock.Path.String(), ShouldEqual, "linkedMap")
Wish(t, progress.LastBlock.Link.String(), ShouldEqual, "bagaibqabcmcaymeikm")
Wish(t, progress.LastBlock.Link.String(), ShouldEqual, "baguqeeyezhlahvq")
nb := prev.Prototype().NewBuilder()
nb.AssignString("new string!")
return nb.Build(), nil
Expand Down
30 changes: 20 additions & 10 deletions traversal/walk_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package traversal_test

import (
"fmt"
"io"
"log"
"strings"
Expand All @@ -23,8 +24,11 @@ import (

/* Remember, we've got the following fixtures in scope:
var (
leafAlpha, leafAlphaLnk = encode(basicnode.NewString("alpha"))
leafBeta, leafBetaLnk = encode(basicnode.NewString("beta"))
// baguqeeyexkjwnfy
leafAlpha, leafAlphaLnk = encode(basicnode.NewString("alpha"))
// baguqeeyeqvc7t3a
leafBeta, leafBetaLnk = encode(basicnode.NewString("beta"))
// baguqeeyezhlahvq
middleMapNode, middleMapNodeLnk = encode(fluent.MustBuildMap(basicnode.Prototype.Map, 3, func(na fluent.MapAssembler) {
na.AssembleEntry("foo").AssignBool(true)
na.AssembleEntry("bar").AssignBool(false)
Expand All @@ -33,19 +37,20 @@ var (
na.AssembleEntry("nonlink").AssignString("zoo")
})
}))
// baguqeeyehfkkfwa
middleListNode, middleListNodeLnk = encode(fluent.MustBuildList(basicnode.Prototype.List, 4, func(na fluent.ListAssembler) {
na.AssembleValue().AssignLink(leafAlphaLnk)
na.AssembleValue().AssignLink(leafAlphaLnk)
na.AssembleValue().AssignLink(leafBetaLnk)
na.AssembleValue().AssignLink(leafAlphaLnk)
}))
// baguqeeyeie4ajfy
rootNode, rootNodeLnk = encode(fluent.MustBuildMap(basicnode.Prototype.Map, 4, func(na fluent.MapAssembler) {
na.AssembleEntry("plain").AssignString("olde string")
na.AssembleEntry("linkedString").AssignLink(leafAlphaLnk)
na.AssembleEntry("linkedMap").AssignLink(middleMapNodeLnk)
na.AssembleEntry("linkedList").AssignLink(middleListNodeLnk)
}))
)
})))
*/

// covers traverse using a variety of selectors.
Expand Down Expand Up @@ -330,6 +335,8 @@ func TestWalkBudgets(t *testing.T) {

func TestWalkBlockLoadOrder(t *testing.T) {
// a more nested root that we can use to test SkipMe as well
// note that in using `rootNodeLnk` here rather than `rootNode` we're using the
// dag-json round-trip version which will have different field ordering
newRootNode, newRootLink := encode(fluent.MustBuildList(basicnode.Prototype.List, 6, func(na fluent.ListAssembler) {
na.AssembleValue().AssignLink(rootNodeLnk)
na.AssembleValue().AssignLink(middleListNodeLnk)
Expand All @@ -348,17 +355,20 @@ func TestWalkBlockLoadOrder(t *testing.T) {
linkNames[middleListNodeLnk] = "middleListNodeLnk"
linkNames[leafAlphaLnk] = "leafAlphaLnk"
linkNames[leafBetaLnk] = "leafBetaLnk"
for v, n := range linkNames {
fmt.Printf("n:%v:%v\n", n, v)
}
// the links that we expect from the root node, starting _at_ the root node itself
rootNodeExpectedLinks := []datamodel.Link{
rootNodeLnk,
leafAlphaLnk,
middleMapNodeLnk,
leafAlphaLnk,
middleListNodeLnk,
leafAlphaLnk,
leafAlphaLnk,
leafBetaLnk,
leafAlphaLnk,
middleMapNodeLnk,
leafAlphaLnk,
leafAlphaLnk,
}
// same thing but for middleListNode
middleListNodeLinks := []datamodel.Link{
Expand Down Expand Up @@ -423,14 +433,14 @@ func TestWalkBlockLoadOrder(t *testing.T) {
// the block is not traversed when the SkipMe is received
expectedSkipMeBlocks := []datamodel.Link{
rootNodeLnk,
leafAlphaLnk,
middleMapNodeLnk,
leafAlphaLnk,
middleListNodeLnk,
leafAlphaLnk,
leafAlphaLnk,
leafBetaLnk,
leafAlphaLnk,
middleMapNodeLnk,
leafAlphaLnk,
leafAlphaLnk,
middleListNodeLnk,
rootNodeLnk,
middleListNodeLnk,
Expand Down
18 changes: 9 additions & 9 deletions traversal/walk_with_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,34 +176,34 @@ func TestStopInChain(t *testing.T) {
"",
"plain",
"ch3",
"ch3/linkedString",
"ch3/ch2",
"ch3/ch2/linkedMap",
"ch3/ch2/linkedMap/foo",
"ch3/ch2/linkedMap/bar",
"ch3/ch2/linkedMap/foo",
"ch3/ch2/linkedMap/nested",
"ch3/ch2/linkedMap/nested/alink",
"ch3/ch2/linkedMap/nested/nonlink",
"ch3/linkedString",
})
// Get the full chain
stopAtInChainTest(t, chainNode, nil, []string{
"",
"plain",
"ch3",
"ch3/linkedString",
"ch3/ch2",
"ch3/ch2/linkedMap",
"ch3/ch2/linkedMap/foo",
"ch3/ch2/linkedMap/bar",
"ch3/ch2/linkedMap/nested",
"ch3/ch2/linkedMap/nested/alink",
"ch3/ch2/linkedMap/nested/nonlink",
"ch3/ch2/ch1",
"ch3/ch2/ch1/linkedList",
"ch3/ch2/ch1/linkedList/0",
"ch3/ch2/ch1/linkedList/1",
"ch3/ch2/ch1/linkedList/2",
"ch3/ch2/ch1/linkedList/3",
"ch3/ch2/linkedMap",
"ch3/ch2/linkedMap/bar",
"ch3/ch2/linkedMap/foo",
"ch3/ch2/linkedMap/nested",
"ch3/ch2/linkedMap/nested/alink",
"ch3/ch2/linkedMap/nested/nonlink",
"ch3/linkedString",
})
}

Expand Down

0 comments on commit 47336e2

Please sign in to comment.