aboutsummaryrefslogtreecommitdiffstats
path: root/repository/config_testing.go
blob: 8c22934a7d8f8f45164f9bc3ec9a34406acf0096 (plain) (blame)
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
155
package repository

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func testConfig(t *testing.T, config Config) {
	// string
	err := config.StoreString("section.key", "value")
	require.NoError(t, err)

	val, err := config.ReadString("section.key")
	require.NoError(t, err)
	require.Equal(t, "value", val)

	// bool
	err = config.StoreBool("section.true", true)
	require.NoError(t, err)

	val2, err := config.ReadBool("section.true")
	require.NoError(t, err)
	require.Equal(t, true, val2)

	// timestamp
	err = config.StoreTimestamp("section.time", time.Unix(1234, 0))
	require.NoError(t, err)

	val3, err := config.ReadTimestamp("section.time")
	require.NoError(t, err)
	require.Equal(t, time.Unix(1234, 0), val3)

	// ReadAll
	configs, err := config.ReadAll("section")
	require.NoError(t, err)
	require.Equal(t, map[string]string{
		"section.key":  "value",
		"section.true": "true",
		"section.time": "1234",
	}, configs)

	// RemoveAll
	err = config.RemoveAll("section.true")
	require.NoError(t, err)

	configs, err = config.ReadAll("section")
	require.NoError(t, err)
	require.Equal(t, map[string]string{
		"section.key":  "value",
		"section.time": "1234",
	}, configs)

	_, err = config.ReadBool("section.true")
	require.ErrorIs(t, err, ErrNoConfigEntry)

	err = config.RemoveAll("section.nonexistingkey")
	require.Error(t, err)

	err = config.RemoveAll("section.key")
	require.NoError(t, err)

	_, err = config.ReadString("section.key")
	require.ErrorIs(t, err, ErrNoConfigEntry)

	err = config.RemoveAll("nonexistingsection")
	require.Error(t, err)

	err = config.RemoveAll("section.time")
	require.NoError(t, err)

	err = config.RemoveAll("section")
	require.Error(t, err)

	_, err = config.ReadString("section.key")
	require.Error(t, err)

	err = config.RemoveAll("section.key")
	require.Error(t, err)

	// section + subsections
	require.NoError(t, config.StoreString("section.opt1", "foo"))
	require.NoError(t, config.StoreString("section.opt2", "foo2"))
	require.NoError(t, config.StoreString("section.subsection.opt1", "foo3"))
	require.NoError(t, config.StoreString("section.subsection.opt2", "foo4"))
	require.NoError(t, config.StoreString("section.subsection.subsection.opt1", "foo5"))
	require.NoError(t, config.StoreString("section.subsection.subsection.opt2", "foo6"))

	all, err := config.ReadAll("section")
	require.NoError(t, err)
	require.Equal(t, map[string]string{
		"section.opt1":                       "foo",
		"section.opt2":                       "foo2",
		"section.subsection.opt1":            "foo3",
		"section.subsection.opt2":            "foo4",
		"section.subsection.subsection.opt1": "foo5",
		"section.subsection.subsection.opt2": "foo6",
	}, all)

	all, err = config.ReadAll("section.subsection")
	require.NoError(t, err)
	require.Equal(t, map[string]string{
		"section.subsection.opt1":            "foo3",
		"section.subsection.opt2":            "foo4",
		"section.subsection.subsection.opt1": "foo5",
		"section.subsection.subsection.opt2": "foo6",
	}, all)

	all, err = config.ReadAll("section.subsection.subsection")
	require.NoError(t, err)
	require.Equal(t, map[string]string{
		"section.subsection.subsection.opt1": "foo5",
		"section.subsection.subsection.opt2": "foo6",
	}, all)

	// missing section + case insensitive
	val, err = config.ReadString("section2.opt1")
	require.Error(t, err)

	val, err = config.ReadString("section.opt1")
	require.NoError(t, err)
	require.Equal(t, "foo", val)

	val, err = config.ReadString("SECTION.OPT1")
	require.NoError(t, err)
	require.Equal(t, "foo", val)

	_, err = config.ReadString("SECTION2.OPT3")
	require.Error(t, err)

	// missing subsection + case insensitive
	val, err = config.ReadString("section.subsection.opt1")
	require.NoError(t, err)
	require.Equal(t, "foo3", val)

	// for some weird reason, subsection ARE case sensitive
	_, err = config.ReadString("SECTION.SUBSECTION.OPT1")
	require.Error(t, err)

	_, err = config.ReadString("SECTION.SUBSECTION1.OPT1")
	require.Error(t, err)

	// missing sub-subsection + case insensitive
	val, err = config.ReadString("section.subsection.subsection.opt1")
	require.NoError(t, err)
	require.Equal(t, "foo5", val)

	// for some weird reason, subsection ARE case sensitive
	_, err = config.ReadString("SECTION.SUBSECTION.SUBSECTION.OPT1")
	require.Error(t, err)

	_, err = config.ReadString("SECTION.SUBSECTION.SUBSECTION1.OPT1")
	require.Error(t, err)
}