-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
273 additions
and
234 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
RSpec.describe "Kangaru application configuration", with_gem: :some_gem do | ||
before { gem.main_file.write(main_file) } | ||
|
||
let(:main_file) do | ||
<<~RUBY | ||
require "kangaru" | ||
module SomeGem | ||
extend Kangaru::Initialiser | ||
#{config_block} | ||
end | ||
RUBY | ||
end | ||
|
||
let(:config_block) do | ||
<<~RUBY | ||
configure do |config| | ||
#{config} | ||
end | ||
RUBY | ||
end | ||
|
||
describe "database configuration" do | ||
describe "setup" do | ||
let(:config) do | ||
<<~RUBY | ||
#{adaptor_config} | ||
#{path_config} | ||
RUBY | ||
end | ||
|
||
shared_examples :does_not_set_database do | ||
it "does not raise any errors" do | ||
expect { gem.load! }.not_to raise_error | ||
end | ||
|
||
it "does not set a database" do | ||
gem.load! | ||
expect(Kangaru.application.database).to be_nil | ||
end | ||
end | ||
|
||
shared_examples :sets_database do | ||
it "does not raise any errors" do | ||
expect { gem.load! }.not_to raise_error | ||
end | ||
|
||
it "sets the application database instance" do | ||
gem.load! | ||
expect(Kangaru.application.database).to be_a(Kangaru::Database) | ||
end | ||
|
||
it "sets the database path" do | ||
gem.load! | ||
expect(Kangaru.application.database.path).to eq(expected_path) | ||
end | ||
end | ||
|
||
context "when adaptor is not set" do | ||
let(:adaptor_config) { nil } | ||
let(:path_config) { nil } | ||
|
||
include_examples :does_not_set_database | ||
end | ||
|
||
context "when adaptor is set" do | ||
let(:adaptor_config) do | ||
<<~RUBY | ||
config.database.adaptor = :#{adaptor} | ||
RUBY | ||
end | ||
|
||
context "and adaptor is invalid" do | ||
let(:adaptor) { :invalid } | ||
let(:path_config) { nil } | ||
|
||
it "raises an error" do | ||
expect { gem.load! }.to raise_error( | ||
Kangaru::Database::AdaptorError, "invalid adaptor 'invalid'" | ||
) | ||
end | ||
end | ||
|
||
context "and adaptor is sqlite" do | ||
let(:adaptor) { :sqlite } | ||
|
||
context "and database path is not set" do | ||
let(:path_config) { nil } | ||
|
||
it "raises an error" do | ||
expect { gem.load! }.to raise_error( | ||
Kangaru::Database::SQLiteError, "path can't be blank" | ||
) | ||
end | ||
end | ||
|
||
context "and database path is set" do | ||
let(:path_config) do | ||
<<~RUBY | ||
config.database.path = "#{path}" | ||
RUBY | ||
end | ||
|
||
let(:path) { gem.gem_path("database", ext: :sqlite3).to_s } | ||
|
||
include_examples :sets_database do | ||
let(:expected_path) { path } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "migrations" do | ||
let(:config) do | ||
<<~RUBY | ||
config.database.adaptor = :sqlite | ||
config.database.path = "#{gem.gem_path('db', ext: :sqlite3)}" | ||
#{migration_config} | ||
RUBY | ||
end | ||
|
||
shared_examples :does_not_migrate_database do | ||
let(:tables) { Kangaru.application.database.handler.tables } | ||
|
||
it "does not apply any migrations" do | ||
gem.load! | ||
expect(tables).to be_empty | ||
end | ||
end | ||
|
||
shared_examples :migrates_database do | ||
let(:tables) { Kangaru.application.database.handler.tables } | ||
|
||
it "applies the migrations" do | ||
gem.load! | ||
expect(tables).to include(*table_names) | ||
end | ||
end | ||
|
||
shared_context :write_migrations do |options| | ||
let(:table_names) { options[:tables] } | ||
|
||
before do | ||
table_names.each.with_index(1) do |table, index| | ||
write_migration!(migration_path, table, index) | ||
end | ||
end | ||
|
||
def write_migration!(migration_path, table, index) | ||
migration_path.join("#{index}_#{table}.rb").write(<<~RUBY) | ||
Sequel.migration do | ||
change do | ||
create_table :#{table} do | ||
primary_key :id | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "and migration path is not set" do | ||
let(:migration_config) { nil } | ||
|
||
include_examples :does_not_migrate_database | ||
end | ||
|
||
context "and migration path is set" do | ||
let(:migration_config) do | ||
<<~RUBY | ||
config.database.migration_path = "#{migration_path}" | ||
RUBY | ||
end | ||
|
||
let(:db_path) { gem.gem_path("db") } | ||
let(:migration_path) { db_path.join("migrate") } | ||
|
||
before { db_path.mkdir } | ||
|
||
context "and migration directory does not exist" do | ||
include_examples :does_not_migrate_database | ||
end | ||
|
||
context "and migration directory exists" do | ||
before { migration_path.mkdir } | ||
|
||
context "and no migrations are present" do | ||
include_examples :does_not_migrate_database | ||
end | ||
|
||
context "and migrations are present" do | ||
include_context :write_migrations, tables: %i[foo bar baz] | ||
|
||
include_examples :migrates_database, tables: %i[foo bar baz] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "application configuration" do | ||
describe "external config" do | ||
shared_examples :does_not_set_external_config do | ||
let(:external_config) do | ||
Kangaru.application.config.external.serialise | ||
end | ||
|
||
it "does not raise any errors" do | ||
expect { gem.load! }.not_to raise_error | ||
end | ||
|
||
it "does not set any external config" do | ||
gem.load! | ||
expect(external_config).to be_empty | ||
end | ||
end | ||
|
||
shared_examples :sets_external_config do |options| | ||
let(:external_config) do | ||
Kangaru.application.config.external.serialise | ||
end | ||
|
||
let(:expected) { options[:to] } | ||
|
||
it "does not raise any errors" do | ||
expect { gem.load! }.not_to raise_error | ||
end | ||
|
||
it "sets the external config" do | ||
gem.load! | ||
expect(external_config).to eq(expected) | ||
end | ||
end | ||
|
||
context "when config_path is not set" do | ||
let(:config) { nil } | ||
|
||
include_examples :does_not_set_external_config | ||
end | ||
|
||
context "when config_path is set" do | ||
let(:config) do | ||
<<~RUBY | ||
config.application.config_path = "#{config_path}" | ||
RUBY | ||
end | ||
|
||
let(:config_path) { gem.gem_path("config", ext: :yml) } | ||
|
||
context "and no config exists at the specified path" do | ||
include_examples :does_not_set_external_config | ||
end | ||
|
||
context "and config exists at the specified path" do | ||
let(:config_file) do | ||
<<~YAML | ||
frodo: | ||
race: hobbit | ||
age: 48 | ||
YAML | ||
end | ||
|
||
before { config_path.write(config_file) } | ||
|
||
include_examples :sets_external_config, | ||
to: { frodo: { race: "hobbit", age: 48 } } | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.