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

import (
	"context"
Łukasz Magiera's avatar
Łukasz Magiera committed
5
	path "github.com/ipfs/interface-go-ipfs-core/path"
6
	"math"
Łukasz Magiera's avatar
Łukasz Magiera committed
7
	gopath "path"
8 9 10
	"strings"
	"testing"

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
18
func (tp *TestSuite) TestDag(t *testing.T) {
19 20 21 22 23 24 25
	tp.hasApi(t, func(api coreiface.CoreAPI) error {
		if api.Dag() == nil {
			return apiNotImplemented
		}
		return nil
	})

26 27 28 29 30
	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)
31 32
}

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

Łukasz Magiera's avatar
Łukasz Magiera committed
43
func (tp *TestSuite) TestPut(t *testing.T) {
44 45
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
46
	api, err := tp.makeAPI(ctx)
47
	if err != nil {
48
		t.Fatal(err)
49 50
	}

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

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
66
func (tp *TestSuite) TestPutWithHash(t *testing.T) {
67 68
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
69
	api, err := tp.makeAPI(ctx)
70
	if err != nil {
71
		t.Fatal(err)
72 73
	}

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

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
89
func (tp *TestSuite) TestDagPath(t *testing.T) {
90 91
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
92
	api, err := tp.makeAPI(ctx)
93
	if err != nil {
94
		t.Fatal(err)
95 96
	}

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

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

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

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

117
	p := path.New(gopath.Join(nd.Cid().String(), "lnk"))
118

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

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
134
func (tp *TestSuite) TestTree(t *testing.T) {
135 136
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
137
	api, err := tp.makeAPI(ctx)
138
	if err != nil {
139
		t.Fatal(err)
140 141
	}

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

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

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
169
func (tp *TestSuite) TestBatch(t *testing.T) {
170 171
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
172
	api, err := tp.makeAPI(ctx)
173
	if err != nil {
174
		t.Fatal(err)
175 176
	}

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

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

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

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

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