2011-09-08 22:57:52 -04:00
|
|
|
require 'test_helper'
|
2010-02-11 14:59:58 -05:00
|
|
|
|
|
|
|
class FavoriteTest < ActiveSupport::TestCase
|
2010-08-26 14:36:02 -04:00
|
|
|
setup do
|
2018-04-02 13:51:26 -04:00
|
|
|
user = FactoryBot.create(:user)
|
2010-08-26 14:36:02 -04:00
|
|
|
CurrentUser.user = user
|
|
|
|
CurrentUser.ip_addr = "127.0.0.1"
|
2011-06-05 04:05:21 -04:00
|
|
|
Favorite # need to force loading the favorite model
|
2010-08-26 14:36:02 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-08-26 14:36:02 -04:00
|
|
|
teardown do
|
|
|
|
CurrentUser.user = nil
|
|
|
|
CurrentUser.ip_addr = nil
|
|
|
|
end
|
|
|
|
|
2010-02-12 18:46:43 -05:00
|
|
|
context "A favorite" do
|
2012-05-11 18:35:05 -04:00
|
|
|
should "delete from all tables" do
|
2018-04-02 13:51:26 -04:00
|
|
|
user1 = FactoryBot.create(:user)
|
|
|
|
p1 = FactoryBot.create(:post)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2018-08-12 13:35:15 -04:00
|
|
|
p1.add_favorite!(user1)
|
2012-05-11 18:35:05 -04:00
|
|
|
assert_equal(1, Favorite.count)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2018-04-02 13:51:26 -04:00
|
|
|
Favorite.where(:user_id => user1.id, :post_id => p1.id).delete_all
|
2012-05-11 18:35:05 -04:00
|
|
|
assert_equal(0, Favorite.count)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-02-12 18:46:43 -05:00
|
|
|
should "know which table it belongs to" do
|
2018-04-02 13:51:26 -04:00
|
|
|
user1 = FactoryBot.create(:user)
|
|
|
|
user2 = FactoryBot.create(:user)
|
|
|
|
p1 = FactoryBot.create(:post)
|
|
|
|
p2 = FactoryBot.create(:post)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2018-08-12 13:35:15 -04:00
|
|
|
p1.add_favorite!(user1)
|
|
|
|
p2.add_favorite!(user1)
|
|
|
|
p1.add_favorite!(user2)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-06-05 04:05:21 -04:00
|
|
|
favorites = user1.favorites.order("id desc")
|
|
|
|
assert_equal(2, favorites.count)
|
|
|
|
assert_equal(p2.id, favorites[0].post_id)
|
|
|
|
assert_equal(p1.id, favorites[1].post_id)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-06-05 04:05:21 -04:00
|
|
|
favorites = user2.favorites.order("id desc")
|
|
|
|
assert_equal(1, favorites.count)
|
|
|
|
assert_equal(p1.id, favorites[0].post_id)
|
2010-02-12 18:46:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-06-05 04:05:21 -04:00
|
|
|
should "not allow duplicates" do
|
2018-04-02 13:51:26 -04:00
|
|
|
user1 = FactoryBot.create(:user)
|
|
|
|
p1 = FactoryBot.create(:post)
|
|
|
|
p2 = FactoryBot.create(:post)
|
2018-08-12 13:35:15 -04:00
|
|
|
p1.add_favorite!(user1)
|
|
|
|
p1.add_favorite!(user1)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-06-05 04:05:21 -04:00
|
|
|
assert_equal(1, user1.favorites.count)
|
2010-02-12 18:46:43 -05:00
|
|
|
end
|
2010-02-11 14:59:58 -05:00
|
|
|
end
|
|
|
|
end
|