2022-01-13 20:51:33 +00:00
|
|
|
package sqlite3_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-30 19:33:11 +00:00
|
|
|
"regexp"
|
2022-01-13 20:51:33 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
2022-01-13 20:51:33 +00:00
|
|
|
|
2022-03-13 10:58:34 +00:00
|
|
|
"source.toby3d.me/toby3d/auth/internal/domain"
|
|
|
|
repository "source.toby3d.me/toby3d/auth/internal/session/repository/sqlite3"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/testing/sqltest"
|
2022-01-13 20:51:33 +00:00
|
|
|
)
|
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
//nolint: gochecknoglobals // slices cannot be contants
|
2022-02-17 16:14:37 +00:00
|
|
|
var tableColumns = []string{"created_at", "code", "data"}
|
2022-01-30 19:33:11 +00:00
|
|
|
|
2022-01-13 20:51:33 +00:00
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
session := domain.TestSession(t)
|
2022-02-17 21:46:31 +00:00
|
|
|
session.Profile = nil
|
2022-02-17 16:14:37 +00:00
|
|
|
|
|
|
|
model, err := repository.NewSession(session)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
db, mock, cleanup := sqltest.Open(t)
|
|
|
|
t.Cleanup(cleanup)
|
2022-01-13 20:51:33 +00:00
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
createTable(t, mock)
|
|
|
|
mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO sessions`)).
|
|
|
|
WithArgs(
|
|
|
|
sqltest.Time{},
|
|
|
|
model.Code,
|
2022-02-17 16:14:37 +00:00
|
|
|
model.Data,
|
2022-01-30 19:33:11 +00:00
|
|
|
).
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
if err := repository.NewSQLite3SessionRepository(db).
|
2022-06-09 19:14:54 +00:00
|
|
|
Create(context.Background(), session); err != nil {
|
2022-01-30 19:33:11 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-01-13 20:51:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:56:27 +00:00
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
session := domain.TestSession(t)
|
2022-02-17 21:46:31 +00:00
|
|
|
session.Profile = nil
|
2022-02-17 16:14:37 +00:00
|
|
|
|
|
|
|
model, err := repository.NewSession(session)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
db, mock, cleanup := sqltest.Open(t)
|
2022-01-29 19:56:27 +00:00
|
|
|
t.Cleanup(cleanup)
|
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
createTable(t, mock)
|
|
|
|
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM sessions`)).
|
|
|
|
WithArgs(session.Code).
|
|
|
|
WillReturnRows(sqlmock.NewRows(tableColumns).
|
|
|
|
AddRow(
|
|
|
|
model.CreatedAt.Time,
|
|
|
|
model.Code,
|
2022-02-17 16:14:37 +00:00
|
|
|
model.Data,
|
2022-01-30 19:33:11 +00:00
|
|
|
))
|
2022-01-29 19:56:27 +00:00
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
result, err := repository.NewSQLite3SessionRepository(db).
|
2022-06-09 19:14:54 +00:00
|
|
|
Get(context.Background(), session.Code)
|
2022-01-30 19:33:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Code != session.Code {
|
|
|
|
t.Errorf("Get(%s) = %+v, want %+v", session.Code, result, session)
|
|
|
|
}
|
2022-01-29 19:56:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 20:51:33 +00:00
|
|
|
func TestGetAndDelete(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
session := domain.TestSession(t)
|
2022-02-17 21:46:31 +00:00
|
|
|
session.Profile = nil
|
2022-02-17 16:14:37 +00:00
|
|
|
|
|
|
|
model, err := repository.NewSession(session)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
db, mock, cleanup := sqltest.Open(t)
|
2022-01-13 20:51:33 +00:00
|
|
|
t.Cleanup(cleanup)
|
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
createTable(t, mock)
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM sessions`)).
|
|
|
|
WithArgs(session.Code).
|
|
|
|
WillReturnRows(sqlmock.NewRows(tableColumns).
|
|
|
|
AddRow(
|
|
|
|
model.CreatedAt.Time,
|
|
|
|
model.Code,
|
2022-02-17 16:14:37 +00:00
|
|
|
model.Data,
|
2022-01-30 19:33:11 +00:00
|
|
|
))
|
|
|
|
mock.ExpectExec(regexp.QuoteMeta(`DELETE FROM sessions`)).
|
|
|
|
WithArgs(model.Code).
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
mock.ExpectCommit()
|
2022-01-13 20:51:33 +00:00
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
result, err := repository.NewSQLite3SessionRepository(db).
|
2022-06-09 19:14:54 +00:00
|
|
|
GetAndDelete(context.Background(), session.Code)
|
2022-01-30 19:33:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Code != session.Code {
|
|
|
|
t.Errorf("GetAndDelete(%s) = %+v, want %+v", session.Code, result, session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTable(tb testing.TB, mock sqlmock.Sqlmock) {
|
|
|
|
tb.Helper()
|
2022-01-29 19:56:27 +00:00
|
|
|
|
2022-01-30 19:33:11 +00:00
|
|
|
mock.ExpectExec(regexp.QuoteMeta(repository.QueryTable)).
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
2022-01-13 20:51:33 +00:00
|
|
|
}
|