-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_open_test.go
68 lines (61 loc) · 1.54 KB
/
table_open_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
package yti_test
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/toudi/yti"
)
func TestOpenFile(t *testing.T) {
t.Run("open nonexisting file should not yield errors by default", func(t *testing.T) {
t.Parallel()
_, err := yti.OpenFile[TestStruct]("non-existing-file", nil)
assert.NoError(t, err)
})
t.Run("open nonexisting file with mustExist option should yield error", func(t *testing.T) {
t.Parallel()
_, err := yti.OpenFile[TestStruct]("non-existing-file", &yti.TableOptions[TestStruct]{
MustExist: true,
})
assert.True(t, os.IsNotExist(err))
})
t.Run("open existing file (invalid yaml)", func(t *testing.T) {
t.Parallel()
WithTempFile(func(file *os.File) {
_, err := yti.OpenFile[TestStruct](
file.Name(),
&yti.TableOptions[TestStruct]{MustExist: true},
)
assert.Equal(t, yti.ErrLoadingTable, err)
})
})
t.Run("open existing file (empty yaml)", func(t *testing.T) {
t.Parallel()
WithTempFile(func(file *os.File) {
_, _ = file.WriteString("---\n")
file.Close()
_, err := yti.OpenFile[TestStruct](
file.Name(),
&yti.TableOptions[TestStruct]{MustExist: true},
)
assert.NoError(t, err)
})
})
t.Run("open empty file with indices", func(t *testing.T) {
t.Parallel()
WithTempFileContent(
[]TestStruct{
{
Id: 1,
Name: "Test",
},
}, func(file *os.File) {
_, err := yti.OpenFile[TestStruct](
file.Name(),
&yti.TableOptions[TestStruct]{
Indices: TestStructIndices,
},
)
assert.NoError(t, err)
})
})
}