Skip to content

Commit 724ec69

Browse files
authored
test: Add failing test for timestamptz comparisons (#663)
This PR adds a failing test for #651. The problem is that Ash casts `timestamptz` to `timestamp`, but only when the attribute is first in the comparison. See the discussion in #651 for more details.
1 parent 23cc10a commit 724ec69

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

test/calculation_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,4 +1647,26 @@ defmodule AshPostgres.CalculationTest do
16471647
assert loaded2.last_unread_message_formatted_fn == "FnMessage: Unread message"
16481648
assert loaded3.last_unread_message_formatted_fn == nil
16491649
end
1650+
1651+
test "can correctly compare timestamptz, regardless of database timezone" do
1652+
post =
1653+
Post
1654+
|> Ash.Changeset.for_create(:create, %{
1655+
title: "match",
1656+
price: 10_024,
1657+
datetime: DateTime.utc_now()
1658+
})
1659+
|> Ash.create!()
1660+
1661+
assert {:ok, %Postgrex.Result{}} =
1662+
Ecto.Adapters.SQL.query(
1663+
AshPostgres.TestRepo,
1664+
"""
1665+
set timezone = 'Europe/Copenhagen';
1666+
"""
1667+
)
1668+
1669+
assert Ash.calculate!(post, :past_datetime1?)
1670+
assert Ash.calculate!(post, :past_datetime2?)
1671+
end
16501672
end

test/support/resources/post.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,9 @@ defmodule AshPostgres.Test.Post do
11591159
) do
11601160
public?(true)
11611161
end
1162+
1163+
calculate(:past_datetime1?, :boolean, expr(now() > datetime))
1164+
calculate(:past_datetime2?, :boolean, expr(datetime <= now()))
11621165
end
11631166

11641167
aggregates do

0 commit comments

Comments
 (0)