lib/flextures/rspec_flextures_support.rb in flextures-3.1.3 vs lib/flextures/rspec_flextures_support.rb in flextures-4.0.0
- old
+ new
@@ -1,100 +1,154 @@
-# encoding: utf-8
-
-
+# flextures function use like fixtures method in RSpec
module RSpec
module Rails
- module SetupAndTeardownAdapter
- extend ActiveSupport::Concern
+ module FlextureSupport
+ @@once_included = false
+ def self.included(m)
+ init_tables
+ end
- module ClassMethods
- def flextures_prepend_before(&block)
- prepend_before(&block)
+ def self.init_tables
+ unless @@once_included
+ Flextures::init_tables
+ @@once_included = true
end
-
- def flextures_before(&block)
- before(&block)
- end
end
end
end
+
+ RSpec.configure do |c|
+ c.include RSpec::Rails::FlextureSupport
+ end
end
-# flextures function use like fixtures method in RSpec
-module RSpec
- module Core
- module Hooks
- # load fixtture data
- # @params [Array] _ fixture file names
- def flextures( *_ )
- flextures_loader = create_or_get_flextures_loader(__method__)
- flextures_before do
- flextures_loader.loads( *_ )
+# override setup_fixtures function
+module ActiveRecord
+ module TestFixtures
+ PARENT = self
+ @@flextures_loader = Flextures::Loader.new
+ @@all_cached_flextures = {}
+ @@already_loaded_flextures = {}
+
+ alias :setup_fixtures_bkup :setup_fixtures
+ def setup_fixtures
+ Flextures::load_configurations
+ setup_fixtures_bkup
+ end
+
+ alias :teardown_fixtures_bkup :teardown_fixtures
+ def teardown_fixtures
+ teardown_fixtures_bkup
+ end
+
+ # load initial fixtures
+ # There is fixtures load before start rspec
+ def self.init_load_should_cache_fixtures(table_load_settings)
+ table_load_settings.each do |load_setting|
+ if should_cache_setting?(load_setting) and !cached_table?(load_setting)
+ @@flextures_loader.load(load_setting)
+ set_cached_settng_list(load_setting)
end
end
+ end
- # flexturesの読み出し
- def create_or_get_flextures_loader(*_)
- @@flextures_loader ||= Flextures::Loader.new(*_)
+ # Usually, fixture is cached when is exist under "spec/fixture/" directly.
+ def self.should_cache_setting?(load_setting)
+ load_setting.keys.sort == %i[table file loader].sort &&
+ load_setting[:file].to_s == load_setting[:table].to_s &&
+ load_setting[:loader] == :fun
+ end
+
+ # check: same data is exist in DB.
+ def self.cached_table?(load_setting)
+ flextures_cached?(load_setting) || fixture_cached?(load_setting)
+ end
+
+ def self.flextures_cached?(load_setting)
+ config = @@all_cached_flextures[load_setting[:table]]
+ config && config == load_setting
+ end
+
+ # flextures check fixture function already loaded data.
+ def self.fixture_cached?(load_setting)
+ default_file_path = File.join(Flextures::Configuration.load_directory, "#{load_setting[:table]}.yml")
+
+ load_setting[:file] == default_file_path &&
+ yml_fixture_cached?(load_setting[:table])
+ end
+
+ def self.yml_fixture_cached?(table_name)
+ connection = ActiveRecord::Base.connection
+ !!ActiveRecord::FixtureSet.fixture_is_cached?(connection, table_name)
+ end
+
+ def self.set_cached_settng_list(load_setting)
+ @@all_cached_flextures[load_setting[:table]] = load_setting
+ end
+
+ def load_not_cached_fixtures(table_load_settings)
+ table_load_settings.each do |load_setting|
+ if PARENT.cached_table?(load_setting) and load_setting[:cache] != false
+ next
+ else
+ @@flextures_loader.load(load_setting)
+ end
end
+ end
- def flextures_get_options
- @@flextures_loader
+ def load_all_fixtures(table_load_settings)
+ table_load_settings.each do |load_setting|
+ @@flextures_loader.load(load_setting)
end
+ end
+ module ClassMethods
+ def get_or_initialize_flextures_loader_options
+ @flextures_loader_options ||= {}
+ end
+
+ def flextures_loader_options
+ get_or_initialize_flextures_loader_options
+ end
+
+ def flextures_loader
+ PARENT.class_variable_get(:@@flextures_loader)
+ end
+
+ def flextures(*fixtures)
+ loads_use_cache_fixtures(*fixtures)
+ end
+
+ def loads_use_cache_fixtures(*fixtures)
+ table_load_settings = Flextures::Loader.parse_flextures_options(flextures_loader_options, *fixtures)
+
+ if use_transactional_fixtures
+ PARENT.init_load_should_cache_fixtures(table_load_settings)
+ before do
+ load_not_cached_fixtures(table_load_settings)
+ end
+ else
+ before do
+ load_all_fixtures(table_load_settings)
+ end
+ end
+ end
+
# delete table data
# @params [Array] _ table names
- def flextures_delete( *_ )
- flextures_loader = create_or_get_flextures_loader(__method__)
- flextures_before do
+ def flextures_delete(*_)
+ before do
if _.empty?
Flextures::init_tables
else
- Flextures::delete_tables( *_ )
+ Flextures::delete_tables(*_)
end
end
end
- def flextures_set_options( options )
- flextures_loader = create_or_get_flextures_loader(__method__)
- flextures_prepend_before do
- flextures_loader.set_options( options )
- end
+ def flextures_set_options(options)
+ @flextures_loader_options = get_or_initialize_flextures_loader_options
+ @flextures_loader_options = @flextures_loader_options.merge(options)
end
- end
- end
-
- module Rails
- module FlextureSupport
- def self.included(m)
- Flextures::init_tables
- end
- end
- end
-
- RSpec.configure do |c|
- c.include RSpec::Rails::FlextureSupport
- end
-end
-
-# override setup_fixtures function
-module ActiveRecord
- module TestFixtures
- alias :setup_fixtures_bkup :setup_fixtures
- def setup_fixtures
- Flextures::init_load
- setup_fixtures_bkup
- set_transactional_filter_params
- end
-
- # nilで無い時は値をtransactional_filterが有効
- def set_transactional_filter_params
- return if Flextures::Config.use_transactional_fixtures.nil?
- self.use_transactional_fixtures = Flextures::Config.use_transactional_fixtures
- end
-
- alias :teardown_fixtures_bkup :teardown_fixtures
- def teardown_fixtures
- teardown_fixtures_bkup
end
end
end