dag.go 4.04 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
	}

116
	p, err := coreiface.ParsePath(path.Join(nd.Cid().String(), "lnk"))
117
	if err != nil {
118
		t.Fatal(err)
119 120
	}

121
	rp, err := api.ResolvePath(ctx, p)
122
	if err != nil {
123
		t.Fatal(err)
124 125
	}

126
	ndd, err := api.Dag().Get(ctx, rp.Cid())
127
	if err != nil {
128
		t.Fatal(err)
129 130
	}

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

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

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

149
	err = api.Dag().Add(ctx, nd)
150
	if err != nil {
151
		t.Fatal(err)
152 153
	}

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

	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)
		}
	}
}

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

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

184 185
	if nd.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
		t.Errorf("got wrong cid: %s", nd.Cid().String())
186 187
	}

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

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

197
	_, err = api.Dag().Get(ctx, nd.Cid())
198
	if err != nil {
199
		t.Fatal(err)
200 201
	}
}