update tests to show many_to_many filter problem

This commit is contained in:
Barnabas Jovanovics 2024-05-28 11:47:07 +02:00
parent cacf71cd55
commit bb2b80227d
9 changed files with 452 additions and 3 deletions

View file

@ -0,0 +1,95 @@
{
"attributes": [
{
"default": "fragment(\"gen_random_uuid()\")",
"size": null,
"type": "uuid",
"source": "id",
"references": null,
"allow_nil?": false,
"primary_key?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "bigint",
"source": "order",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "post_id",
"references": {
"name": "post_followers_post_id_fkey",
"table": "posts",
"primary_key?": true,
"schema": "public",
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"destination_attribute": "id",
"on_delete": null,
"on_update": null,
"deferrable": false,
"match_with": null,
"match_type": null,
"destination_attribute_default": null,
"destination_attribute_generated": null
},
"allow_nil?": false,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "follower_id",
"references": {
"name": "post_followers_follower_id_fkey",
"table": "users",
"primary_key?": true,
"schema": "public",
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"destination_attribute": "id",
"on_delete": null,
"on_update": null,
"deferrable": false,
"match_with": null,
"match_type": null,
"destination_attribute_default": null,
"destination_attribute_generated": null
},
"allow_nil?": false,
"primary_key?": false,
"generated?": false
}
],
"table": "post_followers",
"hash": "A2DAE11511C56029F1F2AC8776796059F1AF680CD88DFBF91F7DBEA70302B6AF",
"repo": "Elixir.AshPostgres.TestRepo",
"schema": null,
"check_constraints": [],
"identities": [],
"custom_indexes": [],
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"base_filter": null,
"custom_statements": [],
"has_create_action": true
}

View file

@ -0,0 +1,108 @@
{
"attributes": [
{
"default": "fragment(\"gen_random_uuid()\")",
"size": null,
"type": "uuid",
"source": "id",
"references": null,
"allow_nil?": false,
"generated?": false,
"primary_key?": true
},
{
"default": "nil",
"size": null,
"type": "bigint",
"source": "order",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "post_id",
"references": {
"name": "post_followers_post_id_fkey",
"table": "posts",
"schema": "public",
"on_delete": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"primary_key?": true,
"destination_attribute": "id",
"on_update": null,
"deferrable": false,
"match_type": null,
"match_with": null,
"destination_attribute_default": null,
"destination_attribute_generated": null
},
"allow_nil?": false,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "follower_id",
"references": {
"name": "post_followers_follower_id_fkey",
"table": "users",
"schema": "public",
"on_delete": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"primary_key?": true,
"destination_attribute": "id",
"on_update": null,
"deferrable": false,
"match_type": null,
"match_with": null,
"destination_attribute_default": null,
"destination_attribute_generated": null
},
"allow_nil?": false,
"generated?": false,
"primary_key?": false
}
],
"table": "post_followers",
"hash": "9BAFF682E73CBFDA82F7AE21BEE571C7390BACAE3DDCD658232753A0424300A5",
"repo": "Elixir.AshPostgres.TestRepo",
"identities": [
{
"name": "join_attributes",
"keys": [
"follower_id",
"post_id"
],
"where": null,
"base_filter": null,
"nils_distinct?": true,
"all_tenants?": false,
"index_name": "post_followers_join_attributes_index"
}
],
"schema": null,
"check_constraints": [],
"custom_indexes": [],
"base_filter": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"custom_statements": [],
"has_create_action": true
}

View file

@ -0,0 +1,118 @@
{
"attributes": [
{
"default": "fragment(\"gen_random_uuid()\")",
"size": null,
"type": "uuid",
"source": "id",
"references": null,
"allow_nil?": false,
"primary_key?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "bigint",
"source": "order",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "\"active\"",
"size": null,
"type": "text",
"source": "state",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "post_id",
"references": {
"name": "post_followers_post_id_fkey",
"table": "posts",
"primary_key?": true,
"destination_attribute": "id",
"schema": "public",
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"on_delete": null,
"on_update": null,
"deferrable": false,
"match_with": null,
"match_type": null,
"destination_attribute_default": null,
"destination_attribute_generated": null
},
"allow_nil?": false,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "follower_id",
"references": {
"name": "post_followers_follower_id_fkey",
"table": "users",
"primary_key?": true,
"destination_attribute": "id",
"schema": "public",
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"on_delete": null,
"on_update": null,
"deferrable": false,
"match_with": null,
"match_type": null,
"destination_attribute_default": null,
"destination_attribute_generated": null
},
"allow_nil?": false,
"primary_key?": false,
"generated?": false
}
],
"table": "post_followers",
"hash": "D6DAC5347205272ED280B52E2A95167A56504474352E22F212BE07B99CE7D21B",
"repo": "Elixir.AshPostgres.TestRepo",
"identities": [
{
"name": "join_attributes",
"keys": [
"follower_id",
"post_id"
],
"where": null,
"base_filter": null,
"all_tenants?": false,
"nils_distinct?": true,
"index_name": "post_followers_join_attributes_index"
}
],
"check_constraints": [],
"custom_indexes": [],
"base_filter": null,
"schema": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"custom_statements": [],
"has_create_action": true
}

