-
Notifications
You must be signed in to change notification settings - Fork 1
/
store_test.go
89 lines (77 loc) · 2.63 KB
/
store_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
/*
* Copyright (C) 2022 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package stoabs
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
assert.NotEmpty(t, cfg.LockAcquireTimeout)
assert.NotNil(t, cfg.Log)
}
func TestAcquireLockTimeout(t *testing.T) {
cfg := DefaultConfig()
WithLockAcquireTimeout(time.Hour)(&cfg)
assert.Equal(t, time.Hour, cfg.LockAcquireTimeout)
}
func TestWriteLockOption(t *testing.T) {
assert.True(t, WriteLockOption{}.Enabled([]TxOption{WithWriteLock()}))
assert.False(t, WriteLockOption{}.Enabled([]TxOption{}))
}
func TestDatabaseError(t *testing.T) {
t.Run("wraps db errors", func(t *testing.T) {
assert.ErrorAs(t, ErrStoreIsClosed, new(ErrDatabase), "ErrStoreIsClosed should be a ErrDatabase")
assert.ErrorAs(t, ErrCommitFailed, new(ErrDatabase), "ErrCommitFailed should be a ErrDatabase")
})
t.Run("does not wrap non-db errors", func(t *testing.T) {
assert.False(t, errors.As(ErrKeyNotFound, new(ErrDatabase)), "ErrKeyNotFound is not a ErrDatabase")
})
t.Run("does not double wrap", func(t *testing.T) {
firstError := DatabaseError(errors.New("this is wrapped"))
secondError := DatabaseError(fmt.Errorf("this is not wrapped: %w", firstError))
target := new(ErrDatabase)
assert.ErrorAs(t, firstError, new(ErrDatabase))
assert.ErrorAs(t, secondError, target)
assert.ErrorIs(t, target, firstError)
})
}
func TestNewErrorWriter(t *testing.T) {
t.Run("it wraps an DatabaseError", func(t *testing.T) {
writer := NewErrorWriter(errors.New("test"))
_, err := writer.Get(BytesKey{})
assert.ErrorIs(t, ErrDatabase{}, err)
})
}
func TestNilReader_Get(t *testing.T) {
t.Run("returns error", func(t *testing.T) {
data, err := NilReader{}.Get(BytesKey{})
assert.ErrorIs(t, err, ErrKeyNotFound)
assert.Nil(t, data)
})
}
func TestNilReader_Empty(t *testing.T) {
t.Run("returns true", func(t *testing.T) {
data, err := NilReader{}.Empty()
assert.NoError(t, err)
assert.True(t, data)
})
}