Skip to content

Commit

Permalink
Update RSpec; address deprecation warnings
Browse files Browse the repository at this point in the history
Task 1007
  • Loading branch information
greyblake committed Sep 10, 2014
1 parent 43e00d0 commit cf672b5
Show file tree
Hide file tree
Showing 31 changed files with 128 additions and 123 deletions.
4 changes: 1 addition & 3 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
--colour
--format nested
#--debug
--format documentation
--profile
#--order rand
7 changes: 5 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
source "https://rubygems.org"

# To test against different rails versions with TravisCI
rails_version = ENV['RAILS_VERSION'] || '~> 4.0'
#
# NOTE: migrations fail on Rails 4.0.9 because remove_foreign_key can't proceed.
# sergey.potapov 2014-09-10
rails_version = ENV['RAILS_VERSION'] || '4.0.0'

# NOTE: This is a Gemfile for a gem.
# Using 'platforms' is contraindicated because they won't make it into
Expand All @@ -15,7 +18,7 @@ gem 'pg'
gem 'rails', rails_version

group :development do
gem 'rspec-rails'
gem 'rspec-rails',"~> 3.1.0"

# code metrics:
gem 'rcov' if version18
Expand Down
3 changes: 1 addition & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ load 'rails/tasks/engine.rake'
require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.verbose = false
spec.pattern = FileList['spec/**/*_spec.rb']
spec.pattern = 'spec/**/*_spec.rb'
end

task 'spec' => ['db:drop', 'db:create', 'db:migrate', 'app:db:test:load']
5 changes: 5 additions & 0 deletions lib/pg_power/tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def create_schema(schema_name)
connection.execute sql
end

def create_schema_if_not_exists(schema_name)
sql = %{CREATE SCHEMA IF NOT EXISTS "#{schema_name}"}
connection.execute sql
end

# Drops PostgreSQL schema
def drop_schema(schema_name)
sql = %{DROP SCHEMA "#{schema_name}"}
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@

# Expands the lines which load the assets
config.assets.debug = true

config.eager_load = true
end
2 changes: 2 additions & 0 deletions spec/dummy/config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@

# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify

config.eager_load = true
end
2 changes: 2 additions & 0 deletions spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@

# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.eager_load = true
end
12 changes: 1 addition & 11 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,7 @@
add_index "users", ["name"], :name => "index_users_on_name"

create_view "demography.citizens_view", <<-SQL
SELECT citizens.id,
citizens.country_id,
citizens.user_id,
citizens.first_name,
citizens.last_name,
citizens.birthday,
citizens.bio,
citizens.created_at,
citizens.updated_at,
citizens.active
FROM demography.citizens;
SELECT citizens.id, citizens.country_id, citizens.user_id, citizens.first_name, citizens.last_name, citizens.birthday, citizens.bio, citizens.created_at, citizens.updated_at, citizens.active FROM demography.citizens;
SQL

set_table_comment 'demography.citizens', 'Citizens Info'
Expand Down
10 changes: 2 additions & 8 deletions spec/foreign_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@
'CREATE INDEX CONCURRENTLY "index_steroids_on_user_id" ON "steroids" ("user_id")'
end

before do
ActiveRecord::Base.connection.stub(:execute)
ActiveRecord::Migration.stub(:id_column_name_from_table_name => 'user_id')
end


it 'should create index concurrently' do
ActiveRecord::Base.connection.should_receive(:execute).ordered
ActiveRecord::Base.connection.should_receive(:execute).ordered.once do |query|
expect(ActiveRecord::Base.connection).to receive(:execute)
expect(ActiveRecord::Base.connection).to receive(:execute).once do |query|
query.should == expected_index_query
end

Expand Down
42 changes: 21 additions & 21 deletions spec/indexes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@

ActiveRecord::Migration.add_index(:pets, :name, index_options)

PgPower::Explorer.index_exists?(:pets, :name, index_options).should be_true
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be true
end

it 'should allow indexes with expressions using functions' do
ActiveRecord::Migration.add_index(:pets, ["lower(name)", "lower(color)"])

PgPower::Explorer.index_exists?(:pets, ["lower(name)", "lower(color)"] ).should be_true
PgPower::Explorer.index_exists?(:pets, ["lower(name)", "lower(color)"] ).should be true
end

