Skip to content

Commit

Permalink
Merge pull request #1444 from CristinaRO/0.9.12-test-with-caching-and…
Browse files Browse the repository at this point in the history
…-issue-3846

0.9.12 test with caching and issue 3846
  • Loading branch information
elzj committed Jan 18, 2014
2 parents f20895a + 836643a commit 45d0abe
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rvm:
services:
- elasticsearch
- redis-server
- memcache
- memcached
script:
- bundle exec rake db:create:all --trace
- bundle exec rake db:schema:load --trace
Expand Down
1 change: 1 addition & 0 deletions app/models/pseud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def creations
def replace_me_with_default
self.creations.each {|creation| change_ownership(creation, self.user.default_pseud) }
Comment.update_all("pseud_id = #{self.user.default_pseud.id}", "pseud_id = #{self.id}") unless self.comments.blank?
# NB: updates the kudos to use the new default pseud, but the cache will not expire
Kudo.update_all("pseud_id = #{self.user.default_pseud.id}", "pseud_id = #{self.id}") unless self.kudos.blank?
change_collections_membership
change_gift_recipients
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def direct_filters

def remove_pseud_from_kudos
ids = self.pseuds.collect(&:id).join(',')
# NB: updates the kudos to remove the pseud, but the cache will not expire, and there's also issue 2198
Kudo.update_all("pseud_id = NULL", "pseud_id IN (#{ids})") if ids.present?
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -879,13 +879,13 @@ def creator_comments
end

def guest_kudos_count
Rails.cache.fetch "works/#{id}/guest_kudos_count", :expires_in => 5.minutes do
Rails.cache.fetch "works/#{id}/guest_kudos_count" do
kudos.by_guest.count
end
end

def all_kudos_count
Rails.cache.fetch "works/#{id}/kudos_count", :expires_in => 5.minutes do
Rails.cache.fetch "works/#{id}/kudos_count" do
kudos.count
end
end
Expand Down
21 changes: 5 additions & 16 deletions app/sweepers/kudos_sweeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@ class KudosSweeper < ActionController::Caching::Sweeper
observe Kudo

def after_create(kudo)
# invalidates the cache for the kudos section in the view
expire_fragment "#{kudo.commentable.cache_key}/kudos"

if kudo.commentable_type == "Work"
# expire the total kudos count on the work
expire_fragment "works/#{kudo.commentable_id}/kudos_count"
# if this is a guest kudo, expire the guest_kudos_count to avoid guest kudos being stuck
expire_fragment "works/#{kudo.commentable_id}/guest_kudos_count" if kudo.pseud_id.nil?
# delete the cache entry for the total kudos count on the work
Rails.cache.delete "works/#{kudo.commentable_id}/kudos_count"
# if guest kudo, delete the cache entry for guest_kudos_count to avoid guest kudos being stuck
Rails.cache.delete "works/#{kudo.commentable_id}/guest_kudos_count" if kudo.pseud_id.nil?
end
end

def after_update(kudo)
return unless kudo.pseud_id_changed?

# expire the cache for the kudos section in the view
expire_fragment "#{kudo.commentable.cache_key}/kudos"

if kudo.commentable_type == "Work"
# if someone has deleted their account: expire guest kudos count
expire_fragment "works/#{kudo.commentable_id}/guest_kudos_count" if kudo.pseud_id.nil?
end
end
end
6 changes: 3 additions & 3 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
config.cache_store = :memory_store

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
# Show full error reports and enable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_controller.perform_caching = true
config.cache_store = :memory_store

# Raise exceptions instead of rendering exception templates
config.action_dispatch.show_exceptions = false
Expand Down
4 changes: 3 additions & 1 deletion features/collections/collectible.feature
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ Feature: Collectible items
And I have a bookmark for "Tundra penguins"
When I add my bookmark to the collection
Then I should see "Added"
When I follow "Various Penguins"
# caching prevents the link from showing up immediately
# When I follow "Various Penguins"
When I go to "Various Penguins" collection's page
Then I should see "Bookmarks (1)" within "#dashboard"
And I should see "Tundra penguins"

