Skip to content

Commit 6533a12

Browse files
committed
Add more data tests.
1 parent 28eb72a commit 6533a12

File tree

6 files changed

+161
-44
lines changed

6 files changed

+161
-44
lines changed

data/author_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package data
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"github.com/ipfs/go-merkledag/dagutils"
9+
)
10+
11+
func TestAuthorRoundtrip(t *testing.T) {
12+
ctx := context.Background()
13+
dag := dagutils.NewMemoryDagService()
14+
15+
data, err := os.ReadFile("testdata/author.json")
16+
if err != nil {
17+
t.Fatal("failed to read file")
18+
}
19+
20+
author, err := AuthorFromJSON(data)
21+
if err != nil {
22+
t.Fatal("failed to decode author json")
23+
}
24+
25+
id, err := AddAuthor(ctx, dag, author)
26+
if err != nil {
27+
t.Fatal("failed to add author to dag")
28+
}
29+
30+
author, err = GetAuthor(ctx, dag, id)
31+
if err != nil {
32+
t.Fatal("failed to get author from dag")
33+
}
34+
35+
if len(author.Metadata) != 1 {
36+
t.Error("unexpected metadata")
37+
}
38+
39+
meta, ok := author.Metadata["foo"]
40+
if !ok || meta != "bar" {
41+
t.Error("unexpected metadata value")
42+
}
43+
44+
if len(author.Repositories) != 1 {
45+
t.Error("unexpected repositories")
46+
}
47+
48+
repo, ok := author.Repositories["test"]
49+
if !ok || repo.String() != "bafyreib2rnmsqouz67uvb4jcjsqdvsmakdn3zrpswt4ud7aegbfohyrkbe" {
50+
t.Error("unexpected repository value")
51+
}
52+
}

data/commit_test.go

+21-44
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,44 @@
11
package data
22

33
import (
4+
"context"
5+
"os"
46
"testing"
57
"time"
68

7-
"github.com/ipfs/go-ipld-cbor"
9+
"github.com/ipfs/go-merkledag/dagutils"
810
)
911

