commit: 7c13abb3d98fdac4fdab67828e7fe509ad868431
parent 5addbf39fbdc67d93f6b8605ce02157e14c3edb1
Author: Phantasm <phantasm@centrum.cz>
Date: Wed, 14 May 2025 16:37:43 +0200
Elixir 1.18 Use NaiveDateTime.compare/2 instead of <>= comparisons
warning: comparison with structs found:
left <= right
given types:
dynamic() <= dynamic(%NaiveDateTime{})
where "left" (context ExUnit.Assertions) was given the type:
# type: dynamic()
# from: test/pleroma/web/plugs/user_tracking_plug_test.exs:25
left = user.last_active_at
where "right" (context ExUnit.Assertions) was given the type:
# type: dynamic(%NaiveDateTime{})
# from: test/pleroma/web/plugs/user_tracking_plug_test.exs:25
right = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
Comparison operators (>, <, >=, <=, min, and max) perform structural and not semantic comparison. Comparing with a struct won't give meaningful results. Structs that can be compared typically define a compare/2 function within their modules that can be used for semantic comparison.
typing violation found at:
│
25 │ assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
│ ~
│
└─ test/pleroma/web/plugs/user_tracking_plug_test.exs:25:32: Pleroma.Web.Plugs.UserTrackingPlugTest."test updates last_active_at for a new user"/1
Diffstat:
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs
@@ -2669,8 +2669,8 @@ defmodule Pleroma.UserTest do
assert {:ok, user} = User.update_last_active_at(user)
- assert user.last_active_at >= test_started_at
- assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+ assert NaiveDateTime.compare(user.last_active_at, test_started_at) in [:gt, :eq]
+ assert NaiveDateTime.compare(user.last_active_at, NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)) in [:lt, :eq]
last_active_at =
NaiveDateTime.utc_now()
@@ -2681,11 +2681,11 @@ defmodule Pleroma.UserTest do
user
|> cast(%{last_active_at: last_active_at}, [:last_active_at])
|> User.update_and_set_cache()
+ assert NaiveDateTime.compare(user.last_active_at, last_active_at) == :eq
- assert user.last_active_at == last_active_at
assert {:ok, user} = User.update_last_active_at(user)
- assert user.last_active_at >= test_started_at
- assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+ assert NaiveDateTime.compare(user.last_active_at, test_started_at) in [:gt, :eq]
+ assert NaiveDateTime.compare(user.last_active_at, NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)) in [:lt, :eq]
end
test "active_user_count/1" do
diff --git a/test/pleroma/web/plugs/user_tracking_plug_test.exs b/test/pleroma/web/plugs/user_tracking_plug_test.exs
@@ -21,8 +21,8 @@ defmodule Pleroma.Web.Plugs.UserTrackingPlugTest do
|> assign(:user, user)
|> UserTrackingPlug.call(%{})
- assert user.last_active_at >= test_started_at
- assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+ assert NaiveDateTime.compare(user.last_active_at, test_started_at) in [:gt, :eq]
+ assert NaiveDateTime.compare(user.last_active_at, NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)) in [:lt, :eq]
end
test "doesn't update last_active_at if it was updated recently", %{conn: conn} do
@@ -38,7 +38,7 @@ defmodule Pleroma.Web.Plugs.UserTrackingPlugTest do
|> assign(:user, user)
|> UserTrackingPlug.call(%{})
- assert user.last_active_at == last_active_at
+ assert NaiveDateTime.compare(user.last_active_at, last_active_at) == :eq
end
test "skips updating last_active_at if user ID is nil", %{conn: conn} do