Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.

Commit 323c938

Browse files
committed
Fix specs to fit Rails updates.
1 parent f5a810c commit 323c938

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
lines changed

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
require 'pry'
12
require 'faker'
23
require 'active_record'
34
require 'active_support'
5+
require 'active_support/core_ext/string'
46

57
module ActiveRecord
68
# Shamelessly swiped from the AR test code

spec/squeel/adapters/active_record/relation_extensions_spec.rb

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,44 @@ module ActiveRecord
686686

687687
it 'uses Squeel and Arel at the same time' do
688688
relation = User.where{id.in([1,2,3]) & User.arel_table[:id].not_eq(nil) }
689-
relation.to_sql.should match /SELECT #{Q}users#{Q}.\* FROM #{Q}users#{Q}\s+WHERE \(\(#{Q}users#{Q}.#{Q}id#{Q} IN \(1, 2, 3\) AND #{Q}users#{Q}.#{Q}id#{Q} IS NOT NULL\)\)/
690-
relation = User.where{
691-
(id.in([1,2,3]) | User.arel_table[:id].eq(1)) & ((id == 1) | User.arel_table[:id].not_eq(nil)) }
692-
relation.to_sql.should match /SELECT #{Q}users#{Q}.\* FROM #{Q}users#{Q}\s+WHERE \(\(\(#{Q}users#{Q}.#{Q}id#{Q} IN \(1, 2, 3\) OR #{Q}users#{Q}.#{Q}id#{Q} = 1\) AND \(#{Q}users#{Q}.#{Q}id#{Q} = 1 OR #{Q}users#{Q}.#{Q}id#{Q} IS NOT NULL\)\)\)/
689+
690+
if activerecord_version_at_least '4.2.0'
691+
relation.to_sql.should eq "
692+
SELECT #{Q}users#{Q}.* FROM #{Q}users#{Q}
693+
WHERE (#{Q}users#{Q}.#{Q}id#{Q} IN (1, 2, 3)
694+
AND #{Q}users#{Q}.#{Q}id#{Q} IS NOT NULL)
695+
".squish
696+
else
697+
relation.to_sql.should match /SELECT #{Q}users#{Q}.\* FROM #{Q}users#{Q}\s+WHERE \(\(#{Q}users#{Q}.#{Q}id#{Q} IN \(1, 2, 3\) AND #{Q}users#{Q}.#{Q}id#{Q} IS NOT NULL\)\)/
698+
end
699+
700+
relation = User.where {
701+
(id.in([1,2,3]) | User.arel_table[:id].eq(1)) &
702+
((id == 1) | User.arel_table[:id].not_eq(nil)) }
703+
704+
if activerecord_version_at_least '4.2.0'
705+
relation.to_sql.should eq "
706+
SELECT #{Q}users#{Q}.*
707+
FROM #{Q}users#{Q}
708+
WHERE ((#{Q}users#{Q}.#{Q}id#{Q} IN (1, 2, 3) OR #{Q}users#{Q}.#{Q}id#{Q} = 1)
709+
AND (#{Q}users#{Q}.#{Q}id#{Q} = 1 OR #{Q}users#{Q}.#{Q}id#{Q} IS NOT NULL))
710+
".squish
711+
else
712+
relation.to_sql.should match /SELECT #{Q}users#{Q}.\* FROM #{Q}users#{Q}\s+WHERE \(\(\(#{Q}users#{Q}.#{Q}id#{Q} IN \(1, 2, 3\) OR #{Q}users#{Q}.#{Q}id#{Q} = 1\) AND \(#{Q}users#{Q}.#{Q}id#{Q} = 1 OR #{Q}users#{Q}.#{Q}id#{Q} IS NOT NULL\)\)\)/
713+
end
693714
end
694715

716+
it "large than & less than" do
717+
if activerecord_version_at_least '4.1.0'
718+
relation = User.where { created_at <= 1.hours.ago }
719+
expect { relation.to_sql }.not_to raise_error
720+
721+
relation = User.where { created_at < 1.hours.ago }
722+
expect { relation.to_sql }.not_to raise_error
723+
else
724+
pending 'Unsupported under Rails 4.1'
725+
end
726+
end
695727
end
696728

697729
describe '#joins' do
@@ -745,7 +777,14 @@ module ActiveRecord
745777
it 'validates polymorphic relationship with source type' do
746778
if activerecord_version_at_least('4.0.0')
747779
relation = Group.joins{users}
748-
relation.to_sql.should match /#{Q}memberships#{Q}.#{Q}active#{Q} = ['1t']{1,3} AND #{Q}memberships#{Q}.#{Q}member_type#{Q} = 'User'/
780+
781+
if MYSQL_ENV
782+
relation.to_sql.should match /#{Q}memberships#{Q}.#{Q}active#{Q} = 1/
783+
else
784+
relation.to_sql.should match /#{Q}memberships#{Q}.#{Q}active#{Q} = 't'/
785+
end
786+
787+
relation.to_sql.should match /#{Q}memberships#{Q}.#{Q}member_type#{Q} = 'User'/
749788
relation.to_sql.should match /INNER JOIN #{Q}users#{Q} ON #{Q}users#{Q}.#{Q}id#{Q} = #{Q}memberships#{Q}.#{Q}member_id#{Q}/
750789
relation.to_sql.should match /INNER JOIN #{Q}memberships#{Q} ON #{Q}memberships#{Q}.#{Q}group_id#{Q} = #{Q}groups#{Q}.#{Q}id#{Q}/
751790
elsif activerecord_version_at_least('3.2.7')
@@ -778,11 +817,27 @@ module ActiveRecord
778817
else
779818
User.first.groups.to_sql.should match /#{Q}memberships#{Q}.#{Q}active#{Q} = 't'/
780819
end
781-
782820
else
783821
pending "Rails 3.0.x doesn't support to_sql in an association."
784822
end
785823
end
824+
825+
it 'default scopes with multiple wheres' do
826+
if activerecord_version_at_least('4.0.0')
827+
relation = Dept.joins { people_named_bill_with_low_salary }
828+
829+
relation.to_sql.should eq "
830+
SELECT #{Q}depts#{Q}.*
831+
FROM #{Q}depts#{Q}
832+
INNER JOIN #{Q}people#{Q} ON
833+
#{Q}people#{Q}.#{Q}dept_id#{Q} = #{Q}depts#{Q}.#{Q}id#{Q} AND
834+
#{Q}people#{Q}.#{Q}name#{Q} = 'Bill' AND
835+
#{Q}people#{Q}.#{Q}salary#{Q} < 20000
836+
".squish
837+
else
838+
pending "Rails 3.x doesn't support default scope in joins"
839+
end
840+
end
786841
end
787842

788843
describe '#having' do

spec/support/models.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Person < ActiveRecord::Base
2+
belongs_to :dept
23
belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id
34
has_many :children, :class_name => 'Person', :foreign_key => :parent_id
45
has_many :articles
@@ -58,6 +59,19 @@ class PersonNamedBill < ActiveRecord::Base
5859
scope :with_salary_equal_to, lambda { |value| where{abs(salary) == value} }
5960
end
6061

62+
class Dept < ActiveRecord::Base
63+
has_many :people_named_bill_with_low_salary,
64+
class_name: 'PersonNamedBillAndLowSalary', foreign_key: 'dept_id'
65+
end
66+
67+
class PersonNamedBillAndLowSalary < Person
68+
if ActiveRecord::VERSION::MAJOR > 3 || ActiveRecord::VERSION::MINOR > 0
69+
default_scope { where { name == 'Bill' }.where { salary < 20000 } }
70+
else # 3.0 doesn't support callables for default_scope
71+
default_scope where { name == 'Bill' }.where { salary < 20000 }
72+
end
73+
end
74+
6175
class Message < ActiveRecord::Base
6276
belongs_to :author, :class_name => 'Person'
6377
belongs_to :recipient, :class_name => 'Person'
@@ -145,9 +159,14 @@ class Payment < ActiveRecord::Base
145159

146160
class Models
147161
def self.make
162+
dept = Dept.create(name: Faker::Lorem.name)
163+
148164
10.times do |i|
149165
# 10 people total, salary gt 30000
150-
person = Person.create(name: Faker::Name.name, salary: 30000 + (i + 1) * 1000)
166+
person = Person.create(name: Faker::Name.name,
167+
salary: 30000 + (i + 1) * 1000,
168+
dept: dept)
169+
151170
2.times do
152171
# 20 unidentified object total, 2 per person
153172
person.unidentified_objects.create(name: Faker::Lorem.words(1).first)

spec/support/schema.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,15 @@ def rebuild_postgresql_db
7979
ActiveRecord::Migration.verbose = false
8080

8181
ActiveRecord::Schema.define do
82+
create_table :depts, :force => true do |t|
83+
t.string :name
84+
end
85+
8286
create_table :people, :force => true do |t|
8387
t.integer :parent_id
8488
t.string :name
8589
t.integer :salary
90+
t.integer :dept_id
8691
end
8792

8893
create_table :messages, :force => true do |t|

squeel.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Gem::Specification.new do |s|
2727
s.add_development_dependency 'mysql', '~> 2.9.1'
2828
s.add_development_dependency 'mysql2', '~> 0.3.16'
2929
s.add_development_dependency 'pg', '~> 0.17.1'
30+
s.add_development_dependency 'git_pretty_accept', '~> 0.4.0'
31+
s.add_development_dependency 'pry'
3032

3133
s.files = `git ls-files`.split("\n")
3234
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")

0 commit comments

Comments
 (0)