Skip to content

Commit

Permalink
test: Failing test for module calculation inside expression one (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam authored Jan 8, 2025
1 parent 5f26d69 commit fbc4346
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 1 deletion.
59 changes: 59 additions & 0 deletions priv/resource_snapshots/test_repo/comedians/20241217232254.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"attributes": [
{
"allow_nil?": false,
"default": "fragment(\"gen_random_uuid()\")",
"generated?": false,
"primary_key?": true,
"references": null,
"size": null,
"source": "id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "name",
"type": "text"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "inserted_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "updated_at",
"type": "utc_datetime_usec"
}
],
"base_filter": null,
"check_constraints": [],
"custom_indexes": [],
"custom_statements": [],
"has_create_action": false,
"hash": "A376A922D4610DEAD0294DD863E8E2FF72FEC6316AC6B809C3DFC17B29094EBA",
"identities": [],
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"repo": "Elixir.AshPostgres.TestRepo",
"schema": null,
"table": "comedians"
}
98 changes: 98 additions & 0 deletions priv/resource_snapshots/test_repo/jokes/20241217232254.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"attributes": [
{
"allow_nil?": false,
"default": "fragment(\"gen_random_uuid()\")",
"generated?": false,
"primary_key?": true,
"references": null,
"size": null,
"source": "id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "text",
"type": "text"
},
{
"allow_nil?": true,
"default": "false",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "is_good",
"type": "boolean"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "inserted_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": false,
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "updated_at",
"type": "utc_datetime_usec"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": {
"deferrable": false,
"destination_attribute": "id",
"destination_attribute_default": null,
"destination_attribute_generated": null,
"index?": false,
"match_type": null,
"match_with": null,
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"name": "jokes_comedian_id_fkey",
"on_delete": null,
"on_update": null,
"primary_key?": true,
"schema": "public",
"table": "comedians"
},
"size": null,
"source": "comedian_id",
"type": "uuid"
}
],
"base_filter": null,
"check_constraints": [],
"custom_indexes": [],
"custom_statements": [],
"has_create_action": false,
"hash": "CFDCBDBAC925B20A1EA8EB4B3C37ACF1FD915FA21F6B07DE4D066AA167B9EEF5",
"identities": [],
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"repo": "Elixir.AshPostgres.TestRepo",
"schema": null,
"table": "jokes"
}
77 changes: 77 additions & 0 deletions priv/test_repo/migrations/20241217232254_migrate_resources45.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
defmodule AshPostgres.TestRepo.Migrations.MigrateResources45 do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
create table(:jokes, primary_key: false) do
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
add(:text, :text)
add(:is_good, :boolean, default: false)

add(:inserted_at, :utc_datetime_usec,
null: false,
default: fragment("(now() AT TIME ZONE 'utc')")
)

add(:updated_at, :utc_datetime_usec,
null: false,
default: fragment("(now() AT TIME ZONE 'utc')")
)

add(:comedian_id, :uuid)
end

create table(:comedians, primary_key: false) do
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
end

alter table(:jokes) do
modify(
:comedian_id,
references(:comedians,
column: :id,
name: "jokes_comedian_id_fkey",
type: :uuid,
prefix: "public"
)
)
end

alter table(:comedians) do
add(:name, :text)

add(:inserted_at, :utc_datetime_usec,
null: false,
default: fragment("(now() AT TIME ZONE 'utc')")
)

add(:updated_at, :utc_datetime_usec,
null: false,
default: fragment("(now() AT TIME ZONE 'utc')")
)
end
end

def down do
alter table(:comedians) do
remove(:updated_at)
remove(:inserted_at)
remove(:name)
end

drop(constraint(:jokes, "jokes_comedian_id_fkey"))

alter table(:jokes) do
modify(:comedian_id, :uuid)
end

drop(table(:comedians))

drop(table(:jokes))
end
end
8 changes: 7 additions & 1 deletion test/calculation_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule AshPostgres.CalculationTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.{Account, Author, Comment, Post, User}
alias AshPostgres.Test.{Account, Author, Comedian, Comment, Post, User}

