1
1
package data
2
2
3
3
import (
4
+ "context"
5
+ "os"
4
6
"testing"
5
7
"time"
6
8
7
- "github.com/ipfs/go-ipld-cbor "
9
+ "github.com/ipfs/go-merkledag/dagutils "
8
10
)
9
11
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 ()
17
15
18
- func TestCommitFromJSON (t * testing.T ) {
19
- commit , err := CommitFromJSON (data )
16
+ data , err := os .ReadFile ("testdata/commit.json" )
20
17
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" )
26
19
}
27
20
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 ) {
50
21
commit , err := CommitFromJSON (data )
51
22
if err != nil {
52
- t .Fatalf ("failed to decode commit" )
23
+ t .Fatal ("failed to decode commit json " )
53
24
}
54
25
55
- data , err := cbornode . DumpObject ( commit )
26
+ id , err := AddCommit ( ctx , dag , commit )
56
27
if err != nil {
57
- t .Fatalf ("failed to encode commit" )
28
+ t .Fatal ("failed to add commit to dag " )
58
29
}
59
30
60
- commit , err = CommitFromCBOR ( data )
31
+ commit , err = GetCommit ( ctx , dag , id )
61
32
if err != nil {
62
- t .Fatalf ("failed to decode commit" )
33
+ t .Fatal ("failed to add commit to dag " )
63
34
}
64
35
65
36
if commit .Message != "big changes" {
66
37
t .Error ("message does not match" )
67
38
}
68
39
69
40
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" )
71
42
}
72
43
73
44
if len (commit .Parents ) != 1 {
@@ -82,12 +53,18 @@ func TestCommitFromCBOR(t *testing.T) {
82
53
t .Error ("work tree does not match" )
83
54
}
84
55
85
- if commit .Metadata ["foo" ] != "bar" {
56
+ meta , ok := commit .Metadata ["foo" ]
57
+ if ! ok || meta != "bar" {
86
58
t .Error ("metadata does not match" )
87
59
}
88
60
}
89
61
90
62
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
+
91
68
commit , err := CommitFromJSON (data )
92
69
if err != nil {
93
70
t .Fatal ("failed to decode commit" )
0 commit comments