-
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.
- Loading branch information
1 parent
bf6e3f6
commit b2d8f28
Showing
10 changed files
with
479 additions
and
14 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
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
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
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
74 changes: 74 additions & 0 deletions
74
internal/auth/repository/auth/tests/check_credentials_test.go
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/jackc/pgx/v5" | ||
"github.com/pashagolub/pgxmock/v4" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"kudago/internal/auth/repository/auth" | ||
"testing" | ||
|
||
"kudago/internal/models" | ||
) | ||
|
||
func TestUserDB_CheckCredentials(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
|
||
tests := []struct { | ||
name string | ||
username string | ||
password string | ||
mockSetup func(m pgxmock.PgxConnIface) | ||
expectedUser models.User | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "Пользователь не найден", | ||
username: "testuser", | ||
password: "wrongpassword", | ||
mockSetup: func(m pgxmock.PgxConnIface) { | ||
m.ExpectQuery(`SELECT id, username, email, created_at, url_to_avatar`). | ||
WithArgs("testuser", "wrongpassword"). | ||
WillReturnError(pgx.ErrNoRows) | ||
}, | ||
expectedUser: models.User{}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "Ошибка при выполнении запроса", | ||
username: "testuser", | ||
password: "password123", | ||
mockSetup: func(m pgxmock.PgxConnIface) { | ||
m.ExpectQuery(`SELECT id, username, email, created_at, url_to_avatar`). | ||
WithArgs("testuser", "password123"). | ||
WillReturnError(fmt.Errorf("database error")) | ||
}, | ||
expectedUser: models.User{}, | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
mockConn, err := pgxmock.NewConn() | ||
require.NoError(t, err) | ||
defer mockConn.Close(ctx) | ||
tt.mockSetup(mockConn) | ||
db := userRepository.UserDB{Pool: mockConn} | ||
user, err := db.CheckCredentials(ctx, tt.username, tt.password) | ||
|
||
if tt.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expectedUser, user) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/pashagolub/pgxmock/v4" | ||
"kudago/internal/auth/repository/auth" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"kudago/internal/models" | ||
) | ||
|
||
func TestUserDB_CreateUser(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
|
||
tests := []struct { | ||
name string | ||
user models.User | ||
mockSetup func(m pgxmock.PgxConnIface) | ||
expectedUser models.User | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "Успешное создание пользователя", | ||
user: models.User{ | ||
Username: "newuser", | ||
Email: "[email protected]", | ||
Password: "password123", | ||
ImageURL: "http://example.com/avatar.jpg", | ||
}, | ||
mockSetup: func(m pgxmock.PgxConnIface) { | ||
m.ExpectQuery(`INSERT INTO "USER" \(username, email, password_hash, url_to_avatar\)`). | ||
WithArgs("newuser", "[email protected]", "password123", "http://example.com/avatar.jpg"). | ||
WillReturnRows(pgxmock.NewRows([]string{"id", "created_at"}). | ||
AddRow(1, time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC))) | ||
}, | ||
expectedUser: models.User{ | ||
ID: 1, | ||
Username: "newuser", | ||
Email: "[email protected]", | ||
Password: "", | ||
ImageURL: "http://example.com/avatar.jpg", | ||
}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "Ошибка при создании пользователя", | ||
user: models.User{ | ||
Username: "newuser", | ||
Email: "[email protected]", | ||
Password: "password123", | ||
ImageURL: "http://example.com/avatar.jpg", | ||
}, | ||
mockSetup: func(m pgxmock.PgxConnIface) { | ||
// Мокируем ошибку при выполнении запроса | ||
m.ExpectQuery(`INSERT INTO "USER" \(username, email, password_hash, url_to_avatar\)`). | ||
WithArgs("newuser", "[email protected]", "password123", "http://example.com/avatar.jpg"). | ||
WillReturnError(fmt.Errorf("database error")) | ||
}, | ||
expectedUser: models.User{}, | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Создание мокированного соединения | ||
mockConn, err := pgxmock.NewConn() | ||
require.NoError(t, err) | ||
defer mockConn.Close(ctx) | ||
|
||
// Настройка моков | ||
tt.mockSetup(mockConn) | ||
|
||
// Инициализация репозитория с мокированным соединением | ||
db := userRepository.UserDB{Pool: mockConn} | ||
|
||
// Вызов функции | ||
user, err := db.CreateUser(ctx, tt.user) | ||
|
||
// Проверка ошибок | ||
if tt.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expectedUser, user) | ||
} | ||
}) | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
internal/auth/repository/auth/tests/get_user_by_email_or_username_test.go
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/jackc/pgx/v5" | ||
"github.com/pashagolub/pgxmock/v4" | ||
"kudago/internal/auth/repository/auth" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"kudago/internal/models" | ||
) | ||
|
||
func TestUserDB_UserExists(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
|
||
tests := []struct { | ||
name string | ||
user models.User | ||
mockSetup func(m pgxmock.PgxConnIface) | ||
expected bool | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "Пользователь существует по username", | ||
user: models.User{ | ||
Username: "existinguser", | ||
Email: "[email protected]", | ||
}, | ||
mockSetup: func(m pgxmock.PgxConnIface) { | ||
m.ExpectQuery(`SELECT id FROM "USER" WHERE \(username = \$1 OR email = \$2\)`). | ||
WithArgs("existinguser", "[email protected]"). | ||
WillReturnRows(pgxmock.NewRows([]string{"id"}).AddRow(1)) | ||
}, | ||
expected: true, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "Пользователь не существует", | ||
user: models.User{ | ||
Username: "nonexistinguser", | ||
Email: "[email protected]", | ||
}, | ||
mockSetup: func(m pgxmock.PgxConnIface) { | ||
m.ExpectQuery(`SELECT id FROM "USER" WHERE \(username = \$1 OR email = \$2\)`). | ||
WithArgs("nonexistinguser", "[email protected]"). | ||
WillReturnError(pgx.ErrNoRows) | ||
}, | ||
expected: false, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "Ошибка при выполнении запроса", | ||
user: models.User{ | ||
Username: "user_with_error", | ||
Email: "[email protected]", | ||
}, | ||
mockSetup: func(m pgxmock.PgxConnIface) { | ||
m.ExpectQuery(`SELECT id FROM "USER" WHERE \(username = \$1 OR email = \$2\)`). | ||
WithArgs("user_with_error", "[email protected]"). | ||
WillReturnError(fmt.Errorf("database error")) | ||
}, | ||
expected: false, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "Пользователь существует, но исключаем его по ID", | ||
user: models.User{ | ||
ID: 2, | ||
Username: "existinguser", | ||
Email: "[email protected]", | ||
}, | ||
mockSetup: func(m pgxmock.PgxConnIface) { | ||
m.ExpectQuery(`SELECT id FROM "USER" WHERE \(username = \$1 OR email = \$2\) AND id != \$3`). | ||
WithArgs("existinguser", "[email protected]", 2). | ||
WillReturnRows(pgxmock.NewRows([]string{"id"}).AddRow(1)) | ||
}, | ||
expected: true, | ||
expectErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
mockConn, err := pgxmock.NewConn() | ||
require.NoError(t, err) | ||
defer mockConn.Close(ctx) | ||
|
||
tt.mockSetup(mockConn) | ||
|
||
db := userRepository.UserDB{Pool: mockConn} | ||
|
||
exists, err := db.UserExists(ctx, tt.user) | ||
|
||
if tt.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, exists) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.