diff options
author | re4k <re4k@re4k.info> | 2013-04-23 22:20:39 +0900 |
---|---|---|
committer | re4k <re4k@re4k.info> | 2013-04-23 22:20:39 +0900 |
commit | 17f9b3cf660086427b7089ba2f1ba5b2adc738be (patch) | |
tree | fe29e4506a0e7f78e09d29814c17bf8de810df17 /spec | |
parent | 667d88c12805d2a1f5b62b9ff4e04d6e9ba595fe (diff) | |
download | aclog-17f9b3cf660086427b7089ba2f1ba5b2adc738be.tar.gz |
rewrite Tweet tests/fixtures
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/favorites.rb | 2 | ||||
-rw-r--r-- | spec/factories/retweets.rb | 2 | ||||
-rw-r--r-- | spec/factories/tweets.rb | 6 | ||||
-rw-r--r-- | spec/factories/users.rb | 4 | ||||
-rw-r--r-- | spec/models/tweet_spec.rb | 169 |
5 files changed, 112 insertions, 71 deletions
diff --git a/spec/factories/favorites.rb b/spec/factories/favorites.rb index 4b9551d..56a794e 100644 --- a/spec/factories/favorites.rb +++ b/spec/factories/favorites.rb @@ -1,6 +1,4 @@ FactoryGirl.define do factory :favorite do - user - tweet end end diff --git a/spec/factories/retweets.rb b/spec/factories/retweets.rb index 9f9dba6..67bb1b0 100644 --- a/spec/factories/retweets.rb +++ b/spec/factories/retweets.rb @@ -1,6 +1,4 @@ FactoryGirl.define do factory :retweet do - user - tweet end end diff --git a/spec/factories/tweets.rb b/spec/factories/tweets.rb index 01f7919..9b4250c 100644 --- a/spec/factories/tweets.rb +++ b/spec/factories/tweets.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :tweet do |t| - sequence(:text){|n| "text_" + (n ** 2).to_s} - sequence(:source){|n| "source_" + (n % 10).to_s} - sequence(:tweeted_at){|n| Time.at((1199113200..1388502000).to_a.sample)} + sequence(:text){|n| "text_#{n}"} + sequence(:source){|n| "source_#{n / 2}"} + sequence(:tweeted_at){|n| Time.at(1360000000 + n * 1000)} end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 35270ad..87159e0 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -8,6 +8,7 @@ FactoryGirl.define do protected false end + # screen_name won't be changed factory :user_exists, class: User do id 15926668 screen_name "toshi_a" @@ -16,6 +17,7 @@ FactoryGirl.define do protected false end + # nil factory :user_not_exists, class: User do id 0 screen_name "" @@ -24,6 +26,7 @@ FactoryGirl.define do protected false end + # permanent suspended factory :user_suspended, class: User do id 230367516 screen_name "re4k" @@ -32,6 +35,7 @@ FactoryGirl.define do protected false end + # model user factory :user do |u| sequence(:screen_name){|n| "screen_name_#{n}"} sequence(:name){|n| "name_#{n}"} diff --git a/spec/models/tweet_spec.rb b/spec/models/tweet_spec.rb index 78bf391..25bff25 100644 --- a/spec/models/tweet_spec.rb +++ b/spec/models/tweet_spec.rb @@ -2,93 +2,134 @@ require 'spec_helper' describe Tweet do - context "scopes" do - before :all do - @favoriters = [] - @retweeters = [] - @created_users = FactoryGirl.create_list(:user, 5) - @created_users.each do |u| - ts = FactoryGirl.create_list(:tweet, 3, user: u) - us = @created_users.reject{|m| m == u} # 4 - - # favs: 3, 1, 0 - us.shuffle.take(3).each do |m| - FactoryGirl.create(:favorite, tweet: ts[0], user: m) - @favoriters << m - end - _s = us.shuffle[1] - FactoryGirl.create(:favorite, tweet: ts[1], user: _s) - @favoriters << _s - - # rts: 2, 1, 0 - us.shuffle.take(2).each do |m| - FactoryGirl.create(:retweet, tweet: ts[0], user: m) - @retweeters << m - end - _m = us.shuffle[1] - FactoryGirl.create(:retweet, tweet: ts[1], user: _m) - @retweeters << _m - end - @favoriters.uniq! - @retweeters.uniq! + before do + @user_0, @user_1, @user_2 = FactoryGirl.create_list(:user, 3) # t/f/r = 3/1/0, 1/2/1, 0/0/1 + + @tweet_0_0 = FactoryGirl.create(:tweet, user: @user_0, tweeted_at: 2.days.ago) # f/r = 2/0 + @tweet_0_1 = FactoryGirl.create(:tweet, user: @user_0, tweeted_at: 4.days.ago) # f/r = 0/1 + @tweet_0_2 = FactoryGirl.create(:tweet, user: @user_0, tweeted_at: 6.days.ago) # f/r = 0/0 + @tweet_1_0 = FactoryGirl.create(:tweet, user: @user_1, tweeted_at: 1.days.ago) # f/r = 1/1 + + @tweet_0_0_f_0 = FactoryGirl.create(:favorite, user: @user_0, tweet: @tweet_0_0) + @tweet_0_0_f_1 = FactoryGirl.create(:favorite, user: @user_1, tweet: @tweet_0_0) + @tweet_0_1_r_1 = FactoryGirl.create(:retweet, user: @user_1, tweet: @tweet_0_1) + @tweet_1_0_f_1 = FactoryGirl.create(:favorite, user: @user_1, tweet: @tweet_1_0) + @tweet_1_0_r_2 = FactoryGirl.create(:retweet, user: @user_2, tweet: @tweet_1_0) + end + + describe ".create!" do + subject { @tweet_0_0 } + its(:text) { should_not be nil } + its(:source) { should_not be nil } + its(:user_id) { should be @user_0.id } + its(:user) { should eq @user_0 } + end + + describe "counter_cache" do + subject { @tweet_0_0.reload } + its(:favorites_count) { should be subject.favorites.count } + its(:retweets_count) { should be subject.retweets.count } + end + + describe ".cached" do + subject { Tweet.cached(@tweet_0_0) } + its(:id) { should be @tweet_0_0.id } + its(:text) { should eq @tweet_0_0.text } + its(:source) { should eq @tweet_0_0.source } + its(:user) { should eq @tweet_0_0.user } + end + + describe ".delete_from_id" do + context "when of tweet" do + subject { Tweet.delete_from_id(@tweet_1_0.id) } + it { should be @tweet_1_0.id } + it { Tweet.where(id: subject).first.should be nil } + it { Favorite.where(tweet_id: subject).count.should be 0 } + it { Retweet.where(tweet_id: subject).count.should be 0 } end - after :all do - @created_users.map(&:destroy) + + context "when of retweet" do + subject { Tweet.delete_from_id(@tweet_1_0_r_2.id).tap { @tweet_1_0.reload } } + it { should be @tweet_1_0_r_2.id } + it { Retweet.where(id: subject).first.should be nil } + it { @tweet_1_0.retweets_count.should be 0 } end + end - it "3日分" do - tweets = Tweet.recent - tweets.should_not include -> tweet {tweet.tweeted_at < Time.zone.now - 3.days} + describe ".from_hash" do + let(:test_data) do + {text: "abc", + source: "web", + tweeted_at: Time.now, + user_id: @user_0.id} end - it "反応があった分" do - tweets = Tweet.reacted - tweets.should_not include -> tweet {tweet.favorites_count + tweet.retweets_count == 0} + subject { Tweet.from_hash(test_data) } + it { should be_a Tweet } + its(:text) { should eq test_data[:text] } + its(:source) { should eq test_data[:source] } + its(:tweeted_at) { should eq test_data[:tweeted_at] } + its(:user) { should eq @user_0 } + end + + context "scopes" do + describe "recent" do + subject { Tweet.recent } + it { should_not include -> tweet { tweet.tweeted_at < Time.zone.now - 3.days } } + its(:count) { should be 2 } end - it "新しい順" do - tweets = Tweet.order_by_id.limit(2) - tweets.first.id.should be > tweets.last.id + describe "reacted" do + subject { Tweet.reacted } + it { should_not include -> tweet { tweet.favorites_count + tweet.retweets_count == 0 } } + it { should_not include -> tweet { tweet.favorites.count + tweet.retweets.count == 0 } } + its(:count) { should be 3 } end - it "ふぁぼ数順" do - tweets = Tweet.order_by_favorites - tweets.first.favorites.count.should be >= tweets.last.favorites.count + describe "order_by_id" do + subject { Tweet.order_by_id } + it { subject.first.id.should be > subject.last.id } end - it "RT数順" do - tweets = Tweet.order_by_retweets - tweets.first.retweets.count.should be >= tweets.last.retweets.count + describe "order_by_favorites" do + subject { Tweet.order_by_favorites } + it { subject.first.favorites.count.should be >= subject.last.favorites.count } end - it "反応数順" do - tweets = Tweet.order_by_reactions - tweets.first.retweets.count.should be >= tweets.last.retweets.count + describe "order_by_retweets" do + subject { Tweet.order_by_retweets } + it { subject.first.retweets.count.should be >= subject.last.retweets.count } end - it "ユーザーにふぁぼられたツイート" do - tweets = Tweet.favorited_by(@favoriters.first) - tweets.should_not include -> tweet {tweet.user_id != @favoriters.first.id} + describe "order_by_reactions" do + subject { Tweet.order_by_favorites } + it { + (subject.first.favorites.count + subject.first.retweets.count) + .should be >= (subject.last.favorites.count + subject.last.retweets.count) + } end - it "ユーザーにRTされたツイート" do - tweets = Tweet.retweeted_by(@retweeters.first) - tweets.should_not include -> tweet {tweet.user_id != @retweeters.first.id} + describe "favorited_by" do + subject { Tweet.favorited_by(@user_1) } + its(:count) { should be 2 } + it { should_not include -> tweet { not tweet.favorites.any? {|a| a.user_id == @user_1.id } } } end - it "ユーザーが反応したツイート" do - user = (@favoriters + @retweeters).sample - tweets = Tweet.favorited_by(user) - tweets.should_not include -> tweet {tweet.user_id != user.id} + describe "retweeted_by" do + subject { Tweet.retweeted_by(@user_2) } + its(:count) { should be 1 } + it { should_not include -> tweet { not tweet.retweets.any? {|a| a.user_id == @user_2.id } } } end - it "オリジナルのツイート" do - # TODO + describe "discovered_by" do + subject { Tweet.discovered_by(@user_1) } + its(:count) { should be 3 } + it { subject.select {|m| m.favorites.any? {|n| n.user_id == @user_1.id } }.count.should be 2 } + it { subject.select {|m| m.retweets.any? {|n| n.user_id == @user_1.id } }.count.should be 1 } + it { should_not include -> tweet { not (tweet.retweets + tweet.favorites).any? {|a| a.user_id == @user_1.id } } } end - end - context "class methods" do - it "Tweet.cached" do + describe "original" do # TODO end end |