TTime/backend/internal/config/config_test.go

69 lines
1.6 KiB
Go
Raw Normal View History

package config
import (
"os"
"testing"
)
func TestNewConfig(t *testing.T) {
c := NewConfig()
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)
}
}
func TestWriteConfig(t *testing.T) {
c := NewConfig()
err := WriteConfigToFile(c, "test.toml")
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
// Remove the file after the test
_ = os.Remove("test.toml")
}
func TestReadConfig(t *testing.T) {
c := NewConfig()
err := WriteConfigToFile(c, "test.toml")
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
c2, err := ReadConfigFromFile("test.toml")
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")
}