Expand Down
5 changes: 2 additions & 3 deletions features/other/bookmark_privacy.feature
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,15 @@ Feature: Private bookmarks
And I should see "Public Masterpiece"
And I should not see "bookmarker"
And I should see "otheruser"
# And I should see "Bookmarked 1 time"
And I should not see "Bookmarked 2 times"
When I go to bookmarker's bookmarks page
Then I should not see "Secret Masterpiece"
And I should not see "Public Masterpiece"
When I go to the works page
Then I should not see "Secret Masterpiece"
And I should see "Public Masterpiece"
And I should not see "Bookmarks: 2"
And I should see "Bookmarks: 1"
# CACHING (perform_caching: true)
# And I should see "Bookmarks: 1"
When I view the work "Public Masterpiece"
Then I should not see "Bookmarks:2"
And I should see "Bookmarks:1"
Expand Down
197 changes: 96 additions & 101 deletions features/other/kudos.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,121 +13,116 @@ Feature: Leave kudos
And I post the work "Awesome Story"
And I log out


Scenario: post kudos

When I am logged in as "myname2"
And all emails have been delivered
And I view the work "Awesome Story"
Then I should not see "left kudos on this work"
# Note: this step cannot be put into the steps file because of the heart character
When I press "Kudos ♥"
Then I should see "myname2 left kudos on this work!"
# make sure no emails go out until notifications are sent
And 0 emails should be delivered
When kudos are sent
Given I am logged in as "myname2"
And all emails have been delivered
And I view the work "Awesome Story"
Then I should not see "left kudos on this work"
# Note: this step cannot be put into the steps file because of the heart character
When I press "Kudos ♥"
Then I should see "myname2 left kudos on this work!"
# make sure no emails go out until notifications are sent
And 0 emails should be delivered
When kudos are sent
Then 1 email should be delivered to "myname1@foo.com"
And the email should contain "myname2"
And the email should contain "left kudos"
And the email should contain "."
And all emails have been delivered
When I press "Kudos ♥"
Then I should see "You have already left kudos here. :)"
And I should not see "myname2 and myname2 left kudos on this work!"
When I log out
And I press "Kudos ♥"
Then I should see "Thank you for leaving kudos!"
And I should see "myname2 as well as a guest left kudos on this work!"
When I press "Kudos ♥"
Then I should see "You have already left kudos here. :)"
When kudos are sent
Then 1 email should be delivered to "myname1@foo.com"
And the email should contain "myname2"
And the email should contain "left kudos"
And the email should contain "."
And all emails have been delivered
When I press "Kudos ♥"
Then I should see "You have already left kudos here. :)"
And I should not see "myname2 and myname2 left kudos on this work!"
When I log out
And I press "Kudos ♥"
And kudos are sent
Then 1 email should be delivered to "myname1@foo.com"
And the email should contain "A guest"
And the email should contain "left kudos"
And the email should contain "."
Then I should see "Thank you for leaving kudos!"
# guest kudos don't show up immediately
# Then I should see "myname2 as well as a guest left kudos on this work!"
When I press "Kudos ♥"
Then I should see "You have already left kudos here. :)"
When I am logged in as "myname3"
And I view the work "Awesome Story"
And I press "Kudos ♥"
# guest kudos don't show up immediately
# Then I should see "myname3 and myname2 as well as a guest left kudos on this work!"
Then I should see "myname3 and myname2 left kudos on this work!"
When I am logged in as "myname1"
And I view the work "Awesome Story"
Then I should not see "Kudos ♥"
# Then I should see "You can't leave kudos for yourself. :)"
And the email should contain "A guest"
And the email should contain "left kudos"
And the email should contain "."
When I am logged in as "myname3"
And I view the work "Awesome Story"
And I press "Kudos ♥"
Then I should see "myname3 and myname2 as well as a guest left kudos on this work!"
When I am logged in as "myname1"
And I view the work "Awesome Story"
Then I should not see "Kudos ♥"

Scenario: kudos on a multi-chapter work
When I am logged in as "myname1"
And I post the chaptered work "Epic Saga"
And I follow "Add Chapter"
And I fill in "content" with "third chapter is a draft"
And I press "Preview"
When I am logged in as "myname3"
And I view the work "Epic Saga"
And I press "Kudos ♥"
Then I should see kudos on every chapter
When I am logged in as "myname1"
And I view the work "Epic Saga"
Then I should see kudos on every chapter but the draft
Given I am logged in as "myname1"
And I post the chaptered work "Epic Saga"
And I follow "Add Chapter"
And I fill in "content" with "third chapter is a draft"
And I press "Preview"
When I am logged in as "myname3"
And I view the work "Epic Saga"
And I press "Kudos ♥"
Then I should see kudos on every chapter
When I am logged in as "myname1"
And I view the work "Epic Saga"
Then I should see kudos on every chapter but the draft

