Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #24 from lleger/lleger-fix-tests

Update test suite and test directly against ActiveRecord
  • Loading branch information...
commit 5e406666d39a60ced4de072539a235c46acbeaac 2 parents b9d15e1 + b328353
@trptcolin authored
Showing with 391 additions and 349 deletions.
  1. +1 −0  .rspec
  2. +2 −1  consistency_fail.gemspec
  3. +37 −18 spec/index_spec.rb
  4. +14 −89 spec/introspectors/has_one_spec.rb
  5. +12 −68 spec/introspectors/polymorphic_spec.rb
  6. +42 −36 spec/introspectors/table_data_spec.rb
  7. +38 −75 spec/introspectors/validates_uniqueness_of_spec.rb
  8. +2 −3 spec/models_spec.rb
  9. +88 −49 spec/reporter_spec.rb
  10. +11 −10 spec/spec_helper.rb
  11. +1 −0  spec/support/active_record.rb
  12. +3 −0  spec/support/models/correct_account.rb
  13. +3 −0  spec/support/models/correct_address.rb
  14. +3 −0  spec/support/models/correct_attachment.rb
  15. +3 −0  spec/support/models/correct_person.rb
  16. +3 −0  spec/support/models/correct_post.rb
  17. +5 −0 spec/support/models/correct_user.rb
  18. +2 −0  spec/support/models/nonexistent.rb
  19. +3 −0  spec/support/models/wrong_account.rb
  20. +3 −0  spec/support/models/wrong_address.rb
  21. +3 −0  spec/support/models/wrong_attachment.rb
  22. +3 −0  spec/support/models/wrong_business.rb
  23. +3 −0  spec/support/models/wrong_person.rb
  24. +3 −0  spec/support/models/wrong_post.rb
  25. +3 −0  spec/support/models/wrong_user.rb
  26. +100 −0 spec/support/schema.rb
