forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
odb_test.go
154 lines (125 loc) · 3.15 KB
/
odb_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package git
import (
"bytes"
"errors"
"io"
"io/ioutil"
"testing"
)
func TestOdbRead(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, err := repo.Odb()
if err != nil {
t.Fatalf("Odb: %v", err)
}
data := []byte("hello")
id, err := odb.Write(data, ObjectBlob)
if err != nil {
t.Fatalf("odb.Write: %v", err)
}
sz, typ, err := odb.ReadHeader(id)
if err != nil {
t.Fatalf("ReadHeader: %v", err)
}
if sz != uint64(len(data)) {
t.Errorf("ReadHeader got size %d, want %d", sz, len(data))
}
if typ != ObjectBlob {
t.Errorf("ReadHeader got object type %s", typ)
}
obj, err := odb.Read(id)
if err != nil {
t.Fatalf("Read: %v", err)
}
if !bytes.Equal(obj.Data(), data) {
t.Errorf("Read got wrong data")
}
if sz := obj.Len(); sz != uint64(len(data)) {
t.Errorf("Read got size %d, want %d", sz, len(data))
}
if typ := obj.Type(); typ != ObjectBlob {
t.Errorf("Read got object type %s", typ)
}
}
func TestOdbStream(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, error := repo.Odb()
checkFatal(t, error)
str := "hello, world!"
writeStream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob)
checkFatal(t, error)
n, error := io.WriteString(writeStream, str)
checkFatal(t, error)
if n != len(str) {
t.Fatalf("Bad write length %v != %v", n, len(str))
}
error = writeStream.Close()
checkFatal(t, error)
expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
checkFatal(t, error)
if writeStream.Id.Cmp(expectedId) != 0 {
t.Fatal("Wrong data written")
}
readStream, error := odb.NewReadStream(&writeStream.Id)
checkFatal(t, error)
data, error := ioutil.ReadAll(readStream)
if str != string(data) {
t.Fatalf("Wrong data read %v != %v", str, string(data))
}
}
func TestOdbHash(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, error := repo.Odb()
checkFatal(t, error)
str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7
parent 66e1c476199ebcd3e304659992233132c5a52c6c
author John Doe <[email protected]> 1390682018 +0000
committer John Doe <[email protected]> 1390682018 +0000
Initial commit.`
for _, data := range [][]byte{[]byte(str), doublePointerBytes()} {
oid, error := odb.Hash(data, ObjectCommit)
checkFatal(t, error)
coid, error := odb.Write(data, ObjectCommit)
checkFatal(t, error)
if oid.Cmp(coid) != 0 {
t.Fatal("Hash and write Oids are different")
}
}
}
func TestOdbForeach(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
expect := 3
count := 0
err = odb.ForEach(func(id *Oid) error {
count++
return nil
})
checkFatal(t, err)
if count != expect {
t.Fatalf("Expected %v objects, got %v", expect, count)
}
expect = 1
count = 0
to_return := errors.New("not really an error")
err = odb.ForEach(func(id *Oid) error {
count++
return to_return
})
if err != to_return {
t.Fatalf("Odb.ForEach() did not return the expected error, got %v", err)
}
}