diffenum_test.go 4.6 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5 6 7 8 9 10
package dagutils

import (
	"context"
	"fmt"
	"testing"

	dag "github.com/ipfs/go-ipfs/merkledag"
	mdtest "github.com/ipfs/go-ipfs/merkledag/test"

11 12
	ipld "gx/ipfs/QmWi2BYBL5gJ3CiAiQchg6rn1A8iBsrWy51EYxvHVjFvLb/go-ipld-format"
	cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
Jeromy's avatar
Jeromy committed
13 14
)

15
func buildNode(name string, desc map[string]ndesc, out map[string]ipld.Node) ipld.Node {
Jeromy's avatar
Jeromy committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	this := desc[name]
	nd := new(dag.ProtoNode)
	nd.SetData([]byte(name))
	for k, v := range this {
		child, ok := out[v]
		if !ok {
			child = buildNode(v, desc, out)
			out[v] = child
		}

		if err := nd.AddNodeLink(k, child); err != nil {
			panic(err)
		}
	}

	return nd
}

type ndesc map[string]string

36 37
func mkGraph(desc map[string]ndesc) map[string]ipld.Node {
	out := make(map[string]ipld.Node)
Jeromy's avatar
Jeromy committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	for name := range desc {
		if _, ok := out[name]; ok {
			continue
		}

		out[name] = buildNode(name, desc, out)
	}
	return out
}

var tg1 = map[string]ndesc{
	"a1": ndesc{
		"foo": "b",
	},
	"b": ndesc{},
	"a2": ndesc{
		"foo": "b",
		"bar": "c",
	},
	"c": ndesc{},
}

var tg2 = map[string]ndesc{
	"a1": ndesc{
		"foo": "b",
	},
	"b": ndesc{},
	"a2": ndesc{
		"foo": "b",
		"bar": "c",
	},
	"c": ndesc{"baz": "d"},
	"d": ndesc{},
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86
var tg3 = map[string]ndesc{
	"a1": ndesc{
		"foo": "b",
		"bar": "c",
	},
	"b": ndesc{},
	"a2": ndesc{
		"foo": "b",
		"bar": "d",
	},
	"c": ndesc{},
	"d": ndesc{},
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
var tg4 = map[string]ndesc{
	"a1": ndesc{
		"key1": "b",
		"key2": "c",
	},
	"a2": ndesc{
		"key1": "b",
		"key2": "d",
	},
}

var tg5 = map[string]ndesc{
	"a1": ndesc{
		"key1": "a",
		"key2": "b",
	},
	"a2": ndesc{
		"key1": "c",
		"key2": "d",
	},
}

func TestNameMatching(t *testing.T) {
	nds := mkGraph(tg4)

	diff := getLinkDiff(nds["a1"], nds["a2"])
	if len(diff) != 1 {
		t.Fatal(fmt.Errorf("node diff didn't match by name"))
	}
}

func TestNameMatching2(t *testing.T) {
	nds := mkGraph(tg5)

	diff := getLinkDiff(nds["a1"], nds["a2"])
	if len(diff) != 2 {
		t.Fatal(fmt.Errorf("incorrect number of link diff elements"))
	}
	if !(diff[0].bef.Equals(nds["a1"].Links()[0].Cid) && diff[0].aft.Equals(nds["a2"].Links()[0].Cid)) {
		t.Fatal(fmt.Errorf("node diff didn't match by name"))
	}
}

Jeromy's avatar
Jeromy committed
130 131 132 133 134 135 136 137 138
func TestDiffEnumBasic(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	nds := mkGraph(tg1)

	ds := mdtest.Mock()
	lgds := &getLogger{ds: ds}

	for _, nd := range nds {
139
		err := ds.Add(ctx, nd)
Jeromy's avatar
Jeromy committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		if err != nil {
			t.Fatal(err)
		}
	}

	err := DiffEnumerate(ctx, lgds, nds["a1"].Cid(), nds["a2"].Cid())
	if err != nil {
		t.Fatal(err)
	}

	err = assertCidList(lgds.log, []*cid.Cid{nds["a1"].Cid(), nds["a2"].Cid(), nds["c"].Cid()})
	if err != nil {
		t.Fatal(err)
	}
}

type getLogger struct {
157
	ds  ipld.NodeGetter
Jeromy's avatar
Jeromy committed
158 159 160
	log []*cid.Cid
}

161
func (gl *getLogger) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) {
Jeromy's avatar
Jeromy committed
162 163 164 165 166 167 168 169
	nd, err := gl.ds.Get(ctx, c)
	if err != nil {
		return nil, err
	}
	gl.log = append(gl.log, c)
	return nd, nil
}

170 171
func (gl *getLogger) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *ipld.NodeOption {
	outCh := make(chan *ipld.NodeOption, len(cids))
172 173 174 175 176 177 178 179 180 181 182 183 184 185
	nds := gl.ds.GetMany(ctx, cids)
	for no := range nds {
		if no.Err == nil {
			gl.log = append(gl.log, no.Node.Cid())
		}
		select {
		case outCh <- no:
		default:
			panic("too many responses")
		}
	}
	return nds
}

Jeromy's avatar
Jeromy committed
186 187 188 189 190 191 192 193 194 195 196
func assertCidList(a, b []*cid.Cid) error {
	if len(a) != len(b) {
		return fmt.Errorf("got different number of cids than expected")
	}
	for i, c := range a {
		if !c.Equals(b[i]) {
			return fmt.Errorf("expected %s, got %s", c, b[i])
		}
	}
	return nil
}
197

Jeromy's avatar
Jeromy committed
198 199 200 201 202 203 204 205 206
func TestDiffEnumFail(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	nds := mkGraph(tg2)

	ds := mdtest.Mock()
	lgds := &getLogger{ds: ds}

	for _, s := range []string{"a1", "a2", "b", "c"} {
207
		err := ds.Add(ctx, nds[s])
Jeromy's avatar
Jeromy committed
208 209 210 211 212 213
		if err != nil {
			t.Fatal(err)
		}
	}

	err := DiffEnumerate(ctx, lgds, nds["a1"].Cid(), nds["a2"].Cid())
214
	if err != ipld.ErrNotFound {
Jeromy's avatar
Jeromy committed
215 216 217 218 219 220 221 222 223
		t.Fatal("expected err not found")
	}

	err = assertCidList(lgds.log, []*cid.Cid{nds["a1"].Cid(), nds["a2"].Cid(), nds["c"].Cid()})
	if err != nil {
		t.Fatal(err)
	}

}
224 225 226 227 228 229 230 231 232 233

func TestDiffEnumRecurse(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	nds := mkGraph(tg3)

	ds := mdtest.Mock()
	lgds := &getLogger{ds: ds}

	for _, s := range []string{"a1", "a2", "b", "c", "d"} {
234
		err := ds.Add(ctx, nds[s])
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		if err != nil {
			t.Fatal(err)
		}
	}

	err := DiffEnumerate(ctx, lgds, nds["a1"].Cid(), nds["a2"].Cid())
	if err != nil {
		t.Fatal(err)
	}

	err = assertCidList(lgds.log, []*cid.Cid{nds["a1"].Cid(), nds["a2"].Cid(), nds["c"].Cid(), nds["d"].Cid()})
	if err != nil {
		t.Fatal(err)
	}
}