Skip to content

Commit

Permalink
Merge branch 'tests-settings-repo' into tests-repo
Browse files Browse the repository at this point in the history
test: tests for settings repo
  • Loading branch information
rmnvlv committed Apr 29, 2022
2 parents f249446 + 3ff9fd2 commit 5ef9736
Showing 1 changed file with 56 additions and 42 deletions.
98 changes: 56 additions & 42 deletions internal/repository/postgres/settings_postgres_test.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,78 @@
package postgres_test

import (
"borda/internal/repository"
"borda/internal/repository/postgres"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_NewSettingsRepository(t *testing.T) {
db := MustOpenDB(t)

settingsRepository := postgres.NewSettingsRepository(db)

require.Implements(t, (*repository.SettingsRepository)(nil), settingsRepository)
}

func Test_SettingsRepository_Set(t *testing.T) {
func TestSettingsRepository_Get(t *testing.T) {
db := MustOpenDB(t)
repo := postgres.NewSettingsRepository(db)
assert := assert.New(t)

key := "team_limit"
value := "5"
settingsId := 1
require := require.New(t)

id, err := repo.Set(key, value)
if err != nil {
t.Fatalf("Set error: %v\n", err)
type args struct {
key string
}
testTable := []struct {
name string
args args
wantResponse string
wantErr error
}{
// TODO: Add test cases.
{
name: "OK",
args: args{
key: "team_limit",
},
wantResponse: "4",
wantErr: nil,
},
}
for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
actualResponse, actualErr := repo.Get(testCase.args.key)

assert.Equal(id, settingsId, "they should be equal")
assert.NotNil(id, "must be not nil")
require.Equal(testCase.wantErr, actualErr, t)
require.Equal(testCase.wantResponse, actualResponse, t)
})
}
}

func Test_SettingsRepository_Get(t *testing.T) {
func TestSettingsRepository_Set(t *testing.T) {
db := MustOpenDB(t)
repo := postgres.NewSettingsRepository(db)
assert := assert.New(t)

key := "team_limit"
value := "5"

_, err := repo.Set(key, value)
if err != nil {
t.Fatalf("Test settings asserted not created: %v\n", err)
require := require.New(t)

type args struct {
key string
value string
}
testTable := []struct {
name string
args args
wantResponse int
wantErr error
}{
// TODO: Add test cases.
{
name: "OK",
args: args{
key: "TEST",
value: "1337",
},
wantResponse: 2,
wantErr: nil,
},
}
for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
actualResponse, actualErr := repo.Set(testCase.args.key, testCase.args.value)

testValue, err := repo.Get(key)
// if err != nil {
// t.Fatalf("Test settings asserted not created: %v\n", err)
// }

assert.Error(err, "error should be nil")
assert.Equal(value, testValue, fmt.Sprintf("value should be equal <%v>, not <%v>", value, testValue))

_, err = repo.Get("1337")
assert.Error(err, "Settings not found with key=1337", "they should be equal")

require.Equal(testCase.wantErr, actualErr, t)
require.Equal(testCase.wantResponse, actualResponse, t)
})
}
}

0 comments on commit 5ef9736

Please sign in to comment.