initial commit
This commit is contained in:
108
internal/repository/postgres/auth/test_helper.go
Normal file
108
internal/repository/postgres/auth/test_helper.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
domainAuth "base/internal/domain/auth"
|
||||
)
|
||||
|
||||
// setupTestDB creates an in-memory SQLite database for testing
|
||||
func setupTestDB(t *testing.T) *gorm.DB {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create tables manually with SQLite-compatible syntax
|
||||
// This avoids PostgreSQL-specific syntax like gen_random_uuid() and timestamptz
|
||||
|
||||
createUsersTable := `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY,
|
||||
first_name TEXT NOT NULL,
|
||||
last_name TEXT NOT NULL,
|
||||
display_name TEXT NOT NULL,
|
||||
phone_number TEXT,
|
||||
email TEXT NOT NULL,
|
||||
email_verified INTEGER NOT NULL DEFAULT 0,
|
||||
status INTEGER NOT NULL DEFAULT 0,
|
||||
invitation_code TEXT,
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL,
|
||||
deleted_at DATETIME,
|
||||
UNIQUE(email)
|
||||
)
|
||||
`
|
||||
require.NoError(t, db.Exec(createUsersTable).Error)
|
||||
|
||||
createRolesTable := `
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL,
|
||||
deleted_at DATETIME,
|
||||
UNIQUE(name)
|
||||
)
|
||||
`
|
||||
require.NoError(t, db.Exec(createRolesTable).Error)
|
||||
|
||||
createAccountsTable := `
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL,
|
||||
provider INTEGER,
|
||||
password TEXT,
|
||||
access_token TEXT,
|
||||
refresh_token TEXT,
|
||||
scope TEXT,
|
||||
meta TEXT,
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL
|
||||
)
|
||||
`
|
||||
require.NoError(t, db.Exec(createAccountsTable).Error)
|
||||
require.NoError(t, db.Exec("CREATE INDEX IF NOT EXISTS accounts_user_id_idx ON accounts(user_id)").Error)
|
||||
require.NoError(t, db.Exec("CREATE INDEX IF NOT EXISTS accounts_provider_idx ON accounts(provider)").Error)
|
||||
|
||||
createUserRolesTable := `
|
||||
CREATE TABLE IF NOT EXISTS user_roles (
|
||||
user_id TEXT NOT NULL,
|
||||
role_id TEXT NOT NULL,
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL,
|
||||
deleted_at DATETIME,
|
||||
PRIMARY KEY (user_id, role_id)
|
||||
)
|
||||
`
|
||||
require.NoError(t, db.Exec(createUserRolesTable).Error)
|
||||
require.NoError(t, db.Exec("CREATE INDEX IF NOT EXISTS user_roles_user_id_idx ON user_roles(user_id)").Error)
|
||||
require.NoError(t, db.Exec("CREATE INDEX IF NOT EXISTS user_roles_role_id_idx ON user_roles(role_id)").Error)
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
// createTestUserRepository creates a user repository for testing
|
||||
func createTestUserRepository(db *gorm.DB) domainAuth.UserRepository {
|
||||
return &userRepository{db: db}
|
||||
}
|
||||
|
||||
// createTestRoleRepository creates a role repository for testing
|
||||
func createTestRoleRepository(db *gorm.DB) domainAuth.RoleRepository {
|
||||
return &roleRepository{db: db}
|
||||
}
|
||||
|
||||
// createTestAccountRepository creates an account repository for testing
|
||||
func createTestAccountRepository(db *gorm.DB) domainAuth.AccountRepository {
|
||||
return &accountRepository{db: db}
|
||||
}
|
||||
|
||||
// createTestUserRoleRepository creates a user role repository for testing
|
||||
func createTestUserRoleRepository(db *gorm.DB) domainAuth.UserRoleRepository {
|
||||
return &userRoleRepository{db: db}
|
||||
}
|
||||
Reference in New Issue
Block a user