Skip to content

Commit

Permalink
fixup: part two of "remove attachment_data_with_asset" factory
Browse files Browse the repository at this point in the history
This removes the temorarily renamed factory method, and also
tidied up some test file `let` declarations that were unused
(following the approach of "if it's only used once, define it
inline rather than as a `let`").

Note that I've deliberately used `csv_attachment` rather than
`file_attachment` as the resulting path for the latter is quite
different to the resulting path for the former. `csv_attachment`
generates a path that is most similar to the docx path that these
tests had relied on until now.
  • Loading branch information
ChrisBAshton committed Dec 6, 2024
1 parent 57947b2 commit e79cf88
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 48 deletions.
9 changes: 0 additions & 9 deletions test/factories/attachment_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
end
end

trait(:doc) do
file { File.open(Rails.root.join("test/fixtures/sample.docx")) }

after(:build) do |attachment_data|
attachment_data.assets << build(:asset, asset_manager_id: "asset_manager_id", variant: Asset.variants[:original], filename: attachment_data.filename)
end
end

trait(:csv) do
file { File.open(Rails.root.join("test/fixtures/sample.csv")) }

Expand All @@ -28,7 +20,6 @@
end

factory :attachment_data, parent: :generic_attachment_data, traits: [:pdf]
factory :attachment_data_for_doc, parent: :generic_attachment_data, traits: [:doc]
factory :attachment_data_for_csv, parent: :generic_attachment_data, traits: [:csv]
factory :attachment_data_with_no_assets, parent: :generic_attachment_data
end
11 changes: 0 additions & 11 deletions test/factories/attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@
end
end

trait(:doc) do
transient do
file { File.open(Rails.root.join("test/fixtures/sample.docx")) }
end

after(:build) do |attachment, evaluator|
attachment.attachment_data ||= build(:attachment_data_for_doc, file: evaluator.file, attachable: attachment.attachable)
end
end

trait(:csv) do
transient do
file { File.open(Rails.root.join("test/fixtures/sample.csv")) }
Expand All @@ -47,7 +37,6 @@
end

factory :file_attachment, parent: :attachment, traits: [:pdf]
factory :file_attachment_with_asset, parent: :attachment, traits: [:doc]
factory :csv_attachment, parent: :attachment, traits: [:csv]
factory :file_attachment_with_no_assets, parent: :attachment, traits: [:with_no_assets]

Expand Down
8 changes: 4 additions & 4 deletions test/integration/asset_access_options_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def setup_publishing_api_for(edition)

def add_file_attachment_with_asset(filename, to:)
to.attachments << FactoryBot.build(
:file_attachment_with_asset,
:csv_attachment,
title: filename,
attachable: to,
)
Expand All @@ -266,10 +266,10 @@ def path_to_attachment(filename)
fixture_path.join(filename)
end

def stub_asset(asset_manger_id, attributes = {})
url_id = "http://asset-manager/assets/#{asset_manger_id}"
def stub_asset(asset_manager_id, attributes = {})
url_id = "http://asset-manager/assets/#{asset_manager_id}"
Services.asset_manager.stubs(:asset)
.with(asset_manger_id)
.with(asset_manager_id)
.returns(attributes.merge(id: url_id).stringify_keys)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/attachment_deletion_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AttachmentDeletionIntegrationTest < ActionDispatch::IntegrationTest
describe "attachment deletion" do
context "given a draft document with multiple file attachments" do
let(:managing_editor) { create(:managing_editor) }
let(:first_attachment) { build(:file_attachment_with_asset, attachable: edition, title: "first attachment") }
let(:first_attachment) { build(:csv_attachment, attachable: edition, title: "first attachment") }
let(:first_asset_id) { "asset_manager_id" }
let(:second_attachment) { build(:file_attachment, attachable: edition) }
let(:second_asset_id_original) { "asset_manager_id_original" }
Expand Down
4 changes: 1 addition & 3 deletions test/integration/attachment_draft_status_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class AttachmentDraftStatusIntegrationTest < ActionDispatch::IntegrationTest
let(:filename) { "sample.docx" }
let(:asset_manager_id) { "asset_manager_id" }
let(:topic_taxon) { build(:taxon_hash) }
let(:variant) { Asset.variants[:original] }

before do
login_as create(:managing_editor)
Expand All @@ -20,12 +19,11 @@ class AttachmentDraftStatusIntegrationTest < ActionDispatch::IntegrationTest
end

context "given a file attachment" do
let(:attachment) { build(:file_attachment_with_asset, attachable:) }
let(:attachable) { edition }

before do
setup_publishing_api_for(edition)
attachable.attachments << attachment
attachable.attachments << build(:csv_attachment, attachable:)
attachable.save!
end

Expand Down
9 changes: 1 addition & 8 deletions test/integration/attachment_link_header_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class AttachmentLinkHeaderIntegrationTest < ActionDispatch::IntegrationTest
include TaxonomyHelper

describe "attachment link header" do
let(:filename) { "sample.docx" }
let(:asset_manager_id) { "asset_manager_id" }
let(:variant) { Asset.variants[:original] }

before do
login_as create(:managing_editor)
Expand All @@ -20,12 +18,11 @@ class AttachmentLinkHeaderIntegrationTest < ActionDispatch::IntegrationTest
end

context "given a file attachment" do
let(:attachment) { build(:file_attachment_with_asset, attachable:) }
let(:attachable) { edition }
let(:topic_taxon) { build(:taxon_hash) }

before do
attachable.attachments << attachment
attachable.attachments << build(:csv_attachment, attachable:)
attachable.save!
end

Expand Down Expand Up @@ -55,10 +52,6 @@ def setup_publishing_api_for(edition)
stub_publishing_api_links_with_taxons(edition.content_id, [topic_taxon["content_id"]])
end

def path_to_attachment(filename)
fixture_path.join(filename)
end

def stub_asset(asset_manger_id, attributes = {})
url_id = "http://asset-manager/assets/#{asset_manger_id}"
Services.asset_manager.stubs(:asset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ class AttachmentRedirectDueToUnpublishingIntegrationTest < ActionDispatch::Integ
include TaxonomyHelper

describe "attachment redirect due to unpublishing" do
let(:filename) { "sample.docx" }
let(:attachable) { edition }
let(:asset_manager_id) { "asset_manager_id" }
let(:redirect_path) { edition.public_path }
let(:redirect_url) { edition.public_url }
let(:topic_taxon) { build(:taxon_hash) }
let(:variant) { Asset.variants[:original] }
let(:attachment) { build(:file_attachment_with_asset, attachable:) }

before do
login_as create(:managing_editor)
Expand All @@ -25,7 +21,7 @@ class AttachmentRedirectDueToUnpublishingIntegrationTest < ActionDispatch::Integ
stub_publishing_api_expanded_links_with_taxons(edition.content_id, [])
stub_asset(asset_manager_id)

attachable.attachments << attachment
attachable.attachments << build(:csv_attachment, attachable:)
attachable.save!
end

Expand Down
4 changes: 2 additions & 2 deletions test/integration/attachment_replacement_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class AttachmentReplacementIntegrationTest < ActionDispatch::IntegrationTest

describe "attachment replacement" do
let(:managing_editor) { create(:managing_editor) }
let(:filename) { "sample.docx" }
let(:filename) { "sample.csv" }
let(:asset_manager_id) { "asset_manager_id" }
let(:replacement_filename) { "sample.rtf" }
let(:replacement_asset_manager_id) { "replacement-asset-id" }
let(:variant) { Asset.variants[:original] }
let(:attachment) { build(:file_attachment_with_asset, title: "attachment-title", attachable: edition) }
let(:attachment) { build(:csv_attachment, title: "attachment-title", attachable: edition) }

before do
login_as(managing_editor)
Expand Down
10 changes: 5 additions & 5 deletions test/unit/lib/tasks/asset_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AssetManagerTest < ActiveSupport::TestCase
%w[unpublished superseded].each do |state|
context "for an asset attached only to a #{state} edition" do
let(:edition) { create(:"#{state}_publication") }
let(:attachment) { create(:file_attachment_with_asset, attachable: edition) }
let(:attachment) { create(:csv_attachment, attachable: edition) }

context "when asset is not deleted from asset manager" do
before do
Expand Down Expand Up @@ -57,7 +57,7 @@ class AssetManagerTest < ActiveSupport::TestCase

context "for an asset attached only to a draft edition" do
let(:edition) { create(:draft_publication) }
let(:attachment) { create(:file_attachment_with_asset, attachable: edition) }
let(:attachment) { create(:csv_attachment, attachable: edition) }

context "when asset is not deleted from asset manager" do
before do
Expand Down Expand Up @@ -86,7 +86,7 @@ class AssetManagerTest < ActiveSupport::TestCase

context "for an asset attached only to a published edition" do
let(:edition) { create(:published_publication) }
let(:attachment) { create(:file_attachment_with_asset, attachable: edition) }
let(:attachment) { create(:file_attachment, attachable: edition) }

context "when asset is not deleted from asset manager" do
before do
Expand Down Expand Up @@ -114,7 +114,7 @@ class AssetManagerTest < ActiveSupport::TestCase
end

context "for an asset attached to both published and superseded editions" do
let(:attachment) { create(:file_attachment_with_asset) }
let(:attachment) { create(:csv_attachment) }
let(:edition_1) { create(:superseded_publication, :with_alternative_format_provider, attachments: [attachment]) }
let(:edition_2) { create(:published_publication, :with_alternative_format_provider, attachments: [attachment]) }

Expand Down Expand Up @@ -144,7 +144,7 @@ class AssetManagerTest < ActiveSupport::TestCase
end

context "for an asset attached only to something that is not an edition" do
let(:attachment) { create(:file_attachment_with_asset, attachable: create(:double)) }
let(:attachment) { create(:csv_attachment, attachable: create(:double)) }

test "it should include no output in the report" do
assert_output("") { task.invoke }
Expand Down

0 comments on commit e79cf88

Please sign in to comment.