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

Fallback to textdiffer when csv parsing fails #5876

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 20 additions & 6 deletions app/helpers/renderers/feedback_table_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,31 @@ def messages(msgs)
end
end

def render_accepted(builder, t)
if t[:format] == 'csv'
begin
return CsvDiffer.render_accepted(builder, t[:expected]) if CsvDiffer.limited_columns?(t[:expected])
rescue CSV::MalformedCSVError
# default to text if parsing failed
end
end
TextDiffer.render_accepted(builder, t[:expected])
end

def differ(t)
if t[:format] == 'csv' && CsvDiffer.limited_columns?(t[:generated]) && CsvDiffer.limited_columns?(t[:expected])
CsvDiffer
else
TextDiffer
if t[:format] == 'csv'
begin
return CsvDiffer.new(t[:generated], t[:expected]) if CsvDiffer.limited_columns?(t[:generated]) && CsvDiffer.limited_columns?(t[:expected])
rescue CSV::MalformedCSVError
# default to text if parsing failed
end
end
TextDiffer.new(t[:generated], t[:expected])
end

def test_accepted(t)
@builder.div(class: 'test-accepted') do
differ(t).render_accepted(@builder, t[:generated])
render_accepted(@builder, t)
end
end

Expand All @@ -340,7 +354,7 @@ def test_failed(t)
end

def diff(t)
differ = differ(t).new(t[:generated], t[:expected])
differ = differ(t)
@builder.div(class: "diffs show-#{@diff_type}") do
@builder << differ.split
@builder << differ.unified
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/submissions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,13 @@ def expected_score_string(*args)

assert_response :ok
end

test 'Should be able to render submission with malformed csv' do
submission = create :wrong_submission, code: 'select * from track where trackid = 1610',
result: '{"accepted":false,"status":"wrong","description":"Test failed","annotations":[{"text":"An SQL query should end with a semicolon","type":"info","row":1,"rows":1,"column":null,"columns":null,"externalUrl":null}],"groups":[{"description":"Result Validation","badgeCount":2,"groups":[{"accepted":false,"groups":[{"description":"Compare results","accepted":false,"tests":[{"expected":"\"title\"\\n\"Grace\"\\n\"Monster\"\\n\"The Greatest Hits\"\\n\"Throwing Copper\"\\n\"Toward the Within\"\\n\"Troublegum\"\\n\"Worst Case Scenario\"","format":"csv","messages":[{"format":"callout-danger","description":"Incorrect row count"}],"generated":"\"title\",\"trackid\",\"genre\",\"albumid\",\"artistid\",\"tracknumber\"\\n\"\"So Fast, So Numb\"\",\"1610\",\"Rock\",\"523\",\"229\",\"12\"","accepted":false}]}]},{"accepted":true,"groups":[{"description":"Column Types","accepted":true,"tests":[{"expected":"\"column\",\"type\"\\n\"title\",\"STRING\"","format":"csv","generated":"\"column\",\"type\"\\n\"title\",\"STRING\"\\n\"trackid\",\"INTEGER\"\\n\"genre\",\"STRING\"\\n\"albumid\",\"INTEGER\"\\n\"artistid\",\"INTEGER\"\\n\"tracknumber\",\"INTEGER\"","accepted":true}]}]},{"accepted":false,"groups":[{"description":"Order By","accepted":false,"tests":[{"expected":"correct order","generated":"incorrect order","accepted":false}]}]}]},{"description":"Query Result","badgeCount":0,"groups":[{"accepted":true,"groups":[{"description":"Full result","accepted":true,"tests":[{"expected":"","format":"csv","generated":"\"title\",\"trackid\",\"genre\",\"albumid\",\"artistid\",\"tracknumber\"\\n\"\"So Fast, So Numb\"\",\"1610\",\"Rock\",\"523\",\"229\",\"12\"","accepted":true}]}]}]}],"messages":[{"format":"html","description":"\\u003cstrong\\u003eWorker:\\u003c/strong\\u003e ixion","permission":"zeus"},{"format":"html","description":"\\u003cstrong\\u003eMemory usage:\\u003c/strong\\u003e 40.61 MiB","permission":"zeus"},{"format":"html","description":"\\u003cstrong\\u003ePrepare:\\u003c/strong\\u003e 0.13 seconds","permission":"zeus"},{"format":"html","description":"\\u003cstrong\\u003eRuntime:\\u003c/strong\\u003e 0.92 seconds","permission":"zeus"},{"format":"html","description":"\\u003cstrong\\u003eResult construction:\\u003c/strong\\u003e 0.02 seconds","permission":"zeus"},{"format":"html","description":"\\u003cstrong\\u003eFinalize:\\u003c/strong\\u003e 0.01 seconds","permission":"zeus"},{"format":"html","description":"\\u003cstrong\\u003eTotal time:\\u003c/strong\\u003e 1.14 seconds","permission":"zeus"}]}'
sign_in submission.user
get submission_path(submission)

assert_response :ok
end
end