it 'should allow indexes with expressions using functions with multiple arguments' do
ActiveRecord::Migration.add_index(:pets, "to_tsvector('english', name)", :using => 'gin')

PgPower::Explorer.index_exists?(:pets, "gin(to_tsvector('english', name))" ).should be_true
PgPower::Explorer.index_exists?(:pets, "gin(to_tsvector('english', name))" ).should be true
end

it 'should allow indexes with expressions using functions with multiple arguments as dumped' do
ActiveRecord::Migration.add_index(:pets,
"to_tsvector('english'::regconfig, name)",
:using => 'gin')

PgPower::Explorer.index_exists?(:pets, "gin(to_tsvector('english', name))" ).should be_true
PgPower::Explorer.index_exists?(:pets, "gin(to_tsvector('english', name))" ).should be true
end

# TODO support this canonical example
it 'should allow indexes with advanced expressions' do
pending "Not sophisticated enough for this yet"
ActiveRecord::Migration.add_index(:pets, ["(color || ' ' || name)"])

PgPower::Explorer.index_exists?(:pets, ["(color || ' ' || name)"] ).should be_true
PgPower::Explorer.index_exists?(:pets, ["(color || ' ' || name)"] ).should be true
end

it "should allow partial indexes with expressions" do
opts = {:where => 'color IS NULL'}

ActiveRecord::Migration.add_index(:pets, ['upper(name)', 'lower(color)'], opts)
PgPower::Explorer.index_exists?(:pets, ['upper(name)', 'lower(color)'], opts).should be_true
PgPower::Explorer.index_exists?(:pets, ['upper(name)', 'lower(color)'], opts).should be true
end
end

Expand All @@ -51,7 +51,7 @@
ActiveRecord::Migration.add_index(:pets, ["lower(name)", "lower(color)"])
ActiveRecord::Migration.remove_index(:pets, ["lower(name)", "lower(color)"])

PgPower::Explorer.index_exists?(:pets, ["lower(name)", "lower(color)"] ).should be_false
PgPower::Explorer.index_exists?(:pets, ["lower(name)", "lower(color)"] ).should be false
end

it 'should remove indexes built with the :where option' do
Expand All @@ -62,29 +62,29 @@
ActiveRecord::Migration.add_index(:pets, :name, index_options)
ActiveRecord::Migration.remove_index(:pets, :name, index_options)

PgPower::Explorer.index_exists?(:pets, :name, index_options).should be_false
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be false
end
end

describe '#index_exists' do
it 'should be true for simple options' do
PgPower::Explorer.index_exists?('pets', :color).should be_true
PgPower::Explorer.index_exists?('pets', :color).should be true
end

it 'should support table name as a symbol' do
PgPower::Explorer.index_exists?(:pets, :color).should be_true
PgPower::Explorer.index_exists?(:pets, :color).should be true
end

it 'should be true for simple options on a schema table' do
PgPower::Explorer.index_exists?('demography.cities', :country_id).should be_true
PgPower::Explorer.index_exists?('demography.cities', :country_id).should be true
end

it 'should be true for a valid set of options' do
index_options = {:unique => true, :where => 'active'}
PgPower::Explorer.index_exists?('demography.citizens',
[:country_id, :user_id],
index_options
).should be_true
).should be true
end

it 'should be true for a valid set of options including name' do
Expand All @@ -94,58 +94,58 @@
PgPower::Explorer.index_exists?('demography.citizens',
[:country_id, :user_id],
index_options
).should be_true
).should be true
end

it 'should be false for a subset of valid options' do
index_options = {:where => 'active'}
PgPower::Explorer.index_exists?('demography.citizens',
[:country_id, :user_id],
index_options
).should be_false
).should be false
end

it 'should be false for invalid options' do
index_options = {:where => 'active'}
PgPower::Explorer.index_exists?('demography.citizens',
[:country_id],
index_options
).should be_false
).should be false
end

it 'should be true for a :where clause that includes boolean comparison' do
index_options = {:where => 'active'}
ActiveRecord::Migration.add_index(:pets, :name, index_options)
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be_true
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be true
end

