From 51d0da9b45eda4a3eb9f94baf7411651e3cf565f Mon Sep 17 00:00:00 2001 From: codermuss Date: Mon, 29 Jul 2024 22:35:36 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20improve=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/mock/store.go | 14 ++++++++++++++ db/query/sessions.sql | 6 +++--- db/sqlc/categories_test.go | 12 ++++++++++++ db/sqlc/querier.go | 6 ++++++ db/sqlc/sessions.sql.go | 16 ++++++++++++++++ db/sqlc/sessions_test.go | 17 +++++++++++++++-- db/sqlc/tags_test.go | 11 +++++++++++ 7 files changed, 77 insertions(+), 5 deletions(-) diff --git a/db/mock/store.go b/db/mock/store.go index f1230a8..d96b44f 100644 --- a/db/mock/store.go +++ b/db/mock/store.go @@ -148,6 +148,20 @@ func (mr *MockStoreMockRecorder) DeleteProfile(arg0, arg1 interface{}) *gomock.C return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteProfile", reflect.TypeOf((*MockStore)(nil).DeleteProfile), arg0, arg1) } +// DeleteSession mocks base method. +func (m *MockStore) DeleteSession(arg0 context.Context, arg1 uuid.UUID) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteSession", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteSession indicates an expected call of DeleteSession. +func (mr *MockStoreMockRecorder) DeleteSession(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteSession", reflect.TypeOf((*MockStore)(nil).DeleteSession), arg0, arg1) +} + // DeleteTag mocks base method. func (m *MockStore) DeleteTag(arg0 context.Context, arg1 int32) error { m.ctrl.T.Helper() diff --git a/db/query/sessions.sql b/db/query/sessions.sql index f23ee8d..3a79c24 100644 --- a/db/query/sessions.sql +++ b/db/query/sessions.sql @@ -14,6 +14,6 @@ WHERE id = $1; -- WHERE id = $8 -- RETURNING *; --- -- name: DeleteSession :exec --- DELETE FROM sessions --- WHERE id = $1; \ No newline at end of file +-- name: DeleteSession :exec +DELETE FROM sessions +WHERE id = $1; \ No newline at end of file diff --git a/db/sqlc/categories_test.go b/db/sqlc/categories_test.go index 08628d4..f3e2cfd 100644 --- a/db/sqlc/categories_test.go +++ b/db/sqlc/categories_test.go @@ -33,6 +33,18 @@ func TestGetCategory(t *testing.T) { } +func TestGetCategories(t *testing.T) { + createRandomCategory(t) + createRandomCategory(t) + createRandomCategory(t) + + categories, err := testStore.GetCategories(context.Background()) + require.NoError(t, err) + require.NotEmpty(t, categories) + require.True(t, len(categories) > 2) + +} + func TestUpdateCategory(t *testing.T) { randomcategory := createRandomCategory(t) newCategoryName := util.RandomString(6) diff --git a/db/sqlc/querier.go b/db/sqlc/querier.go index 8333cc0..a421a6d 100644 --- a/db/sqlc/querier.go +++ b/db/sqlc/querier.go @@ -19,6 +19,12 @@ type Querier interface { DeletePostCategory(ctx context.Context, arg DeletePostCategoryParams) error DeletePostTag(ctx context.Context, arg DeletePostTagParams) error DeleteProfile(ctx context.Context, userID int32) error + // -- name: UpdateSession :one + // UPDATE sessions + // SET user_id = $1, refresh_token = $2, user_agent = $3, client_ip = $4, is_blocked = $5, expires_at = $6, created_at = $7 + // WHERE id = $8 + // RETURNING *; + DeleteSession(ctx context.Context, id uuid.UUID) error DeleteTag(ctx context.Context, id int32) error DeleteUser(ctx context.Context, id int32) error DeleteUserFollower(ctx context.Context, arg DeleteUserFollowerParams) error diff --git a/db/sqlc/sessions.sql.go b/db/sqlc/sessions.sql.go index 3791e39..8fc5c95 100644 --- a/db/sqlc/sessions.sql.go +++ b/db/sqlc/sessions.sql.go @@ -12,6 +12,22 @@ import ( "github.com/google/uuid" ) +const deleteSession = `-- name: DeleteSession :exec + +DELETE FROM sessions +WHERE id = $1 +` + +// -- name: UpdateSession :one +// UPDATE sessions +// SET user_id = $1, refresh_token = $2, user_agent = $3, client_ip = $4, is_blocked = $5, expires_at = $6, created_at = $7 +// WHERE id = $8 +// RETURNING *; +func (q *Queries) DeleteSession(ctx context.Context, id uuid.UUID) error { + _, err := q.db.Exec(ctx, deleteSession, id) + return err +} + const getSession = `-- name: GetSession :one SELECT id, user_id, refresh_token, user_agent, client_ip, is_blocked, expires_at, created_at FROM sessions diff --git a/db/sqlc/sessions_test.go b/db/sqlc/sessions_test.go index 4cd67d9..6742469 100644 --- a/db/sqlc/sessions_test.go +++ b/db/sqlc/sessions_test.go @@ -35,7 +35,20 @@ func TestCreateCreateSession(t *testing.T) { func TestGetSession(t *testing.T) { session := createSession(t) - comments, err := testStore.GetSession(context.Background(), session.ID) + takenSession, err := testStore.GetSession(context.Background(), session.ID) require.NoError(t, err) - require.NotEmpty(t, comments) + require.NotEmpty(t, takenSession) +} + +func TestDeleteSession(t *testing.T) { + session := createSession(t) + takenSession, err := testStore.GetSession(context.Background(), session.ID) + require.NoError(t, err) + require.NotEmpty(t, takenSession) + err = testStore.DeleteSession(context.Background(), session.ID) + require.NoError(t, err) + deletedSession, err := testStore.GetSession(context.Background(), session.ID) + require.Error(t, err) + require.ErrorIs(t, err, ErrRecordNotFound) + require.Empty(t, deletedSession) } diff --git a/db/sqlc/tags_test.go b/db/sqlc/tags_test.go index 1c45f7b..4290cdf 100644 --- a/db/sqlc/tags_test.go +++ b/db/sqlc/tags_test.go @@ -31,6 +31,17 @@ func TestGetTag(t *testing.T) { require.Equal(t, randomtag.ID, tag.ID) require.Equal(t, randomtag.Name, tag.Name) +} +func TestGetTags(t *testing.T) { + createRandomTag(t) + createRandomTag(t) + createRandomTag(t) + + tags, err := testStore.GetTags(context.Background()) + require.NoError(t, err) + require.NotEmpty(t, tags) + require.True(t, len(tags) > 2) + } func TestUpdateTag(t *testing.T) {