filebrowser/backend/settings/settings_test.go

52 lines
1.4 KiB
Go
Raw Normal View History

2023-09-03 00:16:19 +00:00
package settings
import (
2025-01-21 14:02:43 +00:00
"fmt"
2023-09-03 00:16:19 +00:00
"testing"
2023-09-03 17:28:00 +00:00
2025-02-16 14:07:38 +00:00
yaml "github.com/goccy/go-yaml"
2023-09-03 17:28:00 +00:00
"github.com/google/go-cmp/cmp"
2025-01-21 14:02:43 +00:00
"github.com/gtsteffaniak/filebrowser/backend/logger"
2023-09-03 00:16:19 +00:00
)
2023-09-03 17:28:00 +00:00
func TestConfigLoadChanged(t *testing.T) {
2025-01-21 14:02:43 +00:00
yamlData, _ := loadConfigFile("./testingConfig.yaml")
2023-09-03 17:28:00 +00:00
// Marshal the YAML data to a more human-readable format
newConfig := setDefaults()
Config := setDefaults()
2023-09-03 17:28:00 +00:00
err := yaml.Unmarshal(yamlData, &newConfig)
if err != nil {
2025-01-21 14:02:43 +00:00
logger.Fatal(fmt.Sprintf("Error unmarshaling YAML data: %v", err))
2023-09-03 17:28:00 +00:00
}
// Use go-cmp to compare the two structs
if diff := cmp.Diff(newConfig, Config); diff == "" {
2023-09-03 17:28:00 +00:00
t.Errorf("No change when there should have been (-want +got):\n%s", diff)
}
}
func TestConfigLoadSpecificValues(t *testing.T) {
2025-01-21 14:02:43 +00:00
yamlData, _ := loadConfigFile("./testingConfig.yaml")
2023-09-03 17:28:00 +00:00
// Marshal the YAML data to a more human-readable format
newConfig := setDefaults()
Config := setDefaults()
2023-09-03 17:28:00 +00:00
err := yaml.Unmarshal(yamlData, &newConfig)
if err != nil {
2025-01-21 14:02:43 +00:00
logger.Fatal(fmt.Sprintf("Error unmarshaling YAML data: %v", err))
2023-09-03 17:28:00 +00:00
}
testCases := []struct {
fieldName string
globalVal interface{}
newVal interface{}
}{
{"Server.Database", Config.Server.Database, newConfig.Server.Database},
2023-09-03 17:28:00 +00:00
}
for _, tc := range testCases {
if tc.globalVal == tc.newVal {
2025-01-05 19:05:33 +00:00
t.Errorf("Differences should have been found:\nConfig.%s: %v \nSetConfig: %v \n", tc.fieldName, tc.globalVal, tc.newVal)
}
2023-09-03 17:28:00 +00:00
}
2023-09-03 00:16:19 +00:00
}