View
1  .rspec
@@ -1 +1,2 @@
--color
+--require spec_helper
View
3  consistency_fail.gemspec
@@ -22,7 +22,8 @@ EOF
s.license = "MIT"
s.add_development_dependency "activerecord", "~>3.0"
- s.add_development_dependency "rspec", "~>3.1"
+ s.add_development_dependency "sqlite3", "~>1.3"
+ s.add_development_dependency "rspec", "~>3.2"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
View
55 spec/index_spec.rb
@@ -1,40 +1,59 @@
-require 'spec_helper'
-require 'consistency_fail/index'
-
describe ConsistencyFail::Index do
-
+
+ let(:index) do
+ ConsistencyFail::Index.new(
+ CorrectAddress,
+ CorrectAddress.table_name,
+ ["city", "state"]
+ )
+ end
+
describe "value objectiness" do
it "holds onto model, table name, and columns" do
- model = double("model")
- index = ConsistencyFail::Index.new(model, "addresses", ["city", "state"])
- expect(index.model).to eq(model)
- expect(index.table_name).to eq("addresses")
- expect(index.columns).to eq(["city", "state"])
+ expect(index.model).to eq(CorrectAddress)
+ expect(index.table_name).to eq("correct_addresses")
+ expect(index.columns).to eq(
+ ["city", "state"]
+ )
end
it "leaves columns in the initial order (since we only care about presence, not performance)" do
- index = ConsistencyFail::Index.new(double('model'), "addresses", ["state", "city"])
- expect(index.columns).to eq(["state", "city"])
+ expect(index.columns).to eq(
+ ["city", "state"]
+ )
end
end
- describe "equality test" do
+ describe "equality test" do
it "passes when everything matches" do
- expect(ConsistencyFail::Index.new(double('model'), "addresses", ["city", "state"])).to eq(
- ConsistencyFail::Index.new(double('model'),"addresses", ["city", "state"])
+ expect(index).to eq(
+ ConsistencyFail::Index.new(
+ "CorrectAddress".constantize,
+ "correct_addresses",
+ ["city", "state"]
+ )
)
end
it "fails when tables are different" do
- expect(ConsistencyFail::Index.new(double('model'),"locations", ["city", "state"])).not_to eq(
- ConsistencyFail::Index.new(double('model'),"addresses", ["city", "state"])
+ expect(index).not_to eq(
+ ConsistencyFail::Index.new(
+ CorrectAttachment,
+ CorrectAttachment.table_name,
+ ["attachable_id", "attachable_type"]
+ )
)
end
it "fails when columns are different" do
- expect(ConsistencyFail::Index.new(double('model'),"addresses", ["city", "state"])).not_to eq(
- ConsistencyFail::Index.new(double('model'),"addresses", ["state", "zip"])
+ expect(index).not_to eq(
+ ConsistencyFail::Index.new(
+ CorrectAddress,
+ CorrectAddress.table_name,
+ ["correct_user_id"]
+ )
)
end
end
+
end
View
103 spec/introspectors/has_one_spec.rb
@@ -1,96 +1,21 @@
-require 'spec_helper'
-require 'consistency_fail/introspectors/table_data'
-require 'consistency_fail/introspectors/has_one'
-
describe ConsistencyFail::Introspectors::HasOne do
- def introspector(model)
- ConsistencyFail::Introspectors::HasOne.new(model)
- end
-
- describe "instances of has_one" do
- it "finds none" do
- model = fake_ar_model("User")
- allow(model).to receive(:reflect_on_all_associations).and_return([])
-
- expect(subject.instances(model)).to eq([])
- end
-
- it "finds one" do
- model = fake_ar_model("User")
- association = double("association", :macro => :has_one, :options => {})
- allow(model).to receive(:reflect_on_all_associations).and_return([association])
-
- expect(subject.instances(model)).to eq([association])
- end
-
- it "finds other associations, but not has_one" do
- model = fake_ar_model("User")
- validation = double("validation", :macro => :has_many)
- allow(model).to receive(:reflect_on_all_associations).and_return([validation])
-
- expect(subject.instances(model)).to eq([])
- end
-
- it "finds one, but it's a polymorphic association" do
- model = fake_ar_model("User")
- association = double("association", :macro => :has_one, :options => {:as => "addressable"})
- allow(model).to receive(:reflect_on_all_associations).and_return([association])
-
- expect(subject.instances(model)).to eq([])
- end
-
- it "finds one, but it's a :through association" do
- model = fake_ar_model("User")
- association = double("association", :macro => :has_one, :options => {:through => :amodel})
- allow(model).to receive(:reflect_on_all_associations).and_return([association])
-
- expect(subject.instances(model)).to eq([])
- end
- end
-
+
describe "finding missing indexes" do
- before do
- @association = double("association", :macro => :has_one, :options => {})
- @model = fake_ar_model("User", :table_exists? => true,
- :table_name => "users",
- :class_name => "User",
- :reflect_on_all_associations => [@association])
- @address_class = double("Address Class")
- @address_string = "Address"
- allow(@address_string).to receive(:constantize).and_return(@address_class)
- end
-
it "finds one" do
- allow(@association).to receive_messages(:table_name => :addresses, :class_name => @address_string, :foreign_key => "user_id")
- allow(@address_class).to receive_message_chain(:connection, :indexes).with("addresses").and_return([])
-
- indexes = subject.missing_indexes(@model)
- expect(indexes).to eq([ConsistencyFail::Index.new(fake_ar_model("Address"), "addresses", ["user_id"])])
- end
-
- it "finds one in Rails 3.0.x (where foreign_key is not defined)" do
- allow(@association).to receive_messages(:table_name => :addresses, :class_name => @address_string, :primary_key_name => "user_id")
- allow(@address_class).to receive_message_chain(:connection, :indexes).with("addresses").and_return([])
-
- indexes = subject.missing_indexes(@model)
- expect(indexes).to eq([ConsistencyFail::Index.new(fake_ar_model("Address"), "addresses", ["user_id"])])
- end
-
+ indexes = subject.missing_indexes(WrongUser)
+
+ expect(indexes).to eq([
+ ConsistencyFail::Index.new(
+ WrongAddress,
+ WrongAddress.table_name,
+ ["wrong_user_id"]
+ )
+ ])
+ end
+
it "finds none when they're already in place" do
- allow(@association).to receive_messages(:table_name => :addresses, :class_name => @address_string, :foreign_key => "user_id")
- index = ConsistencyFail::Index.new(double('model'), "addresses", ["user_id"])
-
- fake_connection = double("connection")
- allow(@address_class).to receive_message_chain(:connection).and_return(fake_connection)
-
- allow(ConsistencyFail::Introspectors::TableData).to receive_message_chain(:new, :unique_indexes_by_table).
- with(@address_class, fake_connection, "addresses").
- and_return([index])
-
- expect(subject.missing_indexes(@model)).to eq([])
+ expect(subject.missing_indexes(CorrectUser)).to eq([])
end
-
end
+
end
-
-
View
80 spec/introspectors/polymorphic_spec.rb
@@ -1,77 +1,21 @@
-require 'spec_helper'
-require 'consistency_fail/introspectors/table_data'
-require 'consistency_fail/introspectors/polymorphic'
-
describe ConsistencyFail::Introspectors::Polymorphic do
- def introspector(model)
- ConsistencyFail::Introspectors::Polymorphic.new(model)
- end
-
- describe "instances of polymorphic" do
- it "finds none" do
- model = fake_ar_model("User")
- allow(model).to receive(:reflect_on_all_associations).and_return([])
-
- expect(subject.instances(model)).to eq([])
- end
-
- it "finds one" do
- model = fake_ar_model("User")
- association = double("association", :macro => :has_one, :options => {:as => "addressable"})
- allow(model).to receive(:reflect_on_all_associations).and_return([association])
-
- expect(subject.instances(model)).to eq([association])
- end
-
- it "finds other has_one associations, but not polymorphic" do
- model = fake_ar_model("User")
- validation = double("association", :macro => :has_one, :options => {})
- allow(model).to receive(:reflect_on_all_associations).and_return([validation])
-
- expect(subject.instances(model)).to eq([])
- end
-
- it "finds other non has_one associations" do
- model = fake_ar_model("User")
- validation = double("association", :macro => :has_many)
- allow(model).to receive(:reflect_on_all_associations).and_return([validation])
-
- expect(subject.instances(model)).to eq([])
- end
- end
-
+
describe "finding missing indexes" do
- before do
- @association = double("association", :macro => :has_one, :options => {:as => "addressable"})
- @model = fake_ar_model("User", :table_exists? => true,
- :table_name => "users",
- :class_name => "User",
- :reflect_on_all_associations => [@association])
- @address_class = double("Address Class")
- @address_string = "Address"
- allow(@address_string).to receive(:constantize).and_return(@address_class)
- end
-
it "finds one" do
- allow(@association).to receive_messages(:table_name => :addresses, :class_name => @address_string)
- allow(@address_class).to receive_message_chain(:connection, :indexes).with("addresses").and_return([])
-
- indexes = subject.missing_indexes(@model)
- expect(indexes).to eq([ConsistencyFail::Index.new(fake_ar_model("Address"), "addresses", ["addressable_type", "addressable_id"])])
+ indexes = subject.missing_indexes(WrongPost)
+
+ expect(indexes).to eq([
+ ConsistencyFail::Index.new(
+ WrongAttachment,
+ WrongAttachment.table_name,
+ ["attachable_type", "attachable_id"]
+ )
+ ])
end
it "finds none when they're already in place" do
- allow(@association).to receive_messages(:table_name => :addresses, :class_name => @address_string)
- index = ConsistencyFail::Index.new(double('model'), "addresses", ["addressable_type", "addressable_id"])
-
- fake_connection = double("connection")
- allow(@address_class).to receive_message_chain(:connection).and_return(fake_connection)
-
- allow(ConsistencyFail::Introspectors::TableData).to receive_message_chain(:new, :unique_indexes_by_table).
- with(@address_class, fake_connection, "addresses").
- and_return([index])
-
- expect(subject.missing_indexes(@model)).to eq([])
+ expect(subject.missing_indexes(CorrectPost)).to eq([])
end
end
+
end
View
78 spec/introspectors/table_data_spec.rb
@@ -1,52 +1,58 @@
-require 'spec_helper'
-require 'consistency_fail/introspectors/table_data'
-
describe ConsistencyFail::Introspectors::TableData do
+
describe "finding unique indexes" do
it "finds none when the table does not exist" do
- model = fake_ar_model("User", :table_exists? => false)
-
- expect(subject.unique_indexes(model)).to eq([])
+ expect(subject.unique_indexes(Nonexistent)).to be_empty
end
it "gets one" do
- model = fake_ar_model("User", :table_exists? => true,
- :table_name => "users")
-
- allow(model).to receive_message_chain(:connection, :indexes).
- with("users").
- and_return([fake_index_on(["a"], :unique => true)])
-
- indexes = subject.unique_indexes(model)
- expect(indexes).to eq([ConsistencyFail::Index.new(double('model'), "users", ["a"])])
+ index = ConsistencyFail::Index.new(
+ CorrectAddress,
+ CorrectAddress.table_name,
+ ["correct_user_id"]
+ )
+
+ expect(
+ ConsistencyFail::Introspectors::TableData.new.unique_indexes_by_table(
+ CorrectAddress,
+ ActiveRecord::Base.connection,
+ CorrectAddress.table_name
+ )
+ ).to eq [index]
end
it "doesn't get non-unique indexes" do
- model = fake_ar_model("User", :table_exists? => true,
- :table_name => "users")
-
- allow(model).to receive_message_chain(:connection, :indexes).
- with("users").
- and_return([fake_index_on(["a"], :unique => false)])
-
- expect(subject.unique_indexes(model)).to eq([])
+ expect(
+ ConsistencyFail::Introspectors::TableData.new.unique_indexes_by_table(
+ WrongAddress,
+ ActiveRecord::Base.connection,
+ WrongAddress.table_name
+ )
+ ).to be_empty
end
it "gets multiple unique indexes" do
- model = fake_ar_model("User", :table_exists? => true,
- :table_name => "users")
-
- allow(model).to receive_message_chain(:connection, :indexes).
- with("users").
- and_return([fake_index_on(["a"], :unique => true),
- fake_index_on(["b", "c"], :unique => true)])
-
- indexes = subject.unique_indexes(model)
- expect(indexes.size).to eq(2)
- expect(indexes).to eq([ConsistencyFail::Index.new(double('model'), "users", ["a"]),
- ConsistencyFail::Index.new(double('model'), "users", ["b", "c"])])
+ indexes = [
+ ConsistencyFail::Index.new(
+ CorrectAttachment,
+ CorrectAttachment.table_name,
+ ["name"]
+ ),
+ ConsistencyFail::Index.new(
+ CorrectAttachment,
+ CorrectAttachment.table_name,
+ ["attachable_id", "attachable_type"]
+ )
+ ]
+
+ expect(
+ ConsistencyFail::Introspectors::TableData.new.unique_indexes_by_table(
+ CorrectAttachment,
+ ActiveRecord::Base.connection,
+ CorrectAttachment.table_name
+ )
+ ).to eq indexes
end
end
end
-
View
113 spec/introspectors/validates_uniqueness_of_spec.rb
@@ -1,93 +1,56 @@
-require 'spec_helper'
-require 'consistency_fail/introspectors/validates_uniqueness_of'
-
describe ConsistencyFail::Introspectors::ValidatesUniquenessOf do
- def introspector(model)
- ConsistencyFail::Introspectors::ValidatesUniquenessOf.new(model)
- end
-
- describe "instances of validates_uniqueness_of" do
- it "finds none" do
- model = fake_ar_model("User")
- allow(model).to receive(:validators).and_return([])
-
- expect(subject.instances(model)).to eq([])
- end
-
- it "finds one" do
- model = fake_ar_model("User")
- validation = double("validation", :class => ActiveRecord::Validations::UniquenessValidator)
- allow(model).to receive(:validators).and_return([validation])
-
- expect(subject.instances(model)).to eq([validation])
- end
-
- it "finds other validations, but not uniqueness" do
- model = fake_ar_model("User")
- validation = double("validation", :class => ActiveModel::Validations::FormatValidator)
- allow(model).to receive(:validators).and_return([validation])
-
- expect(subject.instances(model)).to eq([])
- end
- end
-
+
describe "finding missing indexes" do
- before do
- @validation = double("validation", :class => ActiveRecord::Validations::UniquenessValidator)
- @model = fake_ar_model("User", :table_exists? => true,
- :table_name => "users",
- :validators => [@validation])
- end
-
it "finds one" do
- allow(@validation).to receive_messages(:attributes => [:email], :options => {})
- allow(@model).to receive_message_chain(:connection, :indexes).with("users").and_return([])
-
- indexes = subject.missing_indexes(@model)
- expect(indexes).to eq([ConsistencyFail::Index.new(double('model'), "users", ["email"])])
+ indexes = subject.missing_indexes(WrongAccount)
+
+ expect(indexes).to eq([
+ ConsistencyFail::Index.new(
+ WrongAccount,
+ WrongAccount.table_name,
+ ["email"]
+ )
+ ])
end
it "finds one where the validation has scoped columns" do
- allow(@validation).to receive_messages(:attributes => [:city], :options => {:scope => [:email, :state]})
- allow(@model).to receive_message_chain(:connection, :indexes).with("users").and_return([])
-
- indexes = subject.missing_indexes(@model)
- expect(indexes).to eq([ConsistencyFail::Index.new(double('model'), "users", ["city", "email", "state"])])
- end
-
- it "leaves the columns in the given order" do
- allow(@validation).to receive_messages(:attributes => [:email], :options => {:scope => [:city, :state]})
- allow(@model).to receive_message_chain(:connection, :indexes).with("users").and_return([])
-
- indexes = subject.missing_indexes(@model)
- expect(indexes).to eq([ConsistencyFail::Index.new(double('model'), "users", ["email", "city", "state"])])
+ indexes = subject.missing_indexes(WrongBusiness)
+
+ expect(indexes).to eq([
+ ConsistencyFail::Index.new(
+ WrongBusiness,
+ WrongBusiness.table_name,
+ ["name", "city", "state"]
+ )
+ ])
end
it "finds two where there are multiple attributes" do
- allow(@validation).to receive_messages(:attributes => [:email, :name], :options => {:scope => [:city, :state]})
- allow(@model).to receive_message_chain(:connection, :indexes).with("users").and_return([])
-
- indexes = subject.missing_indexes(@model)
- expect(indexes).to eq([ConsistencyFail::Index.new(double('model'), "users", ["email", "city", "state"]),
- ConsistencyFail::Index.new(double('model'), "users", ["name", "city", "state"])])
+ indexes = subject.missing_indexes(WrongPerson)
+
+ expect(indexes).to eq(
+ [
+ ConsistencyFail::Index.new(
+ WrongPerson,
+ WrongPerson.table_name,
+ ["email", "city", "state"]
+ ),
+ ConsistencyFail::Index.new(
+ WrongPerson,
+ WrongPerson.table_name,
+ ["name", "city", "state"]
+ )
+ ]
+ )
end
it "finds none when they're already in place" do
- allow(@validation).to receive_messages(:attributes => [:email], :options => {})
- index = fake_index_on(["email"], :unique => true)
- allow(@model).to receive_message_chain(:connection, :indexes).with("users").
- and_return([index])
-
- expect(subject.missing_indexes(@model)).to eq([])
+ expect(subject.missing_indexes(CorrectAccount)).to be_empty
end
it "finds none when indexes are there but in a different order" do
- allow(@validation).to receive_messages(:attributes => [:email], :options => {:scope => [:city, :state]})
- index = fake_index_on(["state", "email", "city"], :unique => true)
- allow(@model).to receive_message_chain(:connection, :indexes).with("users").
- and_return([index])
-
- expect(subject.missing_indexes(@model)).to eq([])
+ expect(subject.missing_indexes(CorrectPerson)).to be_empty
end
end
+
end
View
5 spec/models_spec.rb
@@ -1,7 +1,5 @@
-require 'spec_helper'
-require 'consistency_fail/models'
-
describe ConsistencyFail::Models do
+
def models(load_path)
ConsistencyFail::Models.new(load_path)
end
@@ -46,4 +44,5 @@ def models(load_path)
expect(models([]).all).to eq([model_a, model_c, model_b])
end
+
end
View
137 spec/reporter_spec.rb
@@ -1,90 +1,129 @@
-require 'spec_helper'
-require 'consistency_fail/reporter'
-require 'consistency_fail/index'
-
describe ConsistencyFail::Reporter do
- before(:each) do
- @real_out = $stdout
- @fake_out = StringIO.new
- $stdout = @fake_out
- end
- after(:each) do
- $stdout = @real_out
- end
-
+
context "validates_uniqueness_of" do
it "says everything's good" do
- subject.report_validates_uniqueness_problems([])
-
- expect(@fake_out.string).to match(/Hooray!/)
+ expect {
+ subject.report_validates_uniqueness_problems([])
+ }.to output(/Hooray!/).to_stdout
end
it "shows a missing single-column index on a single model" do
- missing_indexes = [ConsistencyFail::Index.new(double('model'), "users", ["email"])]
-
- subject.report_validates_uniqueness_problems(fake_ar_model("User", :table_name => "users") => missing_indexes)
+ missing_indexes = [
+ ConsistencyFail::Index.new(
+ WrongAccount,
+ WrongAccount.table_name,
+ ["email"]
+ )
+ ]
- expect(@fake_out.string).to match(/users\s+\(email\)/)
+ expect {
+ subject.report_validates_uniqueness_problems(
+ WrongAccount => missing_indexes
+ )
+ }.to output(/wrong_accounts\s+\(email\)/).to_stdout
end
it "shows a missing multiple-column index on a single model" do
- missing_indexes = [ConsistencyFail::Index.new(double('model'),"addresses", ["number", "street", "zip"])]
-
- subject.report_validates_uniqueness_problems(fake_ar_model("Address", :table_name => "addresses") => missing_indexes)
+ missing_indexes = [
+ ConsistencyFail::Index.new(
+ WrongBusiness,
+ WrongBusiness.table_name,
+ ["name", "city", "state"]
+ )
+ ]
- expect(@fake_out.string).to match(/addresses\s+\(number, street, zip\)/)
+ expect {
+ subject.report_validates_uniqueness_problems(
+ WrongBusiness => missing_indexes
+ )
+ }.to output(/wrong_businesses\s+\(name, city, state\)/).to_stdout
end
context "with problems on multiple models" do
- before(:each) do
- subject.report_validates_uniqueness_problems(
- fake_ar_model("User", :table_name => "users") =>
- [ConsistencyFail::Index.new(double('model'),"users", ["email"])],
- fake_ar_model("Citizen", :table_name => "citizens") =>
- [ConsistencyFail::Index.new(double('model'),"citizens", ["ssn"])]
- )
+ def report
+ missing_indices = {
+ WrongAccount => [
+ ConsistencyFail::Index.new(
+ WrongAccount,
+ WrongAccount.table_name,
+ ["email"]
+ )
+ ],
+ WrongBusiness => [
+ ConsistencyFail::Index.new(
+ WrongBusiness,
+ WrongBusiness.table_name,
+ ["name", "city", "state"]
+ )
+ ]
+ }
+
+ subject.report_validates_uniqueness_problems(missing_indices)
end
it "shows all problems" do
- expect(@fake_out.string).to match(/users\s+\(email\)/m)
- expect(@fake_out.string).to match(/citizens\s+\(ssn\)/m)
+ expect { report }.to output(/wrong_accounts\s+\(email\)/).to_stdout
+ expect { report }.to output(
+ /wrong_businesses\s+\(name, city, state\)/
+ ).to_stdout
end
it "orders the models alphabetically" do
- expect(@fake_out.string).to match(/citizens\s+\(ssn\).*users\s+\(email\)/m)
+ expect { report }.to output(/
+ wrong_accounts\s+\(email\)
+ (\s|\S)*
+ wrong_businesses\s+\(name,\scity,\sstate\)
+ /x).to_stdout
end
end
end
context "has_one" do
it "says everything's good" do
- subject.report_has_one_problems([])
-
- expect(@fake_out.string).to match(/Hooray!/)
+ expect {
+ subject.report_has_one_problems([])
+ }.to output(/Hooray!/).to_stdout
end
it "shows a missing single-column index on a single model" do
- missing_indexes = [ConsistencyFail::Index.new(double('model'),"users", ["email"])]
-
- subject.report_has_one_problems(fake_ar_model("Friend", :table_name => "users") => missing_indexes)
+ missing_indexes = [
+ ConsistencyFail::Index.new(
+ WrongAddress,
+ WrongAddress.table_name,
+ ["wrong_user_id"]
+ )
+ ]
- expect(@fake_out.string).to match(/Friend\s+users\s+\(email\)/m)
+ expect {
+ subject.report_has_one_problems(WrongAddress => missing_indexes)
+ }.to output(/wrong_addresses\s+\(wrong_user_id\)/).to_stdout
end
end
context "polymorphic" do
it "says everything's good" do
- subject.report_polymorphic_problems([])
-
- expect(@fake_out.string).to match(/Hooray!/)
+ expect {
+ subject.report_polymorphic_problems([])
+ }.to output(/Hooray!/).to_stdout
end
it "shows a missing compound index on a single model" do
- missing_indexes = [ConsistencyFail::Index.new(double('model'), "addresses", ["addressable_type", "addressable_id"])]
-
- subject.report_polymorphic_problems(fake_ar_model("Address", :table_name => "addresses") => missing_indexes)
-
- expect(@fake_out.string).to match(/Address\s+addresses\s+\(addressable_type, addressable_id\)/m)
+ missing_indexes = [
+ ConsistencyFail::Index.new(
+ WrongAttachment,
+ WrongAttachment.table_name,
+ ["attachable_type", "attachable_id"]
+ )
+ ]
+
+ expect {
+ subject.report_polymorphic_problems(WrongAttachment => missing_indexes)
+ }.to(
+ output(
+ /wrong_attachments\s+\(attachable_type, attachable_id\)/
+ ).to_stdout
+ )
end
end
+
end
View
21 spec/spec_helper.rb
@@ -1,13 +1,14 @@
-$:<< File.expand_path('..', __FILE__)
-$:<< File.expand_path('../../lib', __FILE__)
+require 'bundler/setup'
-require 'rubygems'
+Bundler.require(:default, :test)
-def fake_ar_model(name, options = {})
- double("AR model: #{name}", options.merge(:name => name))
-end
-
-def fake_index_on(columns, options = {})
- double("index on #{columns.inspect}", options.merge(:columns => columns))
-end
+Dir['./spec/support/**/*.rb'].each { |file| require file }
+RSpec.configure do |config|
+ config.around do |example|
+ ActiveRecord::Base.transaction do
+ example.run
+ raise ActiveRecord::Rollback
+ end
+ end
+end
View
1  spec/support/active_record.rb
@@ -0,0 +1 @@
+ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
View
3  spec/support/models/correct_account.rb
@@ -0,0 +1,3 @@
+class CorrectAccount < ActiveRecord::Base
+ validates :email, uniqueness: true
+end
View
3  spec/support/models/correct_address.rb
@@ -0,0 +1,3 @@
+class WrongAddress < ActiveRecord::Base
+ belongs_to :wrong_user
+end
View
3  spec/support/models/correct_attachment.rb
@@ -0,0 +1,3 @@
+class CorrectAttachment < ActiveRecord::Base
+ belongs_to :attachable, polymorphic: true
+end
View
3  spec/support/models/correct_person.rb
@@ -0,0 +1,3 @@
+class CorrectPerson < ActiveRecord::Base
+ validates :email, uniqueness: { scope: [:city, :state] }
+end
View
3  spec/support/models/correct_post.rb
@@ -0,0 +1,3 @@
+class CorrectPost < ActiveRecord::Base
+ has_one :correct_attachment, as: :attachable
+end
View
5 spec/support/models/correct_user.rb
@@ -0,0 +1,5 @@
+class CorrectUser < ActiveRecord::Base
+ has_one :correct_address
+
+ validates :email, uniqueness: true
+end
View
2  spec/support/models/nonexistent.rb
@@ -0,0 +1,2 @@
+class Nonexistent < ActiveRecord::Base
+end
View
3  spec/support/models/wrong_account.rb
@@ -0,0 +1,3 @@
+class WrongAccount < ActiveRecord::Base
+ validates :email, uniqueness: true
+end
View
3  spec/support/models/wrong_address.rb
@@ -0,0 +1,3 @@
+class CorrectAddress < ActiveRecord::Base
+ belongs_to :correct_user
+end
View
3  spec/support/models/wrong_attachment.rb
@@ -0,0 +1,3 @@
+class WrongAttachment < ActiveRecord::Base
+ belongs_to :attachable, polymorphic: true
+end
View
3  spec/support/models/wrong_business.rb
@@ -0,0 +1,3 @@
+class WrongBusiness < ActiveRecord::Base
+ validates :name, uniqueness: { scope: [:city, :state] }
+end
View
3  spec/support/models/wrong_person.rb
@@ -0,0 +1,3 @@
+class WrongPerson < ActiveRecord::Base
+ validates :email, :name, uniqueness: { scope: [:city, :state] }
+end
View
3  spec/support/models/wrong_post.rb
@@ -0,0 +1,3 @@
+class WrongPost < ActiveRecord::Base
+ has_one :wrong_attachment, as: :attachable
+end
View
3  spec/support/models/wrong_user.rb
@@ -0,0 +1,3 @@
+class WrongUser < ActiveRecord::Base
+ has_one :wrong_address
+end
View
100 spec/support/schema.rb
@@ -0,0 +1,100 @@
+ActiveRecord::Schema.define(version: 0) do
+ self.verbose = false
+
+ create_table :correct_accounts do |t|
+ t.string :email
+ t.timestamps
+ end
+
+ add_index "correct_accounts", ["email"], name: "index_correct_accounts_on_email", unique: true, using: :btree
+
+ create_table :correct_addresses do |t|
+ t.string :city
+ t.integer :correct_user_id
+ t.string :state
+ t.timestamps
+ end
+
+ add_index "correct_addresses", ["correct_user_id"], name: "index_correct_addresses_on_user_id", unique: true, using: :btree
+
+ create_table :correct_attachments do |t|
+ t.integer :attachable_id
+ t.string :attachable_type
+ t.string :name
+ t.timestamps
+ end
+
+ add_index "correct_attachments", ["attachable_id", "attachable_type"], name: "index_correct_attachments_on_attachable_id_and_attachable_type", unique: true, using: :btree
+ add_index "correct_attachments", ["name"], name: "index_correct_attachments_on_name", unique: true, using: :btree
+
+ create_table :correct_people do |t|
+ t.string :city
+ t.string :email
+ t.string :name
+ t.string :state
+ t.timestamps
+ end
+
+ add_index "correct_people", ["state", "city", "email"], name: "index_correct_people_on_city_and_state_and_email", unique: true, using: :btree
+
+ create_table :correct_posts do |t|
+ t.text :content
+ t.string :title
+ t.timestamps
+ end
+
+ create_table :correct_users do |t|
+ t.string :email
+ t.string :name
+ t.timestamps
+ end
+
+ create_table :wrong_accounts do |t|
+ t.string :email
+ t.timestamps
+ end
+
+ create_table :wrong_addresses do |t|
+ t.string :city
+ t.string :state
+ t.integer :wrong_user_id
+ t.timestamps
+ end
+
+ add_index "wrong_addresses", ["wrong_user_id"], name: "index_wrong_addresses_on_user_id", using: :btree
+
+ create_table :wrong_attachments do |t|
+ t.integer :attachable_id
+ t.string :attachable_type
+ t.string :name
+ t.timestamps
+ end
+
+ create_table :wrong_businesses do |t|
+ t.string :city
+ t.string :name
+ t.string :state
+ t.timestamps
+ end
+
+ create_table :wrong_people do |t|
+ t.string :city
+ t.string :email
+ t.string :name
+ t.string :state
+ t.timestamps
+ end
+
+ create_table :wrong_posts do |t|
+ t.text :content
+ t.string :title
+ t.timestamps
+ end
+
+ create_table :wrong_users do |t|
+ t.string :email
+ t.string :name
+ t.timestamps
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.