From 2e6c8b1426e84ce45294545653dd4c2af3a82348 Mon Sep 17 00:00:00 2001 From: rhenium Date: Wed, 5 Jun 2013 05:19:34 +0900 Subject: update specs --- spec/models/account_spec.rb | 38 +++++++++++++++++++++-- spec/models/tweet_spec.rb | 74 +++++++++++++++++++++++++-------------------- spec/models/user_spec.rb | 34 +++++++++++++++++++++ 3 files changed, 110 insertions(+), 36 deletions(-) (limited to 'spec') diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 76ef4a2..02239a4 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -10,18 +10,49 @@ describe Account do its(:oauth_token) { should eq account.oauth_token } its(:oauth_token_secret) { should eq account.oauth_token_secret } its(:consumer_version) { should be account.consumer_version } + its(:status) { should be Account::ACTIVE } end context "when already recorded" do - let(:old_account) { FactoryGirl.create(:account_1) } + before { @old_account = FactoryGirl.create(:account_1).update_settings!(notification: false, private: true) } let(:new_account) { FactoryGirl.build(:account_2) } subject { Account.create_or_update(new_account.attributes.symbolize_keys) } - its(:id) { should be old_account.id } - its(:user_id) { should be old_account.user_id } + its(:id) { should be @old_account.id } + its(:user_id) { should be @old_account.user_id } its(:user_id) { should be new_account.user_id } its(:oauth_token) { should eq new_account.oauth_token } its(:oauth_token_secret) { should eq new_account.oauth_token_secret } its(:consumer_version) { should eq new_account.consumer_version } + its(:notification) { should be false } + its(:private) { should be true } + its(:status) { should be Account::ACTIVE } + end + end + + describe "#update_settings!" do + let(:account) { FactoryGirl.create(:account_1) } + subject { account.update_settings!(notification: false, private: true) } + its(:notification) { should be false } + its(:private) { should be true } + end + + describe "#deactivate!" do + let(:account) { FactoryGirl.create(:account_1) } + it { account.active?.should be true } + subject { account.tap(&:deactivate!) } + its(:active?) { should be false } + end + + describe "#active?" do + let(:account) { FactoryGirl.create(:account_1) } + context "when active" do + subject { account } + its(:active?) { should be true } + end + + context "when inactive" do + subject { account.tap(&:deactivate!) } + its(:active?) { should be false } end end @@ -37,6 +68,7 @@ describe Account do let(:account) { FactoryGirl.create(:account_1) } subject { account.client } it { should be_a Twitter::Client } + it { subject.__send__(:credentials)[:token].should eq account.oauth_token } end describe "#import_favorites" do diff --git a/spec/models/tweet_spec.rb b/spec/models/tweet_spec.rb index 6d4f35e..c631358 100644 --- a/spec/models/tweet_spec.rb +++ b/spec/models/tweet_spec.rb @@ -1,14 +1,16 @@ # -*- coding: utf-8 -*- -require 'spec_helper' +require "spec_helper" + +include Aclog::Twitter describe Tweet do 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 = FactoryGirl.create(:tweet, id: snowflake(2.days.ago) + 5000, user: @user_0, tweeted_at: 2.days.ago) # f/r = 2/0 + @tweet_0_1 = FactoryGirl.create(:tweet, id: snowflake(4.days.ago) + 5000, user: @user_0, tweeted_at: 4.days.ago) # f/r = 0/1 + @tweet_0_2 = FactoryGirl.create(:tweet, id: snowflake(6.days.ago) + 5000, user: @user_0, tweeted_at: 6.days.ago) # f/r = 0/0 + @tweet_1_0 = FactoryGirl.create(:tweet, id: snowflake(1.days.ago) + 5000, 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) @@ -17,14 +19,6 @@ describe Tweet do @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 } @@ -33,18 +27,28 @@ describe Tweet do 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 } + before do + @id = @tweet_1_0.id + @result = OpenStruct.new(Tweet.delete_from_id(@id)) + end + it { @result.tweets.should be 1 } + it { @result.favorites.should be 1 } + it { @result.retweets.should be 1 } + it { Tweet.find_by(id: @id).should be nil } + it { Favorite.where(tweet_id: @id).count.should be 0 } + it { Retweet.where(tweet_id: @id).count.should be 0 } end 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 } + before do + @id = @tweet_1_0_r_2.id + @result = OpenStruct.new(Tweet.delete_from_id(@id)) + end + it { @result.tweets.should be 0 } + it { @result.retweets.should be 1 } + it { Tweet.find_by(id: @tweet_1_0).retweets_count.should be 0 } + it { Favorite.where(tweet_id: @id).count.should be 0 } + it { Retweet.where(id: @id).count.should be 0 } end end @@ -66,8 +70,8 @@ describe Tweet do context "scopes" do describe "recent" do - subject { Tweet.recent } - it { should_not include -> tweet { tweet.tweeted_at < Time.zone.now - 3.days } } + subject { Tweet.recent(3) } + it { should_not include -> tweet { tweet.tweeted_at < Time.now - 3.days } } its(:count) { should be 2 } end @@ -101,11 +105,6 @@ describe Tweet do } end - describe "of" do - subject { Tweet.of(@user_1) } - its(:to_sql) { should eq @user_1.tweets.to_sql } - end - describe "favorited_by" do subject { Tweet.favorited_by(@user_1) } its(:count) { should be 2 } @@ -126,12 +125,21 @@ describe Tweet do it { should_not include -> tweet { not (tweet.retweets + tweet.favorites).any? {|a| a.user_id == @user_1.id } } } end - describe "original" do - # TODO + describe "not_protected" do + subject { Tweet.not_protected.includes(:user) } + it { should_not include -> tweet { tweet.user.protected? } } end - describe "not_protected" do - # TODO + describe "max_id" do + subject { Tweet.max_id(@tweet_0_0.id - 1) } + its(:count) { should be 2 } + it { should_not include -> tweet { tweet.id > @tweet_0_0.id - 1 } } + end + + describe "since" do + subject { Tweet.since_id(@tweet_0_0.id) } + its(:count) { should be 1 } + it { should_not include -> tweet { tweet.id <= @tweet_0_0.id } } end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a10e04d..06aae92 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -93,4 +93,38 @@ describe User do its(:favorited_count) { should be 2 } its(:retweeted_count) { should be 1 } end + + describe "#count_discovered_by" do + before do + @user = FactoryGirl.create_list(:user, 3) + tweet_1, tweet_2 = FactoryGirl.create_list(:tweet, 2, user: @user[0]) + FactoryGirl.create(:favorite, tweet: tweet_1, user: @user[0]) + FactoryGirl.create(:favorite, tweet: tweet_1, user: @user[1]) + FactoryGirl.create(:retweet, tweet: tweet_1, user: @user[1]) + FactoryGirl.create(:favorite, tweet: tweet_2, user: @user[1]) + FactoryGirl.create(:favorite, tweet: tweet_1, user: @user[2]) + FactoryGirl.create(:favorite, tweet: tweet_2, user: @user[2]) + end + subject { @user.first.count_discovered_by } + its(:size) { should be 3 } + it { should eq [[@user[1].id, 2, 1], [@user[2].id, 2, 0], [@user[0].id, 1, 0]] } + end + + describe "#count_discovered_users" do + before do + @user = FactoryGirl.create_list(:user, 3) + tweet_1 = FactoryGirl.create(:tweet, user: @user[1]) + tweet_2 = FactoryGirl.create(:tweet, user: @user[2]) + tweet_3 = FactoryGirl.create(:tweet, user: @user[2]) + FactoryGirl.create(:favorite, tweet: tweet_1, user: @user[0]) + FactoryGirl.create(:favorite, tweet: tweet_1, user: @user[1]) + FactoryGirl.create(:retweet, tweet: tweet_1, user: @user[0]) + FactoryGirl.create(:favorite, tweet: tweet_2, user: @user[0]) + FactoryGirl.create(:retweet, tweet: tweet_2, user: @user[0]) + FactoryGirl.create(:favorite, tweet: tweet_3, user: @user[0]) + end + subject { @user[0].count_discovered_users } + its(:size) { should be 2 } + it { should eq [[@user[2].id, 2, 1], [@user[1].id, 1, 1]] } + end end -- cgit v1.2.3