-
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
file_fixture
in Factory definitions
Related to [factory_bot#1282][] [rails/rails#45606][] has been merged and is likely to be released as part of Rails 7.1. With that addition, the path toward resolving [factory_bot#1282][] becomes more clear. If factories can pass along [Pathname][] instances to attachment attributes, Active Support will handle the rest. Instances of `ActiveSupport::TestCase` provide a [file_fixture][] helper to construct a `Pathname` instance based on the path defined by `ActiveSupport::TestCase.file_fixture_path` (relative to the Rails root directory). [factory_bot#1282]: thoughtbot/factory_bot#1282 (comment) [rails/rails#45606]: rails/rails#45606 [Pathname]: https://docs.ruby-lang.org/en/master/Pathname.html [file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
- Loading branch information
1 parent
346e3c7
commit b9df074
Showing
17 changed files
with
269 additions
and
68 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,9 @@ | ||
module FactoryBotRails | ||
module FileFixtureSupport | ||
def self.included(klass) | ||
klass.cattr_accessor :file_fixture_support | ||
|
||
klass.delegate :file_fixture, to: "self.class.file_fixture_support" | ||
end | ||
end | ||
end |
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
56 changes: 56 additions & 0 deletions
56
spec/dummy/db/migrate/20170806125915_create_active_storage_tables.rb
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,56 @@ | ||
class CreateActiveStorageTables < ActiveRecord::Migration[7.0] | ||
def change | ||
# Use Active Record's configured type for primary and foreign keys | ||
primary_key_type, foreign_key_type = primary_and_foreign_key_types | ||
|
||
create_table :active_storage_blobs, id: primary_key_type do |t| | ||
t.string :key, null: false | ||
t.string :filename, null: false | ||
t.string :content_type | ||
t.text :metadata | ||
t.string :service_name, null: false | ||
t.bigint :byte_size, null: false | ||
t.string :checksum | ||
|
||
if connection.supports_datetime_with_precision? | ||
t.datetime :created_at, precision: 6, null: false | ||
else | ||
t.datetime :created_at, null: false | ||
end | ||
|
||
t.index [ :key ], unique: true | ||
end | ||
|
||
create_table :active_storage_attachments, id: primary_key_type do |t| | ||
t.string :name, null: false | ||
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type | ||
t.references :blob, null: false, type: foreign_key_type | ||
|
||
if connection.supports_datetime_with_precision? | ||
t.datetime :created_at, precision: 6, null: false | ||
else | ||
t.datetime :created_at, null: false | ||
end | ||
|
||
t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
|
||
create_table :active_storage_variant_records, id: primary_key_type do |t| | ||
t.belongs_to :blob, null: false, index: false, type: foreign_key_type | ||
t.string :variation_digest, null: false | ||
|
||
t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
end | ||
|
||
private | ||
def primary_and_foreign_key_types | ||
config = Rails.configuration.generators | ||
setting = config.options[config.orm][:primary_key_type] | ||
primary_key_type = setting || :primary_key | ||
foreign_key_type = setting || :bigint | ||
[ primary_key_type, foreign_key_type ] | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
spec/dummy/db/migrate/20190112182829_add_service_name_to_active_storage_blobs.rb
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,21 @@ | ||
class AddServiceNameToActiveStorageBlobs < ActiveRecord::Migration[6.0] | ||
def up | ||
return unless table_exists?(:active_storage_blobs) | ||
|
||
unless column_exists?(:active_storage_blobs, :service_name) | ||
add_column :active_storage_blobs, :service_name, :string | ||
|
||
if configured_service = ActiveStorage::Blob.service.name | ||
ActiveStorage::Blob.unscoped.update_all(service_name: configured_service) | ||
end | ||
|
||
change_column :active_storage_blobs, :service_name, :string, null: false | ||
end | ||
end | ||
|
||
def down | ||
return unless table_exists?(:active_storage_blobs) | ||
|
||
remove_column :active_storage_blobs, :service_name | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
spec/dummy/db/migrate/20191206030411_create_active_storage_variant_records.rb
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,26 @@ | ||
class CreateActiveStorageVariantRecords < ActiveRecord::Migration[6.0] | ||
def change | ||
return unless table_exists?(:active_storage_blobs) | ||
|
||
# Use Active Record's configured type for primary key | ||
create_table :active_storage_variant_records, id: primary_key_type, if_not_exists: true do |t| | ||
t.belongs_to :blob, null: false, index: false, type: blobs_primary_key_type | ||
t.string :variation_digest, null: false | ||
|
||
t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
end | ||
|
||
private | ||
def primary_key_type | ||
config = Rails.configuration.generators | ||
config.options[config.orm][:primary_key_type] || :primary_key | ||
end | ||
|
||
def blobs_primary_key_type | ||
pkey_name = connection.primary_key(:active_storage_blobs) | ||
pkey_column = connection.columns(:active_storage_blobs).find { |c| c.name == pkey_name } | ||
pkey_column.bigint? ? :bigint : pkey_column.type | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
spec/dummy/db/migrate/20211119233751_remove_not_null_on_active_storage_blobs_checksum.rb
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,7 @@ | ||
class RemoveNotNullOnActiveStorageBlobsChecksum < ActiveRecord::Migration[6.0] | ||
def change | ||
return unless table_exists?(:active_storage_blobs) | ||
|
||
change_column_null(:active_storage_blobs, :checksum, true) | ||
end | ||
end |
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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe "factory extensions" do | ||
describe "#file_fixture" do | ||
it "delegates to the test harness" do | ||
FactoryBot.define do | ||
factory :upload, class: Struct.new(:filename) do | ||
filename { file_fixture("file.txt") } | ||
end | ||
end | ||
|
||
upload = FactoryBot.build(:upload) | ||
|
||
expect(Pathname(upload.filename)).to eq(file_fixture("file.txt")) | ||
end | ||
|
||
it "uploads an ActiveStorage::Blob" do | ||
FactoryBot.define do | ||
factory :active_storage_blob, class: ActiveStorage::Blob do | ||
filename { pathname.basename } | ||
|
||
transient do | ||
pathname { file_fixture("file.txt") } | ||
end | ||
|
||
after :build do |model, factory| | ||
model.upload factory.pathname.open | ||
end | ||
end | ||
end | ||
|
||
blob = FactoryBot.create(:active_storage_blob) | ||
|
||
expect(blob.filename.to_s).to eq("file.txt") | ||
expect(blob.download).to eq(file_fixture("file.txt").read) | ||
end | ||
end | ||
end |
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
Empty file.
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,10 @@ | ||
# frozen_string_literal: true | ||
|
||
Rails.root.glob("../dummy/db/migrate/*.rb").each do |file| | ||
require file | ||
|
||
if file.read =~ /\Aclass (\w+)/ && (migration = Regexp.last_match(1).safe_constantize) | ||
migration.verbose = false | ||
migration.migrate :up | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class FactoryBotRails::FactoryTest < ActiveSupport::TestCase | ||
self.file_fixture_path = "test/fixtures/files" | ||
|
||
test "delegates #file_fixture to the test harness" do | ||
FactoryBot.define do | ||
factory :upload, class: Struct.new(:filename) do | ||
filename { file_fixture("file.txt") } | ||
end | ||
end | ||
|
||
upload = FactoryBot.build(:upload) | ||
|
||
assert_equal file_fixture("file.txt"), upload.filename | ||
end | ||
|
||
test "uploads an ActiveStorage::Blob" do | ||
FactoryBot.define do | ||
factory :active_storage_blob, class: ActiveStorage::Blob do | ||
filename { pathname.basename } | ||
|
||
transient do | ||
pathname { file_fixture("file.txt") } | ||
end | ||
|
||
after :build do |model, factory| | ||
model.upload factory.pathname.open | ||
end | ||
end | ||
end | ||
|
||
blob = FactoryBot.create(:active_storage_blob) | ||
|
||
assert_equal "file.txt", blob.filename.to_s | ||
assert_equal file_fixture("file.txt").read, blob.download | ||
end | ||
end |
Empty file.
Oops, something went wrong.