[Tests] Replace timecop with native Rails method

Also removes it at a bunch of places where it's not needed
This commit is contained in:
Earlopain 2022-11-25 14:32:56 +01:00
parent 044901ab44
commit c9f65608de
No known key found for this signature in database
GPG Key ID: 237AA8F8D0D0577F
16 changed files with 33 additions and 75 deletions

View File

@ -65,7 +65,6 @@ group :test do
gem "factory_bot"
gem "mocha", :require => "mocha/minitest"
gem "ffaker"
gem "timecop"
gem "webmock"
gem "mock_redis"
end

View File

@ -360,7 +360,6 @@ GEM
multi_json (~> 1.8)
thor (1.2.1)
tilt (2.0.11)
timecop (0.9.5)
timeout (0.3.0)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
@ -441,7 +440,6 @@ DEPENDENCIES
simple_form
solargraph
streamio-ffmpeg
timecop
unicorn
unicorn-worker-killer
webmock

View File

@ -2,9 +2,7 @@ require 'test_helper'
class UploadServiceTest < ActiveSupport::TestCase
setup do
Timecop.travel(2.weeks.ago) do
@user = FactoryBot.create(:user)
end
@user = FactoryBot.create(:user, created_at: 2.weeks.ago)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
UploadWhitelist.create!(pattern: '*', reason: 'test')

View File

@ -19,7 +19,7 @@ class ArtistTest < ActiveSupport::TestCase
context "An artist" do
setup do
user = Timecop.travel(1.month.ago) {FactoryBot.create(:user)}
user = FactoryBot.create(:user, created_at: 1.month.ago)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
end
@ -254,9 +254,7 @@ class ArtistTest < ActiveSupport::TestCase
assert_difference("ArtistVersion.count") do
artist.other_names = "xxx"
Timecop.travel(1.day.from_now) do
artist.save
end
artist.save
end
first_version = ArtistVersion.first

View File

@ -150,7 +150,7 @@ class BanTest < ActiveSupport::TestCase
end
should "not return expired bans" do
Timecop.travel(2.days.from_now) do
travel_to(2.days.from_now) do
assert(!Ban.is_banned?(@user))
end
end

View File

@ -72,7 +72,7 @@ class CommentTest < ActiveSupport::TestCase
Danbooru.config.stubs(:comment_threshold).returns(1)
p = FactoryBot.create(:post)
c1 = FactoryBot.create(:comment, :post => p)
Timecop.travel(2.seconds.from_now) do
travel_to(2.seconds.from_now) do
c2 = FactoryBot.create(:comment, :post => p)
end
p.reload
@ -87,11 +87,9 @@ class CommentTest < ActiveSupport::TestCase
post.reload
assert_equal(c1.created_at.to_s, post.last_commented_at.to_s)
Timecop.travel(2.seconds.from_now) do
c2 = FactoryBot.create(:comment, :post => post)
post.reload
assert_equal(c2.created_at.to_s, post.last_commented_at.to_s)
end
c2 = FactoryBot.create(:comment, :post => post)
post.reload
assert_equal(c2.created_at.to_s, post.last_commented_at.to_s)
end
should "not record the user id of the voter" do

View File

@ -32,7 +32,7 @@ class ForumPostTest < ActiveSupport::TestCase
9.times do
@posts << FactoryBot.create(:forum_post, topic_id: @topic.id, body: rand(100_000))
end
Timecop.travel(2.seconds.from_now) do
travel_to(2.seconds.from_now) do
@posts << FactoryBot.create(:forum_post, topic_id: @topic.id, body: rand(100_000))
end
end
@ -87,8 +87,8 @@ class ForumPostTest < ActiveSupport::TestCase
should "update the topic when created" do
@original_topic_updated_at = @topic.updated_at
Timecop.travel(1.second.from_now) do
post = FactoryBot.create(:forum_post, :topic_id => @topic.id)
travel_to(1.second.from_now) do
post = FactoryBot.create(:forum_post, topic_id: @topic.id)
end
@topic.reload
assert_not_equal(@original_topic_updated_at.to_s, @topic.updated_at.to_s)

View File

@ -63,9 +63,7 @@ class NoteTest < ActiveSupport::TestCase
should "create a version" do
assert_difference("NoteVersion.count", 1) do
Timecop.travel(1.day.from_now) do
@note = FactoryBot.create(:note, :post => @post)
end
@note = FactoryBot.create(:note, post: @post)
end
assert_equal(1, @note.versions.count)
@ -114,18 +112,14 @@ class NoteTest < ActiveSupport::TestCase
should "update the post's last_noted_at field" do
assert_equal(@post.last_noted_at, @note.updated_at)
Timecop.travel(1.day.from_now) do
@note.update(x: 500)
@post.reload
assert_equal(@post.last_noted_at, @note.updated_at)
end
@note.update(x: 500)
@post.reload
assert_equal(@post.last_noted_at, @note.updated_at)
end
should "create a version" do
assert_difference("NoteVersion.count", 1) do
Timecop.travel(1.day.from_now) do
@note.update(:body => "fafafa")
end
@note.update(body: "fafafa")
end
assert_equal(2, @note.versions.count)
assert_equal(2, @note.versions.last.version)
@ -169,9 +163,7 @@ class NoteTest < ActiveSupport::TestCase
assert_equal(2, NoteVersion.count)
assert_equal([1, 2], @note.versions.map(&:version))
assert_equal([@user.id, @vandal.id], @note.versions.map(&:updater_id))
Timecop.travel(1.day.from_now) do
Note.undo_changes_by_user(@vandal.id)
end
Note.undo_changes_by_user(@vandal.id)
@note.reload
assert_equal([1, 3], @note.versions.map(&:version))
assert_equal([@user.id, @user.id], @note.versions.map(&:updater_id))

