Skip to content

Commit

Permalink
Add tests for CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
rhomeister committed Apr 15, 2022
1 parent a4ffde4 commit c14a983
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class Submission < ApplicationRecord
end

def self.to_csv
attributes = %w[id uploaded_by_id created_at updated_at test_results_count successful_test_results_count language status]
attributes = %w[id uploaded_by_name created_at updated_at test_results_count successful_test_results_count language status]

CSV.generate(headers: true) do |csv|
csv << attributes

all.each do |submission|
csv << submission.attributes.values_at(*attributes)
csv << attributes.map { |a| submission.send(a) }
end
end
end
Expand All @@ -34,12 +34,19 @@ def self.to_csv
next unless checks_completed?

notify_users
successful_test_results_count # store successful tests count in db column
end

def uploaded_by_name
uploaded_by&.name
end

def successful_test_results_count
value = self[:successful_test_results_count]
return value if value

return unless test_results.any?

self.successful_test_results_count = test_results.where(status: %w[success skipped]).count
save
successful_test_results_count
Expand Down Expand Up @@ -84,6 +91,7 @@ def tempdir
end

def rerun_tests
update(successful_test_results_count: nil)
SubmissionCheckWorker.perform_async(id)
end

Expand Down
20 changes: 20 additions & 0 deletions spec/models/submission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,24 @@
expect(result.submission).to eq submission
end
end

it 'can be exported to csv' do
travel_to '2022-01-01'
assignment = create(:assignment)
assignment.tests << create(:expected_output_test)

user = create(:user, name: 'Ruben')
submission = build(:file_submission, assignment: assignment, uploaded_by: user)
submission.file = File.new('spec/fixtures/dummy_submissions/correct.zip')
submission.save!

expect(submission.reload.status).to eq 'success'

expected_csv = <<~EXPECTED
id,uploaded_by_name,created_at,updated_at,test_results_count,successful_test_results_count,language,status
#{submission.id},Ruben,2022-01-01 00:00:00 UTC,2022-01-01 00:00:00 UTC,1,1,,success
EXPECTED

expect(Submission.to_csv).to eq expected_csv
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.include ActiveSupport::Testing::TimeHelpers

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
Expand Down

0 comments on commit c14a983

Please sign in to comment.