2020-06-23 19:02:54 +03:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-09-27 20:25:37 +03:00
|
|
|
"time"
|
2020-06-23 19:02:54 +03:00
|
|
|
|
2020-09-27 20:25:37 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-06-23 19:02:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig(t *testing.T, config Config) {
|
2020-09-27 20:25:37 +03:00
|
|
|
// string
|
2020-06-23 19:02:54 +03:00
|
|
|
err := config.StoreString("section.key", "value")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.NoError(t, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
val, err := config.ReadString("section.key")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "value", val)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
2020-09-27 20:25:37 +03:00
|
|
|
// bool
|
|
|
|
err = config.StoreBool("section.true", true)
|
|
|
|
require.NoError(t, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
val2, err := config.ReadBool("section.true")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, val2)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
2020-09-27 20:25:37 +03:00
|
|
|
// 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
|
2020-06-23 19:02:54 +03:00
|
|
|
configs, err := config.ReadAll("section")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, map[string]string{
|
2020-06-23 19:02:54 +03:00
|
|
|
"section.key": "value",
|
|
|
|
"section.true": "true",
|
2020-09-27 20:25:37 +03:00
|
|
|
"section.time": "1234",
|
2020-06-23 19:02:54 +03:00
|
|
|
}, configs)
|
|
|
|
|
2020-09-27 20:25:37 +03:00
|
|
|
// RemoveAll
|
2020-06-23 19:02:54 +03:00
|
|
|
err = config.RemoveAll("section.true")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.NoError(t, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
configs, err = config.ReadAll("section")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, map[string]string{
|
|
|
|
"section.key": "value",
|
|
|
|
"section.time": "1234",
|
2020-06-23 19:02:54 +03:00
|
|
|
}, configs)
|
|
|
|
|
|
|
|
_, err = config.ReadBool("section.true")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.Equal(t, ErrNoConfigEntry, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
err = config.RemoveAll("section.nonexistingkey")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.Error(t, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
err = config.RemoveAll("section.key")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.NoError(t, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
_, err = config.ReadString("section.key")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.Equal(t, ErrNoConfigEntry, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
err = config.RemoveAll("nonexistingsection")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
err = config.RemoveAll("section.time")
|
|
|
|
require.NoError(t, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
err = config.RemoveAll("section")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.Error(t, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
_, err = config.ReadString("section.key")
|
2020-09-27 20:25:37 +03:00
|
|
|
require.Error(t, err)
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
err = config.RemoveAll("section.key")
|
2020-09-27 20:25:37 +03:00
|
|
|
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)
|
2021-03-28 23:18:01 +03:00
|
|
|
|
|
|
|
// 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)
|
2020-06-23 19:02:54 +03:00
|
|
|
}
|