add more tests

This commit is contained in:
grumbulon 2023-03-11 09:08:13 -05:00
parent b5302bdc8c
commit 6ecf051982
3 changed files with 88 additions and 3 deletions

View file

@ -22,7 +22,7 @@ func ReadConfig() (*Config, error) {
if data, err = os.ReadFile(filepath.Clean(defaultConfigPath)); err == nil {
if err = yaml.Unmarshal(data, &config); err != nil {
return &Config{}, fmt.Errorf("unable to unmarshal config file: %w", err)
return nil, fmt.Errorf("unable to unmarshal config file: %w", err)
}
return &config, nil
@ -30,11 +30,11 @@ func ReadConfig() (*Config, error) {
if data, err = os.ReadFile(filepath.Clean("./config.yaml")); err == nil {
if err = yaml.Unmarshal(data, &config); err != nil {
return &Config{}, fmt.Errorf("unable to unmarshal config file: %w", err)
return nil, fmt.Errorf("unable to unmarshal config file: %w", err)
}
return &config, nil
}
return &Config{}, fmt.Errorf("unable to read config file: %w", err)
return nil, fmt.Errorf("unable to read config file: %w", err)
}

View file

@ -0,0 +1,51 @@
package internal
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConfig(t *testing.T) {
testCases := []struct {
name string
fileContents string
}{
{
name: "Should fail to read config",
},
{
name: "Should read config file successfully",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.name == "Should read config file successfully" {
f, err := os.Create("config.yaml")
if err != nil {
assert.NotNil(t, err)
}
defer os.Remove(f.Name())
} else {
err := os.Remove("config.yaml")
if err != nil {
assert.NotNil(t, err)
}
}
config, err := ReadConfig()
if err != nil {
assert.NotNil(t, err)
}
switch tc.name {
case "Should read config file successfully":
assert.NotNil(t, config)
default:
assert.Nil(t, config)
}
})
}
}

34
internal/db/db_test.go Normal file
View file

@ -0,0 +1,34 @@
package db
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDB(t *testing.T) {
testCases := []struct {
name string
dbMode string
dbName string
}{
{
name: "Should fail to open db",
dbMode: "nothing",
dbName: "",
},
{
name: "Should open test db",
dbMode: "test",
dbName: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err, ok := InitDb(tc.dbName, tc.dbMode)
if err != nil && !ok {
assert.NotNil(t, err)
}
})
}
}