Scenario: deleting pseud and user after creating kudos should orphan them

When I am logged in as "myname3"
And "myname3" creates the default pseud "foobar"
And I view the work "Awesome Story"
And I press "Kudos ♥"
Then I should see "foobar (myname3) left kudos on this work!"
When "myname3" creates the default pseud "barfoo"
And I am on myname3's pseuds page
#'
And I follow "delete_foobar"
And I view the work "Awesome Story"
Then I should see "barfoo (myname3) left kudos on this work!"
When "myname3" deletes their account
And I view the work "Awesome Story"
And "issue 2198" is fixed
# Then I should see "a guest left kudos on this work!"
Given I am logged in as "myname3"
When "myname3" creates the default pseud "foobar"
And I view the work "Awesome Story"
And I press "Kudos ♥"
Then I should see "foobar (myname3) left kudos on this work!"
When "myname3" creates the default pseud "barfoo"
And I am on myname3's pseuds page
#'
And I follow "delete_foobar"
And I view the work "Awesome Story"
# there's no clean way to expire the cache for all kudos after a pseud change
# Then I should see "barfoo (myname3) left kudos on this work!"
When "myname3" deletes their account
And I view the work "Awesome Story"
And "issue 2198" is fixed
# Then I should see "a guest left kudos on this work!"

Scenario: redirection when kudosing on a middle chapter, with default preferences

Given the chaptered work setup
And I am logged in as a random user
When I view the work "BigBang"
And I view the 2nd chapter
And I press "Kudos ♥"
Then I should see "Chapter 2" within "div#chapters"
And I should not see "Chapter 1" within "div#chapters"
Given the chaptered work setup
And I am logged in as a random user
When I view the work "BigBang"
And I view the 2nd chapter
And I press "Kudos ♥"
Then I should see "Chapter 2" within "div#chapters"
And I should not see "Chapter 1" within "div#chapters"

Scenario: redirection when kudosing on a middle chapter, with default preferences but in temporary view full mode

Given the chaptered work setup
And I am logged in as a random user
When I view the work "BigBang" in full mode
And I press "Kudos ♥"
Then I should see "Chapter 2" within "div#chapters"
And I should see "Chapter 3" within "div#chapters"
Given the chaptered work setup
And I am logged in as a random user
When I view the work "BigBang" in full mode
And I press "Kudos ♥"
Then I should see "Chapter 2" within "div#chapters"
And I should see "Chapter 3" within "div#chapters"

Scenario: batched kudos email

When I am logged in as "myname1"
And I post the work "Another Awesome Story"
And all emails have been delivered
And the kudos queue is cleared
And I am logged in as "myname2"
And I leave kudos on "Awesome Story"
And I leave kudos on "Another Awesome Story"
And I am logged in as "someone_else"
And I leave kudos on "Awesome Story"
And I leave kudos on "Another Awesome Story"
And I am logged out
And I leave kudos on "Awesome Story"
And I leave kudos on "Another Awesome Story"
And kudos are sent
Then 1 email should be delivered to "myname1@foo.com"
And the email should contain "myname2"
And the email should contain "someone_else"
And the email should contain "guest"
And the email should contain "Awesome Story"
And the email should contain "Another Awesome Story"

Given I am logged in as "myname1"
And I post the work "Another Awesome Story"
And all emails have been delivered
And the kudos queue is cleared
And I am logged in as "myname2"
And I leave kudos on "Awesome Story"
And I leave kudos on "Another Awesome Story"
And I am logged in as "someone_else"
And I leave kudos on "Awesome Story"
And I leave kudos on "Another Awesome Story"
And I am logged out
And I leave kudos on "Awesome Story"
And I leave kudos on "Another Awesome Story"
When kudos are sent
Then 1 email should be delivered to "myname1@foo.com"
And the email should contain "myname2"
And the email should contain "someone_else"
And the email should contain "guest"
And the email should contain "Awesome Story"
And the email should contain "Another Awesome Story"
Loading

0 comments on commit 45d0abe

Please sign in to comment.