Skip to content

Commit

Permalink
Added more tests for author/item errors and made some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
brianna-dardin committed Dec 20, 2023
1 parent bc74254 commit b399fdd
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def otw_client

def get_host(request)
if @client.config.archive_host.split("//")[1].split(":")[0] =~ Resolv::IPv4::Regex
host = @client.config.archive_host.clone.sub! "3000", "3010"
host = @client.config.archive_host.dup.sub("3000", "3010")
else
host = request.host_with_port
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def self.size_errors(author_ids)
items.map do |item|
errors = item[:model].where("author_id in (#{author_ids})").group(:author_id).having("count(id) > #{NUMBER_OF_ITEMS}").count
errors.map do |a_id, count|
msg = "Author '#{author_names[a_id]}' has more than #{count} #{item[:label]} - the Archive can only import #{NUMBER_OF_ITEMS} at a time"
msg = "Author '#{author_names[a_id]}' has #{count} #{item[:label]} - the Archive can only import #{NUMBER_OF_ITEMS} at a time"
if author_errors.key?(a_id)
author_errors[a_id] << msg
else
Expand Down
19 changes: 12 additions & 7 deletions app/models/concerns/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,26 @@ def self.iterate_errors(item_errors, author_errors)
end

def self.missing_fandom(type_model, type_sym, author_ids)
fandom_errors = type_model.where("fandoms = null AND author_id IN (#{author_ids})").group_by { |item| item[:author_id] }
where = Item.get_auth_id_query("(fandoms is null OR length(fandoms) = 0)", author_ids)
fandom_errors = type_model.where(where).group_by { |item| item[:author_id] }
missing_fandom_text = Proc.new do |type_sym, col, item|
"Fandom for #{type_sym} '#{item.title}' is missing"
end
Item.parse_author_errors(fandom_errors, type_sym, :fandoms, missing_fandom_text)
end

def self.too_long_errors(type_model, type_sym, col, max, author_ids)
length_errors = type_model.where("length(#{col}) > #{max} AND author_id IN (#{author_ids})").group_by { |item| item[:author_id] }
where = Item.get_auth_id_query("#{col} is not null AND length(#{col}) > #{max}", author_ids)
length_errors = type_model.where(where).group_by { |item| item[:author_id] }
too_long_text = Proc.new do |type_sym, col, item|
"#{col.capitalize} for #{type_sym} '#{item.title}' is too long (#{item[col].length})"
end
Item.parse_author_errors(length_errors, type_sym, col, too_long_text)
end

def self.chapter_errors(col, max, author_ids)
length_errors = Chapter.joins(:story).where("length(chapters.#{col.to_s}) > #{max} AND stories.author_id IN (#{author_ids})").select(Arel.sql("chapters.*, stories.author_id as a_id, stories.title as s_title")).group_by { |c| c[:a_id] }
where = Item.get_auth_id_query("chapters.#{col} is not null AND length(chapters.#{col}) > #{max}", author_ids).dup.sub("author_id", "stories.author_id")
length_errors = Chapter.joins(:story).where(where).select(Arel.sql("chapters.*, stories.author_id as a_id, stories.title as s_title")).group_by { |c| c[:a_id] }
chapter_text = Proc.new do |type_sym, col, item|
"#{col.capitalize} for #{type_sym} #{item.position} in story '#{item.s_title}' is too long (#{item[col].length})"
end
Expand All @@ -114,18 +117,20 @@ def self.parse_author_errors(item_errors, type_sym, col, text_proc)
end

def self.auth_id_to_not_imported(type, author_ids = nil)
Item.get_auth_id_hash(type, "imported = false", author_ids)
where = Item.get_auth_id_query("imported = false", author_ids)
type.where(where).group(:author_id).count
end

def self.auth_id_to_not_imported_dni(type, author_ids = nil)
Item.get_auth_id_hash(type, "imported = false AND do_not_import = false", author_ids)
where = Item.get_auth_id_query("imported = false AND do_not_import = false", author_ids)
type.where(where).group(:author_id).count
end

