Skip to content

Commit

Permalink
Feat/unittest (#20)
Browse files Browse the repository at this point in the history
* replace integration tests with unit tests

* update .gitignore

* remove queue and db setup from workflow

* adress review

* cleanup
  • Loading branch information
HermanPlay authored Jan 8, 2025
1 parent 332ca9d commit 96076d7
Show file tree
Hide file tree
Showing 14 changed files with 480 additions and 331 deletions.
42 changes: 21 additions & 21 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ jobs:

test:
runs-on: ubuntu-latest
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres:17
# Provide the password for postgres
env:
POSTGRES_DB: test-maxit
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
rabbitmq:
image: rabbitmq:3.13-management
ports:
- 5672:5672
# services:
# # Label used to access the service container
# postgres:
# # Docker Hub image
# image: postgres:17
# # Provide the password for postgres
# env:
# POSTGRES_DB: test-maxit
# POSTGRES_PASSWORD: postgres
# # Set health checks to wait until postgres has started
# options: >-
# --health-cmd pg_isready
# --health-interval 10s
# --health-timeout 5s
# --health-retries 5
# ports:
# - 5432:5432
# rabbitmq:
# image: rabbitmq:3.13-management
# ports:
# - 5672:5672
steps:
- uses: actions/checkout@v4

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
coverage*

logger/logs/*
**/logger/logs/
2 changes: 1 addition & 1 deletion internal/initialization/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func NewInitialization(cfg *config.Config) *Initialization {

// Services
userService := service.NewUserService(userRepository)
taskService := service.NewTaskService(cfg, taskRepository, submissionRepository)
taskService := service.NewTaskService(cfg, taskRepository)
queueService, err := service.NewQueueService(taskRepository, submissionRepository, queueRepository, conn, channel, cfg.BrokerConfig.QueueName, cfg.BrokerConfig.ResponseQueueName)
if err != nil {
log.Panicf("Failed to create queue service: %s", err.Error())
Expand Down
274 changes: 274 additions & 0 deletions internal/testutils/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
package testutils

import (
"time"

"github.com/mini-maxit/backend/package/domain/models"
"github.com/mini-maxit/backend/package/domain/schemas"
"gorm.io/gorm"
)

type MockUserRepository struct {
users map[string]*models.User
counter int64
}

func (ur *MockUserRepository) CreateUser(tx *gorm.DB, user *models.User) (int64, error) {
if tx == nil {
return 0, gorm.ErrInvalidDB
}
ur.users[user.Email] = user
ur.counter++
user.Id = ur.counter
return ur.counter, nil
}

func (ur *MockUserRepository) GetUser(tx *gorm.DB, userId int64) (*models.User, error) {
if tx == nil {
return nil, gorm.ErrInvalidDB
}
for _, user := range ur.users {
if user.Id == userId {
return user, nil
}
}
return nil, gorm.ErrRecordNotFound
}

func (ur *MockUserRepository) GetUserByEmail(tx *gorm.DB, email string) (*models.User, error) {
if tx == nil {
return nil, gorm.ErrInvalidDB
}
if user, ok := ur.users[email]; ok {
return user, nil
}
return nil, gorm.ErrRecordNotFound
}

func (ur *MockUserRepository) GetAllUsers(tx *gorm.DB, limit, offset, sort string) ([]models.User, error) {
if tx == nil {
return nil, gorm.ErrInvalidDB
}
users := make([]models.User, 0, len(ur.users))
for _, user := range ur.users {
users = append(users, *user)
}
return users, nil
}

func (ur *MockUserRepository) EditUser(tx *gorm.DB, user *schemas.User) error {
if tx == nil {
return gorm.ErrInvalidDB
}
var userModel *models.User
for _, u := range ur.users {
if u.Id == user.Id {
userModel = u
break
}
}
if userModel == nil {
return gorm.ErrRecordNotFound
}
userModel.Name = user.Name
userModel.Surname = user.Surname
userModel.Email = user.Email
userModel.Username = user.Username
return nil
}

func NewMockUserRepository() *MockUserRepository {
return &MockUserRepository{
users: make(map[string]*models.User),
counter: 0,
}
}

type MockSessionRepository struct {
sessions map[string]*models.Session
}

func (sr *MockSessionRepository) CreateSession(tx *gorm.DB, session *models.Session) error {
sr.sessions[session.Id] = session
return nil
}

func (sr *MockSessionRepository) GetSession(tx *gorm.DB, sessionId string) (*models.Session, error) {
if session, ok := sr.sessions[sessionId]; ok {
return session, nil
}
return nil, gorm.ErrRecordNotFound
}

func (sr *MockSessionRepository) GetSessionByUserId(tx *gorm.DB, userId int64) (*models.Session, error) {
for _, session := range sr.sessions {
if session.UserId == userId {
return session, nil
}
}
return nil, gorm.ErrRecordNotFound
}

func (sr *MockSessionRepository) UpdateExpiration(tx *gorm.DB, sessionId string, expires_at time.Time) error {
if session, ok := sr.sessions[sessionId]; ok {
session.ExpiresAt = expires_at
return nil
}
return gorm.ErrRecordNotFound
}

func (sr *MockSessionRepository) DeleteSession(tx *gorm.DB, sessionId string) error {
delete(sr.sessions, sessionId)
return nil
}

func NewMockSessionRepository() *MockSessionRepository {
return &MockSessionRepository{
sessions: make(map[string]*models.Session),
}
}

type MockTaskRepository struct {
tasks map[int64]*models.Task
counter int64
}

func (tr *MockTaskRepository) Create(tx *gorm.DB, task *models.Task) (int64, error) {
tr.counter++
task.Id = tr.counter
tr.tasks[task.Id] = task
return task.Id, nil
}

func (tr *MockTaskRepository) GetTask(tx *gorm.DB, taskId int64) (*models.Task, error) {
if task, ok := tr.tasks[taskId]; ok {
return task, nil
}
return nil, gorm.ErrRecordNotFound
}

func (tr *MockTaskRepository) GetAllTasks(tx *gorm.DB, limit, offset, sort string) ([]models.Task, error) {
tasks := make([]models.Task, 0, len(tr.tasks))
for _, task := range tr.tasks {
tasks = append(tasks, *task)
}
return tasks, nil
}

func (tr *MockTaskRepository) GetAllForUser(tx *gorm.DB, userId int64, limit, offset, sort string) ([]models.Task, error) {
panic("implement me")
// var tasks []models.Task
// for _, task := range tr.tasks {
// if task.CreatedBy == userId {
// tasks = append(tasks, *task)
// }
// }
// return tasks, nil
}

func (tr *MockTaskRepository) GetAllForGroup(tx *gorm.DB, groupId int64, limit, offset, sort string) ([]models.Task, error) {
panic("implement me")
// var tasks []models.Task
// for _, task := range tr.tasks {
// if task.GroupId == groupId {
// tasks = append(tasks, *task)
// }
// }
// return tasks, nil
}

func (tr *MockTaskRepository) GetTaskByTitle(tx *gorm.DB, title string) (*models.Task, error) {
for _, task := range tr.tasks {
if task.Title == title {
return task, nil
}
}
return nil, gorm.ErrRecordNotFound
}

func (tr *MockTaskRepository) GetTaskTimeLimits(tx *gorm.DB, taskId int64) ([]float64, error) {
panic("implement me")
// if task, ok := tr.tasks[taskId]; ok {
// return task.TimeLimits, nil
// }
// return nil, gorm.ErrRecordNotFound
}

func (tr *MockTaskRepository) GetTaskMemoryLimits(tx *gorm.DB, taskId int64) ([]float64, error) {
panic("implement me")
// if task, ok := tr.tasks[taskId]; ok {
// return task.MemoryLimits, nil
// }
// return nil, gorm.ErrRecordNotFound
}

func (tr *MockTaskRepository) Clear() {
tr.tasks = make(map[int64]*models.Task)
tr.counter = 0
}

func (tr *MockTaskRepository) UpdateTask(tx *gorm.DB, taskId int64, task *models.Task) error {
if _, ok := tr.tasks[taskId]; ok {
tr.tasks[taskId] = task
return nil
}
return gorm.ErrRecordNotFound
}

func NewMockTaskRepository() *MockTaskRepository {
return &MockTaskRepository{
tasks: make(map[int64]*models.Task),
counter: 0,
}
}

type MockSubmissionRepository struct {
submissions map[int64]*models.Submission
counter int64
}

func (sr *MockSubmissionRepository) CreateSubmission(tx *gorm.DB, submission *models.Submission) (int64, error) {
sr.counter++
submission.Id = sr.counter
sr.submissions[submission.Id] = submission
return submission.Id, nil
}

func (sr *MockSubmissionRepository) GetSubmission(tx *gorm.DB, submissionId int64) (*models.Submission, error) {
if submission, ok := sr.submissions[submissionId]; ok {
return submission, nil
}
return nil, gorm.ErrRecordNotFound
}

func (sr *MockSubmissionRepository) MarkSubmissionProcessing(tx *gorm.DB, submissionId int64) error {
if submission, ok := sr.submissions[submissionId]; ok {
submission.Status = "processing"
return nil
}
return gorm.ErrRecordNotFound
}

func (sr *MockSubmissionRepository) MarkSubmissionComplete(tx *gorm.DB, submissionId int64) error {
if submission, ok := sr.submissions[submissionId]; ok {
submission.Status = "completed"
return nil
}
return gorm.ErrRecordNotFound
}

func (sr *MockSubmissionRepository) MarkSubmissionFailed(tx *gorm.DB, submissionId int64, errorMsg string) error {
if submission, ok := sr.submissions[submissionId]; ok {
submission.Status = "failed"
submission.StatusMessage = errorMsg
return nil
}
return gorm.ErrRecordNotFound
}

func NewMockSubmissionRepository() *MockSubmissionRepository {
return &MockSubmissionRepository{
submissions: make(map[int64]*models.Submission),
counter: 0,
}
}
6 changes: 3 additions & 3 deletions package/repository/submission_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type SubmissionRepository interface {
GetSubmission(tx *gorm.DB, submissionId int64) (*models.Submission, error)
CreateSubmission(tx *gorm.DB, submission models.Submission) (int64, error)
CreateSubmission(tx *gorm.DB, submission *models.Submission) (int64, error)
MarkSubmissionProcessing(tx *gorm.DB, submissionId int64) error
MarkSubmissionComplete(tx *gorm.DB, submissionId int64) error
MarkSubmissionFailed(db *gorm.DB, submissionId int64, errorMsg string) error
Expand Down Expand Up @@ -228,8 +228,8 @@ func (us *SubmissionRepositoryImpl) GetAllForTaskStudent(tx *gorm.DB, taskId, st
return submissions, nil
}

func (us *SubmissionRepositoryImpl) CreateSubmission(tx *gorm.DB, submission models.Submission) (int64, error) {
err := tx.Create(&submission).Error
func (us *SubmissionRepositoryImpl) CreateSubmission(tx *gorm.DB, submission *models.Submission) (int64, error) {
err := tx.Create(submission).Error
if err != nil {
return 0, err
}
Expand Down
6 changes: 3 additions & 3 deletions package/repository/task_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type TaskRepository interface {
// Create creates a new empty task and returns the task ID
Create(tx *gorm.DB, task models.Task) (int64, error)
Create(tx *gorm.DB, task *models.Task) (int64, error)
GetTask(tx *gorm.DB, taskId int64) (*models.Task, error)
GetAllTasks(tx *gorm.DB, limit, offset, sort string) ([]models.Task, error)
GetAllForUser(tx *gorm.DB, userId int64, limit, offset, sort string) ([]models.Task, error)
Expand All @@ -22,8 +22,8 @@ type TaskRepository interface {
type TaskRepositoryImpl struct {
}

func (tr *TaskRepositoryImpl) Create(tx *gorm.DB, task models.Task) (int64, error) {
err := tx.Model(&models.Task{}).Create(&task).Error
func (tr *TaskRepositoryImpl) Create(tx *gorm.DB, task *models.Task) (int64, error) {
err := tx.Model(models.Task{}).Create(&task).Error
if err != nil {
return 0, err
}
Expand Down
Loading

0 comments on commit 96076d7

Please sign in to comment.