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

defmodule AshPostgres.ArrayLengthExprTest do
@moduledoc false
use AshPostgres.RepoCase, async: false

alias AshPostgres.Test.Post

require Ash.Query
import Ash.Expr

defp length_of(list) do
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "a", list_containing_nils: list})
|> Ash.create!()

Post
|> Ash.Query.filter(id == ^post.id)
|> Ash.Query.calculate(:len, :integer, expr(length(list_containing_nils)))
|> Ash.read_one!()
|> Map.get(:calculations)
|> Map.get(:len)
end

test "an empty list has length zero" do

Check failure on line 28 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test an empty list has length zero (AshPostgres.ArrayLengthExprTest)

Check failure on line 28 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test an empty list has length zero (AshPostgres.ArrayLengthExprTest)

Check failure on line 28 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test an empty list has length zero (AshPostgres.ArrayLengthExprTest)

Check failure on line 28 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test an empty list has length zero (AshPostgres.ArrayLengthExprTest)

Check failure on line 28 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test an empty list has length zero (AshPostgres.ArrayLengthExprTest)
assert length_of([]) == 0
end

test "a populated list has its length" do
assert length_of(["a", "b", "c"]) == 3
end

test "a nil list has no length" do
assert length_of(nil) == nil
end

test "filtering on the length of an empty list matches" do

Check failure on line 40 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test filtering on the length of an empty list matches (AshPostgres.ArrayLengthExprTest)

Check failure on line 40 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test filtering on the length of an empty list matches (AshPostgres.ArrayLengthExprTest)

Check failure on line 40 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test filtering on the length of an empty list matches (AshPostgres.ArrayLengthExprTest)

Check failure on line 40 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test filtering on the length of an empty list matches (AshPostgres.ArrayLengthExprTest)

Check failure on line 40 in test/array_length_expr_test.exs

View workflow job for this annotation

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

test filtering on the length of an empty list matches (AshPostgres.ArrayLengthExprTest)
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "a", list_containing_nils: []})
|> Ash.create!()

assert [found] =
Post
|> Ash.Query.filter(length(list_containing_nils) == 0)

Check warning on line 48 in test/array_length_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix credo --strict

Using `length/1` is expensive, prefer comparing against an empty list.

Check warning on line 48 in test/array_length_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (18) / mix credo --strict

Using `length/1` is expensive, prefer comparing against an empty list.

Check warning on line 48 in test/array_length_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix credo --strict

Using `length/1` is expensive, prefer comparing against an empty list.

Check warning on line 48 in test/array_length_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix credo --strict

Using `length/1` is expensive, prefer comparing against an empty list.

Check warning on line 48 in test/array_length_expr_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (17) / mix credo --strict

Using `length/1` is expensive, prefer comparing against an empty list.
|> Ash.read!()

assert found.id == post.id
end
end
Loading