-
Notifications
You must be signed in to change notification settings - Fork 10
/
memory_storer_test.go
89 lines (65 loc) · 1.87 KB
/
memory_storer_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
package gitobj
import (
"bytes"
"encoding/hex"
"io"
"io/ioutil"
"strings"
"testing"
"github.com/git-lfs/gitobj/v2/errors"
"github.com/stretchr/testify/assert"
)
func TestMemoryStorerIncludesGivenEntries(t *testing.T) {
sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
hex, err := hex.DecodeString(sha)
assert.Nil(t, err)
ms := newMemoryStorer(map[string]io.ReadWriter{
sha: bytes.NewBuffer([]byte{0x1}),
})
buf, err := ms.Open(hex)
assert.Nil(t, err)
contents, err := ioutil.ReadAll(buf)
assert.Nil(t, err)
assert.Equal(t, []byte{0x1}, contents)
}
func TestMemoryStorerAcceptsNilEntries(t *testing.T) {
ms := newMemoryStorer(nil)
assert.NotNil(t, ms)
assert.Equal(t, 0, len(ms.fs))
assert.NoError(t, ms.Close())
}
func TestMemoryStorerDoesntOpenMissingEntries(t *testing.T) {
sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
hex, err := hex.DecodeString(sha)
assert.Nil(t, err)
ms := newMemoryStorer(nil)
f, err := ms.Open(hex)
assert.Equal(t, errors.NoSuchObject(hex), err)
assert.Nil(t, f)
}
func TestMemoryStorerStoresNewEntries(t *testing.T) {
hex, err := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
assert.Nil(t, err)
ms := newMemoryStorer(nil)
assert.Equal(t, 0, len(ms.fs))
_, err = ms.Store(hex, strings.NewReader("hello"))
assert.Nil(t, err)
assert.Equal(t, 1, len(ms.fs))
got, err := ms.Open(hex)
assert.Nil(t, err)
contents, err := ioutil.ReadAll(got)
assert.Nil(t, err)
assert.Equal(t, "hello", string(contents))
}
func TestMemoryStorerStoresExistingEntries(t *testing.T) {
hex, err := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
assert.Nil(t, err)
ms := newMemoryStorer(nil)
assert.Equal(t, 0, len(ms.fs))
_, err = ms.Store(hex, new(bytes.Buffer))
assert.Nil(t, err)
assert.Equal(t, 1, len(ms.fs))
n, err := ms.Store(hex, new(bytes.Buffer))
assert.Nil(t, err)
assert.EqualValues(t, 0, n)
}