Skip to content

Commit 5e21f89

Browse files
committed
test: add test for ash_sql datetime time zone issues
1 parent 7c8b89a commit 5e21f89

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

test/calculation_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ defmodule AshPostgres.CalculationTest do
10181018
end
10191019

10201020
test "calculation references use the appropriate schema" do
1021-
record = Record |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!()
1021+
Record |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!()
10221022

10231023
TempEntity |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!()
10241024

test/complex_calculations_test.exs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,31 @@ defmodule AshPostgres.Test.ComplexCalculationsTest do
346346
|> Ash.Query.for_read(:failing_many_reference)
347347
|> Ash.read!(page: [count: true])
348348
end
349+
350+
test "lazy datetime with timezone is respected in calculations" do
351+
documentation_before =
352+
AshPostgres.Test.ComplexCalculations.Documentation
353+
|> Ash.Changeset.for_create(:create, %{
354+
status: :demonstrated,
355+
# 2 hours before Seoul time in UTC
356+
inserted_at: ~U[2024-05-01 10:00:00Z]
357+
})
358+
|> Ash.create!()
359+
360+
documentation_after =
361+
AshPostgres.Test.ComplexCalculations.Documentation
362+
|> Ash.Changeset.for_create(:create, %{
363+
status: :demonstrated,
364+
# 2 hours after Seoul time in UTC
365+
inserted_at: ~U[2024-05-01 14:00:00Z]
366+
})
367+
|> Ash.create!()
368+
369+
[doc_before, doc_after] =
370+
[documentation_before, documentation_after]
371+
|> Ash.load!([:is_active_with_timezone])
372+
373+
assert doc_before.is_active_with_timezone == false
374+
assert doc_after.is_active_with_timezone == true
375+
end
349376
end

test/support/complex_calculations/resources/documentation.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ defmodule AshPostgres.Test.ComplexCalculations.Documentation do
4242
end
4343
)
4444
)
45+
46+
calculate :is_active_with_timezone, :boolean do
47+
calculation(expr(inserted_at > lazy({AshPostgres.Test.TimezoneHelper, :seoul_time, []})))
48+
end
4549
end
4650

4751
postgres do
@@ -55,3 +59,10 @@ defmodule AshPostgres.Test.ComplexCalculations.Documentation do
5559
end
5660
end
5761
end
62+
63+
defmodule AshPostgres.Test.TimezoneHelper do
64+
def seoul_time do
65+
# Fixed datetime for testing - equivalent to 2024-05-01 21:00:00 in Seoul (UTC+9)
66+
~U[2024-05-01 12:00:00Z] |> DateTime.shift_zone!("Asia/Seoul")
67+
end
68+
end

0 commit comments

Comments
 (0)