Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions test/duration_expr_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshPostgres.DurationExprTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.Post

require Ash.Query

defp post!(title, created_at) do
Post
|> Ash.Changeset.for_create(:create, %{title: title, created_at: created_at})
|> Ash.create!()
end

describe "ago/1" do
test "filters on a duration" do

Check failure on line 18 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (17) / mix test

test ago/1 filters on a duration (AshPostgres.DurationExprTest)

Check failure on line 18 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (18) / mix test

test ago/1 filters on a duration (AshPostgres.DurationExprTest)

Check failure on line 18 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test ago/1 filters on a duration (AshPostgres.DurationExprTest)

Check failure on line 18 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test ago/1 filters on a duration (AshPostgres.DurationExprTest)

Check failure on line 18 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test ago/1 filters on a duration (AshPostgres.DurationExprTest)
post!("old", DateTime.add(DateTime.utc_now(), -60, :day))
post!("recent", DateTime.utc_now())

assert [%Post{title: "recent"}] =
Post
|> Ash.Query.filter(created_at > ago(^Duration.new!(day: 30)))
|> Ash.read!()
end

test "filters on a multi-unit duration" do

Check failure on line 28 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (17) / mix test

test ago/1 filters on a multi-unit duration (AshPostgres.DurationExprTest)

Check failure on line 28 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (18) / mix test

test ago/1 filters on a multi-unit duration (AshPostgres.DurationExprTest)

Check failure on line 28 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test ago/1 filters on a multi-unit duration (AshPostgres.DurationExprTest)

Check failure on line 28 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test ago/1 filters on a multi-unit duration (AshPostgres.DurationExprTest)

Check failure on line 28 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test ago/1 filters on a multi-unit duration (AshPostgres.DurationExprTest)
post!("old", DateTime.add(DateTime.utc_now(), -60, :day))
post!("recent", DateTime.add(DateTime.utc_now(), -20, :day))

assert [%Post{title: "recent"}] =
Post
|> Ash.Query.filter(created_at > ago(^Duration.new!(month: 1, day: 2)))
|> Ash.read!()
end
end

describe "from_now/1" do
test "filters on a duration" do

Check failure on line 40 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (17) / mix test

test from_now/1 filters on a duration (AshPostgres.DurationExprTest)

Check failure on line 40 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (18) / mix test

test from_now/1 filters on a duration (AshPostgres.DurationExprTest)

Check failure on line 40 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test from_now/1 filters on a duration (AshPostgres.DurationExprTest)

Check failure on line 40 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test from_now/1 filters on a duration (AshPostgres.DurationExprTest)

Check failure on line 40 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test from_now/1 filters on a duration (AshPostgres.DurationExprTest)
post!("soon", DateTime.add(DateTime.utc_now(), 10, :day))
post!("later", DateTime.add(DateTime.utc_now(), 60, :day))

assert [%Post{title: "soon"}] =
Post
|> Ash.Query.filter(created_at < from_now(^Duration.new!(day: 30)))
|> Ash.read!()
end
end

describe "datetime_add/2" do
test "adds a duration to a datetime" do

Check failure on line 52 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (17) / mix test

test datetime_add/2 adds a duration to a datetime (AshPostgres.DurationExprTest)

Check failure on line 52 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (18) / mix test

test datetime_add/2 adds a duration to a datetime (AshPostgres.DurationExprTest)

Check failure on line 52 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test datetime_add/2 adds a duration to a datetime (AshPostgres.DurationExprTest)

Check failure on line 52 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test datetime_add/2 adds a duration to a datetime (AshPostgres.DurationExprTest)

Check failure on line 52 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test datetime_add/2 adds a duration to a datetime (AshPostgres.DurationExprTest)
now = DateTime.utc_now()
post!("before", DateTime.add(now, 3, :day))
post!("after", DateTime.add(now, 30, :day))

assert [%Post{title: "before"}] =
Post
|> Ash.Query.filter(created_at < datetime_add(^now, ^Duration.new!(day: 7)))
|> Ash.read!()
end

test "a negative duration subtracts" do

Check failure on line 63 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (17) / mix test

test datetime_add/2 a negative duration subtracts (AshPostgres.DurationExprTest)

Check failure on line 63 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (18) / mix test

test datetime_add/2 a negative duration subtracts (AshPostgres.DurationExprTest)

Check failure on line 63 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test datetime_add/2 a negative duration subtracts (AshPostgres.DurationExprTest)

Check failure on line 63 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test datetime_add/2 a negative duration subtracts (AshPostgres.DurationExprTest)

Check failure on line 63 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test datetime_add/2 a negative duration subtracts (AshPostgres.DurationExprTest)
now = DateTime.utc_now()
post!("old", DateTime.add(now, -30, :day))
post!("recent", now)

assert [%Post{title: "recent"}] =
Post
|> Ash.Query.filter(created_at > datetime_add(^now, ^Duration.new!(day: -7)))
|> Ash.read!()
end
end

describe "date_add/2" do
test "adds a duration to a date, and stays a date" do

Check failure on line 76 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (17) / mix test

test date_add/2 adds a duration to a date, and stays a date (AshPostgres.DurationExprTest)

Check failure on line 76 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (18) / mix test

test date_add/2 adds a duration to a date, and stays a date (AshPostgres.DurationExprTest)

Check failure on line 76 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test date_add/2 adds a duration to a date, and stays a date (AshPostgres.DurationExprTest)

Check failure on line 76 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test date_add/2 adds a duration to a date, and stays a date (AshPostgres.DurationExprTest)

Check failure on line 76 in test/duration_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test date_add/2 adds a duration to a date, and stays a date (AshPostgres.DurationExprTest)
today = Date.utc_today()
post!("old", DateTime.add(DateTime.utc_now(), -60, :day))
post!("recent", DateTime.utc_now())

assert [%Post{title: "recent"}] =
Post
|> Ash.Query.filter(
type(created_at, :date) >= date_add(^today, ^Duration.new!(day: -30))
)
|> Ash.read!()
end
end
end
Loading