it 'should be true for a :where clause that includes text comparison' do
index_options = {:where => "color = 'black'"}
ActiveRecord::Migration.add_index(:pets, :name, index_options)
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be_true
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be true
end

it 'should be true for a :where clause that includes NULL comparison' do
index_options = {:where => 'color IS NULL'}
ActiveRecord::Migration.add_index(:pets, :name, index_options)
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be_true
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be true
end

it 'should be true for a :where clause that includes integer comparison' do
index_options = {:where => 'id = 4'}
ActiveRecord::Migration.add_index(:pets, :name, index_options)
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be_true
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be true
end

it 'should be true for a compound :where clause' do
index_options = {:where => "id = 4 and color = 'black' and active"}
ActiveRecord::Migration.add_index(:pets, :name, index_options)
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be_true
PgPower::Explorer.index_exists?(:pets, :name, index_options).should be true
end

it 'should be true for concurrently created index' do
index_options = {:concurrently => true}
PgPower::Explorer.index_exists?(:users, :email, index_options).should be_true
PgPower::Explorer.index_exists?(:users, :email, index_options).should be true
end

end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
'CREATE INDEX CONCURRENTLY "index_users_on_phone_number" ON "users" ("phone_number")'
end

before { ActiveRecord::Base.connection.stub(:execute) }


it 'concurrently creates index' do
ActiveRecord::Base.connection.should_receive(:execute) do |query|
expect(ActiveRecord::Base.connection).to receive(:execute) do |query|
query.should == expected_query
end

Expand All @@ -19,8 +16,8 @@
end

it 'raises index exists error' do
ActiveRecord::Base.connection.stub(:index_exists? => true)
ActiveRecord::Base.connection.should_receive(:index_exists?).once
expect(ActiveRecord::Base.connection).
to receive(:index_exists?).once.and_return(true)

ActiveRecord::Migration.add_index :users, :phone_number, :concurrently => true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class AbstractAdapter
let(:adapter_stub) { AbstractAdapter.new }

it ".supports_comments?" do
expect(adapter_stub.supports_comments?).to be_false
expect(adapter_stub.supports_comments?).to be false
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AbstractAdapter
let(:adapter_stub) { AbstractAdapter.new }

it ".supports_foreign_keys?" do
expect(adapter_stub.supports_foreign_keys?).to be_false
expect(adapter_stub.supports_foreign_keys?).to be false
end

it ".foreign_keys" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class AbstractAdapter
let(:adapter_stub) { AbstractAdapter.new }

it ".supports_partial_index?" do
expect(adapter_stub.supports_partial_index?).to be_false
expect(adapter_stub.supports_partial_index?).to be false
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

describe "#create_table_with_schema_option" do
it "creates table with schema option" do
connection.should_receive(:create_table_without_schema_option).
expect(connection).to receive(:create_table_without_schema_option).
with("demography.something", {}).and_call_original

connection.create_table("something", schema: "demography")

expect(connection.table_exists?("demography.something")).to be_true
expect(connection.table_exists?("demography.something")).to be true

connection.drop_table("something", schema: "demography")
end
Expand All @@ -23,13 +24,14 @@
describe "#drop_table_with_schema_option" do
it "drops table with schema option" do
connection.create_table("something", schema: "demography")
expect(connection.table_exists?("demography.something")).to be_true
expect(connection.table_exists?("demography.something")).to be true

connection.should_receive(:drop_table_without_schema_option).
expect(connection).to receive(:drop_table_without_schema_option).
with("demography.something", {}).and_call_original

connection.drop_table("something", schema: "demography")

expect(connection.table_exists?("demography.something")).to be_false
expect(connection.table_exists?("demography.something")).to be false
end

it "allows options to be a frozen Hash" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def self.alias_method_chain(*args)
:remove_column_comments,
:set_index_comment,
:remove_index_comment
].each { |method_name| adapter_stub.respond_to?(method_name).should be_true }
].each { |method_name| adapter_stub.respond_to?(method_name).should be true }
end

it 'should define method stubs for foreign key methods' do
[ :add_foreign_key,
:remove_foreign_key
].each { |method_name| adapter_stub.respond_to?(method_name).should be_true }
].each { |method_name| adapter_stub.respond_to?(method_name).should be true }
end
end
end
Loading

0 comments on commit cf672b5

Please sign in to comment.