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 3df965d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ GEM
net-ssh (>= 2.6.5)
net-ssh (5.0.2)
nio4r (2.5.7)
nokogiri (1.13.3)
nokogiri (1.13.4)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
oauth2 (1.4.1)
Expand Down Expand Up @@ -374,7 +374,7 @@ GEM
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-rails (5.1.1)
Expand Down
11 changes: 9 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
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 3df965d

Please sign in to comment.