feat(postgres): added more tests
All checks were successful
Gitea Build Check / Build (push) Successful in 10m40s

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-24 14:28:09 +02:00
parent 57729359d2
commit 30fa03ef9a
4 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package utils
import (
"reflect"
"testing"
)
func TestGetOrDefault(t *testing.T) {
type args struct {
data map[string]any
key string
defaultVal any
}
tests := []struct {
name string
args args
want any
}{
{
name: "Test 1: Nil map",
args: args{
data: nil,
key: "key1",
defaultVal: "default",
},
want: "default",
},
{
name: "Test 2: Existing key",
args: args{
data: map[string]interface{}{
"key1": "value1",
"key2": "value2",
},
key: "key1",
defaultVal: "default",
},
want: "value1",
},
{
name: "Test 3: Non-existing key",
args: args{
data: map[string]interface{}{
"key1": "value1",
"key2": "value2",
},
key: "key3",
defaultVal: "default",
},
want: "default",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetOrDefault(tt.args.data, tt.args.key, tt.args.defaultVal); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetOrDefault() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -40,6 +40,8 @@ func (p *postgresqlConnection) Connect(_ context.Context, config models.Database
localSSL = "disable"
}
p.debug = config.Debug
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone)
var err error

View File

@ -0,0 +1,26 @@
package database
import (
"testing"
)
func TestNewPostgresqlConnection(t *testing.T) {
// Test
tests := []struct {
name string
want OtterSpace
}{
{
name: "Test 1: Create new postgresql connection",
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.want != NewPostgresqlConnection() {
} else {
t.Errorf("NewPostgresqlConnection() = %s", tt.want)
}
})
}
}

59
pkg/models/const_test.go Normal file
View File

@ -0,0 +1,59 @@
package models
import (
"reflect"
"testing"
)
func TestRating_Convert(t *testing.T) {
type args struct {
e621Rating string
}
tests := []struct {
name string
r *Rating
args args
want Rating
}{
{
name: "Test 1: NSFW Rating",
r: new(Rating),
args: args{
e621Rating: "e",
},
want: NSFW,
},
{
name: "Test 2: Questionable Rating",
r: new(Rating),
args: args{
e621Rating: "q",
},
want: Questionable,
},
{
name: "Test 3: SFW Rating",
r: new(Rating),
args: args{
e621Rating: "s",
},
want: SFW,
},
{
name: "Test 4: Unknown Rating",
r: new(Rating),
args: args{
e621Rating: "x",
},
want: Unknown,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.r.Convert(tt.args.e621Rating)
if !reflect.DeepEqual(*tt.r, tt.want) {
t.Errorf("Convert() = %v, want %v", *tt.r, tt.want)
}
})
}
}