directory_test.go 2.54 KB
Newer Older
Jakub Sztandera's avatar
Jakub Sztandera committed
1 2 3 4
package io

import (
	"context"
5
	"fmt"
Jakub Sztandera's avatar
Jakub Sztandera committed
6 7
	"testing"

8 9
	mdtest "github.com/ipfs/go-ipfs/merkledag/test"
	ft "github.com/ipfs/go-ipfs/unixfs"
Jakub Sztandera's avatar
Jakub Sztandera committed
10 11 12
)

func TestEmptyNode(t *testing.T) {
13
	n := ft.EmptyDirNode()
14
	if len(n.Links()) != 0 {
Jakub Sztandera's avatar
Jakub Sztandera committed
15 16 17 18
		t.Fatal("empty node should have 0 links")
	}
}

19 20 21 22 23 24
func TestDirectoryGrowth(t *testing.T) {
	ds := mdtest.Mock()
	dir := NewDirectory(ds)
	ctx := context.Background()

	d := ft.EmptyDirNode()
25
	ds.Add(ctx, d)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

	nelems := 10000

	for i := 0; i < nelems; i++ {
		err := dir.AddChild(ctx, fmt.Sprintf("dir%d", i), d)
		if err != nil {
			t.Fatal(err)
		}
	}

	_, err := dir.GetNode()
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
41
	links, err := dir.Links(ctx)
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	if err != nil {
		t.Fatal(err)
	}

	if len(links) != nelems {
		t.Fatal("didnt get right number of elements")
	}

	dirc := d.Cid()

	names := make(map[string]bool)
	for _, l := range links {
		names[l.Name] = true
		if !l.Cid.Equals(dirc) {
			t.Fatal("link wasnt correct")
		}
	}

	for i := 0; i < nelems; i++ {
		dn := fmt.Sprintf("dir%d", i)
		if !names[dn] {
			t.Fatal("didnt find directory: ", dn)
		}

		_, err := dir.Find(context.Background(), dn)
		if err != nil {
			t.Fatal(err)
		}
	}
}

func TestDuplicateAddDir(t *testing.T) {
	ds := mdtest.Mock()
	dir := NewDirectory(ds)
	ctx := context.Background()
	nd := ft.EmptyDirNode()

	err := dir.AddChild(ctx, "test", nd)
	if err != nil {
		t.Fatal(err)
	}

	err = dir.AddChild(ctx, "test", nd)
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
89
	lnks, err := dir.Links(ctx)
90 91 92 93 94 95 96 97 98
	if err != nil {
		t.Fatal(err)
	}

	if len(lnks) != 1 {
		t.Fatal("expected only one link")
	}
}

Jakub Sztandera's avatar
Jakub Sztandera committed
99
func TestDirBuilder(t *testing.T) {
100 101 102
	ds := mdtest.Mock()
	dir := NewDirectory(ds)
	ctx := context.Background()
Jakub Sztandera's avatar
Jakub Sztandera committed
103

104
	child := ft.EmptyDirNode()
105
	err := ds.Add(ctx, child)
106 107 108
	if err != nil {
		t.Fatal(err)
	}
Jakub Sztandera's avatar
Jakub Sztandera committed
109

110
	count := 5000
Jakub Sztandera's avatar
Jakub Sztandera committed
111

112 113 114 115 116 117 118 119
	for i := 0; i < count; i++ {
		err := dir.AddChild(ctx, fmt.Sprintf("entry %d", i), child)
		if err != nil {
			t.Fatal(err)
		}
	}

	dirnd, err := dir.GetNode()
Jakub Sztandera's avatar
Jakub Sztandera committed
120 121 122 123
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
124
	links, err := dir.Links(ctx)
Jakub Sztandera's avatar
Jakub Sztandera committed
125 126 127 128
	if err != nil {
		t.Fatal(err)
	}

129 130 131 132 133
	if len(links) != count {
		t.Fatal("not enough links dawg", len(links), count)
	}

	adir, err := NewDirectoryFromNode(ds, dirnd)
Jakub Sztandera's avatar
Jakub Sztandera committed
134 135 136 137
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
138
	links, err = adir.Links(ctx)
Jakub Sztandera's avatar
Jakub Sztandera committed
139 140 141 142
	if err != nil {
		t.Fatal(err)
	}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	names := make(map[string]bool)
	for _, lnk := range links {
		names[lnk.Name] = true
	}

	for i := 0; i < count; i++ {
		n := fmt.Sprintf("entry %d", i)
		if !names[n] {
			t.Fatal("COULDNT FIND: ", n)
		}
	}

	if len(links) != count {
		t.Fatal("wrong number of links", len(links), count)
	}
Jakub Sztandera's avatar
Jakub Sztandera committed
158
}