10-
var data = []byte(`{
11-
"date": "2020-10-25T15:26:12.168056-07:00",
12-
"message": "big changes",
13-
"parents": [{"/": "bagaybqabciqeutn2u7n3zuk5b4ykgfwpkekb7ctgnlwik5zfr6bcukvknj2jtpa"}],
14-
"tree": {"/": "QmQycvPQd5tAVP4Xx1dp1Yfb9tmjKQAa5uxPoTfUQr9tFZ"},
15-
"metadata": {"foo": "bar"}
16-
}`)
12+
func TestCommitRoundtrip(t *testing.T) {
13+
ctx := context.Background()
14+
dag := dagutils.NewMemoryDagService()
1715

18-
func TestCommitFromJSON(t *testing.T) {
19-
commit, err := CommitFromJSON(data)
16+
data, err := os.ReadFile("testdata/commit.json")
2017
if err != nil {
21-
t.Fatalf("failed to decode commit json")
22-
}
23-
24-
if commit.Message != "big changes" {
25-
t.Error("message does not match")
18+
t.Fatal("failed to read file")
2619
}
2720

28-
if commit.Date.Format(time.RFC3339) != "2020-10-25T15:26:12-07:00" {
29-
t.Error("date does not match")
30-
}
31-
32-
if len(commit.Parents) != 1 {
33-
t.Error("parents does not match")
34-
}
35-
36-
if commit.Parents[0].String() != "bagaybqabciqeutn2u7n3zuk5b4ykgfwpkekb7ctgnlwik5zfr6bcukvknj2jtpa" {
37-
t.Error("parents does not match")
38-
}
39-
40-
if commit.Tree.String() != "QmQycvPQd5tAVP4Xx1dp1Yfb9tmjKQAa5uxPoTfUQr9tFZ" {
41-
t.Error("work tree does not match")
42-
}
43-
44-
if commit.Metadata["foo"] != "bar" {
45-
t.Error("metadata does not match")
46-
}
47-
}
48-
49-
func TestCommitFromCBOR(t *testing.T) {
5021
commit, err := CommitFromJSON(data)
5122
if err != nil {
52-
t.Fatalf("failed to decode commit")
23+
t.Fatal("failed to decode commit json")
5324
}
5425

55-
data, err := cbornode.DumpObject(commit)
26+
id, err := AddCommit(ctx, dag, commit)
5627
if err != nil {
57-
t.Fatalf("failed to encode commit")
28+
t.Fatal("failed to add commit to dag")
5829
}
5930

60-
commit, err = CommitFromCBOR(data)
31+
commit, err = GetCommit(ctx, dag, id)
6132
if err != nil {
62-
t.Fatalf("failed to decode commit")
33+
t.Fatal("failed to add commit to dag")
6334
}
6435

6536
if commit.Message != "big changes" {
6637
t.Error("message does not match")
6738
}
6839

6940
if commit.Date.Format(time.RFC3339) != "2020-10-25T15:26:12-07:00" {
70-
t.Errorf("date does not match")
41+
t.Error("date does not match")
7142
}
7243

7344
if len(commit.Parents) != 1 {
@@ -82,12 +53,18 @@ func TestCommitFromCBOR(t *testing.T) {
8253
t.Error("work tree does not match")
8354
}
8455

85-
if commit.Metadata["foo"] != "bar" {
56+
meta, ok := commit.Metadata["foo"]
57+
if !ok || meta != "bar" {
8658
t.Error("metadata does not match")
8759
}
8860
}
8961

9062
func TestCommitParentLinks(t *testing.T) {
63+
data, err := os.ReadFile("testdata/commit.json")
64+
if err != nil {
65+
t.Fatal("failed to read file")
66+
}
67+
9168
commit, err := CommitFromJSON(data)
9269
if err != nil {
9370
t.Fatal("failed to decode commit")

data/repository_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package data
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"github.com/ipfs/go-merkledag/dagutils"
9+
)
10+
11+
func TestRepositoryRoundtrip(t *testing.T) {
12+
ctx := context.Background()
13+
dag := dagutils.NewMemoryDagService()
14+
15+
data, err := os.ReadFile("testdata/repository.json")
16+
if err != nil {
17+
t.Fatal("failed to read file")
18+
}
19+
20+
repo, err := RepositoryFromJSON(data)
21+
if err != nil {
22+
t.Fatal("failed to decode repo json")
23+
}
24+
25+
id, err := AddRepository(ctx, dag, repo)
26+
if err != nil {
27+
t.Fatal("failed to add repo to dag")
28+
}
29+
30+
repo, err = GetRepository(ctx, dag, id)
31+
if err != nil {
32+
t.Fatal("failed to get repo from dag")
33+
}
34+
35+
if repo.DefaultBranch != "default" {
36+
t.Error("default branch does not match")
37+
}
38+
39+
if len(repo.Branches) != 1 {
40+
t.Fatal("unexpected branches")
41+
}
42+
43+
branch, ok := repo.Branches["default"]
44+
if !ok || branch.String() != "bafyreieo2mhnqyqntenwyndzxoovw5nhbpit727kjrl3mjbyb5nv6zs2pu" {
45+
t.Error("unexpected branch value")
46+
}
47+
48+
if len(repo.Tags) != 1 {
49+
t.Fatal("unexpected tags")
50+
}
51+
52+
tag, ok := repo.Tags["v0.0.1"]
53+
if !ok || tag.String() != "bafyreieo2mhnqyqntenwyndzxoovw5nhbpit727kjrl3mjbyb5nv6zs3pu" {
54+
t.Error("unexpected tag value")
55+
}
56+
57+
if len(repo.Metadata) != 1 {
58+
t.Fatal("unexpected metadata")
59+
}
60+
61+
meta, ok := repo.Metadata["foo"]
62+
if !ok || meta != "bar" {
63+
t.Error("unexpected metadata value")
64+
}
65+
}

data/testdata/author.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"repositories": {
3+
"test": {"/": "bafyreib2rnmsqouz67uvb4jcjsqdvsmakdn3zrpswt4ud7aegbfohyrkbe"}
4+
},
5+
"metadata": {"foo": "bar"}
6+
}

data/testdata/commit.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"date": "2020-10-25T15:26:12.168056-07:00",
3+
"message": "big changes",
4+
"parents": [{"/": "bagaybqabciqeutn2u7n3zuk5b4ykgfwpkekb7ctgnlwik5zfr6bcukvknj2jtpa"}],
5+
"tree": {"/": "QmQycvPQd5tAVP4Xx1dp1Yfb9tmjKQAa5uxPoTfUQr9tFZ"},
6+
"metadata": {"foo": "bar"}
7+
}

data/testdata/repository.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"default_branch": "default",
3+
"branches": {
4+
"default": {"/": "bafyreieo2mhnqyqntenwyndzxoovw5nhbpit727kjrl3mjbyb5nv6zs2pu"}
5+
},
6+
"tags": {
7+
"v0.0.1": {"/": "bafyreieo2mhnqyqntenwyndzxoovw5nhbpit727kjrl3mjbyb5nv6zs3pu"}
8+
},
9+
"metadata": {"foo": "bar"}
10+
}

0 commit comments

Comments
 (0)