Skip to content

Commit

Permalink
Merge pull request #314 from cbeer/rspec3
Browse files Browse the repository at this point in the history
Update to use rspec 3.x syntax
  • Loading branch information
bradrobertson committed May 16, 2016
2 parents 8e0b4e4 + b41bb64 commit b805197
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 111 deletions.
4 changes: 2 additions & 2 deletions apartment.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ Gem::Specification.new do |s|

s.add_development_dependency 'appraisal'
s.add_development_dependency 'rake', '~> 0.9'
s.add_development_dependency 'rspec', '~> 2.14'
s.add_development_dependency 'rspec-rails', '~> 2.14'
s.add_development_dependency 'rspec', '~> 3.4'
s.add_development_dependency 'rspec-rails', '~> 3.4'
s.add_development_dependency 'capybara', '~> 2.0'

if defined?(JRUBY_VERSION)
Expand Down
2 changes: 1 addition & 1 deletion spec/adapters/mysql2_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def tenant_names
it "should process model exclusions" do
Apartment::Tenant.init

Company.table_name.should == "#{default_tenant}.companies"
expect(Company.table_name).to eq("#{default_tenant}.companies")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/apartment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

describe Apartment do
it "should be valid" do
Apartment.should be_a(Module)
expect(Apartment).to be_a(Module)
end

it "should be a valid app" do
::Rails.application.should be_a(Dummy::Application)
expect(::Rails.application).to be_a(Dummy::Application)
end

it "should deprecate Apartment::Database in favor of Apartment::Tenant" do
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/connection_adapter_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end
Apartment::Tenant.init

Company.connection.object_id.should_not == ActiveRecord::Base.connection.object_id
expect(Company.connection.object_id).not_to eq(ActiveRecord::Base.connection.object_id)
end
end

Expand Down
30 changes: 15 additions & 15 deletions spec/examples/generic_adapter_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
describe "#create" do

it "should create the new databases" do
tenant_names.should include(db1)
tenant_names.should include(db2)
expect(tenant_names).to include(db1)
expect(tenant_names).to include(db2)
end

it "should load schema.rb to new schema" do
subject.switch(db1) do
connection.tables.should include('companies')
expect(connection.tables).to include('companies')
end
end

Expand All @@ -31,32 +31,32 @@

subject.create(db2) do
@count = User.count
subject.current.should == db2
expect(subject.current).to eq(db2)
User.create
end

subject.current.should_not == db2
expect(subject.current).not_to eq(db2)

subject.switch(db2){ User.count.should == @count + 1 }
subject.switch(db2){ expect(User.count).to eq(@count + 1) }
end
end

describe "#drop" do
it "should remove the db" do
subject.drop db1
tenant_names.should_not include(db1)
expect(tenant_names).not_to include(db1)
end
end

describe "#switch!" do
it "should connect to new db" do
subject.switch!(db1)
subject.current.should == db1
expect(subject.current).to eq(db1)
end

it "should reset connection if database is nil" do
subject.switch!
subject.current.should == default_tenant
expect(subject.current).to eq(default_tenant)
end

it "should raise an error if database is invalid" do
Expand All @@ -69,9 +69,9 @@
describe "#switch" do
it "connects and resets the tenant" do
subject.switch(db1) do
subject.current.should == db1
expect(subject.current).to eq(db1)
end
subject.current.should == default_tenant
expect(subject.current).to eq(default_tenant)
end

# We're often finding when using Apartment in tests, the `current` (ie the previously connect to db)
Expand All @@ -88,7 +88,7 @@
expect(Apartment::Deprecation).to receive(:warn)

subject.switch(db1)
subject.current.should == db1
expect(subject.current).to eq(db1)
end
end

Expand All @@ -97,7 +97,7 @@
expect(Apartment::Deprecation).to receive(:warn)