require Ash.Query
import Ash.Expr
Expand Down Expand Up @@ -937,6 +937,12 @@ defmodule AshPostgres.CalculationTest do
|> Ash.read!()
end

test "module calculation inside expr calculation works" do

Check failure on line 940 in test/calculation_test.exs

View workflow job for this annotation

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

test module calculation inside expr calculation works (AshPostgres.CalculationTest)

Check failure on line 940 in test/calculation_test.exs

View workflow job for this annotation

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

test module calculation inside expr calculation works (AshPostgres.CalculationTest)

Check failure on line 940 in test/calculation_test.exs

View workflow job for this annotation

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

test module calculation inside expr calculation works (AshPostgres.CalculationTest)
commedian = Comedian.create!(%{name: "John"})
commedian = Ash.get!(Comedian, commedian.id, load: [:has_jokes_expr], authorize?: false)
assert %{has_jokes_expr: false} = commedian
end

def fred do
"fred"
end
Expand Down
2 changes: 2 additions & 0 deletions test/support/domain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule AshPostgres.Test.Domain do
resources do
resource(AshPostgres.Test.CoAuthorPost)
resource(AshPostgres.Test.Post)
resource(AshPostgres.Test.Comedian)
resource(AshPostgres.Test.Comment)
resource(AshPostgres.Test.IntegerPost)
resource(AshPostgres.Test.Rating)
Expand All @@ -14,6 +15,7 @@ defmodule AshPostgres.Test.Domain do
resource(AshPostgres.Test.Profile)
resource(AshPostgres.Test.User)
resource(AshPostgres.Test.Invite)
resource(AshPostgres.Test.Joke)
resource(AshPostgres.Test.Account)
resource(AshPostgres.Test.Organization)
resource(AshPostgres.Test.Manager)
Expand Down
57 changes: 57 additions & 0 deletions test/support/resources/comedian.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule AshPostgres.Test.Comedian do
@moduledoc false
use Ash.Resource,
domain: AshPostgres.Test.Domain,
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer]

attributes do
uuid_primary_key(:id)
attribute(:name, :string, public?: true)
create_timestamp(:inserted_at, public?: true)
update_timestamp(:updated_at, public?: true)
end

relationships do
has_many(:jokes, AshPostgres.Test.Joke, public?: true)
end

calculations do
calculate(:has_jokes_mod, :boolean, AshPostgres.Test.Comedian.HasJokes)
calculate(:has_jokes_expr, :boolean, expr(has_jokes_mod == true))
end

actions do
defaults([:read])

create :create do
primary?(true)
accept([:name])
end
end

code_interface do
define(:create)
end

postgres do
table("comedians")
repo(AshPostgres.TestRepo)
end
end

defmodule AshPostgres.Test.Comedian.HasJokes do

Check warning on line 43 in test/support/resources/comedian.ex

View workflow job for this annotation

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

Modules should have a @moduledoc tag.

Check warning on line 43 in test/support/resources/comedian.ex

View workflow job for this annotation

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

Modules should have a @moduledoc tag.

Check warning on line 43 in test/support/resources/comedian.ex

View workflow job for this annotation

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

Modules should have a @moduledoc tag.
use Ash.Resource.Calculation

@impl true
def load(_, _, _) do
[:jokes]
end

@impl true
def calculate(comedians, _, _) do
Enum.map(comedians, fn %{jokes: jokes} ->
Enum.any?(jokes)
end)
end
end
28 changes: 28 additions & 0 deletions test/support/resources/joke.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule AshPostgres.Test.Joke do
@moduledoc false
use Ash.Resource,
domain: AshPostgres.Test.Domain,
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer]

attributes do
uuid_primary_key(:id)
attribute(:text, :string, public?: true)
attribute(:is_good, :boolean, default: false, public?: true)
create_timestamp(:inserted_at, public?: true)
update_timestamp(:updated_at, public?: true)
end

relationships do
belongs_to(:comedian, AshPostgres.Test.Comedian, public?: true)
end

actions do
defaults([:read])
end

postgres do
table("jokes")
repo(AshPostgres.TestRepo)
end
end

0 comments on commit fbc4346

Please sign in to comment.