~whereswaldon/forest-go

972f2afc043771da05b4e13bd7386f39f7f5e125 — Chris Waldon 2 months ago cd38454
fix: ensure archive DescendantsOf does not return the root

Previously DescendantsOf returned the requested node's id as
one of its own descendants. This was not intended.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2 files changed, 35 insertions(+), 2 deletions(-)

M store/archive.go
M store/store_test.go
M store/archive.go => store/archive.go +4 -2
@@ 248,8 248,10 @@ func (a *Archive) AncestryOf(id *fields.QualifiedHash) ([]*fields.QualifiedHash,
func (a *Archive) DescendantsOf(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error) {
	descendants := make([]*fields.QualifiedHash, 0)

	err := Walk(a, id, func(id *fields.QualifiedHash) error {
		descendants = append(descendants, id)
	err := Walk(a, id, func(currentID *fields.QualifiedHash) error {
		if !currentID.Equals(id) {
			descendants = append(descendants, currentID)
		}
		return nil
	})
	if err != nil {

M store/store_test.go => store/store_test.go +31 -0
@@ 228,3 228,34 @@ func TestCacheStoreUpPropagation(t *testing.T) {
		}
	}
}

func TestArchiveDescendantsOf(t *testing.T) {
	// create three test nodes, one of each type
	identity, _, community, reply := testutil.MakeReplyOrSkip(t)
	nodes := []forest.Node{identity, community, reply}

	m := store.NewMemoryStore()
	for _, n := range nodes {
		m.Add(n)
	}

	a := store.NewArchive(m)

	try := func(t *testing.T, id *fields.QualifiedHash, expectedLen int) {
		descendants, err := a.DescendantsOf(id)
		if err != nil {
			t.Fatalf("should not fail to return descendants: %v", err)
		}
		for _, d := range descendants {
			if d.Equals(id) {
				t.Fatalf("%s should not be returned as its own descendant", id)
			}
		}
		if len(descendants) != expectedLen {
			t.Fatalf("expected %d descendants, got %d", expectedLen, len(descendants))
		}
	}
	try(t, reply.ID(), 0)
	try(t, community.ID(), 1)
	try(t, identity.ID(), 0)
}