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

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

alias AshPostgres.Test.Post

require Ash.Query
import Ash.Expr

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

Post
|> Ash.Query.filter(id == ^post.id)
|> Ash.Query.calculate(:position, :integer, expr(string_position(title, ^substring)))
|> Ash.read_one!()
|> Map.get(:calculations)
|> Map.get(:position)
end

test "a match at the start of the string is zero" do

Check failure on line 25 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match at the start of the string is zero (AshPostgres.StringPositionExprTest)

Check failure on line 25 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match at the start of the string is zero (AshPostgres.StringPositionExprTest)

Check failure on line 25 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match at the start of the string is zero (AshPostgres.StringPositionExprTest)

Check failure on line 25 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match at the start of the string is zero (AshPostgres.StringPositionExprTest)

Check failure on line 25 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match at the start of the string is zero (AshPostgres.StringPositionExprTest)
assert position("alpha", "a") == 0
end

test "a match inside the string is zero based" do

Check failure on line 29 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match inside the string is zero based (AshPostgres.StringPositionExprTest)

Check failure on line 29 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match inside the string is zero based (AshPostgres.StringPositionExprTest)

Check failure on line 29 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match inside the string is zero based (AshPostgres.StringPositionExprTest)

Check failure on line 29 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match inside the string is zero based (AshPostgres.StringPositionExprTest)

Check failure on line 29 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a match inside the string is zero based (AshPostgres.StringPositionExprTest)
assert position("alpha", "pha") == 2
end

test "a substring that is absent is nil" do

Check failure on line 33 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a substring that is absent is nil (AshPostgres.StringPositionExprTest)

Check failure on line 33 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a substring that is absent is nil (AshPostgres.StringPositionExprTest)

Check failure on line 33 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a substring that is absent is nil (AshPostgres.StringPositionExprTest)

Check failure on line 33 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a substring that is absent is nil (AshPostgres.StringPositionExprTest)

Check failure on line 33 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a substring that is absent is nil (AshPostgres.StringPositionExprTest)
assert position("alpha", "zzz") == nil
end

test "the whole string matches at zero" do

Check failure on line 37 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test the whole string matches at zero (AshPostgres.StringPositionExprTest)

Check failure on line 37 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test the whole string matches at zero (AshPostgres.StringPositionExprTest)

Check failure on line 37 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test the whole string matches at zero (AshPostgres.StringPositionExprTest)

Check failure on line 37 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test the whole string matches at zero (AshPostgres.StringPositionExprTest)

Check failure on line 37 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test the whole string matches at zero (AshPostgres.StringPositionExprTest)
assert position("alpha", "alpha") == 0
end

test "a ci_string is matched case insensitively, and still zero based" do

Check failure on line 41 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a ci_string is matched case insensitively, and still zero based (AshPostgres.StringPositionExprTest)

Check failure on line 41 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a ci_string is matched case insensitively, and still zero based (AshPostgres.StringPositionExprTest)

Check failure on line 41 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a ci_string is matched case insensitively, and still zero based (AshPostgres.StringPositionExprTest)

Check failure on line 41 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a ci_string is matched case insensitively, and still zero based (AshPostgres.StringPositionExprTest)

Check failure on line 41 in test/string_position_expr_test.exs

View workflow job for this annotation

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

test a ci_string is matched case insensitively, and still zero based (AshPostgres.StringPositionExprTest)
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "t", category: "Alpha"})
|> Ash.create!()

assert Post
|> Ash.Query.filter(id == ^post.id)
|> Ash.Query.calculate(:position, :integer, expr(string_position(category, "PHA")))
|> Ash.read_one!()
|> Map.get(:calculations)
|> Map.get(:position) == 2
end
end
Loading