@cryptotaxi247 / kubo / commits / ee6472904

Feat: remove circular dependencies in merkledag package tests

This avoids using unixfs package and importer packages in merkledag, which removes circular depedencies making it hard to extract this module. License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>

Hector Sanjuan committed Feb 15, 2018 at 00:38 UTC ee6472904e4d00d4992c2434369d37735caa5fbd
1 file changed +64 -28
merkledag/merkledag_test.go
+64 -28
@@ -16,14 +16,11 @@ import (
16 bserv "github.com/ipfs/go-ipfs/blockservice"
17 bstest "github.com/ipfs/go-ipfs/blockservice/test"
18 offline "github.com/ipfs/go-ipfs/exchange/offline"
19 - imp "github.com/ipfs/go-ipfs/importer"
19 . "github.com/ipfs/go-ipfs/merkledag"
20 mdpb "github.com/ipfs/go-ipfs/merkledag/pb"
21 dstest "github.com/ipfs/go-ipfs/merkledag/test"
23 - uio "github.com/ipfs/go-ipfs/unixfs/io"
22
23 u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
26 - chunker "gx/ipfs/QmWo8jYc19ppG7YoTsrr2kEtLRbARTJho5oNXFTR6B7Peq/go-ipfs-chunker"
24 cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
25 ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
26 blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
@@ -129,6 +126,61 @@ func TestBatchFetchDupBlock(t *testing.T) {
126 runBatchFetchTest(t, read)
127 }
128
129 +// makeTestDAG creates a simple DAG from the data in a reader.
130 +// First, a node is created from each 512 bytes of data from the reader
131 +// (like a the Size chunker would do). Then all nodes are added as children
132 +// to a root node, which is returned.
133 +func makeTestDAG(t *testing.T, read io.Reader, ds ipld.DAGService) ipld.Node {
134 + p := make([]byte, 512)
135 + nodes := []*ProtoNode{}
136 + var err error = nil
137 + _, err = read.Read(p)
138 + for err == nil {
139 + protoNode := NodeWithData(p)
140 + nodes = append(nodes, protoNode)
141 + _, err = read.Read(p)
142 + }
143 +
144 + if err != io.EOF {
145 + t.Fatal(err)
146 + }
147 +
148 + ctx := context.Background()
149 + // Add a root referencing all created nodes
150 + root := NodeWithData(nil)
151 + for _, n := range nodes {
152 + root.AddNodeLink(n.Cid().String(), n)
153 + err := ds.Add(ctx, n)
154 + if err != nil {
155 + t.Fatal(err)
156 + }
157 + }
158 + err = ds.Add(ctx, root)
159 + if err != nil {
160 + t.Fatal(err)
161 + }
162 + return root
163 +}
164 +
165 +// makeTestDAGReader takes the root node as returned by makeTestDAG and
166 +// provides a reader that reads all the RawData from that node and its children.
167 +func makeTestDAGReader(t *testing.T, root ipld.Node, ds ipld.DAGService) io.Reader {
168 + ctx := context.Background()
169 + buf := new(bytes.Buffer)
170 + buf.Write(root.RawData())
171 + for _, l := range root.Links() {
172 + n, err := ds.Get(ctx, l.Cid)
173 + if err != nil {
174 + t.Fatal(err)
175 + }
176 + _, err = buf.Write(n.RawData())
177 + if err != nil {
178 + t.Fatal(err)
179 + }
180 + }
181 + return buf
182 +}
183 +
184 func runBatchFetchTest(t *testing.T, read io.Reader) {
185 ctx := context.Background()
186 var dagservs []ipld.DAGService
@@ -136,19 +188,11 @@ func runBatchFetchTest(t *testing.T, read io.Reader) {
188 dagservs = append(dagservs, NewDAGService(bsi))
189 }
190
139 - spl := chunker.NewSizeSplitter(read, 512)
140 -
141 - root, err := imp.BuildDagFromReader(dagservs[0], spl)
142 - if err != nil {
143 - t.Fatal(err)
144 - }
191 + root := makeTestDAG(t, read, dagservs[0])
192
193 t.Log("finished setup.")
194
148 - dagr, err := uio.NewDagReader(ctx, root, dagservs[0])
149 - if err != nil {
150 - t.Fatal(err)
151 - }
195 + dagr := makeTestDAGReader(t, root, dagservs[0])
196
197 expected, err := ioutil.ReadAll(dagr)
198 if err != nil {
@@ -181,11 +225,9 @@ func runBatchFetchTest(t *testing.T, read io.Reader) {
225 if !ok {
226 errs <- ErrNotProtobuf
227 }
184 -
185 - read, err := uio.NewDagReader(ctx, firstpb, dagservs[i])
186 - if err != nil {
187 - errs <- err
188 - }
228 + _ = firstpb
229 + _ = expected
230 + read := makeTestDAGReader(t, firstpb, dagservs[i])
231 datagot, err := ioutil.ReadAll(read)
232 if err != nil {
233 errs <- err
@@ -228,12 +270,9 @@ func TestFetchGraph(t *testing.T) {
270 }
271
272 read := io.LimitReader(u.NewTimeSeededRand(), 1024*32)
231 - root, err := imp.BuildDagFromReader(dservs[0], chunker.NewSizeSplitter(read, 512))
232 - if err != nil {
233 - t.Fatal(err)
234 - }
273 + root := makeTestDAG(t, read, dservs[0])
274
236 - err = FetchGraph(context.TODO(), root.Cid(), dservs[1])
275 + err := FetchGraph(context.TODO(), root.Cid(), dservs[1])
276 if err != nil {
277 t.Fatal(err)
278 }
@@ -254,14 +293,11 @@ func TestEnumerateChildren(t *testing.T) {
293 ds := NewDAGService(bsi[0])
294
295 read := io.LimitReader(u.NewTimeSeededRand(), 1024*1024)
257 - root, err := imp.BuildDagFromReader(ds, chunker.NewSizeSplitter(read, 512))
258 - if err != nil {
259 - t.Fatal(err)
260 - }
296 + root := makeTestDAG(t, read, ds)
297
298 set := cid.NewSet()
299
264 - err = EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit)
300 + err := EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit)
301 if err != nil {
302 t.Fatal(err)
303 }