|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseConfig(t *testing.T) { |
| 8 | + _, err := Parse("testdata/valid-config.yml") |
| 9 | + if err != nil { |
| 10 | + t.Errorf("Error loading config: %v", err) |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +func TestParseBadConfigs(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + ConfigFile string |
| 17 | + ExpectedError string |
| 18 | + }{ |
| 19 | + { |
| 20 | + ConfigFile: "testdata/unknown-config.yml", |
| 21 | + ExpectedError: "failed to read config file 'testdata/unknown-config.yml': open testdata/unknown-config.yml: no such file or directory", |
| 22 | + }, |
| 23 | + { |
| 24 | + ConfigFile: "testdata/invalid-duplicated-config.yml", |
| 25 | + ExpectedError: "failed to validate configuration. A database named 'dba' has already been declared", |
| 26 | + }, |
| 27 | + { |
| 28 | + ConfigFile: "testdata/invalid-empty-name-config.yml", |
| 29 | + ExpectedError: "failed to validate configuration. Database name cannot be empty", |
| 30 | + }, |
| 31 | + { |
| 32 | + ConfigFile: "testdata/invalid-empty-url-config.yml", |
| 33 | + ExpectedError: "failed to validate configuration. URL for database 'dba' cannot be empty", |
| 34 | + }, |
| 35 | + { |
| 36 | + ConfigFile: "testdata/invalid-yaml-config.yml", |
| 37 | + ExpectedError: "failed to unmarshall config file 'testdata/invalid-yaml-config.yml': yaml: unmarshal errors:\n line 1: cannot unmarshal !!seq into config.Config", |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + for _, test := range tests { |
| 42 | + _, err := Parse(test.ConfigFile) |
| 43 | + if err == nil { |
| 44 | + t.Errorf("In case %v:\nExpected: %v\nGot: nil", test.ConfigFile, test.ExpectedError) |
| 45 | + continue |
| 46 | + } |
| 47 | + if err.Error() != test.ExpectedError { |
| 48 | + t.Errorf("In case %v:\nExpected: %v\nGot: %v", test.ConfigFile, test.ExpectedError, err.Error()) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments