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 Jun 1, 2024
1 parent 2425997 commit 8f37943
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 39 deletions.
3 changes: 2 additions & 1 deletion app/models/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,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
3 changes: 3 additions & 0 deletions lib/helpers/database_populator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ def generate_tasks_for_unit(unit, unit_details)
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 8f37943

Please sign in to comment.