diff --git a/go.mod b/go.mod index d6f641e..2dbc986 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/services/judge/judge_test.go b/services/judge/judge_test.go new file mode 100644 index 0000000..7c38d85 --- /dev/null +++ b/services/judge/judge_test.go @@ -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) +}