subject.process(db1) do
subject.current.should == db1
expect(subject.current).to eq(db1)
end
end
end
Expand All @@ -106,14 +106,14 @@
it "should reset connection" do
subject.switch!(db1)
subject.reset
subject.current.should == default_tenant
expect(subject.current).to eq(default_tenant)
end
end

describe "#current" do
it "should return the current db name" do
subject.switch!(db1)
subject.current.should == db1
expect(subject.current).to eq(db1)
end
end

Expand Down
50 changes: 25 additions & 25 deletions spec/examples/schema_adapter_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@
it "should process model exclusions" do
Apartment::Tenant.init

Company.table_name.should == "public.companies"
expect(Company.table_name).to eq("public.companies")
end

context "with a default_schema", :default_schema => true do

it "should set the proper table_name on excluded_models" do
Apartment::Tenant.init

Company.table_name.should == "#{default_schema}.companies"
expect(Company.table_name).to eq("#{default_schema}.companies")
end

it 'sets the search_path correctly' do
Apartment::Tenant.init

User.connection.schema_search_path.should =~ %r|#{default_schema}|
expect(User.connection.schema_search_path).to match(%r|#{default_schema}|)
end
end

context "persistent_schemas", :persistent_schemas => true do
it "sets the persistent schemas in the schema_search_path" do
Apartment::Tenant.init
connection.schema_search_path.should end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
end
end
end
Expand All @@ -51,7 +51,7 @@

it "should load schema.rb to new schema" do
connection.schema_search_path = schema1
connection.tables.should include('companies')
expect(connection.tables).to include('companies')
end

it "should yield to block if passed and reset" do
Expand All @@ -61,13 +61,13 @@

subject.create(schema2) do
@count = User.count
connection.schema_search_path.should start_with %{"#{schema2}"}
expect(connection.schema_search_path).to start_with %{"#{schema2}"}
User.create
end

connection.schema_search_path.should_not start_with %{"#{schema2}"}
expect(connection.schema_search_path).not_to start_with %{"#{schema2}"}

subject.switch(schema2){ User.count.should == @count + 1 }
subject.switch(schema2){ expect(User.count).to eq(@count + 1) }
end

context "numeric database names" do
Expand All @@ -76,7 +76,7 @@
expect {
subject.create(db)
}.to_not raise_error
tenant_names.should include(db.to_s)
expect(tenant_names).to include(db.to_s)
end

after{ subject.drop(db) }
Expand All @@ -99,7 +99,7 @@
expect {
subject.drop(db)
}.to_not raise_error
tenant_names.should_not include(db.to_s)
expect(tenant_names).not_to include(db.to_s)
end

after { subject.drop(db) rescue nil }
Expand All @@ -109,25 +109,25 @@
describe "#switch" do
it "connects and resets" do
subject.switch(schema1) do
connection.schema_search_path.should start_with %{"#{schema1}"}
expect(connection.schema_search_path).to start_with %{"#{schema1}"}
end

connection.schema_search_path.should start_with %{"#{public_schema}"}
expect(connection.schema_search_path).to start_with %{"#{public_schema}"}
end
end

describe "#reset" do
it "should reset connection" do
subject.switch!(schema1)
subject.reset
connection.schema_search_path.should start_with %{"#{public_schema}"}
expect(connection.schema_search_path).to start_with %{"#{public_schema}"}
end

context "with default_schema", :default_schema => true do
it "should reset to the default schema" do
subject.switch!(schema1)
subject.reset
connection.schema_search_path.should start_with %{"#{default_schema}"}
expect(connection.schema_search_path).to start_with %{"#{default_schema}"}
end
end

Expand All @@ -138,13 +138,13 @@
end

it "maintains the persistent schemas in the schema_search_path" do
connection.schema_search_path.should end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
end

context "with default_schema", :default_schema => true do
it "prioritizes the switched schema to front of schema_search_path" do
subject.reset # need to re-call this as the default_schema wasn't set at the time that the above reset ran
connection.schema_search_path.should start_with %{"#{default_schema}"}
expect(connection.schema_search_path).to start_with %{"#{default_schema}"}
end
end
end
Expand All @@ -153,12 +153,12 @@
describe "#switch!" do
it "should connect to new schema" do
subject.switch!(schema1)
connection.schema_search_path.should start_with %{"#{schema1}"}
expect(connection.schema_search_path).to start_with %{"#{schema1}"}
end

it "should reset connection if database is nil" do
subject.switch!
connection.schema_search_path.should == %{"#{public_schema}"}
expect(connection.schema_search_path).to eq(%{"#{public_schema}"})
end

it "should raise an error if schema is invalid" do
Expand All @@ -176,7 +176,7 @@
subject.switch!(db)
}.to_not raise_error

