Files
danbooru/test/unit/application_record.rb
evazion 1d2bac7b95 Remove CurrentUser.ip_addr.
Remove the `CurrentUser.ip_addr` global variable and replace it with
`request.remote_ip`. Before we had to track the current user's IP in a
global variable so that when we edited a post for example, we could pass
down the user's IP to the model and save it in the post_versions table.
Now that we now longer save IPs in version tables, we don't need a global
variable to get access to the current user's IP outside of controllers.
2022-09-18 05:02:10 -05:00

62 lines
1.8 KiB
Ruby

require 'test_helper'
class ApplicationRecordTest < ActiveSupport::TestCase
setup do
@tags = FactoryBot.create_list(:tag, 3, post_count: 1)
end
context "ApplicationRecord#search" do
should "support the id param" do
assert_equal([@tags.first], Tag.search(id: @tags.first.id))
end
should "support ranges in the id param" do
assert_equal(@tags.reverse, Tag.search(id: ">=1"))
assert_equal(@tags.reverse, Tag.search(id: "#{@tags[0].id}..#{@tags[2].id}"))
assert_equal(@tags.reverse, Tag.search(id: @tags.map(&:id).join(",")))
end
should "support the created_at and updated_at params" do
assert_equal(@tags.reverse, Tag.search(created_at: ">=#{@tags.first.created_at}"))
assert_equal(@tags.reverse, Tag.search(updated_at: ">=#{@tags.first.updated_at}"))
end
end
context "ApplicationRecord#parallel_each" do
context "in threaded mode" do
should "set CurrentUser correctly" do
@user1 = create(:user)
@user2 = create(:user)
CurrentUser.scoped(@user1) do
Tag.parallel_each do |tag|
assert_equal(@user1, CurrentUser.user)
CurrentUser.scoped(@user2) do
assert_equal(@user2, CurrentUser.user)
end
assert_equal(@user1, CurrentUser.user)
end
end
end
end
end
context "ApplicationRecord#destroy_duplicates!" do
should "destroy all duplicates" do
@post1 = create(:post, score: 42)
@post2 = create(:post, score: 42)
@post3 = create(:post, score: 42)
@post4 = create(:post, score: 23)
Post.destroy_duplicates!(:score)
assert_equal(true, Post.exists?(@post1.id))
assert_equal(false, Post.exists?(@post2.id))
assert_equal(false, Post.exists?(@post3.id))
assert_equal(true, Post.exists?(@post4.id))
end
end
end