Skip to content

Commit

Permalink
Added failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
aggronerd committed Nov 23, 2016
1 parent 265f225 commit 43677a1
Showing 1 changed file with 69 additions and 32 deletions.
101 changes: 69 additions & 32 deletions spec/integration/relation_querying_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,83 @@ class Sub < Super; end

context 'querying a :belongs_to relation' do

before do

# The related class
ActiveRecord::Migration.create_table(:hars) { |t| t.string :related }
class Har < ActiveRecord::Base; has_many :loos; end

# The class on which to call search_for
ActiveRecord::Migration.create_table(:loos) { |t| t.string :foo; t.integer :har_id }
class Loo < ActiveRecord::Base
belongs_to :har
scoped_search :in => :har, :on => :related
context 'basic example' do

before do

# The related class
ActiveRecord::Migration.create_table(:hars) { |t| t.string :related }
class Har < ActiveRecord::Base; has_many :loos; end

# The class on which to call search_for
ActiveRecord::Migration.create_table(:loos) { |t| t.string :foo; t.integer :har_id }
class Loo < ActiveRecord::Base
belongs_to :har
scoped_search :in => :har, :on => :related
end

@har_record = Har.create!(:related => 'bar')

Loo.create!(:foo => 'foo', :har => @har_record)
Loo.create!(:foo => 'foo too', :har => @har_record)
Loo.create!(:foo => 'foo three', :har => Har.create!(:related => 'another bar'))
Loo.create!(:foo => 'foo four')
end

after do
ScopedSearch::RSpec::Database.drop_model(Har)
ScopedSearch::RSpec::Database.drop_model(Loo)
end

it "should find all records with a related bar record containing bar" do
Loo.search_for('bar').length.should == 3
end

it "should find all records with a related bar record having an exact value of bar with an explicit field" do
Loo.search_for('related = bar').length.should == 2
end

it "should find records for which the bar relation is not set using null?" do
Loo.search_for('null? related').length.should == 1
end

it "should find records for which the bar relation is not set using null?" do
Loo.search_for('',:order => 'related asc').first.foo.should eql('foo four')
end

end

context 'model which refers to instances of the same model' do

before do

@har_record = Har.create!(:related => 'bar')
# The related class
ActiveRecord::Migration.create_table(:nodes) { |t| t.string :name; t.integer :parent_id }
class Node < ActiveRecord::Base
has_many :children, foreign_key: :parent_id, class_name: to_s
belongs_to :parent, foreign_key: :parent_id, class_name: to_s

Loo.create!(:foo => 'foo', :har => @har_record)
Loo.create!(:foo => 'foo too', :har => @har_record)
Loo.create!(:foo => 'foo three', :har => Har.create!(:related => 'another bar'))
Loo.create!(:foo => 'foo four')
end
scoped_search in: :parent, on: :name, alias: 'parent_name', only_explicit: true
end

after do
ScopedSearch::RSpec::Database.drop_model(Har)
ScopedSearch::RSpec::Database.drop_model(Loo)
end
@parent = Node.create!(:name => 'parent')

it "should find all records with a related bar record containing bar" do
Loo.search_for('bar').length.should == 3
end
3.times { Node.create!(:name => 'Child 1', :parent => @parent) }

it "should find all records with a related bar record having an exact value of bar with an explicit field" do
Loo.search_for('related = bar').length.should == 2
end
Node.create!(:name => 'Orphan')
end

it "should find records for which the bar relation is not set using null?" do
Loo.search_for('null? related').length.should == 1
end
after do
ScopedSearch::RSpec::Database.drop_model(Node)
end

it "should find records for which the bar relation is not set using null?" do
Loo.search_for('',:order => 'related asc').first.foo.should eql('foo four')
it "should return children whos parent has matching name" do
Node.search_for('parent_name == "Parent"').length.should == 3
end

it "should return orphaned nodes" do
Node.search_for('null? parent_name').length.should == 2
end
end

end
Expand Down

0 comments on commit 43677a1

Please sign in to comment.