connection.schema_search_path.should start_with %{"#{db.to_s}"}
expect(connection.schema_search_path).to start_with %{"#{db.to_s}"}
end

after{ subject.drop(db) }
Expand All @@ -188,11 +188,11 @@
end

it "should switch out the default schema rather than public" do
connection.schema_search_path.should_not include default_schema
expect(connection.schema_search_path).not_to include default_schema
end

it "should still switch to the switched schema" do
connection.schema_search_path.should start_with %{"#{schema1}"}
expect(connection.schema_search_path).to start_with %{"#{schema1}"}
end
end

Expand All @@ -201,25 +201,25 @@
before{ subject.switch!(schema1) }

it "maintains the persistent schemas in the schema_search_path" do
connection.schema_search_path.should end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
end

it "prioritizes the switched schema to front of schema_search_path" do
connection.schema_search_path.should start_with %{"#{schema1}"}
expect(connection.schema_search_path).to start_with %{"#{schema1}"}
end
end
end

describe "#current" do
it "should return the current schema name" do
subject.switch!(schema1)
subject.current.should == schema1
expect(subject.current).to eq(schema1)
end

context "persistent_schemas", :persistent_schemas => true do
it "should exlude persistent_schemas" do
subject.switch!(schema1)
subject.current.should == schema1
expect(subject.current).to eq(schema1)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/apartment_rake_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@

describe "#migrate" do
it "should migrate all databases" do
ActiveRecord::Migrator.should_receive(:migrate).exactly(company_count).times
expect(ActiveRecord::Migrator).to receive(:migrate).exactly(company_count).times

@rake['apartment:migrate'].invoke
end
end

describe "#rollback" do
it "should rollback all dbs" do
ActiveRecord::Migrator.should_receive(:rollback).exactly(company_count).times
expect(ActiveRecord::Migrator).to receive(:rollback).exactly(company_count).times

@rake['apartment:rollback'].invoke
end
end

describe "apartment:seed" do
it "should seed all databases" do
Apartment::Tenant.should_receive(:seed).exactly(company_count).times
expect(Apartment::Tenant).to receive(:seed).exactly(company_count).times

@rake['apartment:seed'].invoke
end
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/query_caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
ActiveRecord::Base.connection.enable_query_cache!

Apartment::Tenant.switch! db_names.first
User.find_by_name(db_names.first).name.should == db_names.first
expect(User.find_by_name(db_names.first).name).to eq(db_names.first)

Apartment::Tenant.switch! db_names.last
User.find_by_name(db_names.first).should be_nil
expect(User.find_by_name(db_names.first)).to be_nil
end
end
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
config.after(:all) do
`git checkout -- spec/dummy/db/schema.rb`
end

# rspec-rails 3 will no longer automatically infer an example group's spec type
# from the file location. You can explicitly opt-in to the feature using this
# config option.
# To explicitly tag specs without using automatic inference, set the `:type`
# metadata manually:
#
# describe ThingsController, :type => :controller do
# # Equivalent to being in spec/controllers
# end
config.infer_spec_type_from_file_location!
end

# Load shared examples, must happen after configure for RSpec 3
Expand Down
Loading

0 comments on commit b805197

Please sign in to comment.