Skip to content

Commit

Permalink
fix problem_test
Browse files Browse the repository at this point in the history
  • Loading branch information
qianxi committed Nov 6, 2024
1 parent c0e263e commit b01b8d3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
2 changes: 2 additions & 0 deletions app/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func runFailTests(t *testing.T, tests []failTest, groupName string) {
httpResp := makeResp(req)
resp := response.Response{}
mustJsonDecode(httpResp, &resp)
t.Logf("Expected Status Code: %d", test.statusCode)
t.Logf("Actual Status Code: %d", httpResp.StatusCode)
assert.Equal(t, test.statusCode, httpResp.StatusCode)
assert.Equal(t, test.resp, resp)
})
Expand Down
66 changes: 35 additions & 31 deletions app/controller/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,6 @@ func TestCreateProblem(t *testing.T) {
LanguageAllowed: "",
BuildArg: "",
CompareScriptName: "",
Sanitize: nil,
},
reqOptions: []reqOption{
applyAdminUser,
Expand Down Expand Up @@ -925,11 +924,6 @@ func TestCreateProblem(t *testing.T) {
"reason": "required",
"translation": "评测脚本为必填字段",
},
map[string]interface{}{
"field": "Sanitize",
"reason": "required",
"translation": "是否清洗数据为必填字段",
},
}),
},
{
Expand Down Expand Up @@ -967,7 +961,6 @@ func TestCreateProblem(t *testing.T) {
TimeLimit: 1000,
LanguageAllowed: "test_create_problem_1_language_allowed",
CompareScriptName: "cmp1",
Sanitize: &boolFalse,
Public: &boolFalse,
Privacy: &boolTrue,
},
Expand All @@ -982,7 +975,6 @@ func TestCreateProblem(t *testing.T) {
TimeLimit: 1000,
LanguageAllowed: "test_create_problem_3_language_allowed",
CompareScriptName: "cmp1",
Sanitize: &boolFalse,
Public: &boolFalse,
Privacy: &boolTrue,
Tags: "tag_1,tag_2",
Expand All @@ -998,7 +990,6 @@ func TestCreateProblem(t *testing.T) {
TimeLimit: 1000,
LanguageAllowed: "test_create_problem_2_language_allowed",
CompareScriptName: "cmp2",
Sanitize: &boolFalse,
Public: &boolTrue,
Privacy: &boolFalse,
},
Expand All @@ -1025,7 +1016,6 @@ func TestCreateProblem(t *testing.T) {
"time_limit": fmt.Sprint(test.req.TimeLimit),
"language_allowed": test.req.LanguageAllowed,
"compare_script_name": fmt.Sprint(test.req.CompareScriptName),
"sanitize": fmt.Sprint(test.req.Sanitize),
"public": fmt.Sprint(*test.req.Public),
"privacy": fmt.Sprint(*test.req.Privacy),
})
Expand Down Expand Up @@ -1066,6 +1056,8 @@ func TestCreateProblem(t *testing.T) {
storageContent := getObjectContent(t, "problems", fmt.Sprintf("%d/attachment", databaseProblem.ID))
expectedContent, err := ioutil.ReadAll(test.attachment.reader)
assert.NoError(t, err)
t.Logf("Expected Content: %+v", expectedContent)
t.Logf("Storage Content: %+v", storageContent)
assert.Equal(t, expectedContent, storageContent)
assert.Equal(t, test.attachment.fileName, databaseProblem.AttachmentFileName)
} else {
Expand Down Expand Up @@ -1745,8 +1737,9 @@ func TestCreateTestCase(t *testing.T) {
newFileContent("input_file", "test_create_test_case_non_existing_problem.in", inputTextBase64),
newFileContent("output_file", "test_create_test_case_non_existing_problem.out", outputTextBase64),
}, map[string]string{
"score": "100",
"sample": "true",
"score": "100",
"sample": "true",
"sanitize": "false",
}),
reqOptions: []reqOption{
applyAdminUser,
Expand All @@ -1761,8 +1754,9 @@ func TestCreateTestCase(t *testing.T) {
req: addFieldContentSlice([]reqContent{
newFileContent("output_file", "test_create_test_case_lack_input_file.out", outputTextBase64),
}, map[string]string{
"score": "100",
"sample": "true",
"score": "100",
"sample": "true",
"sanitize": "false",
}),
reqOptions: []reqOption{
headerOption{
Expand All @@ -1779,8 +1773,9 @@ func TestCreateTestCase(t *testing.T) {
req: addFieldContentSlice([]reqContent{
newFileContent("input_file", "test_create_test_case_lack_output_file.in", inputTextBase64),
}, map[string]string{
"score": "100",
"sample": "true",
"score": "100",
"sample": "true",
"sanitize": "false",
}),
reqOptions: []reqOption{
headerOption{
Expand All @@ -1795,8 +1790,9 @@ func TestCreateTestCase(t *testing.T) {
method: "POST",
path: base.Echo.Reverse("problem.createTestCase", problem.ID),
req: addFieldContentSlice([]reqContent{}, map[string]string{
"score": "100",
"sample": "true",
"score": "100",
"sample": "true",
"sanitize": "false",
}),
reqOptions: []reqOption{
headerOption{
Expand All @@ -1814,8 +1810,9 @@ func TestCreateTestCase(t *testing.T) {
newFileContent("input_file", "test_create_test_case_permission_denied.in", inputTextBase64),
newFileContent("output_file", "test_create_test_case_permission_denied.out", outputTextBase64),
}, map[string]string{
"score": "100",
"sample": "true",
"score": "100",
"sample": "true",
"sanitize": "false",
}),
reqOptions: []reqOption{
applyNormalUser,
Expand All @@ -1833,8 +1830,9 @@ func TestCreateTestCase(t *testing.T) {
newFileContent("input_file", "test_create_test_case_success.in", inputTextBase64),
newFileContent("output_file", "test_create_test_case_success.out", outputTextBase64),
}, map[string]string{
"score": "100",
"sample": "true",
"score": "100",
"sample": "true",
"sanitize": "false",
}), headerOption{
"Set-User-For-Test": {fmt.Sprintf("%d", user.ID)},
})
Expand Down Expand Up @@ -2077,14 +2075,16 @@ func TestUpdateTestCase(t *testing.T) {
problem := createProblemForTest(t, "update_test_case", 0, nil, user)
boolTrue := true

boolFalse := false
failTests := []failTest{
{
name: "NonExistingProblem",
method: "PUT",
path: base.Echo.Reverse("problem.updateTestCase", -1, 1),
req: request.UpdateTestCaseRequest{
Score: 100,
Sample: &boolTrue,
Score: 100,
Sample: &boolTrue,
Sanitize: &boolFalse,
},
reqOptions: []reqOption{
applyAdminUser,
Expand All @@ -2097,8 +2097,9 @@ func TestUpdateTestCase(t *testing.T) {
method: "PUT",
path: base.Echo.Reverse("problem.updateTestCase", problem.ID, -1),
req: request.UpdateTestCaseRequest{
Score: 100,
Sample: &boolTrue,
Score: 100,
Sample: &boolTrue,
Sanitize: &boolFalse,
},
reqOptions: []reqOption{
headerOption{
Expand All @@ -2117,8 +2118,9 @@ func TestUpdateTestCase(t *testing.T) {
method: "PUT",
path: base.Echo.Reverse("problem.updateTestCase", problem.ID, 1),
req: request.UpdateTestCaseRequest{
Score: 100,
Sample: &boolTrue,
Score: 100,
Sample: &boolTrue,
Sanitize: &boolFalse,
},
reqOptions: []reqOption{
applyNormalUser,
Expand All @@ -2139,7 +2141,7 @@ func TestUpdateTestCase(t *testing.T) {
{
name: "SuccessWithoutUpdatingFile",
originalData: testCaseData{
Score: 0,
Score: 100,
Sample: false,
InputFile: newFileContent("input_file", "test_update_test_case_1.in", inputTextBase64),
OutputFile: newFileContent("output_file", "test_update_test_case_1.out", outputTextBase64),
Expand Down Expand Up @@ -2240,10 +2242,12 @@ func TestUpdateTestCase(t *testing.T) {
if test.updatedData.OutputFile != nil {
reqContentSlice = append(reqContentSlice, test.updatedData.OutputFile)
}
sanitizeValue := false
req := makeReq(t, "PUT", base.Echo.Reverse("problem.updateTestCase", problem.ID, testCase.ID), addFieldContentSlice(
reqContentSlice, map[string]string{
"score": fmt.Sprintf("%d", test.updatedData.Score),
"sample": fmt.Sprintf("%t", test.updatedData.Sample),
"score": fmt.Sprintf("%d", test.updatedData.Score),
"sample": fmt.Sprintf("%t", test.updatedData.Sample),
"sanitize": fmt.Sprintf("%t", sanitizeValue),
}), headerOption{
"Set-User-For-Test": {fmt.Sprintf("%d", user.ID)},
})
Expand Down
2 changes: 1 addition & 1 deletion app/request/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type CreateProblemRequest struct {
Public *bool `json:"public" form:"public" query:"public" validate:"required"`
Privacy *bool `json:"privacy" form:"privacy" query:"privacy" validate:"required"`

Sanitize *bool `json:"sanitize" form:"sanitize" query:"sanitize" validate:"required"`
//Sanitize *bool `json:"sanitize" form:"sanitize" query:"sanitize" validate:"required"`

MemoryLimit uint64 `json:"memory_limit" form:"memory_limit" query:"memory_limit" validate:"required"` // Byte
TimeLimit uint `json:"time_limit" form:"time_limit" query:"time_limit" validate:"required"` // ms
Expand Down

0 comments on commit b01b8d3

Please sign in to comment.