Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fixture_builder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake"
s.add_development_dependency "sqlite3"
s.add_development_dependency "test-unit"
s.add_development_dependency "with_model"
end
20 changes: 1 addition & 19 deletions test/fixture_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.table_name
end

class FixtureBuilderTest < Test::Unit::TestCase
include TestDatabase
include TestSchema

def teardown
FixtureBuilder.instance_variable_set(:@configuration, nil)
Expand All @@ -27,7 +27,6 @@ def test_name_with
end

def test_ivar_naming
create_and_blow_away_old_db
force_fixture_generation

FixtureBuilder.configure do |fbuilder|
Expand All @@ -41,7 +40,6 @@ def test_ivar_naming
end

def test_serialization
create_and_blow_away_old_db
force_fixture_generation

FixtureBuilder.configure do |fbuilder|
Expand All @@ -56,7 +54,6 @@ def test_serialization
end

def test_dates_are_iso_formatted_without_mutating_global_date_formats
create_and_blow_away_old_db
force_fixture_generation

default_date_format_exists = Date::DATE_FORMATS.key?(:default)
Expand Down Expand Up @@ -97,7 +94,6 @@ def test_dates_are_iso_formatted_without_mutating_global_date_formats
end

def test_do_not_include_virtual_attributes
create_and_blow_away_old_db
force_fixture_generation

FixtureBuilder.configure do |fbuilder|
Expand All @@ -111,7 +107,6 @@ def test_do_not_include_virtual_attributes
end

def test_generated_columns_are_excluded_for_model_backed_tables
create_and_blow_away_old_db
force_fixture_generation

table_name = GeneratedCreature.table_name
Expand All @@ -131,7 +126,6 @@ def test_generated_columns_are_excluded_for_model_backed_tables
end

def test_generated_columns_are_excluded_for_raw_query_tables
create_and_blow_away_old_db
force_fixture_generation

table_name = GENERATED_COLUMN_RECORDS_TABLE
Expand All @@ -156,7 +150,6 @@ def test_generated_columns_are_excluded_for_raw_query_tables
end

def test_raw_query_select_aliases_are_preserved
create_and_blow_away_old_db
force_fixture_generation

table_name = GENERATED_COLUMN_RECORDS_TABLE
Expand Down Expand Up @@ -185,7 +178,6 @@ def test_raw_query_select_aliases_are_preserved
end

def test_generated_columns_come_from_the_model_table_name
create_and_blow_away_old_db
force_fixture_generation

table_name = RELOCATED_CREATURES_TABLE
Expand All @@ -206,7 +198,6 @@ def test_generated_columns_come_from_the_model_table_name
end

def test_custom_json_attribute_type_round_trips_through_fixtures
create_and_blow_away_old_db
force_fixture_generation
wizard_data = WizardData.new(
level: 99,
Expand Down Expand Up @@ -380,7 +371,6 @@ def test_fixtures_dir
end

def test_rebuilding_due_to_differing_file_hashes
create_and_blow_away_old_db
force_fixture_generation_due_to_differing_file_hashes

FixtureBuilder.configure do |fbuilder|
Expand All @@ -395,7 +385,6 @@ def test_rebuilding_due_to_differing_file_hashes
end

def test_rebuilds_when_generated_fixture_hashes_differ
create_and_blow_away_old_db
force_fixture_generation

FixtureBuilder.configure do |fbuilder|
Expand Down Expand Up @@ -444,7 +433,6 @@ def test_rebuilds_parsed_invalid_manifest(payload)
end

def test_malformed_manifest_raises_without_running_factory
create_and_blow_away_old_db
manifest_path = File.expand_path("../tmp/fixture_builder.yml", __dir__)
File.write(manifest_path, "---\ninvalid: [\n")
factory_called = false
Expand All @@ -471,7 +459,6 @@ def test_lock_path_tracks_fixture_builder_file
end

def test_fresh_manifest_returns_without_acquiring_lock
create_and_blow_away_old_db
force_fixture_generation
builds = 0
lock_path = nil
Expand Down Expand Up @@ -499,7 +486,6 @@ def test_fresh_manifest_returns_without_acquiring_lock
end

def test_skips_rebuild_for_valid_empty_fixture_snapshot
create_and_blow_away_old_db
force_fixture_generation
fixture_snapshot = Dir[test_path("fixtures/*.yml")].to_h do |filename|
[filename, File.binread(filename)]
Expand Down Expand Up @@ -531,7 +517,6 @@ def test_skips_rebuild_for_valid_empty_fixture_snapshot
end

def test_raising_after_build_invalidates_manifest_and_retries
create_and_blow_away_old_db
force_fixture_generation
builds = 0
factory = proc do
Expand All @@ -549,7 +534,6 @@ def test_raising_after_build_invalidates_manifest_and_retries
generated_fixture = YAML.safe_load_file(fixture_path)
generated_fixture["enty"]["retired_column"] = "bogus"
File.write(fixture_path, generated_fixture.to_yaml)
create_and_blow_away_old_db
FixtureBuilder.instance_variable_set(:@configuration, nil)

assert_raise(RuntimeError) do
Expand All @@ -573,7 +557,6 @@ def test_raising_after_build_invalidates_manifest_and_retries
end

def test_sha256_manifest_digests_when_deprecated_use_sha1_digests_is_enabled
create_and_blow_away_old_db
force_fixture_generation_due_to_differing_file_hashes

source_path = Pathname.new(test_path("fixture_builder_test.rb"))
Expand Down Expand Up @@ -613,7 +596,6 @@ def test_sha256_manifest_digests_when_deprecated_use_sha1_digests_is_enabled
private

def assert_manifest_rebuilds(payload)
create_and_blow_away_old_db
force_fixture_generation
builds = 0
factory = proc do
Expand Down
3 changes: 1 addition & 2 deletions test/legacy_fixture_mode_fixture_generation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))

