aboutsummaryrefslogtreecommitdiffstats
path: root/internal/configloader/configloader_test.go
blob: a41c2be99b77679bc8a7bbb00bfcc702496adc51 (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
package configloader_test

import (
	"fmt"
	"os"
	"testing"

	"github.com/eugenetriguba/bolt/internal/bolttest"
	"github.com/eugenetriguba/bolt/internal/configloader"
	"gotest.tools/v3/assert"
)

func TestNewConfigMigrationsDirPathDefault(t *testing.T) {
	bolttest.ChangeCwd(t, os.TempDir())

	cfg, err := configloader.NewConfig()
	assert.NilError(t, err)

	assert.Equal(t, cfg.Migrations.DirectoryPath, "migrations")
}

func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) {
	expectedCfg := configloader.Config{
		Migrations: configloader.MigrationsConfig{DirectoryPath: "myfancymigrations"},
		Connection: configloader.ConnectionConfig{
			Host:     "testhost",
			Port:     1234,
			User:     "testuser",
			Password: "testpassword",
			DBName:   "testdb",
			Driver:   "postgres",
		},
	}
	bolttest.CreateConfigFile(t, &expectedCfg, "bolt.toml")

	cfg, err := configloader.NewConfig()
	assert.NilError(t, err)
	assert.DeepEqual(t, *cfg, expectedCfg)
}

func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
	fileCfg := configloader.Config{
		Migrations: configloader.MigrationsConfig{DirectoryPath: "cfgmigrations"},
		Connection: configloader.ConnectionConfig{
			Host:     "testhost",
			Port:     1234,
			User:     "testuser",
			Password: "testpassword",
			DBName:   "testdb",
			Driver:   "mysql",
		},
	}
	bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml")

	envCfg := configloader.Config{
		Migrations: configloader.MigrationsConfig{DirectoryPath: "envmigrations"},
		Connection: configloader.ConnectionConfig{
			Host:     "envtesthost",
			Port:     4321,
			User:     "envtestuser",
			Password: "envtestpassword",
			DBName:   "envtestdb",
			Driver:   "postgres",
		},
	}
	t.Setenv("BOLT_MIGRATIONS_DIR_PATH", envCfg.Migrations.DirectoryPath)
	t.Setenv("BOLT_DB_CONN_HOST", envCfg.Connection.Host)
	t.Setenv("BOLT_DB_CONN_PORT", fmt.Sprintf("%d", envCfg.Connection.Port))
	t.Setenv("BOLT_DB_CONN_USER", envCfg.Connection.User)
	t.Setenv("BOLT_DB_CONN_PASSWORD", envCfg.Connection.Password)
	t.Setenv("BOLT_DB_CONN_DBNAME", envCfg.Connection.DBName)
	t.Setenv("BOLT_DB_CONN_DRIVER", envCfg.Connection.Driver)

	cfg, err := configloader.NewConfig()
	assert.NilError(t, err)
	assert.DeepEqual(t, *cfg, envCfg)
}