From 4ba02e01a5f2252b3a899b407c751f76ffd37b1e Mon Sep 17 00:00:00 2001 From: Patrick Karsh Date: Wed, 8 Feb 2023 16:19:40 -0500 Subject: [PATCH 1/5] updated ruby version, ran rubocop --- .ruby-version | 2 +- .travis.yml | 2 +- Gemfile | 2 + Rakefile | 4 +- fixture_builder.gemspec | 31 +++++----- lib/fixture_builder.rb | 4 +- lib/fixture_builder/builder.rb | 43 +++++++------ lib/fixture_builder/configuration.rb | 60 +++++++++++-------- lib/fixture_builder/delegations.rb | 11 ++-- lib/fixture_builder/fixtures_path.rb | 14 +++-- lib/fixture_builder/namer.rb | 41 +++++++------ lib/fixture_builder/version.rb | 2 + lib/tasks/fixture_builder.rake | 18 +++--- test/fixture_builder_test.rb | 47 ++++++++------- ...cy_fixture_mode_fixture_generation_test.rb | 33 +++++----- test/legacy_fixture_mode_test.rb | 12 ++-- test/namer_test.rb | 20 ++++--- test/test_helper.rb | 28 ++++----- 18 files changed, 209 insertions(+), 165 deletions(-) diff --git a/.ruby-version b/.ruby-version index d4bcea9..ff365e0 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-2.5.3 +3.1.3 diff --git a/.travis.yml b/.travis.yml index dd6e4d6..98b72c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: ruby rvm: - - 2.5.3 + - 3.1.3 before_install: - gem update --system - gem update bundler diff --git a/Gemfile b/Gemfile index d65e2a6..f7200f1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'http://rubygems.org' gemspec diff --git a/Rakefile b/Rakefile index 568b3ba..97e24c4 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'bundler' include Rake::DSL if defined?(Rake::DSL) Bundler::GemHelper.install_tasks @@ -8,4 +10,4 @@ Rake::TestTask.new(:test) do |t| t.verbose = false end -task :default => :test +task default: :test diff --git a/fixture_builder.gemspec b/fixture_builder.gemspec index 34c7013..545fa33 100644 --- a/fixture_builder.gemspec +++ b/fixture_builder.gemspec @@ -1,15 +1,16 @@ -# -*- encoding: utf-8 -*- -$:.push File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +$LOAD_PATH.push File.expand_path('lib', __dir__) require 'fixture_builder/version' Gem::Specification.new do |s| - s.name = %q{fixture_builder} + s.name = 'fixture_builder' s.version = FixtureBuilder::VERSION s.platform = Gem::Platform::RUBY s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version= s.authors = ['Ryan Dy', 'David Stevenson', 'Chad Woolley'] - s.description = %q{FixtureBuilder allows testers to use their existing factories, like FactoryGirl, to generate high performance fixtures that can be shared across all your tests and development environment. The best of all worlds! Speed, Maintainability, Flexibility, Consistency, and Simplicity!} - s.email = %q{mail@ryandy.com} + s.description = 'FixtureBuilder allows testers to use their existing factories, like FactoryGirl, to generate high performance fixtures that can be shared across all your tests and development environment. The best of all worlds! Speed, Maintainability, Flexibility, Consistency, and Simplicity!' + s.email = 'mail@ryandy.com' s.licenses = ['MIT'] s.extra_rdoc_files = [ 'README.markdown' @@ -19,15 +20,15 @@ Gem::Specification.new do |s| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.require_paths = ['lib'] - s.homepage = %q{http://github.com/rdy/fixture_builder} - s.rubyforge_project = %q{fixture_builder} - s.summary = %q{Build Rails fixtures using object mother factories} + s.homepage = 'http://github.com/rdy/fixture_builder' + s.rubyforge_project = 'fixture_builder' + s.summary = 'Build Rails fixtures using object mother factories' - s.add_dependency %q{activerecord}, '>= 2' - s.add_dependency %q{activesupport}, '>= 2' - s.add_dependency %q{hashdiff} - s.add_development_dependency %q{rake} - s.add_development_dependency %q{rails}, '>= 2' - s.add_development_dependency %q{test-unit} - s.add_development_dependency %q{sqlite3} + s.add_dependency 'activerecord', '>= 2' + s.add_dependency 'activesupport', '>= 2' + s.add_dependency 'hashdiff' + s.add_development_dependency 'rails', '>= 2' + s.add_development_dependency 'rake' + s.add_development_dependency 'sqlite3' + s.add_development_dependency 'test-unit' end diff --git a/lib/fixture_builder.rb b/lib/fixture_builder.rb index e7f99fe..65653b9 100644 --- a/lib/fixture_builder.rb +++ b/lib/fixture_builder.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'fixture_builder/delegations' require 'fixture_builder/configuration' require 'fixture_builder/namer' @@ -18,7 +20,7 @@ def configure(opts = {}) begin class Railtie < ::Rails::Railtie rake_tasks do - load "tasks/fixture_builder.rake" + load 'tasks/fixture_builder.rake' end end rescue LoadError, NameError diff --git a/lib/fixture_builder/builder.rb b/lib/fixture_builder/builder.rb index 83e5d9a..3e63ba7 100644 --- a/lib/fixture_builder/builder.rb +++ b/lib/fixture_builder/builder.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module FixtureBuilder class Builder include Delegations::Namer @@ -10,19 +12,19 @@ def initialize(configuration, namer, builder_block) end def generate! - say "Building fixtures" + say 'Building fixtures' clean_out_old_data create_fixture_objects names_from_ivars! write_data_to_files - after_build.call if after_build + after_build&.call end protected def create_fixture_objects load_legacy_fixtures if legacy_fixtures.present? - surface_errors { instance_eval &@builder_block } + surface_errors { instance_eval(&@builder_block) } end def load_legacy_fixtures @@ -45,11 +47,11 @@ def fixtures_class def surface_errors yield - rescue Object => error + rescue Object => e puts - say "There was an error building fixtures", error.inspect + say 'There was an error building fixtures', e.inspect puts - puts error.backtrace + puts e.backtrace puts exit! end @@ -73,12 +75,17 @@ def clean_out_old_data def delete_tables ActiveRecord::Base.connection.disable_referential_integrity do - tables.each { |t| ActiveRecord::Base.connection.delete(delete_sql % {table: ActiveRecord::Base.connection.quote_table_name(t)}) } + tables.each do |t| + ActiveRecord::Base.connection.delete(format(delete_sql, + table: ActiveRecord::Base.connection.quote_table_name(t))) + end end end def delete_yml_files - FileUtils.rm(*tables.map { |t| fixture_file(t) }) rescue nil + FileUtils.rm(*tables.map { |t| fixture_file(t) }) + rescue StandardError + nil end def say(*messages) @@ -96,19 +103,23 @@ def dump_tables Date::DATE_FORMATS[:default] = Date::DATE_FORMATS[:db] begin fixtures = tables.inject([]) do |files, table_name| - table_klass = table_name.classify.constantize rescue nil + table_klass = begin + table_name.classify.constantize + rescue StandardError + nil + end if table_klass && table_klass < ActiveRecord::Base rows = table_klass.unscoped do table_klass.order(:id).all.collect do |obj| attrs = obj.attributes.select { |attr_name| table_klass.column_names.include?(attr_name) } - attrs.inject({}) do |hash, (attr_name, value)| + attrs.each_with_object({}) do |(attr_name, value), hash| hash[attr_name] = serialized_value_if_needed(table_klass, attr_name, value) - hash end end end else - rows = ActiveRecord::Base.connection.select_all(select_sql % {table: ActiveRecord::Base.connection.quote_table_name(table_name)}) + rows = ActiveRecord::Base.connection.select_all(format(select_sql, + table: ActiveRecord::Base.connection.quote_table_name(table_name))) end next files if rows.empty? @@ -138,12 +149,10 @@ def serialized_value_if_needed(table_klass, attr_name, value) else table_klass.type_for_attribute(attr_name).type_cast_for_schema(value) end + elsif table_klass.serialized_attributes.key? attr_name + table_klass.serialized_attributes[attr_name].dump(value) else - if table_klass.serialized_attributes.has_key? attr_name - table_klass.serialized_attributes[attr_name].dump(value) - else - value - end + value end end diff --git a/lib/fixture_builder/configuration.rb b/lib/fixture_builder/configuration.rb index 056d29c..556d3c0 100644 --- a/lib/fixture_builder/configuration.rb +++ b/lib/fixture_builder/configuration.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'active_support/core_ext' require 'active_support/core_ext/string' require 'digest' @@ -5,24 +7,25 @@ require 'hashdiff' module FixtureBuilder - if Object.const_defined?(:Hashdiff) - # hashdiff version >= 1.0.0 - Differ = Hashdiff - else - Differ = HashDiff - end + Differ = if Object.const_defined?(:Hashdiff) + # hashdiff version >= 1.0.0 + Hashdiff + else + HashDiff + end class Configuration include Delegations::Namer - ACCESSIBLE_ATTRIBUTES = [:select_sql, :delete_sql, :skip_tables, :files_to_check, :record_name_fields, - :fixture_builder_file, :fixture_directory, :after_build, :legacy_fixtures, :model_name_procs, - :write_empty_files] + ACCESSIBLE_ATTRIBUTES = %i[select_sql delete_sql skip_tables files_to_check record_name_fields + fixture_builder_file fixture_directory after_build legacy_fixtures model_name_procs + write_empty_files].freeze attr_accessor(*ACCESSIBLE_ATTRIBUTES) - SCHEMA_FILES = ['db/schema.rb', 'db/development_structure.sql', 'db/test_structure.sql', 'db/production_structure.sql'] + SCHEMA_FILES = ['db/schema.rb', 'db/development_structure.sql', 'db/test_structure.sql', + 'db/production_structure.sql'].freeze - def initialize(opts={}) + def initialize(opts = {}) @namer = Namer.new(self) @use_sha1_digests = opts[:use_sha1_digests] || false @file_hashes = file_hashes @@ -40,36 +43,39 @@ def include(*args) def factory(&block) self.files_to_check += @legacy_fixtures.to_a return unless rebuild_fixtures? + @builder = Builder.new(self, @namer, block).generate! write_config end def select_sql - @select_sql ||= "SELECT * FROM %{table}" + @select_sql ||= 'SELECT * FROM %s' end def select_sql=(sql) if sql =~ /%s/ - ActiveSupport::Deprecation.warn("Passing '%s' into select_sql is deprecated. Please use '%{table}' instead.", caller) - sql = sql.sub(/%s/, '%{table}') + ActiveSupport::Deprecation.warn("Passing '%s' into select_sql is deprecated. Please use '%
s' instead.", + caller) + sql = sql.sub(/%s/, '%
s') end @select_sql = sql end def delete_sql - @delete_sql ||= "DELETE FROM %{table}" + @delete_sql ||= 'DELETE FROM %
s' end def delete_sql=(sql) if sql =~ /%s/ - ActiveSupport::Deprecation.warn("Passing '%s' into delete_sql is deprecated. Please use '%{table}' instead.", caller) - sql = sql.sub(/%s/, '%{table}') + ActiveSupport::Deprecation.warn("Passing '%s' into delete_sql is deprecated. Please use '%
s' instead.", + caller) + sql = sql.sub(/%s/, '%
s') end @delete_sql = sql end def skip_tables - @skip_tables ||= %w{ schema_migrations } + @skip_tables ||= %w[schema_migrations] end def files_to_check @@ -77,9 +83,8 @@ def files_to_check end def schema_definition_files - Dir['db/*'].inject([]) do |result, file| + Dir['db/*'].each_with_object([]) do |file, result| result << file if SCHEMA_FILES.include?(file) - result end end @@ -90,7 +95,7 @@ def files_to_check=(files) end def record_name_fields - @record_name_fields ||= %w{ unique_name display_name name title username login } + @record_name_fields ||= %w[unique_name display_name name title username login] end def fixture_builder_file @@ -117,14 +122,14 @@ def fixtures_dir(path = '') def file_hashes algorithm = @use_sha1_digests ? Digest::SHA1 : Digest::MD5 - files_to_check.inject({}) do |hash, filename| + files_to_check.each_with_object({}) do |filename, hash| hash[filename] = algorithm.hexdigest(File.read(filename)) - hash end end def read_config return {} unless File.exist?(fixture_builder_file) + YAML.load_file(fixture_builder_file) end @@ -134,8 +139,8 @@ def write_config end def rebuild_fixtures? - file_hashes_from_disk= @file_hashes - file_hashes_from_config= read_config + file_hashes_from_disk = @file_hashes + file_hashes_from_config = read_config if Dir.glob("#{fixture_directory}/*.yml").blank? puts "=> rebuilding fixtures because fixture directory #{fixture_directory} has no *.yml files" return true @@ -144,7 +149,10 @@ def rebuild_fixtures? return true elsif file_hashes_from_disk != file_hashes_from_config puts '=> rebuilding fixtures because one or more of the following files have changed (see http://www.rubydoc.info/gems/hashdiff for diff syntax):' - Differ.diff(file_hashes_from_disk, file_hashes_from_config).map {|diff| print ' '; p diff} + Differ.diff(file_hashes_from_disk, file_hashes_from_config).map do |diff| + print ' ' + p diff + end return true end false diff --git a/lib/fixture_builder/delegations.rb b/lib/fixture_builder/delegations.rb index ad39a26..af4fcde 100644 --- a/lib/fixture_builder/delegations.rb +++ b/lib/fixture_builder/delegations.rb @@ -1,20 +1,23 @@ +# frozen_string_literal: true + require 'active_support/core_ext/module/delegation' module FixtureBuilder module Delegations module Configuration def self.included(base) - methods_to_delegate = [:fixtures_dir, :tables, :legacy_fixtures].concat(::FixtureBuilder::Configuration::ACCESSIBLE_ATTRIBUTES).flatten + methods_to_delegate = %i[fixtures_dir tables + legacy_fixtures].concat(::FixtureBuilder::Configuration::ACCESSIBLE_ATTRIBUTES).flatten methods_to_delegate.each do |meth| - base.delegate(meth, :to => :@configuration) + base.delegate(meth, to: :@configuration) end end end module Namer def self.included(base) - base.delegate :record_name, :populate_custom_names, :name, :name_model_with, :to => :@namer + base.delegate :record_name, :populate_custom_names, :name, :name_model_with, to: :@namer end end end -end \ No newline at end of file +end diff --git a/lib/fixture_builder/fixtures_path.rb b/lib/fixture_builder/fixtures_path.rb index 3392be5..57ccf8a 100644 --- a/lib/fixture_builder/fixtures_path.rb +++ b/lib/fixture_builder/fixtures_path.rb @@ -1,14 +1,16 @@ +# frozen_string_literal: true + module FixtureBuilder class FixturesPath def self.absolute_rails_fixtures_path File.expand_path(ActiveRecord::Tasks::DatabaseTasks.fixtures_path) - rescue - if ENV["FIXTURES_PATH"] - ENV["FIXTURES_PATH"] - elsif File.exist?(File.expand_path("spec/fixtures",::Rails.root)) - File.expand_path("spec/fixtures",::Rails.root) + rescue StandardError + if ENV['FIXTURES_PATH'] + ENV['FIXTURES_PATH'] + elsif File.exist?(File.expand_path('spec/fixtures', ::Rails.root)) + File.expand_path('spec/fixtures', ::Rails.root) else - File.expand_path("test/fixtures",::Rails.root) + File.expand_path('test/fixtures', ::Rails.root) end end end diff --git a/lib/fixture_builder/namer.rb b/lib/fixture_builder/namer.rb index 953faf8..b248322 100644 --- a/lib/fixture_builder/namer.rb +++ b/lib/fixture_builder/namer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module FixtureBuilder class Namer include Delegations::Configuration @@ -14,11 +16,14 @@ def name_model_with(model_class, &block) end def name(custom_name, *model_objects) - raise "Cannot name an object blank" unless custom_name.present? + raise 'Cannot name an object blank' unless custom_name.present? + model_objects.each do |model_object| - raise "Cannot name a blank object" unless model_object.present? + raise 'Cannot name a blank object' unless model_object.present? + key = [model_object.class.table_name, model_object.id] raise "Cannot set name for #{key.inspect} object twice" if @custom_names[key] + @custom_names[key] = custom_name model_object end @@ -26,7 +31,7 @@ def name(custom_name, *model_objects) def populate_custom_names(created_fixtures) # Rails 3.1+, create_fixtures returns an array of Fixtures objects - if not created_fixtures.first.is_a? Array + unless created_fixtures.first.is_a? Array # merge all fixtures hashes created_fixtures = created_fixtures.inject({}) { |hash, fixtures| hash.merge(fixtures.fixtures) } end @@ -43,13 +48,12 @@ def populate_custom_names(created_fixtures) def record_name(record_hash, table_name, row_index) key = [table_name, record_hash['id'].to_i] - name = case - when name_proc = @model_name_procs[table_name] - name_proc.call(record_hash, row_index.succ!) - when custom = @custom_names[key] - custom - else - inferred_record_name(record_hash, table_name, row_index) + name = if (name_proc = @model_name_procs[table_name]) + name_proc.call(record_hash, row_index.succ!) + elsif (custom = @custom_names[key]) + custom + else + inferred_record_name(record_hash, table_name, row_index) end @record_names[table_name] ||= [] @record_names[table_name] << name @@ -57,18 +61,19 @@ def record_name(record_hash, table_name, row_index) end protected + def inferred_record_name(record_hash, table_name, row_index) record_name_fields.each do |try| - if name = record_hash[try] - inferred_name = name.underscore.gsub(/\W/, ' ').squeeze(' ').tr(' ', '_') - count = 0 - if @record_names[table_name] - count = @record_names[table_name].select {|name| name.to_s.starts_with?(inferred_name) }.size - end - return count.zero? ? inferred_name : "#{inferred_name}_#{count}" + next unless (name = record_hash[try]) + + inferred_name = name.underscore.gsub(/\W/, ' ').squeeze(' ').tr(' ', '_') + count = 0 + if @record_names[table_name] + count = @record_names[table_name].select { |name| name.to_s.starts_with?(inferred_name) }.size end + return count.zero? ? inferred_name : "#{inferred_name}_#{count}" end [table_name, row_index.succ!].join('_') end end -end \ No newline at end of file +end diff --git a/lib/fixture_builder/version.rb b/lib/fixture_builder/version.rb index 246fb2a..dcf19f6 100644 --- a/lib/fixture_builder/version.rb +++ b/lib/fixture_builder/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module FixtureBuilder VERSION = '0.5.3.rc3' end diff --git a/lib/tasks/fixture_builder.rake b/lib/tasks/fixture_builder.rake index 81b821d..4e79da4 100644 --- a/lib/tasks/fixture_builder.rake +++ b/lib/tasks/fixture_builder.rake @@ -1,19 +1,21 @@ +# frozen_string_literal: true + namespace :spec do namespace :fixture_builder do - desc "Deletes the generated fixtures in spec/fixtures" + desc 'Deletes the generated fixtures in spec/fixtures' task :clean do - FileUtils.rm_f("tmp/fixture_builder.yml") + FileUtils.rm_f('tmp/fixture_builder.yml') FileUtils.rm_f(Dir.glob("#{FixtureBuilder::FixturesPath.absolute_rails_fixtures_path}/*.yml")) - puts "Automatically generated fixtures removed" + puts 'Automatically generated fixtures removed' end - desc "Build the generated fixtures to spec/fixtures if dirty" - task :build => :environment do + desc 'Build the generated fixtures to spec/fixtures if dirty' + task build: :environment do ActiveRecord::Base.establish_connection(:test) - Dir.glob(File.join(::Rails.root, '{spec,test}', '**', 'fixture_builder.rb')).each{|file| require(file)} + Dir.glob(File.join(::Rails.root, '{spec,test}', '**', 'fixture_builder.rb')).each { |file| require(file) } end - desc "Clean and rebuild the generated fixtures to spec/fixtures" - task :rebuild => [:clean, :build] + desc 'Clean and rebuild the generated fixtures to spec/fixtures' + task rebuild: %i[clean build] end end diff --git a/test/fixture_builder_test.rb b/test/fixture_builder_test.rb index eac005d..3591003 100644 --- a/test/fixture_builder_test.rb +++ b/test/fixture_builder_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) class Model @@ -13,8 +15,8 @@ def teardown def test_name_with hash = { - 'id' => 1, - 'email' => 'bob@example.com' + 'id' => 1, + 'email' => 'bob@example.com' } FixtureBuilder.configure do |config| config.name_model_with Model do |record_hash, index| @@ -29,12 +31,12 @@ def test_ivar_naming force_fixture_generation FixtureBuilder.configure do |fbuilder| - fbuilder.files_to_check += Dir[test_path("*.rb")] + fbuilder.files_to_check += Dir[test_path('*.rb')] fbuilder.factory do - @king_of_gnomes = MagicalCreature.create(:name => 'robert', :species => 'gnome') + @king_of_gnomes = MagicalCreature.create(name: 'robert', species: 'gnome') end end - generated_fixture = YAML.load(File.open(test_path("fixtures/magical_creatures.yml"))) + generated_fixture = YAML.load(File.open(test_path('fixtures/magical_creatures.yml'))) assert_equal 'king_of_gnomes', generated_fixture.keys.first end @@ -43,13 +45,13 @@ def test_serialization force_fixture_generation FixtureBuilder.configure do |fbuilder| - fbuilder.files_to_check += Dir[test_path("*.rb")] + fbuilder.files_to_check += Dir[test_path('*.rb')] fbuilder.factory do - @enty = MagicalCreature.create(:name => 'Enty', :species => 'ent', - :powers => %w{shading rooting seeding}) + @enty = MagicalCreature.create(name: 'Enty', species: 'ent', + powers: %w[shading rooting seeding]) end end - generated_fixture = YAML.load(File.open(test_path("fixtures/magical_creatures.yml"))) + generated_fixture = YAML.load(File.open(test_path('fixtures/magical_creatures.yml'))) assert_equal "---\n- shading\n- rooting\n- seeding\n", generated_fixture['enty']['powers'] end @@ -58,9 +60,9 @@ def test_do_not_include_virtual_attributes force_fixture_generation FixtureBuilder.configure do |fbuilder| - fbuilder.files_to_check += Dir[test_path("*.rb")] + fbuilder.files_to_check += Dir[test_path('*.rb')] fbuilder.factory do - MagicalCreature.create(:name => 'Uni', :species => 'unicorn', :powers => %w{rainbows flying}) + MagicalCreature.create(name: 'Uni', species: 'unicorn', powers: %w[rainbows flying]) end end generated_fixture = YAML.load(File.open(test_path('fixtures/magical_creatures.yml'))) @@ -76,11 +78,12 @@ def test_configure end def test_absolute_rails_fixtures_path - assert_equal File.expand_path('../../test/fixtures', __FILE__), FixtureBuilder::FixturesPath.absolute_rails_fixtures_path + assert_equal File.expand_path('../test/fixtures', __dir__), + FixtureBuilder::FixturesPath.absolute_rails_fixtures_path end def test_fixtures_dir - assert_match /test\/fixtures$/, FixtureBuilder.configuration.send(:fixtures_dir).to_s + assert_match(%r{test/fixtures$}, FixtureBuilder.configuration.send(:fixtures_dir).to_s) end def test_rebuilding_due_to_differing_file_hashes @@ -88,13 +91,13 @@ def test_rebuilding_due_to_differing_file_hashes force_fixture_generation_due_to_differing_file_hashes FixtureBuilder.configure do |fbuilder| - fbuilder.files_to_check += Dir[test_path("*.rb")] + fbuilder.files_to_check += Dir[test_path('*.rb')] fbuilder.factory do - @enty = MagicalCreature.create(:name => 'Enty', :species => 'ent', - :powers => %w{shading rooting seeding}) + @enty = MagicalCreature.create(name: 'Enty', species: 'ent', + powers: %w[shading rooting seeding]) end end - generated_fixture = YAML.load(File.open(test_path("fixtures/magical_creatures.yml"))) + generated_fixture = YAML.load(File.open(test_path('fixtures/magical_creatures.yml'))) assert_equal "---\n- shading\n- rooting\n- seeding\n", generated_fixture['enty']['powers'] end @@ -103,15 +106,15 @@ def test_sha1_digests force_fixture_generation_due_to_differing_file_hashes FixtureBuilder.configure(use_sha1_digests: true) do |fbuilder| - fbuilder.files_to_check += Dir[test_path("*.rb")] + fbuilder.files_to_check += Dir[test_path('*.rb')] fbuilder.factory do - @enty = MagicalCreature.create(:name => 'Enty', :species => 'ent', - :powers => %w{shading rooting seeding}) + @enty = MagicalCreature.create(name: 'Enty', species: 'ent', + powers: %w[shading rooting seeding]) end - first_modified_time = File.mtime(test_path("fixtures/magical_creatures.yml")) + first_modified_time = File.mtime(test_path('fixtures/magical_creatures.yml')) fbuilder.factory do end - second_modified_time = File.mtime(test_path("fixtures/magical_creatures.yml")) + second_modified_time = File.mtime(test_path('fixtures/magical_creatures.yml')) assert_equal first_modified_time, second_modified_time end end diff --git a/test/legacy_fixture_mode_fixture_generation_test.rb b/test/legacy_fixture_mode_fixture_generation_test.rb index fc42438..2fd0bac 100644 --- a/test/legacy_fixture_mode_fixture_generation_test.rb +++ b/test/legacy_fixture_mode_fixture_generation_test.rb @@ -1,20 +1,21 @@ +# frozen_string_literal: true + require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) class LegacyFixtureModeFixtureGenerationTest < Test::Unit::TestCase - def setup create_and_blow_away_old_db force_fixture_generation FixtureBuilder.configure do |fbuilder| - fbuilder.legacy_fixtures = Dir[test_path("legacy_fixtures/*.yml"), test_path("other_legacy_fixture_set/*.yml")] + fbuilder.legacy_fixtures = Dir[test_path('legacy_fixtures/*.yml'), test_path('other_legacy_fixture_set/*.yml')] fbuilder.factory do - MagicalCreature.create(:name => "frank", :species => "unicorn") - MagicalCreature.create(:name => "loch ness monster", :species => "sea creature", :deleted => true) + MagicalCreature.create(name: 'frank', species: 'unicorn') + MagicalCreature.create(name: 'loch ness monster', species: 'sea creature', deleted: true) end end - @@magical_creatures = YAML.load(File.open(test_path("fixtures/magical_creatures.yml"))) + @@magical_creatures = YAML.load(File.open(test_path('fixtures/magical_creatures.yml'))) end def teardown @@ -22,28 +23,28 @@ def teardown end def test_legacy_fixtures_created - alice = MagicalCreature.find_by_name("alice") - assert_equal "alice", alice.name - assert_equal "mermaid", alice.species + alice = MagicalCreature.find_by_name('alice') + assert_equal 'alice', alice.name + assert_equal 'mermaid', alice.species end def test_invalid_legacy_fixtures_created bigfoot = MagicalCreature.find(5) - assert_equal "bigfoot", bigfoot.name + assert_equal 'bigfoot', bigfoot.name assert_equal 5, @@magical_creatures['bigfoot']['id'] - assert_equal "bigfoot", @@magical_creatures['bigfoot']['name'] + assert_equal 'bigfoot', @@magical_creatures['bigfoot']['name'] assert_nil @@magical_creatures['bigfoot']['species'] end def test_new_fixtures_are_created - assert_equal "frank", @@magical_creatures['frank']['name'] - assert_equal "unicorn", @@magical_creatures['frank']['species'] - assert_equal "loch ness monster", @@magical_creatures['loch_ness_monster']['name'] + assert_equal 'frank', @@magical_creatures['frank']['name'] + assert_equal 'unicorn', @@magical_creatures['frank']['species'] + assert_equal 'loch ness monster', @@magical_creatures['loch_ness_monster']['name'] end def test_legacy_fixtures_retain_fixture_name - assert_equal "alice", @@magical_creatures['alice_the_mermaid']['name'] - assert_equal "mermaid", @@magical_creatures['alice_the_mermaid']['species'] + assert_equal 'alice', @@magical_creatures['alice_the_mermaid']['name'] + assert_equal 'mermaid', @@magical_creatures['alice_the_mermaid']['species'] end -end \ No newline at end of file +end diff --git a/test/legacy_fixture_mode_test.rb b/test/legacy_fixture_mode_test.rb index 2605231..c246d9b 100644 --- a/test/legacy_fixture_mode_test.rb +++ b/test/legacy_fixture_mode_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) class LegacyFixtureModeTest < Test::Unit::TestCase @@ -12,7 +14,7 @@ def teardown def test_load_legacy_fixtures FixtureBuilder.configure do |fbuilder| - fbuilder.legacy_fixtures = Dir[test_path("legacy_fixtures/*.yml"), test_path("other_legacy_fixture_set/*.yml")] + fbuilder.legacy_fixtures = Dir[test_path('legacy_fixtures/*.yml'), test_path('other_legacy_fixture_set/*.yml')] fbuilder.factory do end end @@ -21,9 +23,9 @@ def test_load_legacy_fixtures def test_generate_new_fixtures_without_legacy FixtureBuilder.configure do |fbuilder| - fbuilder.files_to_check += Dir[test_path("*.rb")] + fbuilder.files_to_check += Dir[test_path('*.rb')] fbuilder.factory do - MagicalCreature.create :name => "Melinda", :species => "Philanthropist" + MagicalCreature.create name: 'Melinda', species: 'Philanthropist' end end assert_equal 1, MagicalCreature.all.size @@ -31,9 +33,9 @@ def test_generate_new_fixtures_without_legacy def test_new_and_old_fixtures FixtureBuilder.configure do |fbuilder| - fbuilder.legacy_fixtures = Dir[test_path("legacy_fixtures/*.yml"), test_path("other_legacy_fixture_set/*.yml")] + fbuilder.legacy_fixtures = Dir[test_path('legacy_fixtures/*.yml'), test_path('other_legacy_fixture_set/*.yml')] fbuilder.factory do - MagicalCreature.create :name => "Barry", :species => "Party Guy" + MagicalCreature.create name: 'Barry', species: 'Party Guy' end end assert_equal 4, MagicalCreature.all.size diff --git a/test/namer_test.rb b/test/namer_test.rb index 4cb60be..293a44d 100644 --- a/test/namer_test.rb +++ b/test/namer_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) class Model @@ -13,7 +15,7 @@ def self.table_name end class MockFixture - def self.[](*args) + def self.[](*_args) '1' end @@ -30,8 +32,8 @@ def setup def test_name_with hash = { - 'id' => 1, - 'email' => 'bob@example.com' + 'id' => 1, + 'email' => 'bob@example.com' } @namer.name_model_with Model do |record_hash, index| @@ -43,21 +45,21 @@ def test_name_with def test_record_name_without_name_with_or_custom_name hash = { - 'id' => 1, - 'email' => 'bob@example.com' + 'id' => 1, + 'email' => 'bob@example.com' } assert_equal 'models_001', @namer.record_name(hash, Model.table_name, '000') end def test_record_name_with_inferred_record_name hash = { - 'id' => 1, - 'title' => 'foo', - 'email' => 'bob@example.com' + 'id' => 1, + 'title' => 'foo', + 'email' => 'bob@example.com' } assert_equal 'foo', @namer.record_name(hash, Model.table_name, '000') end - + def test_name_not_unique_across_tables hash = { 'id' => 1, diff --git a/test/test_helper.rb b/test/test_helper.rb index 778720a..019fa2c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubygems' require 'bundler/setup' require 'test/unit' @@ -32,40 +34,36 @@ class MagicalCreature < ActiveRecord::Base serialize :powers, Array if ActiveRecord::VERSION::MAJOR >= 4 - default_scope -> { where(:deleted => false) } + default_scope -> { where(deleted: false) } attribute :virtual, ActiveRecord::Type::Integer.new else - default_scope :conditions => { :deleted => false } + default_scope conditions: { deleted: false } end end def create_and_blow_away_old_db ActiveRecord::Base.configurations['test'] = { - 'adapter' => 'sqlite3', - 'database' => 'test.db' + 'adapter' => 'sqlite3', + 'database' => 'test.db' } ActiveRecord::Base.establish_connection(:test) - ActiveRecord::Base.connection.create_table(:magical_creatures, :force => true) do |t| + ActiveRecord::Base.connection.create_table(:magical_creatures, force: true) do |t| t.column :name, :string t.column :species, :string t.column :powers, :string - t.column :deleted, :boolean, :default => false, :null => false + t.column :deleted, :boolean, default: false, null: false end end def force_fixture_generation - begin - FileUtils.rm(File.expand_path("../../tmp/fixture_builder.yml", __FILE__)) - rescue - end + FileUtils.rm(File.expand_path('../tmp/fixture_builder.yml', __dir__)) +rescue StandardError end def force_fixture_generation_due_to_differing_file_hashes - begin - path = File.expand_path("../../tmp/fixture_builder.yml", __FILE__) - File.write(path, "blah blah blah") - rescue - end + path = File.expand_path('../tmp/fixture_builder.yml', __dir__) + File.write(path, 'blah blah blah') +rescue StandardError end From 6824b1e89ae7156bfe902e6fe7bf9fc819fe4b1f Mon Sep 17 00:00:00 2001 From: Patrick Karsh Date: Thu, 9 Feb 2023 09:15:23 -0500 Subject: [PATCH 2/5] updated test helper syntax --- test/fixture_builder_test.rb | 2 +- test/test_helper.rb | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/test/fixture_builder_test.rb b/test/fixture_builder_test.rb index 3591003..9bf2e00 100644 --- a/test/fixture_builder_test.rb +++ b/test/fixture_builder_test.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: true +# frozen_string_literal: false require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) diff --git a/test/test_helper.rb b/test/test_helper.rb index 019fa2c..80521f3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -43,10 +43,8 @@ class MagicalCreature < ActiveRecord::Base end def create_and_blow_away_old_db - ActiveRecord::Base.configurations['test'] = { - 'adapter' => 'sqlite3', - 'database' => 'test.db' - } + ActiveRecord::Base.configurations = { 'test' => { 'adapter' => 'sqlite3', 'database' => 'test.db' } } + ActiveRecord::Base.establish_connection(:test) ActiveRecord::Base.connection.create_table(:magical_creatures, force: true) do |t| From 95aa7a9eb6b89bc41dc916b1ba78d8fdf9547762 Mon Sep 17 00:00:00 2001 From: Mike Enriquez Date: Thu, 9 Feb 2023 10:15:26 -0500 Subject: [PATCH 3/5] Ignore virtual columns --- lib/fixture_builder/builder.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/fixture_builder/builder.rb b/lib/fixture_builder/builder.rb index 3e63ba7..9425101 100644 --- a/lib/fixture_builder/builder.rb +++ b/lib/fixture_builder/builder.rb @@ -111,7 +111,11 @@ def dump_tables if table_klass && table_klass < ActiveRecord::Base rows = table_klass.unscoped do table_klass.order(:id).all.collect do |obj| - attrs = obj.attributes.select { |attr_name| table_klass.column_names.include?(attr_name) } + attrs = obj.attributes.select do |attr_name| + column = table_klass.columns.find { |c| c.name == attr_name } + !column.virtual? if column + end + attrs.each_with_object({}) do |(attr_name, value), hash| hash[attr_name] = serialized_value_if_needed(table_klass, attr_name, value) end From 20318fcec1b77afc450fbd073c216bedec708342 Mon Sep 17 00:00:00 2001 From: Mike Enriquez Date: Thu, 9 Feb 2023 10:57:06 -0500 Subject: [PATCH 4/5] Fix frozen string exception --- lib/fixture_builder/namer.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/fixture_builder/namer.rb b/lib/fixture_builder/namer.rb index b248322..7d38052 100644 --- a/lib/fixture_builder/namer.rb +++ b/lib/fixture_builder/namer.rb @@ -47,13 +47,14 @@ def populate_custom_names(created_fixtures) end def record_name(record_hash, table_name, row_index) + next_row_index = row_index.succ key = [table_name, record_hash['id'].to_i] name = if (name_proc = @model_name_procs[table_name]) - name_proc.call(record_hash, row_index.succ!) + name_proc.call(record_hash, next_row_index) elsif (custom = @custom_names[key]) custom else - inferred_record_name(record_hash, table_name, row_index) + inferred_record_name(record_hash, table_name, next_row_index) end @record_names[table_name] ||= [] @record_names[table_name] << name From bf3b4bb033d70582e0b315481dc24623b441741c Mon Sep 17 00:00:00 2001 From: Mike Enriquez Date: Thu, 9 Feb 2023 11:03:48 -0500 Subject: [PATCH 5/5] We have tables without an id column Revert "Stabilize order of records in YAML files" This reverts commit 70115d20a36c760e1d5fb41e5e1164b21a33f17d. --- lib/fixture_builder/builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fixture_builder/builder.rb b/lib/fixture_builder/builder.rb index 9425101..1743eb5 100644 --- a/lib/fixture_builder/builder.rb +++ b/lib/fixture_builder/builder.rb @@ -110,7 +110,7 @@ def dump_tables end if table_klass && table_klass < ActiveRecord::Base rows = table_klass.unscoped do - table_klass.order(:id).all.collect do |obj| + table_klass.all.collect do |obj| attrs = obj.attributes.select do |attr_name| column = table_klass.columns.find { |c| c.name == attr_name } !column.virtual? if column