dag.go 4 KB
Newer Older
1
package tests
2 3 4

import (
	"context"
5
	"math"
6 7 8 9
	"path"
	"strings"
	"testing"

Łukasz Magiera's avatar
Łukasz Magiera committed
10
	coreiface "github.com/ipfs/interface-go-ipfs-core"
11

Łukasz Magiera's avatar
Łukasz Magiera committed
12 13 14
	ipldcbor "github.com/ipfs/go-ipld-cbor"
	ipld "github.com/ipfs/go-ipld-format"
	mh "github.com/multiformats/go-multihash"
15 16
)

17
func (tp *provider) TestDag(t *testing.T) {
18 19 20 21 22 23 24
	tp.hasApi(t, func(api coreiface.CoreAPI) error {
		if api.Dag() == nil {
			return apiNotImplemented
		}
		return nil
	})

25 26 27 28 29
	t.Run("TestPut", tp.TestPut)
	t.Run("TestPutWithHash", tp.TestPutWithHash)
	t.Run("TestPath", tp.TestDagPath)
	t.Run("TestTree", tp.TestTree)
	t.Run("TestBatch", tp.TestBatch)
30 31
}

32 33 34 35 36 37 38 39 40 41
var (
	treeExpected = map[string]struct{}{
		"a":   {},
		"b":   {},
		"c":   {},
		"c/d": {},
		"c/e": {},
	}
)

42
func (tp *provider) TestPut(t *testing.T) {
43 44
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
45
	api, err := tp.makeAPI(ctx)
46
	if err != nil {
47
		t.Fatal(err)
48 49
	}

50
	nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), math.MaxUint64, -1)
51
	if err != nil {
52
		t.Fatal(err)
53 54
	}

55
	err = api.Dag().Add(ctx, nd)
56
	if err != nil {
57
		t.Fatal(err)
58 59
	}

60 61
	if nd.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
		t.Errorf("got wrong cid: %s", nd.Cid().String())
62 63 64
	}
}

65
func (tp *provider) TestPutWithHash(t *testing.T) {
66 67
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
68
	api, err := tp.makeAPI(ctx)
69
	if err != nil {
70
		t.Fatal(err)
71 72
	}

73
	nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), mh.ID, -1)
74
	if err != nil {
75
		t.Fatal(err)
76 77
	}

78
	err = api.Dag().Add(ctx, nd)
79
	if err != nil {
80
		t.Fatal(err)
81 82
	}

83 84
	if nd.Cid().String() != "z5hRLNd2sv4z1c" {
		t.Errorf("got wrong cid: %s", nd.Cid().String())
85 86 87
	}
}

88
func (tp *provider) TestDagPath(t *testing.T) {
89 90
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
91
	api, err := tp.makeAPI(ctx)
92
	if err != nil {
93
		t.Fatal(err)
94 95
	}

96
	snd, err := ipldcbor.FromJSON(strings.NewReader(`"foo"`), math.MaxUint64, -1)
97
	if err != nil {
98
		t.Fatal(err)
99 100
	}

101
	err = api.Dag().Add(ctx, snd)
102
	if err != nil {
103
		t.Fatal(err)
104 105
	}

106
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+snd.Cid().String()+`"}}`), math.MaxUint64, -1)
107
	if err != nil {
108
		t.Fatal(err)
109 110
	}

111
	err = api.Dag().Add(ctx, nd)
112
	if err != nil {
113
		t.Fatal(err)
114 115
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
116
	p := coreiface.ParsePath(path.Join(nd.Cid().String(), "lnk"))
117

118
	rp, err := api.ResolvePath(ctx, p)
119
	if err != nil {
120
		t.Fatal(err)
121 122
	}

123
	ndd, err := api.Dag().Get(ctx, rp.Cid())
124
	if err != nil {
125
		t.Fatal(err)
126 127
	}

128
	if ndd.Cid().String() != snd.Cid().String() {
129
		t.Errorf("got unexpected cid %s, expected %s", ndd.Cid().String(), snd.Cid().String())
130 131 132
	}
}

133
func (tp *provider) TestTree(t *testing.T) {
134 135
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
136
	api, err := tp.makeAPI(ctx)
137
	if err != nil {
138
		t.Fatal(err)
139 140
	}

141
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), math.MaxUint64, -1)
142
	if err != nil {
143
		t.Fatal(err)
144 145
	}

146
	err = api.Dag().Add(ctx, nd)
147
	if err != nil {
148
		t.Fatal(err)
149 150
	}

151
	res, err := api.Dag().Get(ctx, nd.Cid())
152
	if err != nil {
153
		t.Fatal(err)
154 155 156 157 158 159 160 161 162 163 164 165 166 167
	}

	lst := res.Tree("", -1)
	if len(lst) != len(treeExpected) {
		t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
	}

	for _, ent := range lst {
		if _, ok := treeExpected[ent]; !ok {
			t.Errorf("unexpected tree entry %s", ent)
		}
	}
}

168
func (tp *provider) TestBatch(t *testing.T) {
169 170
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
171
	api, err := tp.makeAPI(ctx)
172
	if err != nil {
173
		t.Fatal(err)
174 175
	}

176
	nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), math.MaxUint64, -1)
177
	if err != nil {
178
		t.Fatal(err)
179 180
	}

181 182
	if nd.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
		t.Errorf("got wrong cid: %s", nd.Cid().String())
183 184
	}

185
	_, err = api.Dag().Get(ctx, nd.Cid())
186
	if err == nil || !strings.Contains(err.Error(), "not found") {
187
		t.Fatal(err)
188 189
	}

190
	if err := api.Dag().AddMany(ctx, []ipld.Node{nd}); err != nil {
191
		t.Fatal(err)
192 193
	}

194
	_, err = api.Dag().Get(ctx, nd.Cid())
195
	if err != nil {
196
		t.Fatal(err)
197 198
	}
}