Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple test upload failing #92

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added server/admin/test_assets/tests.zip
Binary file not shown.
28 changes: 10 additions & 18 deletions server/admin/test_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"mime/multipart"
"net/http"
"strconv"
"strings"

"github.com/labstack/echo/v4"
"github.com/natsukagami/kjudge/db"
Expand Down Expand Up @@ -244,6 +243,7 @@ func (g *Group) TestGroupRejudgePost(c echo.Context) error {

// WriteTests writes the given set of tests into the Database.
// If override is set, all tests in the test group gets deleted first.
// The LazyTests are STILL invalid models.Tests. DO NOT USE.
func (r *TestGroupCtx) WriteTests(db db.DBContext, tests []*tests.LazyTest, override bool) error {
for _, test := range tests {
test.TestGroupID = r.ID
Expand All @@ -256,12 +256,7 @@ func (r *TestGroupCtx) WriteTests(db db.DBContext, tests []*tests.LazyTest, over
return errors.WithStack(err)
}
}
var (
terms []string
vars []interface{}
)
for _, test := range tests {
terms = append(terms, "(?, ?, ?, ?)")
input, err := readZip(test.Input)
if err != nil {
return errors.Wrapf(err, "test %v input", test.Name)
Expand All @@ -270,18 +265,15 @@ func (r *TestGroupCtx) WriteTests(db db.DBContext, tests []*tests.LazyTest, over
if err != nil {
return errors.Wrapf(err, "test %v output", test.Name)
}
vars = append(vars, test.Name, test.TestGroupID, input, output)
}
res, err := db.Exec(fmt.Sprintf("INSERT INTO tests(name, test_group_id, input, output) VALUES %s", strings.Join(terms, ", ")), vars...)
if err != nil {
return errors.WithStack(err)
}
id, err := res.LastInsertId()
if err != nil {
return errors.WithStack(err)
}
for i, test := range tests {
test.ID = int(id) - len(tests) + i + 1
if _, err := db.Exec(
"INSERT INTO tests(name, test_group_id, input, output) VALUES (?, ?, ?, ?)",
test.Name,
test.TestGroupID,
input,
output,
); err != nil {
return errors.Wrapf(err, "inserting test `%s`", test.Name)
}
Comment on lines +268 to +276
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for future me: We upload tests one-by-one so that we don't have to store all the tests' content in memory at once. This helps with big test-sets that adds up to gigabytes of text.

}
return nil
}
Expand Down
159 changes: 159 additions & 0 deletions server/admin/test_groups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package admin_test

import (
"bytes"
_ "embed"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"

"github.com/labstack/echo/v4"
"github.com/natsukagami/kjudge/models"
"github.com/natsukagami/kjudge/test"
)

var (
//go:embed test_assets/tests.zip
testsZip []byte

testsLen = 10
)

func TestUploadMultipleTests(t *testing.T) {
testGroup := 7
path := fmt.Sprintf("/admin/test_groups/%d/upload_multiple", testGroup)

makeReq := func(t *testing.T, input, output string, override string) *http.Request {
buf := bytes.NewBuffer(nil)
body := multipart.NewWriter(buf)

if err := body.WriteField("input", input); err != nil {
t.Fatalf("writing field `input`: %v", err)
}
if err := body.WriteField("output", output); err != nil {
t.Fatalf("writing field `output`: %v", err)
}
file, err := body.CreateFormFile("file", "tests.zip")
if err != nil {
t.Fatalf("writing field `file`: %v", err)
}
if _, err := file.Write(testsZip); err != nil {
t.Fatalf("writing file: %v", err)
}
if err := body.WriteField("override", override); err != nil {
t.Fatalf("writing field `override`: %v", err)
}
if err := body.Close(); err != nil {
t.Fatalf("writing tail: %v", err)
}

req := httptest.NewRequest(http.MethodPost, path, buf)
req.Header.Add(echo.HeaderContentType, body.FormDataContentType())

return req
}

t.Run("fail no admin", func(t *testing.T) {
ts := test.NewServer(t)

// get the tests before
testsBefore, err := models.GetTestGroupTests(ts.DB, testGroup)
if err != nil {
t.Fatal("fetching old tests", err)
}

req := makeReq(t, "?.in", "?.out", "false")
resp := ts.Serve(req)

if resp.StatusCode != http.StatusTemporaryRedirect {
t.Errorf("expected redirect got %d", resp.StatusCode)
}

testsAfter, err := models.GetTestGroupTests(ts.DB, testGroup)
if err != nil {
t.Fatal("fetching new tests", err)
}
if len(testsAfter) != len(testsBefore) {
t.Errorf("expected %d tests, got %d", len(testsBefore), len(testsAfter))
}
for i, test := range testsAfter {
if testsBefore[i].ID != test.ID { // but different IDs
t.Errorf("test %d: unexpected different ID", i)
}
}
})

t.Run("success override", func(t *testing.T) {
ts := test.NewServer(t)
withAdmin := ts.WithAdmin(t)

// get the tests before
testsBefore, err := models.GetTestGroupTests(ts.DB, testGroup)
if err != nil {
t.Fatal("fetching old tests", err)
}

req := makeReq(t, "?.in", "?.out", "true")
resp := ts.Serve(req, withAdmin)

if resp.StatusCode >= 400 {
t.Errorf("expected success got %d", resp.StatusCode)
}

testsAfter, err := models.GetTestGroupTests(ts.DB, testGroup)
if err != nil {
t.Fatal("fetching new tests", err)
}
if len(testsAfter) != testsLen {
t.Errorf("expected %d tests, got %d", testsLen, len(testsAfter))
}
for i, test := range testsAfter {
if testsBefore[i].Name != test.Name { // they have the same names
t.Errorf("test %d: expected name `%s` got `%s`", i, testsBefore[i].Name, test.Name)
}
if testsBefore[i].ID == test.ID { // but different IDs
t.Errorf("test %d: unexpected same ID", i)
}

if string(test.Input) != fmt.Sprintf("%d\n", i+1) {
t.Errorf("test %d: unexpected inputs: %v", i, test.Input)
}
if string(test.Output) != fmt.Sprintf("%d\n", 3*(i+1)) {
t.Errorf("test %d: unexpected inputs: %v", i, test.Input)
}
}
})

t.Run("fail no-override", func(t *testing.T) {
ts := test.NewServer(t)
withAdmin := ts.WithAdmin(t)

// get the tests before
testsBefore, err := models.GetTestGroupTests(ts.DB, testGroup)
if err != nil {
t.Fatal("fetching old tests", err)
}

req := makeReq(t, "?.in", "?.out", "false")
resp := ts.Serve(req, withAdmin)

if resp.StatusCode < 400 {
t.Errorf("expected failure got %d", resp.StatusCode)
}

testsAfter, err := models.GetTestGroupTests(ts.DB, testGroup)
if err != nil {
t.Fatal("fetching new tests", err)
}
if len(testsAfter) != len(testsBefore) {
t.Errorf("expected %d tests, got %d", len(testsBefore), len(testsAfter))
}
for i, test := range testsAfter {
if testsBefore[i].ID != test.ID { // but different IDs
t.Errorf("test %d: unexpected different ID", i)
}
}
})
}
7 changes: 7 additions & 0 deletions tests/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ type LazyTest struct {
Input, Output *zip.File
}

// Verify calls the underlying Verify method, respecting their wish to have input and output non-null.
func (lz *LazyTest) Verify() error {
lz.Test.Input = []byte{}
lz.Test.Output = []byte{}
return lz.Test.Verify()
}

type textMap = map[string]*zip.File

// Unpack try to unpack a zip file and extract tests from the given pattern.
Expand Down