def self.get_auth_id_hash(type, where, author_ids = nil)
def self.get_auth_id_query(where, author_ids = nil)
if !author_ids.nil? && author_ids.split(",").length > 0
where += " AND author_id IN (#{author_ids})"
end
type.where(where).group(:author_id).count
where
end

def self.items_responses(ao3_response, check = false)
Expand Down
6 changes: 5 additions & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
end
end

factory :chapter
factory :chapter do
story
id { generate :id }
audit_comment {"Test"}
end

factory :archive_config do
key { APP_CONFIG[:sitekey] }
Expand Down
13 changes: 13 additions & 0 deletions spec/models/authors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,17 @@
expect(Author.all_imported.include?(author.id)).to eq false
end
end

it 'returns too many item errors' do
stub_const("NUMBER_OF_ITEMS", 4)
author = create(:author, name: "testy")
5.times {
story = create(:story, author_id: author.id, audit_comment: "Test")
story_link = create(:story_link, author_id: author.id, audit_comment: "Test")
}
errors = Author.all_errors(author.id.to_s)
expect(errors.key?(author.id)).to eq true
expect(errors[author.id].include?("Author 'testy' has 5 stories - the Archive can only import 4 at a time")).to eq true
expect(errors[author.id].include?("Author 'testy' has 5 bookmarks - the Archive can only import 4 at a time")).to eq true
end
end
57 changes: 57 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'rails_helper'

describe Item, type: :model do
it 'returns summary too long errors' do
stub_const("SUMMARY_LENGTH", 4)
author = create(:author)
story = create(:story, author_id: author.id, title: "title", summary: "Longer than 4 characters", audit_comment: "Test")
story_link = create(:story_link, author_id: author.id, title: "title", summary: "Longer than 4 characters", audit_comment: "Test")
errors = Item.all_errors(author.id.to_s)
expect(errors.key?(author.id)).to eq true
expect(errors[author.id].include?("Summary for story 'title' is too long (24)")).to eq true
expect(errors[author.id].include?("Summary for bookmark 'title' is too long (24)")).to eq true
end

it 'returns notes too long errors' do
stub_const("NOTES_LENGTH", 4)
author = create(:author)
story = create(:story, author_id: author.id, title: "title", notes: "Longer than 4 characters", audit_comment: "Test")
story_link = create(:story_link, author_id: author.id, title: "title", notes: "Longer than 4 characters", audit_comment: "Test")
errors = Item.all_errors(author.id.to_s)
expect(errors.key?(author.id)).to eq true
expect(errors[author.id].include?("Notes for story 'title' is too long (24)")).to eq true
expect(errors[author.id].include?("Notes for bookmark 'title' is too long (24)")).to eq true
end

it 'returns missing fandoms errors' do
author = create(:author)
story = create(:story, author_id: author.id, title: "title", audit_comment: "Test")
story_link = create(:story_link, author_id: author.id, title: "title", audit_comment: "Test")
errors = Item.all_errors(author.id.to_s)
expect(errors.key?(author.id)).to eq true
expect(errors[author.id].include?("Fandom for story 'title' is missing")).to eq true
expect(errors[author.id].include?("Fandom for bookmark 'title' is missing")).to eq true
end

it 'returns chapter notes too long errors' do
stub_const("NOTES_LENGTH", 4)
author = create(:author)
story = create(:story, author_id: author.id, title: "title", audit_comment: "Test")
chapter = create(:chapter, story_id: story.id, position: 1, notes: "Longer than 4 characters", audit_comment: "Test")
errors = Item.all_errors(author.id.to_s)
expect(errors.key?(author.id)).to eq true
expect(errors[author.id].include?("Notes for chapter 1 in story 'title' is too long (24)")).to eq true
end

it 'returns chapter text too long errors' do
stub_const("CHAPTER_LENGTH", 4)
author = create(:author)
story = create(:story, author_id: author.id, title: "title", audit_comment: "Test")
chapter = create(:chapter, story_id: story.id, position: 1, text: "Longer than 4 characters", audit_comment: "Test")
errors = Item.all_errors(author.id.to_s)
expect(errors.key?(author.id)).to eq true
expect(errors[author.id].include?("Text for chapter 1 in story 'title' is too long (24)")).to eq true
end
end

0 comments on commit b399fdd

Please sign in to comment.