aboutsummaryrefslogtreecommitdiff
path: root/pkg/config/config_test.go
blob: af18fc1ad714df4d358d02d8b0bc6fe0cafd942a (plain)
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
package config

import (
	"reflect"
	"testing"

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

func TestLoadFile(t *testing.T) {
	// Minimal yaml file
	minimalConfig := Config{
		Address:   "127.0.0.1",
		Port:      "8080",
		Token:     "12345678-9abc-def0-1234-56789abcdef0",
		TrainStop: "ABCD:test:01",
	}

	// Minimal yaml file with hostname resolving
	minimalConfigWithResolving := Config{
		Address:   "localhost",
		Port:      "www",
		Token:     "12345678-9abc-def0-1234-56789abcdef0",
		TrainStop: "VWXY_Z:test:90",
	}

	// Complete yaml file
	completeConfig := Config{
		Address:   "127.0.0.2",
		Port:      "8082",
		Token:     "12345678-9abc-def0-1234-56789abcdef0",
		TrainStop: "ABCD:test:01",
	}

	// Test cases
	testCases := []struct {
		name          string
		input         string
		expected      *Config
		expectedError interface{}
	}{
		{"Non existant file", "test_data/non-existant", nil, &OpenError{}},
		{"Invalid file content", "test_data/invalid.yaml", nil, &DecodeError{}},
		{"Invalid address should fail to load", "test_data/invalid_address.yaml", nil, &InvalidAddressError{}},
		{"Unresolvable address should fail to load", "test_data/invalid_address_unresolvable.yaml", nil, &InvalidAddressError{}},
		{"Invalid port should fail to load", "test_data/invalid_port.yaml", nil, &InvalidPortError{}},
		{"Invalid token should fail to load", "test_data/invalid_token.yaml", nil, &InvalidTokenError{}},
		{"Invalid trainStop should fail to load", "test_data/invalid_trainStop.yaml", nil, &InvalidTrainStopError{}},
		{"Minimal config", "test_data/minimal.yaml", &minimalConfig, nil},
		{"Minimal config with resolving", "test_data/minimal_with_hostname.yaml", &minimalConfigWithResolving, nil},
		{"Complete config", "test_data/complete.yaml", &completeConfig, nil},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			valid, err := LoadFile(tc.input)
			if tc.expectedError != nil {
				require.Error(t, err)
				assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError))
			} else {
				require.NoError(t, err)
			}
			assert.Equal(t, tc.expected, valid, "Invalid value")
		})
	}
}