From d1604a5bb5d02fd7b6328616d034895815e9a23d Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Thu, 8 Sep 2011 11:58:14 -0700 Subject: [PATCH] Blow out the visible_posts specs so I can more easily see what it's doing --- spec/models/user/querying_spec.rb | 96 ++++++++++++++++++------------- 1 file changed, 57 insertions(+), 39 deletions(-) diff --git a/spec/models/user/querying_spec.rb b/spec/models/user/querying_spec.rb index 8c1ddc933..822f54cd4 100644 --- a/spec/models/user/querying_spec.rb +++ b/spec/models/user/querying_spec.rb @@ -13,16 +13,37 @@ describe User do end describe "#visible_posts" do - it "returns all the posts the user can see" do - self_post = alice.post(:status_message, :text => "hi", :to => @alices_aspect.id) - visible_post = bob.post(:status_message, :text => "hello", :to => @bobs_aspect.id) + it "contains your public posts" do + public_post = alice.post(:status_message, :text => "hi", :to => @alices_aspect.id, :public => true) + alice.visible_posts.should include(public_post) + end + it "contains your non-public posts" do + private_post = alice.post(:status_message, :text => "hi", :to => @alices_aspect.id, :public => false) + alice.visible_posts.should include(private_post) + end + it "contains public posts from people you're following" do + dogs = bob.aspects.create(:name => "dogs") + bobs_public_post = bob.post(:status_message, :text => "hello", :public => true, :to => dogs.id) + alice.visible_posts.should include(bobs_public_post) + end + it "contains non-public posts from people who are following you" do + bobs_post = bob.post(:status_message, :text => "hello", :to => @bobs_aspect.id) + alice.visible_posts.should include(bobs_post) + end + it "does not contain non-public posts from aspects you're not in" do dogs = bob.aspects.create(:name => "dogs") invisible_post = bob.post(:status_message, :text => "foobar", :to => dogs.id) + alice.visible_posts.should_not include(invisible_post) + end + it "does not contain duplicate posts" do + bobs_other_aspect = bob.aspects.create(:name => "cat people") + bob.add_contact_to_aspect(bob.contact_for(alice.person), bobs_other_aspect) + bob.aspects_with_person(alice.person).should =~ [@bobs_aspect, bobs_other_aspect] - stream = alice.visible_posts - stream.should include(self_post) - stream.should include(visible_post) - stream.should_not include(invisible_post) + bobs_post = bob.post(:status_message, :text => "hai to all my people", :to => [@bobs_aspect.id, bobs_other_aspect.id]) + + alice.visible_posts.length.should == 1 + alice.visible_posts.should include(bobs_post) end context 'with many posts' do before do @@ -41,44 +62,42 @@ describe User do end it 'works' do #This is in one spec to save time bob.visible_posts.length.should == 15 #it returns 15 by default - bob.visible_posts.should == bob.visible_posts(:by_members_of => bob.aspects.map{|a| a.id}) # it is the same when joining through aspects - bob.visible_posts.sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.visible_posts.map{|p| p.id}.reverse #it is sorted updated_at desc by default + bob.visible_posts.should == bob.visible_posts(:by_members_of => bob.aspects.map { |a| a.id }) # it is the same when joining through aspects + bob.visible_posts.sort_by { |p| p.updated_at }.map { |p| p.id }.should == bob.visible_posts.map { |p| p.id }.reverse #it is sorted updated_at desc by default opts = {:limit => 40} bob.visible_posts(opts).length.should == 40 #it takes a limit - bob.visible_posts(opts).should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})) - bob.visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.visible_posts(opts).map{|p| p.id}.reverse + bob.visible_posts(opts).should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map { |a| a.id })) + bob.visible_posts(opts).sort_by { |p| p.updated_at }.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse - last_time_of_last_page = bob.visible_posts.last.updated_at + last_time_of_last_page = bob.visible_posts.last.updated_at opts = {:max_time => last_time_of_last_page} bob.visible_posts(opts).length.should == 15 - bob.visible_posts(opts).map{|p| p.id}.should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})).map{|p| p.id} - bob.visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.visible_posts(opts).map{|p| p.id}.reverse - bob.visible_posts(opts).map{|p|p.id}.should == bob.visible_posts(:limit => 40)[15...30].map{|p|p.id} #pagination should return the right posts + bob.visible_posts(opts).map { |p| p.id }.should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map { |a| a.id })).map { |p| p.id } + bob.visible_posts(opts).sort_by { |p| p.updated_at }.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse + bob.visible_posts(opts).map { |p| p.id }.should == bob.visible_posts(:limit => 40)[15...30].map { |p| p.id } #pagination should return the right posts opts = {:max_time => last_time_of_last_page.to_i} bob.visible_posts(opts).length.should == 15 - bob.visible_posts(opts).map{|p| p.id}.should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})).map{|p| p.id} - bob.visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.visible_posts(opts).map{|p| p.id}.reverse - bob.visible_posts(opts).map{|p|p.id}.should == bob.visible_posts(:limit => 40)[15...30].map{|p|p.id} #pagination should return the right posts + bob.visible_posts(opts).map { |p| p.id }.should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map { |a| a.id })).map { |p| p.id } + bob.visible_posts(opts).sort_by { |p| p.updated_at }.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse + bob.visible_posts(opts).map { |p| p.id }.should == bob.visible_posts(:limit => 40)[15...30].map { |p| p.id } #pagination should return the right posts end end - end - context 'with two posts' do - before do - connect_users(eve, @eves_aspect, alice, @alices_aspect) - aspect3 = alice.aspects.create(:name => "Snoozers") - @status_message1 = eve.post :status_message, :text => "hi", :to => @eves_aspect.id - @status_message2 = eve.post :status_message, :text => "hey", :public => true , :to => @eves_aspect.id - @status_message3 = alice.post :status_message, :text => "hey", :public => true , :to => @alices_aspect.id - @status_message4 = eve.post :status_message, :text => "blah", :public => true , :to => @eves_aspect.id - @status_message5 = alice.post :status_message, :text => "secrets", :to => aspect3.id + context 'with two posts' do + before do + connect_users(eve, @eves_aspect, alice, @alices_aspect) + aspect3 = alice.aspects.create(:name => "Snoozers") + @status_message1 = eve.post :status_message, :text => "hi", :to => @eves_aspect.id + @status_message2 = eve.post :status_message, :text => "hey", :public => true, :to => @eves_aspect.id + @status_message3 = alice.post :status_message, :text => "hey", :public => true, :to => @alices_aspect.id + @status_message4 = eve.post :status_message, :text => "blah", :public => true, :to => @eves_aspect.id + @status_message5 = alice.post :status_message, :text => "secrets", :to => aspect3.id - @pending_status_message = eve.post :status_message, :text => "hey", :public => true , :to => @eves_aspect.id, :pending => true - end + @pending_status_message = eve.post :status_message, :text => "hey", :public => true, :to => @eves_aspect.id, :pending => true + end - describe "#visible_posts" do it "queries by person id" do query = eve.visible_posts.where(:author_id => eve.person.id) query.include?(@status_message1).should == true @@ -116,24 +135,23 @@ describe User do eve.visible_posts.should_not include @pending_status_message end - it '#find_visible_post_by_id' do - eve.find_visible_post_by_id(@status_message1.id).should == @status_message1 - eve.find_visible_post_by_id(@status_message5.id).should == nil - end - it 'is not emptied by a load of pending photos' do 15.times { eve.build_post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name), :to => eve.aspect_ids, :updated_at => Time.now + 1.day).save! } query = eve.visible_posts - query.map{|p| p.id}.should =~ [@status_message1, @status_message2, @status_message3, @status_message4].map{|p| p.id} + query.map { |p| p.id }.should =~ [@status_message1, @status_message2, @status_message3, @status_message4].map { |p| p.id } end it 'is not emptied by a load of photos' do 15.times { eve.build_post(:photo, :pending => false, :user_file=> File.open(photo_fixture_name), :to => eve.aspect_ids, :updated_at => Time.now + 1.day).save! } query = eve.visible_posts(:type => 'StatusMessage') - query.map{|p| p.id}.should =~ [@status_message1, @status_message2, @status_message3, @status_message4].map{|p| p.id} + query.map { |p| p.id }.should =~ [@status_message1, @status_message2, @status_message3, @status_message4].map { |p| p.id } + end + it '#find_visible_post_by_id' do + eve.find_visible_post_by_id(@status_message1.id).should == @status_message1 + eve.find_visible_post_by_id(@status_message5.id).should == nil end end end @@ -284,7 +302,7 @@ describe User do msg4.created_at = Time.now+14 msg4.save! - alice.posts_from(@user3.person).map{|p| p.id}.should == [msg4, msg3, @public_message].map{|p| p.id} + alice.posts_from(@user3.person).map { |p| p.id }.should == [msg4, msg3, @public_message].map { |p| p.id } end end end