From 6d87ec04ebe94d4c105b78fe64471ebdb2a26b70 Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Wed, 26 Aug 2020 13:59:39 -0400 Subject: [PATCH] test: add test that ResolveToLastNode does not perform unncessary fetches --- resolver/resolver_test.go | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/resolver/resolver_test.go b/resolver/resolver_test.go index 480ccdf..d3c6913 100644 --- a/resolver/resolver_test.go +++ b/resolver/resolver_test.go @@ -105,3 +105,43 @@ func TestRecurivePathResolution(t *testing.T) { p.String(), rCid.String(), cKey.String())) } } + +func TestResolveToLastNode_NoUnnecessaryFetching(t *testing.T) { + ctx := context.Background() + dagService := dagmock.Mock() + + a := randNode() + b := randNode() + + err := a.AddNodeLink("child", b) + if err != nil { + t.Fatal(err) + } + + err = dagService.Add(ctx, a) + if err != nil { + t.Fatal(err) + } + + aKey := a.Cid() + + segments := []string{aKey.String(), "child"} + p, err := path.FromSegments("/ipfs/", segments...) + if err != nil { + t.Fatal(err) + } + + resolver := resolver.NewBasicResolver(dagService) + resolvedCID, remainingPath, err := resolver.ResolveToLastNode(ctx, p) + if err != nil { + t.Fatal(err) + } + + if len(remainingPath) > 0 { + t.Fatal("cannot have remaining path") + } + + if !resolvedCID.Equals(b.Cid()) { + t.Fatal("resolved to the wrong CID") + } +}