From af2a2ce6e54fa81be7bcdc47597ed8488d4385f9 Mon Sep 17 00:00:00 2001 From: Pysis Date: Wed, 15 Nov 2023 17:59:31 -0500 Subject: [PATCH] Adding and fixing test cases Add generic error check test case. Make sure defaults are still being applied when configuration file does not exist. Move defaults section high in order of increasing dependency usage and complexity. Convert to checking entire, internal configuration and its members, along with the Rails configuration-matching path style. Add test cases for new 'db dir' property. Organize whitespace and other content in file within a narrow, more readable horizontal boundary, and align common data values in a similar, table format. Added to gitignore file where I had lingering files after running a broader `rspec` command, and also seems to never complete. `bundle exec rake spec` does not have this problem, and must be more specifically configured not to. Also added proper, and careful, handling for the init options, since there is no deep_merge.., without all of Rails? --- .gitignore | 1 + lib/standalone_migrations/configurator.rb | 14 +- .../configurator_spec.rb | 136 ++++++++++++------ 3 files changed, 104 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index e0e4887..3cc2183 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ schema.rb spec/tmp pkg Gemfile.lock +tmp diff --git a/lib/standalone_migrations/configurator.rb b/lib/standalone_migrations/configurator.rb index fcc3687..f30311f 100644 --- a/lib/standalone_migrations/configurator.rb +++ b/lib/standalone_migrations/configurator.rb @@ -2,7 +2,6 @@ require 'yaml' module StandaloneMigrations - class InternalConfigurationsProxy attr_reader :configurations @@ -32,6 +31,12 @@ def self.environments_config end def initialize(options = {}) + options = options.dup + @schema = options.delete('schema') + @config_overrides = defaults + c_os['paths'].merge!(options.delete('paths') || {}) + c_os.merge!(options) + load_from_file ENV['SCHEMA'] ||= @schema if @schema @@ -52,6 +57,9 @@ def config_for(environment) end def c_os + config_overrides + end + def config_overrides @config_overrides end @@ -88,6 +96,10 @@ def defaults } end + def schema + @schema + end + private def configuration_file diff --git a/spec/standalone_migrations/configurator_spec.rb b/spec/standalone_migrations/configurator_spec.rb index b082050..c1754a8 100644 --- a/spec/standalone_migrations/configurator_spec.rb +++ b/spec/standalone_migrations/configurator_spec.rb @@ -12,21 +12,47 @@ module StandaloneMigrations end end + it "does not break / emit an error" do + expect { Configurator.new }.not_to raise_error + end + + context "default values when .standalone_configurations is missing" do + let(:configurator) do + Configurator.new + end + + it "use config/database.yml" do + expect(configurator.c_os['paths']['config/database']).to eq('db/config.yml') + end + + it "use db dir" do + expect(configurator.c_os['paths']['db']).to eq('db') + end + + it "use db/migrate dir" do + expect(configurator.c_os['paths']['db/migrate']).to eq('db/migrate') + end + + it "use db/seeds.rb" do + expect(configurator.c_os['paths']['db/seeds.rb']).to eq("db/seeds.rb") + end + end + describe "environment yaml configuration loading" do let(:env_hash_other_db) do { "development" => {"adapter" => "mysql2", "database" => "database_name"}, - "test" => {"adapter" => "mysql2", "database" => "database_name"}, - "production" => {"adapter" => "mysql2", "database" => "database_name"} + "test" => {"adapter" => "mysql2", "database" => "database_name"}, + "production" => {"adapter" => "mysql2", "database" => "database_name"} } end around(:each) do |example| @env_hash = { "development" => {"adapter" => "sqlite3", "database" => "db/development.sql"}, - "test" => {"adapter" => "sqlite3", "database" => "db/test.sql"}, - "production" => {"adapter" => "sqlite3", "database" => ":memory:"} + "test" => {"adapter" => "sqlite3", "database" => "db/test.sql" }, + "production" => {"adapter" => "sqlite3", "database" => ":memory:" } } FileUtils.mkdir_p "db" File.open("db/config.yml", "w") do |f| @@ -88,31 +114,16 @@ module StandaloneMigrations end - context "default values when .standalone_configurations is missing" do - let(:configurator) do - Configurator.new - end - - it "use config/database.yml" do - expect(configurator.config).to eq('db/config.yml') - end - - it "use db/migrate dir" do - expect(configurator.migrate_dir).to eq('db/migrate') - end - - it "use db/seeds.rb" do - expect(configurator.seeds).to eq("db/seeds.rb") - end - end - context "passing configurations as a parameter" do let(:args) do { - :config => "custom/config/database.yml", - :migrate_dir => "custom/db/migrate", - :seeds => "custom/db/seeds.rb", - :schema => "custom/db/schema.rb" + 'paths' => { + 'config/database' => "custom/config/database.yml" , + 'db' => "db" , + 'db/migrate' => "custom/db/migrate" , + 'db/seeds.rb' => "custom/db/seeds.rb" , + }, + 'schema' => "custom/db/schema.rb" } end @@ -121,19 +132,31 @@ module StandaloneMigrations end it "use custom config" do - expect(configurator.config).to eq(args[:config]) + expect(configurator.c_os['paths']['config/database']).to( + eq(args['paths']['config/database']) + ) + end + + it "use custom db dir" do + expect(configurator.c_os['paths']['db']).to( + eq(args['paths']['db']) + ) end it "use custom migrate dir" do - expect(configurator.migrate_dir).to eq(args[:migrate_dir]) + expect(configurator.c_os['paths']['db/migrate']).to( + eq(args['paths']['db/migrate']) + ) end it "use custom seeds" do - expect(configurator.seeds).to eq(args[:seeds]) + expect(configurator.c_os['paths']['db/seeds.rb']).to( + eq(args['paths']['db/seeds.rb']) + ) end it "use custom schema" do - expect(configurator.schema).to eq(args[:schema]) + expect(configurator.schema).to eq(args['schema']) end end @@ -148,9 +171,10 @@ module StandaloneMigrations let(:yaml_hash) do { "db" => { - "seeds" => "file/db/seeds.rb", - "migrate" => "file/db/migrate", - "schema" => "file/db/schema.rb" + "dir" => "file/db" , + "migrate" => "file/db/migrate" , + "seeds" => "file/db/seeds.rb" , + "schema" => "file/db/schema.rb" }, "config" => { "database" => "file/config/database.yml" @@ -161,9 +185,10 @@ module StandaloneMigrations let(:yaml_hash_other_db) do { "db" => { - "seeds" => "db2/seeds.rb", - "migrate" => "db2/migrate", - "schema" => "db2/schema.rb" + "dir" => "db2" , + "migrate" => "db2/migrate" , + "seeds" => "db2/seeds.rb" , + "schema" => "db2/schema.rb" }, "config" => { "database" => "config/config_other.yml" @@ -180,7 +205,9 @@ module StandaloneMigrations before(:each) do ENV['DATABASE'] = "other_db" file_other_db = ".other_db.standalone_migrations" - File.open(file_other_db, "w") { |file| file.write(yaml_hash_other_db.to_yaml) } + File.open(file_other_db, "w") do |file| + file.write(yaml_hash_other_db.to_yaml) + end end let(:other_configurator) do @@ -188,11 +215,15 @@ module StandaloneMigrations end it "look up named dot file" do - expect(other_configurator.config).to eq(yaml_hash_other_db['config']['database']) + expect(other_configurator.c_os['paths']['config/database']).to( + eq(yaml_hash_other_db['config']['database']) + ) end it "load config from named dot file" do - expect(other_configurator.migrate_dir).to eq('db2/migrate') + expect(other_configurator.c_os['paths']['db/migrate']).to( + eq('db2/migrate') + ) end after(:all) do @@ -215,33 +246,46 @@ module StandaloneMigrations end it "use default values for the missing configurations" do - expect(configurator.migrate_dir).to eq('db/migrate') + expect(configurator.c_os['paths']['db']).to( + eq('db' ) + ) + expect(configurator.c_os['paths']['db/migrate']).to( + eq('db/migrate' ) + ) end it "use custom config from file" do - expect(configurator.config).to eq(yaml_hash["config"]["database"]) + expect(configurator.c_os['paths']['config/database']).to( + eq(yaml_hash["config"]["database"]) + ) end it "use custom config value from partial configuration" do - expect(configurator.seeds).to eq(yaml_hash["db"]["seeds"]) + expect(configurator.c_os['paths']['db/seeds.rb']).to( + eq(yaml_hash["db"]["seeds"]) + ) end end it "use custom config from file" do - expect(configurator.config).to eq(yaml_hash["config"]["database"]) + expect(configurator.c_os['paths']['config/database']).to( + eq(yaml_hash["config"]["database"]) + ) end it "use custom migrate dir from file" do - expect(configurator.migrate_dir).to eq(yaml_hash["db"]["migrate"]) + expect(configurator.c_os['paths']['db/migrate']).to eq(yaml_hash["db"]["migrate"]) end it "use custom seeds from file" do - expect(configurator.seeds).to eq(yaml_hash["db"]["seeds"]) + expect(configurator.c_os['paths']['db/seeds.rb']).to eq(yaml_hash["db"]["seeds"]) end it "use custom schema from file" do - expect(configurator.schema).to eq(yaml_hash["db"]["schema"]) + expect(configurator.schema).to( + eq(yaml_hash["db"]["schema"]) + ) end end