TTime/backend/internal/config/config_test.go
2024-03-20 14:06:15 +01:00

87 lines
1.9 KiB
Go

package config
import (
"os"
"testing"
)
// TestNewConfig tests the creation of a new configuration object
func TestNewConfig(t *testing.T) {
// Arrange
c := NewConfig()
// Act & Assert
if c.Port != 8080 {
t.Errorf("Expected port to be 8080, got %d", c.Port)
}
if c.DbPath != "./db.sqlite3" {
t.Errorf("Expected db path to be ./db.sqlite3, got %s", c.DbPath)
}
if c.DbUser != "username" {
t.Errorf("Expected db user to be username, got %s", c.DbUser)
}
if c.DbPass != "password" {
t.Errorf("Expected db pass to be password, got %s", c.DbPass)
}
if c.DbName != "ttime" {
t.Errorf("Expected db name to be ttime, got %s", c.DbName)
}
}
// TestWriteConfig tests the function to write the configuration to a file
func TestWriteConfig(t *testing.T) {
// Arrange
c := NewConfig()
//Act
err := c.WriteConfigToFile("test.toml")
// Assert
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
// Remove the file after the test
_ = os.Remove("test.toml")
}
// TestReadConfig tests the function to read the configuration from a file
func TestReadConfig(t *testing.T) {
// Arrange
c := NewConfig()
// Act
err := c.WriteConfigToFile("test.toml")
// Assert
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
// Act
c2, err := ReadConfigFromFile("test.toml")
// Assert
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if c.Port != c2.Port {
t.Errorf("Expected port to be %d, got %d", c.Port, c2.Port)
}
if c.DbPath != c2.DbPath {
t.Errorf("Expected db path to be %s, got %s", c.DbPath, c2.DbPath)
}
if c.DbUser != c2.DbUser {
t.Errorf("Expected db user to be %s, got %s", c.DbUser, c2.DbUser)
}
if c.DbPass != c2.DbPass {
t.Errorf("Expected db pass to be %s, got %s", c.DbPass, c2.DbPass)
}
if c.DbName != c2.DbName {
t.Errorf("Expected db name to be %s, got %s", c.DbName, c2.DbName)
}
// Remove the file after the test
_ = os.Remove("test.toml")
}