View File

@ -4,11 +4,8 @@ require 'test_helper'
class PoolTest < ActiveSupport::TestCase
setup do
Timecop.travel(1.month.ago) do
@user = FactoryBot.create(:user)
CurrentUser.user = @user
end
@user = FactoryBot.create(:user, created_at: 1.month.ago)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
@ -246,7 +243,7 @@ class PoolTest < ActiveSupport::TestCase
should "create new versions for each distinct user" do
assert_equal(1, @pool.versions.size)
user2 = Timecop.travel(1.month.ago) {FactoryBot.create(:user)}
user2 = FactoryBot.create(:user, created_at: 1.month.ago)
CurrentUser.scoped(user2, "127.0.0.2") do
@pool.post_ids = [@p1.id]

View File

@ -2,10 +2,8 @@ require 'test_helper'
class PostReplacementTest < ActiveSupport::TestCase
setup do
Timecop.travel(2.weeks.ago) do
@user = FactoryBot.create(:user)
@mod_user = FactoryBot.create(:moderator_user)
end
@user = FactoryBot.create(:user, created_at: 2.weeks.ago)
@mod_user = FactoryBot.create(:moderator_user, created_at: 2.weeks.ago)
@upload = UploadService.new(FactoryBot.attributes_for(:jpg_upload).merge(uploader: @mod_user, uploader_ip_addr: '127.0.0.1')).start!
@post = @upload.post
@post.update_columns({is_pending: false, approver_id: @mod_user.id})

View File

@ -9,9 +9,7 @@ class PostTest < ActiveSupport::TestCase
super
Sidekiq::Testing.inline!
Timecop.travel(2.weeks.ago) do
@user = FactoryBot.create(:user)
end
@user = FactoryBot.create(:user, created_at: 2.weeks.ago)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
Post.__elasticsearch__.create_index!
@ -1815,10 +1813,7 @@ class PostTest < ActiveSupport::TestCase
end
should "return posts for a upvote:<user>, downvote:<user> metatag" do
old_user = nil
Timecop.travel(5.days.ago) do
old_user = FactoryBot.create(:mod_user)
end
old_user = FactoryBot.create(:mod_user, created_at: 5.days.ago)
CurrentUser.scoped(old_user) do
upvoted = FactoryBot.create(:post, tag_string: "abc")
downvoted = FactoryBot.create(:post, tag_string: "abc")
@ -2002,10 +1997,7 @@ class PostTest < ActiveSupport::TestCase
end
should "allow undoing of votes" do
user = nil
Timecop.travel(7.days.ago) do
user = FactoryBot.create(:privileged_user)
end
user = FactoryBot.create(:privileged_user, created_at: 7.days.ago)
post = FactoryBot.create(:post)
# We deliberately don't call post.reload until the end to verify that
@ -2154,9 +2146,7 @@ class PostTest < ActiveSupport::TestCase
context "a post that is rating locked" do
setup do
@post = FactoryBot.create(:post, :rating => "s")
Timecop.travel(2.hours.from_now) do
@post.update(rating: "q", is_rating_locked: true)
end
@post.update(rating: "q", is_rating_locked: true)
end
should "not revert the rating" do

View File

@ -3,9 +3,7 @@ require 'test_helper'
class PostVersionTest < ActiveSupport::TestCase
context "A post" do
setup do
Timecop.travel(1.month.ago) do
@user = FactoryBot.create(:user)
end
@user = FactoryBot.create(:user, created_at: 1.month.ago)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end

View File

@ -4,7 +4,7 @@ class PostVoteTest < ActiveSupport::TestCase
def setup
super
Timecop.travel(1.month.ago) { @user = FactoryBot.create(:user) }
@user = FactoryBot.create(:user, created_at: 1.month.ago)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"

View File

@ -13,10 +13,8 @@ class TagAliasTest < ActiveSupport::TestCase
setup do
@admin = FactoryBot.create(:admin_user)
Timecop.travel(1.month.ago) do
user = FactoryBot.create(:user)
CurrentUser.user = user
end
user = FactoryBot.create(:user, created_at: 1.month.ago)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
end

View File

@ -18,7 +18,7 @@ class TagTest < ActiveSupport::TestCase
setup do
Tag.stubs(:trending_count_limit).returns(0)
Timecop.travel(1.week.ago) do
travel_to(1.week.ago) do
FactoryBot.create(:post, :tag_string => "aaa")
FactoryBot.create(:post, :tag_string => "bbb")
end

View File

@ -83,18 +83,12 @@ class WikiPageTest < ActiveSupport::TestCase
end
assert_difference("WikiPageVersion.count") do
@wiki_page.title = "yyy"
Timecop.travel(1.day.from_now) do
@wiki_page.save
end
@wiki_page.update(title: "yyy")
end
end
should "revert to a prior version" do
@wiki_page.title = "yyy"
Timecop.travel(1.day.from_now) do
@wiki_page.save
end
@wiki_page.update(title: "yyy")
version = WikiPageVersion.first
@wiki_page.revert_to!(version)
@wiki_page.reload