Add tests for raw_visible_posts

This commit is contained in:
Raphael Sofaer 2011-04-02 19:33:45 -07:00
parent c800b0bfef
commit d09bf35213
2 changed files with 31 additions and 7 deletions

View file

@ -13,11 +13,12 @@ module Diaspora
end
def raw_visible_posts(opts = {})
opts = opts.dup
opts[:type] ||= ['StatusMessage', 'Photo']
opts[:limit] ||= 20
opts[:order] ||= 'updated_at DESC'
opts[:hidden] ||= false
opts[:order] = '`posts`.' + opts[:order]
order_with_table = '`posts`.' + opts[:order]
opts[:limit] = opts[:limit].to_i * opts[:page].to_i if opts[:page]
posts_from_others = Post.joins(:contacts).where( :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id})
@ -29,10 +30,10 @@ module Diaspora
posts_from_self = posts_from_self.joins(:aspect_visibilities).where(:aspect_visibilities => {:aspect_id => opts[:by_members_of]})
end
post_ids = Post.connection.execute(posts_from_others.select('posts.id').limit(opts[:limit]).order(opts[:order]).to_sql).map{|r| r.first}
post_ids += Post.connection.execute(posts_from_self.select('posts.id').limit(opts[:limit]).order(opts[:order]).to_sql).map{|r| r.first}
post_ids = Post.connection.execute(posts_from_others.select('posts.id').limit(opts[:limit]).order(order_with_table).to_sql).map{|r| r.first}
post_ids += Post.connection.execute(posts_from_self.select('posts.id').limit(opts[:limit]).order(order_with_table).to_sql).map{|r| r.first}
Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT `posts`.*').limit(opts[:limit])
Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT `posts`.*').limit(opts[:limit]).order(opts[:order])
end
def visible_photos

View file

@ -127,9 +127,32 @@ describe User do
end
it "returns an empty array when passed an aspect the user doesn't own" do
other_user = Factory(:user_with_aspect)
connect_users(eve, eve.aspects.first, other_user, other_user.aspects.first)
alice.people_in_aspects([other_user.aspects.first]).should == []
alice.people_in_aspects([eve.aspects.first]).should == []
end
end
context 'with many posts' do
before do
bob.move_contact(eve.person, bob.aspects.first, bob.aspects.create(:name => 'new aspect'))
(1..18).each do |n|
[alice, bob, eve].each do |u|
post = u.post :status_message, :text => "#{u.username} - #{n}", :to => u.aspects.first.id
post.created_at = post.created_at - n*10*u.id
post.updated_at = post.updated_at - n*10*u.id
post.save
end
end
end
it 'works' do #This is in one spec to save time
bob.raw_visible_posts.should == bob.raw_visible_posts(:by_members_of => bob.aspects.map{|a| a.id})
bob.raw_visible_posts.sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts.map{|p| p.id}.reverse
opts = {:limit => 40}
bob.raw_visible_posts(opts).should == bob.raw_visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id}))
bob.raw_visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts(opts).map{|p| p.id}.reverse
opts = {:page => 2}
bob.raw_visible_posts(opts).map{|p| p.id}.should == bob.raw_visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})).map{|p| p.id}
bob.raw_visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts(opts).map{|p| p.id}.reverse
end
end
end