-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tests-settings-repo' into tests-repo
test: tests for settings repo
- Loading branch information
Showing
1 changed file
with
56 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |