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
This commit is contained in:
Phantasm 2025-05-14 16:37:43 +02:00
commit 7c13abb3d9
No known key found for this signature in database
GPG key ID: 2669E588BCC634C8
2 changed files with 8 additions and 8 deletions

View file

@ -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

View file

@ -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