Skip to content

Commit

Permalink
fix: task file import (doubtfire-lms#438)
Browse files Browse the repository at this point in the history
* fix: sort taskdef by abbr length to ensure the longest match

* fix: add a new task to test task files import filename matching

* enhance: validate task sheet content in unit test
  • Loading branch information
ublefo authored and macite committed Jun 1, 2024
1 parent 3536815 commit 7366328
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 42 deletions.
3 changes: 2 additions & 1 deletion app/models/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,8 @@ def import_task_files_from_zip(zip_file)
file_name = File.basename(file.name)
if (File.extname(file.name) == '.pdf') || (File.extname(file.name) == '.zip')
found = false
task_definitions.each do |td|
# sort task definitions by longest abbreviation to ensure longest matches
task_definitions.sort_by{ |td| -td.abbreviation.size }.each do |td|
next unless /^#{td.abbreviation}/ =~ file_name

file.extract ("#{task_path}#{FileHelper.sanitized_filename(td.abbreviation)}#{File.extname(file.name)}") { true }
Expand Down
23 changes: 20 additions & 3 deletions lib/helpers/database_populator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,26 @@ def echo_line *args
def generate_tasks_for_unit(unit, unit_details)
echo "----> Generating #{unit_details[:num_tasks]} tasks"

if File.exist? Rails.root.join('test_files', "#{unit.code}-Tasks.csv")
unit.import_tasks_from_csv File.open(Rails.root.join('test_files', "#{unit.code}-Tasks.csv"))
unit.import_task_files_from_zip Rails.root.join('test_files', "#{unit.code}-Tasks.zip")
csv_to_import = Rails.root.join('test_files', "#{unit.code}-Tasks.csv")
zip_to_import = Rails.root.join('test_files', "#{unit.code}-TaskFiles.zip")

if (File.exist? csv_to_import) && (File.exist? zip_to_import)
echo "----> CSV file found, importing tasks from #{csv_to_import} \n"
result = unit.import_tasks_from_csv(File.open(csv_to_import))
unless result[:errors].empty?
raise("----> Task import from CSV failed with the following errors: #{result[:errors]} \n")
end

echo "----> Importing task files from #{zip_to_import} \n"
result = unit.import_task_files_from_zip zip_to_import
unless result[:errors].empty?
raise("----> Task files import failed with the following errors: #{result[:errors]} \n")
end

unless result[:ignored].empty?
echo "----> Task files import ignored the following files: #{result[:ignored]} \n"
end

return
end

Expand Down
12 changes: 11 additions & 1 deletion test/models/unit_model_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test_helper'
require 'grade_helper'
require './lib/helpers/database_populator'
require 'pdf-reader'

class UnitModelTest < ActiveSupport::TestCase
include TestHelpers::JsonHelper
Expand Down Expand Up @@ -85,7 +86,7 @@ def test_sync_unit

def test_import_tasks_worked
@unit.import_tasks_from_csv File.open(Rails.root.join('test_files',"#{@unit.code}-Tasks.csv"))
assert_equal 36, @unit.task_definitions.count, 'imported all task definitions'
assert_equal 37, @unit.task_definitions.count, 'imported all task definitions'
end

def test_import_task_files
Expand All @@ -97,6 +98,15 @@ def test_import_task_files
end

assert File.exist? @unit.task_definitions.first.task_resources

# extra checks to ensure the filename matching behavior is correct (longest match)
td = @unit.task_definitions.find_by(abbreviation: "T1")
reader = PDF::Reader.new(td.task_sheet)
assert reader.pages[0].text.include? "Task sheet for task T1!"

td = @unit.task_definitions.find_by(abbreviation: "T10")
reader = PDF::Reader.new(td.task_sheet)
assert reader.pages[0].text.include? "Task sheet for task T10!"
end

def test_rollover_of_task_files
Expand Down
Loading

0 comments on commit 7366328

Please sign in to comment.