Tests: Add a helper method to reduce sleeping times in test.

This will 'time travel', i.e. change the inserted_at and update_at
fields of the object in question. This is used to backdate things
were we used sleeping before to ensure time differences.
This commit is contained in:
lain 2020-12-16 10:39:36 +01:00
parent ba19975838
commit 0ef0aed205
5 changed files with 18 additions and 11 deletions

View file

@ -73,7 +73,8 @@ defmodule Pleroma.ChatTest do
other_user = insert(:user)
{:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
:timer.sleep(1500)
{:ok, chat} = time_travel(chat, -2)
{:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
assert chat.id == chat_two.id

View file

@ -96,12 +96,11 @@ defmodule Pleroma.Conversation.ParticipationTest do
{:ok, %Participation{} = participation} =
Participation.create_for_user_and_conversation(user, conversation)
{:ok, participation} = time_travel(participation, -2)
assert participation.user_id == user.id
assert participation.conversation_id == conversation.id
# Needed because updated_at is accurate down to a second
:timer.sleep(1000)
# Creating again returns the same participation
{:ok, %Participation{} = participation_two} =
Participation.create_for_user_and_conversation(user, conversation)

View file

@ -394,11 +394,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
tridi = insert(:user)
{:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
:timer.sleep(1000)
{:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
:timer.sleep(1000)
{:ok, chat_1} = time_travel(chat_1, -3)
{:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
{:ok, _chat_2} = time_travel(chat_2, -2)
{:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
:timer.sleep(1000)
{:ok, chat_3} = time_travel(chat_3, -1)
# bump the second one
{:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)

View file

@ -37,8 +37,7 @@ defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do
user = insert(:user)
{:ok, token} = PasswordResetToken.create_token(user)
:timer.sleep(2000)
{:ok, token} = time_travel(token, -2)
response =
conn
@ -55,7 +54,7 @@ defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do
user = insert(:user)
{:ok, token} = PasswordResetToken.create_token(user)
:timer.sleep(2000)
{:ok, token} = time_travel(token, -2)
{:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
params = %{

View file

@ -55,6 +55,14 @@ defmodule Pleroma.Tests.Helpers do
clear_config: 2
]
def time_travel(entity, seconds) do
new_time = NaiveDateTime.add(entity.inserted_at, seconds)
entity
|> Ecto.Changeset.change(%{inserted_at: new_time, updated_at: new_time})
|> Pleroma.Repo.update()
end
def to_datetime(%NaiveDateTime{} = naive_datetime) do
naive_datetime
|> DateTime.from_naive!("Etc/UTC")