class LegacyFixtureModeFixtureGenerationTest < Test::Unit::TestCase
include TestDatabase
include TestSchema

def setup
create_and_blow_away_old_db
force_fixture_generation

FixtureBuilder.configure do |fbuilder|
Expand Down
3 changes: 1 addition & 2 deletions test/legacy_fixture_mode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))

class LegacyFixtureModeTest < Test::Unit::TestCase
include TestDatabase
include TestSchema

def setup
create_and_blow_away_old_db
force_fixture_generation
end

Expand Down
3 changes: 1 addition & 2 deletions test/namer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.model_class = NamerTestModel
end

class NamerTest < Test::Unit::TestCase
include TestDatabase
include TestSchema

def setup
configuration = FixtureBuilder::Configuration.new
Expand Down Expand Up @@ -64,7 +64,6 @@ def test_name_not_unique_across_tables
end

def test_populate_custom_names_from_current_fixture_sets
create_and_blow_away_old_db
fixture_sets = ActiveRecord::FixtureSet.create_fixtures(
test_path("legacy_fixtures"),
MagicalCreature.table_name
Expand Down
97 changes: 0 additions & 97 deletions test/support/test_database.rb

This file was deleted.

70 changes: 70 additions & 0 deletions test/support/test_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

# Shared fixed-name schemas used by the FixtureBuilder tests.
#
# FixtureBuilder derives model and fixture names from the table name, so these
# tables deliberately use their production-like fixed names rather than
# with_model's generated names.
module TestSchema
GENERATED_CREATURES_TABLE = "generated_creatures"
GENERATED_COLUMN_RECORDS_TABLE = "generated_column_records"
RELOCATED_CREATURES_TABLE = "relocated_creatures"
CREATURE_ARCHIVE_TABLE = "creature_archive"

# The only tables created solely to exercise generated columns, and therefore
# the only fixture files a run is allowed to delete. `magical_creatures` is
# user-authored fixture data and is deliberately absent.
GENERATED_TEST_TABLES = [
GENERATED_CREATURES_TABLE,
GENERATED_COLUMN_RECORDS_TABLE,
RELOCATED_CREATURES_TABLE,
CREATURE_ARCHIVE_TABLE
].freeze

def self.included(test_case)
# Tables are recreated for every test, so legacy fixtures need Rails' cache
# cleared before each setup can load them again.
test_case.setup(before: :prepend) { ActiveRecord::FixtureSet.reset_cache }

test_case.with_table(:magical_creatures) do |t|
t.string :name
t.string :species
t.string :powers
t.json :wizard_data
t.date :born_on
t.boolean :deleted, default: false, null: false
end

test_case.with_table(GENERATED_CREATURES_TABLE) do |t|
t.string :name, null: false
t.virtual :name_length, type: :integer, as: "length(name)", stored: true
end

test_case.with_table(GENERATED_COLUMN_RECORDS_TABLE) do |t|
t.string :name, null: false
t.virtual :name_length, type: :integer, as: "length(name)", stored: true
end

test_case.with_table(RELOCATED_CREATURES_TABLE) do |t|
t.string :unrelated
t.virtual :name, type: :string, as: "upper(unrelated)", stored: true
end

test_case.with_table(CREATURE_ARCHIVE_TABLE) do |t|
t.string :name, null: false
end

test_case.setup(after: :append) do
GeneratedCreature.reset_column_information
RelocatedCreature.reset_column_information
end

# FixtureBuilder writes a fixture file for every generated-column table it
# iterates. Delete exactly those files, never repository-owned fixtures.
test_case.teardown(after: :append) do
GENERATED_TEST_TABLES.each do |table_name|
FileUtils.rm_f(test_path("fixtures/#{table_name}.yml"))
end
end
end
end
8 changes: 6 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def test_path(glob)
require "active_support/concern"
require "active_record"
require "active_record/fixtures"
require "sqlite3"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

require "with_model/test_unit"

def create_fixtures(*table_names, &block)
fixture_set = ActiveRecord::FixtureSet
Expand All @@ -41,9 +46,8 @@ def create_fixtures(*table_names, &block)
end
end

require "sqlite3"
require "fixture_builder"
require_relative "support/test_database"
require_relative "support/test_schema"

class WizardData
attr_reader :level, :title, :allies
Expand Down