Skip to content

Commit

Permalink
Add external config import to application setup
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Oct 17, 2023
1 parent 2d239c6 commit 44587d2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
6 changes: 4 additions & 2 deletions lib/kangaru/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def autoloader
end

def setup
return if config.database.adaptor.nil?
if config.database.adaptor
@database = Database.new(**config.database.serialise).tap(&:setup!)
end

@database = Database.new(**config.database.serialise).tap(&:setup!)
config.import_external_config!
end
end
end
69 changes: 41 additions & 28 deletions spec/kangaru/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,47 +58,60 @@
describe "#configure" do
subject(:configure) { application.configure(&configure_block) }

let(:database) { instance_spy(Kangaru::Database, setup!: nil) }
let(:configure_block) { proc {} }

before do
allow(Kangaru::Database).to receive(:new).and_return(database)
allow(application.config).to receive(:import_external_config!)
end

context "when no configuration is set" do
let(:configure_block) { proc {} }

it "does not create a database" do
configure
expect(Kangaru::Database).not_to have_received(:new)
describe "database setup" do
before do
allow(Kangaru::Database).to receive(:new).and_return(database)
end

it "does not set the application database instance" do
expect { configure }.not_to change { application.database }.from(nil)
end
end
let(:database) { instance_spy(Kangaru::Database, setup!: nil) }

context "when configuration sets a database adaptor" do
let(:configure_block) do
->(config) { config.database.adaptor = :sqlite }
end
context "when no configuration is set" do
let(:configure_block) { proc {} }

it "creates a database" do
configure
expect(Kangaru::Database).to have_received(:new).once
end
it "does not create a database" do
configure
expect(Kangaru::Database).not_to have_received(:new)
end

it "sets up the database" do
configure
expect(database).to have_received(:setup!).once
it "does not set the application database instance" do
expect { configure }.not_to change { application.database }.from(nil)
end
end

it "sets the application database instance" do
expect { configure }
.to change { application.database }
.from(nil)
.to(database)
context "when configuration sets a database adaptor" do
let(:configure_block) do
->(config) { config.database.adaptor = :sqlite }
end

it "creates a database" do
configure
expect(Kangaru::Database).to have_received(:new).once
end

it "sets up the database" do
configure
expect(database).to have_received(:setup!).once
end

it "sets the application database instance" do
expect { configure }
.to change { application.database }
.from(nil)
.to(database)
end
end
end

it "imports external config" do
configure
expect(application.config).to have_received(:import_external_config!).once
end
end

describe "#run!" do
Expand Down

0 comments on commit 44587d2

Please sign in to comment.