View file

@ -0,0 +1,29 @@
defmodule AshPostgres.TestRepo.Migrations.MigrateResources27 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
drop(constraint("post_followers", "post_followers_pkey"))
alter table(:post_followers) do
modify(:follower_id, :uuid, primary_key: false)
modify(:post_id, :uuid, primary_key: false)
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
end
end
def down do
drop(constraint("post_followers", "post_followers_pkey"))
alter table(:post_followers) do
remove(:id)
modify(:post_id, :uuid, primary_key: true)
modify(:follower_id, :uuid, primary_key: true)
end
end
end

View file

@ -0,0 +1,25 @@
defmodule AshPostgres.TestRepo.Migrations.MigrateResources28 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(
unique_index(:post_followers, ["follower_id", "post_id"],
name: "post_followers_join_attributes_index"
)
)
end
def down do
drop_if_exists(
unique_index(:post_followers, ["follower_id", "post_id"],
name: "post_followers_join_attributes_index"
)
)
end
end

View file

@ -0,0 +1,21 @@
defmodule AshPostgres.TestRepo.Migrations.MigrateResources29 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
alter table(:post_followers) do
add(:state, :text, default: "active")
end
end
def down do
alter table(:post_followers) do
remove(:state)
end
end
end

View file

@ -1,6 +1,6 @@
defmodule AshPostgres.Test.LoadTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.{Author, Comment, Post, Record, TempEntity, User}
alias AshPostgres.Test.{Author, Comment, Post, Record, TempEntity, User, PostFollower}
require Ash.Query
@ -74,6 +74,37 @@ defmodule AshPostgres.Test.LoadTest do
assert %{linked_posts: [%{title: "destination"}, %{title: "destination"}]} = results
end
test "many_to_many loads work with filter on join relationship" do
followers =
for i <- 0..2 do
User
|> Ash.Changeset.for_create(:create, %{name: "user#{i}", is_active: true})
|> Ash.create!()
end
Post
|> Ash.Changeset.for_create(:create, %{title: "a"})
|> Ash.Changeset.manage_relationship(:followers, followers, type: :append_and_remove)
|> Ash.create!()
PostFollower
|> Ash.Query.for_read(:read, %{})
|> Ash.Query.limit(1)
|> Ash.read_one!()
|> Ash.Changeset.for_update(:update, %{state: :inactive})
|> Ash.update!()
# Logger.configure(level: :debug)
[post] =
Post
|> Ash.Query.for_read(:read, %{})
|> Ash.Query.load(:active_followers)
|> Ash.read!()
assert length(post.active_followers) == 1
end
test "many_to_many loads work when nested" do
source_post =
Post

View file

@ -334,6 +334,19 @@ defmodule AshPostgres.Test.Post do
read_action: :active
)
has_many :active_followers_assoc, AshPostgres.Test.PostFollower do
public?(true)
filter(expr(state == :active))
end
many_to_many(:active_followers, AshPostgres.Test.User,
public?: true,
through: AshPostgres.Test.PostFollower,
join_relationship: :first_follower_assoc,
source_attribute_on_join_resource: :post_id,
destination_attribute_on_join_resource: :follower_id
)
has_many(:post_followers, AshPostgres.Test.PostFollower)
many_to_many(:sorted_followers, AshPostgres.Test.User,

View file

@ -9,6 +9,10 @@ defmodule AshPostgres.Test.PostFollower do
repo AshPostgres.TestRepo
end
identities do
identity(:join_attributes, [:post_id, :follower_id])
end
actions do
default_accept(:*)
@ -16,18 +20,23 @@ defmodule AshPostgres.Test.PostFollower do
end
attributes do
uuid_primary_key(:id)
attribute(:order, :integer, public?: true)
attribute :state, :atom do
public?(true)
constraints(one_of: [:active, :inactive])
default(:active)
end
end
relationships do
belongs_to :post, AshPostgres.Test.Post do
primary_key?(true)
public?(true)
allow_nil?(false)
end
belongs_to :follower, AshPostgres.Test.User do
primary_key?(true)
public?(true)
allow_nil?(false)
end