Skip to content

Commit

Permalink
Merge pull request #93 from oj-lab/akamya/test-judge-service
Browse files Browse the repository at this point in the history
test: judge service
  • Loading branch information
slhmy authored Jul 27, 2024
2 parents a384832 + a116a65 commit 321feb1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ require (
gorm.io/gorm v1.25.10
)

require github.com/swaggo/swag v1.16.3
require (
github.com/stretchr/testify v1.9.0
github.com/swaggo/swag v1.16.3
)

require (
github.com/ClickHouse/ch-go v0.61.5 // indirect
Expand All @@ -20,6 +23,7 @@ require (
github.com/casbin/govaluate v1.1.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/glebarez/go-sqlite v1.20.3 // indirect
github.com/glebarez/sqlite v1.7.0 // indirect
github.com/go-faster/city v1.0.1 // indirect
Expand All @@ -31,6 +35,7 @@ require (
github.com/paulmach/orb v0.11.1 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand Down
68 changes: 68 additions & 0 deletions services/judge/judge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package judge_service

import (
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

"github.com/gin-gonic/gin"
"github.com/oj-lab/oj-lab-platform/models"
judge_model "github.com/oj-lab/oj-lab-platform/models/judge"
problem_model "github.com/oj-lab/oj-lab-platform/models/problem"
gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
)

func TestGetJudge(t *testing.T) {
db := gorm_agent.GetDefaultDB()
problem := &problem_model.Problem{
Slug: "test-judge-service",
}
var err error
err = problem_model.CreateProblem(db, *problem)
if err != nil {
t.Error(err)
}
judge := &judge_model.Judge{
Language: judge_model.ProgrammingLanguageCpp,
ProblemSlug: problem.Slug,
}
judge, err = judge_model.CreateJudge(db, *judge)
if err != nil {
t.Error(err)
}

ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
response_judge, err := GetJudge(ctx, judge.UID)
if err != nil {
t.Error(err)
}
asserts := assert.New(t)
asserts.Equal(judge.ProblemSlug, response_judge.ProblemSlug)
asserts.Equal(judge.Language, response_judge.Language)
}

func TestCreateJudge(t *testing.T) {
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
judge := &judge_model.Judge{
Language: judge_model.ProgrammingLanguageCpp,
ProblemSlug: "test-judge-service",
}
_, err := CreateJudge(ctx, *judge)
if err != nil {
t.Error(err)
}

db := gorm_agent.GetDefaultDB()
judges, _, err := judge_model.GetJudgeListByOptions(db, judge_model.GetJudgeOptions{OrderByColumns: []models.OrderByColumnOption{{Column: "create_at", Desc: true}}})
if err != nil || len(judges) == 0 {
t.Error(err)
}
insert_judge, err := judge_model.GetJudge(db, judges[0].UID)
if err != nil {
t.Error(err)
}
asserts := assert.New(t)
asserts.Equal(judge.ProblemSlug, insert_judge.ProblemSlug)
asserts.Equal(judge.Language, insert_judge.Language)
}

0 comments on commit 321feb1

Please sign in to comment.