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
90 changes: 90 additions & 0 deletions test/duration_operator_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshPostgres.DurationOperatorTest 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 "adding a duration to a datetime attribute" do
test "filters on a duration" do

Check failure on line 18 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)

Check failure on line 18 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)

Check failure on line 18 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)

Check failure on line 18 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)

Check failure on line 18 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)
now = DateTime.utc_now()
post!("old", DateTime.add(now, -60, :day))
post!("recent", DateTime.add(now, -10, :day))

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

test "filters on a multi-unit duration" do

Check failure on line 29 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)

Check failure on line 29 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)

Check failure on line 29 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)

Check failure on line 29 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)

Check failure on line 29 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)
now = DateTime.utc_now()
post!("old", DateTime.add(now, -60, :day))
post!("recent", DateTime.add(now, -20, :day))

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

test "the duration may be on the left" do

Check failure on line 40 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute the duration may be on the left (AshPostgres.DurationOperatorTest)

Check failure on line 40 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute the duration may be on the left (AshPostgres.DurationOperatorTest)

Check failure on line 40 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute the duration may be on the left (AshPostgres.DurationOperatorTest)

Check failure on line 40 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute the duration may be on the left (AshPostgres.DurationOperatorTest)

Check failure on line 40 in test/duration_operator_test.exs

View workflow job for this annotation

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

test adding a duration to a datetime attribute the duration may be on the left (AshPostgres.DurationOperatorTest)
now = DateTime.utc_now()
post!("old", DateTime.add(now, -60, :day))
post!("recent", DateTime.add(now, -10, :day))

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

describe "subtracting a duration from a datetime attribute" do
test "filters on a duration" do

Check failure on line 53 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)

Check failure on line 53 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)

Check failure on line 53 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)

Check failure on line 53 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)

Check failure on line 53 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a duration (AshPostgres.DurationOperatorTest)
now = DateTime.utc_now()
post!("soon", DateTime.add(now, 10, :day))
post!("later", DateTime.add(now, 60, :day))

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

test "filters on a multi-unit duration" do

Check failure on line 64 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)

Check failure on line 64 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)

Check failure on line 64 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)

Check failure on line 64 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)

Check failure on line 64 in test/duration_operator_test.exs

View workflow job for this annotation

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

test subtracting a duration from a datetime attribute filters on a multi-unit duration (AshPostgres.DurationOperatorTest)
now = DateTime.utc_now()
post!("soon", DateTime.add(now, 20, :day))
post!("later", DateTime.add(now, 60, :day))

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

describe "a duration expression compared to another datetime" do
test "both sides may be expressions" do

Check failure on line 77 in test/duration_operator_test.exs

View workflow job for this annotation

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

test a duration expression compared to another datetime both sides may be expressions (AshPostgres.DurationOperatorTest)

Check failure on line 77 in test/duration_operator_test.exs

View workflow job for this annotation

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

test a duration expression compared to another datetime both sides may be expressions (AshPostgres.DurationOperatorTest)

Check failure on line 77 in test/duration_operator_test.exs

View workflow job for this annotation

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

test a duration expression compared to another datetime both sides may be expressions (AshPostgres.DurationOperatorTest)

Check failure on line 77 in test/duration_operator_test.exs

View workflow job for this annotation

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

test a duration expression compared to another datetime both sides may be expressions (AshPostgres.DurationOperatorTest)

Check failure on line 77 in test/duration_operator_test.exs

View workflow job for this annotation

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

test a duration expression compared to another datetime both sides may be expressions (AshPostgres.DurationOperatorTest)
now = DateTime.utc_now()
post!("old", DateTime.add(now, -60, :day))
post!("recent", DateTime.add(now, -10, :day))

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