dag_test.go 2.21 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3 4 5 6 7 8 9
package coreapi_test

import (
	"context"
	"path"
	"strings"
	"testing"

	coreapi "github.com/ipfs/go-ipfs/core/coreapi"
10 11

	mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
Łukasz Magiera's avatar
Łukasz Magiera committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
)

var (
	treeExpected = map[string]struct{}{
		"a":   {},
		"b":   {},
		"c":   {},
		"c/d": {},
		"c/e": {},
	}
)

func TestPut(t *testing.T) {
	ctx := context.Background()
	_, api, err := makeAPI(ctx)
	if err != nil {
		t.Error(err)
	}

31
	res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`))
Łukasz Magiera's avatar
Łukasz Magiera committed
32 33 34 35 36 37 38 39 40
	if err != nil {
		t.Error(err)
	}

	if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
		t.Errorf("got wrong cid: %s", res[0].Cid().String())
	}
}

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
func TestPutWithHash(t *testing.T) {
	ctx := context.Background()
	_, api, err := makeAPI(ctx)
	if err != nil {
		t.Error(err)
	}

	res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), api.Dag().WithHash(mh.ID, -1))
	if err != nil {
		t.Error(err)
	}

	if res[0].Cid().String() != "z5hRLNd2sv4z1c" {
		t.Errorf("got wrong cid: %s", res[0].Cid().String())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
58 59 60 61 62 63 64
func TestPath(t *testing.T) {
	ctx := context.Background()
	_, api, err := makeAPI(ctx)
	if err != nil {
		t.Error(err)
	}

65
	sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`))
Łukasz Magiera's avatar
Łukasz Magiera committed
66 67 68 69
	if err != nil {
		t.Error(err)
	}

70
	res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`))
Łukasz Magiera's avatar
Łukasz Magiera committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	if err != nil {
		t.Error(err)
	}

	p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk"))
	if err != nil {
		t.Error(err)
	}

	nd, err := api.Dag().Get(ctx, p)
	if err != nil {
		t.Error(err)
	}

	if nd.Cid().String() != sub[0].Cid().String() {
		t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub[0].Cid().String())
	}
}

func TestTree(t *testing.T) {
	ctx := context.Background()
	_, api, err := makeAPI(ctx)
	if err != nil {
		t.Error(err)
	}

97
	res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`))
Łukasz Magiera's avatar
Łukasz Magiera committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	if err != nil {
		t.Error(err)
	}

	lst := res[0].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)
		}
	}
}