-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continued work toward rake task with tests
This adds a bulk_reload method to the SR model, which the task calls after it has laid the foundation. The task's concern are to receive the argument and parse the file initially into a CSV::table object. Once the parsing is done, then the work passes to the SR model for processing. The remaining work is two-fold: 1. The two pathways for receiving a local file and a remote url are not quite working the same - field headers are specified in different ways between the URI and CSV libraries, and we either need them to yield comparable outcomes or to build two different bulk_replace methods (ick). 2. Tests for the happy path are nearly done, but there are a lot of boundary conditions that still need tests off the happy path. Part of that is knowing where we're going to get the URL-based file from (right now I'm using a local Lando, but that's not great)
- Loading branch information
1 parent
fde90f4
commit 26a351b
Showing
9 changed files
with
143 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Title,URL,Phrase | ||
New Example,https://example.org,new example search | ||
Web of Science,https://libraries.mit.edu/webofsci,web of Science |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Title,URL,Phrase,Extra | ||
Example,https://example.org,example search,extra 1 | ||
Web of Science,https://libraries.mit.edu/webofsci,web of Science,extra 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Title,URL | ||
Example,https://example.org | ||
Web of Science,https://libraries.mit.edu/webofsci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Title,URL | ||
Example,https://example.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
require 'rake' | ||
|
||
class SuggestedResourceRakeTest < ActiveSupport::TestCase | ||
def setup | ||
Tacos::Application.load_tasks if Rake::Task.tasks.empty? | ||
Rake::Task['suggested_resources:reload'].reenable | ||
end | ||
|
||
test 'invoked reload can accept a local file' do | ||
records_before = Detector::SuggestedResource.count # We have three fixtures at the moment | ||
first_record_before = Detector::SuggestedResource.first | ||
local_file = Rails.root.join('test','fixtures','files','suggested_resources.csv').to_s | ||
Rake::Task["suggested_resources:reload"].invoke(local_file) | ||
refute_equal records_before, Detector::SuggestedResource.count | ||
refute_equal first_record_before, Detector::SuggestedResource.first | ||
end | ||
|
||
test 'reload task errors without a file argument' do | ||
assert_raises(ArgumentError) { | ||
Rake::Task['suggested_resources:reload'].invoke | ||
} | ||
end | ||
|
||
test 'reload can accept a url' do | ||
VCR.use_cassette('remote csv') do | ||
remote_file = 'http://static.lndo.site/suggested_resources.csv' | ||
Rake::Task["suggested_resources:reload"].invoke(remote_file) | ||
end | ||
end | ||
|
||
test 'reload fails with a non-CSV file' do | ||
local_file = Rails.root.join('test','fixtures','files','suggested_resources.xlsx').to_s | ||
assert_raises(CSV::MalformedCSVError) { | ||
Rake::Task['suggested_resources:reload'].invoke(local_file) | ||
} | ||
end | ||
|
||
test 'reload fails unless all three columns are present: title, url, phrase' do | ||
local_file = Rails.root.join('test','fixtures','files','suggested_resources_missing_field.csv').to_s | ||
error = assert_raises(ArgumentError) { | ||
Rake::Task['suggested_resources:reload'].invoke(local_file) | ||
} | ||
assert_equal 'Some CSV columns missing: [:phrase]', error.message | ||
end | ||
|
||
test 'reload succeeds if extra columns are present' do | ||
local_file = Rails.root.join('test','fixtures','files','suggested_resources_extra.csv').to_s | ||
Rake::Task['suggested_resources:reload'].invoke(local_file) | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.