Skip to content

Commit

Permalink
fix: ran formatter and fixed credo warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
simpers committed Dec 9, 2024
1 parent 05c0e68 commit d934ef4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 87 deletions.
28 changes: 14 additions & 14 deletions lib/sql_implementation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,21 @@ defmodule AshPostgres.SqlImplementation do
end

def expr(
query,
%Ash.Query.Ref{
attribute: %Ash.Resource.Attribute{
type: attr_type,
constraints: constraints,
},
bare?: true
} = ref,
bindings,
embedded?,
acc,
type
) do
query,
%Ash.Query.Ref{
attribute: %Ash.Resource.Attribute{
type: attr_type,
constraints: constraints
},
bare?: true
} = ref,
bindings,
embedded?,
acc,
type
) do
if function_exported?(attr_type, :postgres_reference_expr, 3) do
non_bare_ref = %Ash.Query.Ref{ ref | bare?: nil }
non_bare_ref = %Ash.Query.Ref{ref | bare?: nil}
{expr, acc} = AshSql.Expr.dynamic_expr(query, non_bare_ref, bindings, embedded?, type, acc)

case attr_type.postgres_reference_expr(attr_type, constraints, expr) do
Expand Down
66 changes: 36 additions & 30 deletions test/many_to_many_expr_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ defmodule AshPostgres.ManyToManyExprTest do

co_authors =
if ctx[:co_authors],
do: 1..ctx[:co_authors]
|> Stream.map(&
Author
do:
1..ctx[:co_authors]
|> Stream.map(
&(Author
|> Ash.Changeset.for_create(:create, %{first_name: "John #{&1}", last_name: "Doe"})
|> Ash.create!()
)
|> Enum.into([]),
else: []
|> Ash.create!())
)
|> Enum.into([]),
else: []

%{
main_author: main_author,
co_authors: co_authors,
co_authors: co_authors
}
end

Expand All @@ -52,7 +53,12 @@ defmodule AshPostgres.ManyToManyExprTest do
Author
|> Ash.Query.new()
|> Ash.Query.filter(id == ^author_id)
|> Ash.Query.load([:all_co_authored_posts, :cancelled_co_authored_posts, :editor_of, :writer_of])
|> Ash.Query.load([
:all_co_authored_posts,
:cancelled_co_authored_posts,
:editor_of,
:writer_of
])
|> Ash.read_one!()
end

Expand Down Expand Up @@ -81,10 +87,10 @@ defmodule AshPostgres.ManyToManyExprTest do
@tag main_author?: true
@tag co_authors: 3
test "filter on many_to_many relationship using parent works as expected - basic",
%{
main_author: main_author,
co_authors: co_authors,
} do
%{
main_author: main_author,
co_authors: co_authors
} do
post = create_post(main_author)

[first_ca, second_ca, third_ca] = co_authors
Expand All @@ -98,8 +104,8 @@ defmodule AshPostgres.ManyToManyExprTest do
assert Enum.count(post.co_authors) == 1
assert Enum.count(first_ca.all_co_authored_posts) == 1
assert Enum.count(first_ca.editor_of) == 1
assert Enum.count(first_ca.writer_of) == 0
assert Enum.count(first_ca.cancelled_co_authored_posts) == 0
assert Enum.empty?(first_ca.writer_of) == true
assert Enum.empty?(first_ca.cancelled_co_authored_posts) == true

# Add second co-author
create_co_author_post(second_ca, post, :writer)
Expand All @@ -109,9 +115,9 @@ defmodule AshPostgres.ManyToManyExprTest do

assert Enum.count(post.co_authors) == 2
assert Enum.count(second_ca.all_co_authored_posts) == 1
assert Enum.count(second_ca.editor_of) == 0
assert Enum.count(second_ca.writer_of) == 1
assert Enum.count(second_ca.cancelled_co_authored_posts) == 0
assert Enum.empty?(second_ca.editor_of) == true
assert Enum.empty?(second_ca.cancelled_co_authored_posts) == true

# Add third co-author
create_co_author_post(third_ca, post, :proof_reader)
Expand All @@ -121,18 +127,18 @@ defmodule AshPostgres.ManyToManyExprTest do

assert Enum.count(post.co_authors) == 3
assert Enum.count(third_ca.all_co_authored_posts) == 1
assert Enum.count(third_ca.editor_of) == 0
assert Enum.count(third_ca.writer_of) == 0
assert Enum.count(third_ca.cancelled_co_authored_posts) == 0
assert Enum.empty?(third_ca.editor_of) == true
assert Enum.empty?(third_ca.writer_of) == true
assert Enum.empty?(third_ca.cancelled_co_authored_posts) == true
end

@tag main_author?: true
@tag co_authors: 4
test "filter on many_to_many relationship using parent works as expected - cancelled",
%{
main_author: main_author,
co_authors: co_authors,
} do
%{
main_author: main_author,
co_authors: co_authors
} do
first_post = create_post(main_author)
second_post = create_post(main_author)

Expand All @@ -151,7 +157,7 @@ defmodule AshPostgres.ManyToManyExprTest do
assert Enum.count(first_ca.all_co_authored_posts) == 2
assert Enum.count(first_ca.editor_of) == 1
assert Enum.count(first_ca.writer_of) == 1
assert Enum.count(first_ca.cancelled_co_authored_posts) == 0
assert Enum.empty?(first_ca.cancelled_co_authored_posts) == true

# Add second co-author
create_co_author_post(second_ca, first_post, :proof_reader)
Expand All @@ -165,9 +171,9 @@ defmodule AshPostgres.ManyToManyExprTest do
assert Enum.count(second_post.co_authors_unfiltered) == 2

assert Enum.count(second_ca.all_co_authored_posts) == 2
assert Enum.count(second_ca.editor_of) == 0
assert Enum.count(second_ca.writer_of) == 1
assert Enum.count(second_ca.cancelled_co_authored_posts) == 0
assert Enum.empty?(second_ca.editor_of) == true
assert Enum.empty?(second_ca.cancelled_co_authored_posts) == true

# Add third co-author
create_co_author_post(third_ca, first_post, :proof_reader)
Expand All @@ -184,9 +190,9 @@ defmodule AshPostgres.ManyToManyExprTest do
assert Enum.count(second_post.co_authors_unfiltered) == 3

assert Enum.count(third_ca.all_co_authored_posts) == 2
assert Enum.count(third_ca.editor_of) == 0
assert Enum.count(third_ca.writer_of) == 0
assert Enum.count(third_ca.cancelled_co_authored_posts) == 1
assert Enum.empty?(third_ca.editor_of) == true
assert Enum.empty?(third_ca.writer_of) == true

# Add fourth co-author
create_co_author_post(fourth_ca, first_post, :proof_reader)
Expand All @@ -205,8 +211,8 @@ defmodule AshPostgres.ManyToManyExprTest do

assert Enum.count(fourth_ca.all_co_authored_posts) == 2
assert Enum.count(fourth_ca.editor_of) == 1
assert Enum.count(fourth_ca.writer_of) == 0
assert Enum.count(fourth_ca.cancelled_co_authored_posts) == 2
assert Enum.empty?(fourth_ca.writer_of) == true
end
end
end
38 changes: 19 additions & 19 deletions test/support/resources/author.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,38 @@ defmodule AshPostgres.Test.Author do
end

has_many :credited_posts, AshPostgres.Test.CoAuthorPost do
public? true
public?(true)

destination_attribute :author_id
destination_attribute(:author_id)
end

many_to_many :all_co_authored_posts, AshPostgres.Test.Post do
public? true
join_relationship :credited_posts
source_attribute_on_join_resource :author_id
destination_attribute_on_join_resource :post_id
public?(true)
join_relationship(:credited_posts)
source_attribute_on_join_resource(:author_id)
destination_attribute_on_join_resource(:post_id)
end

many_to_many :writer_of, AshPostgres.Test.Post do
public? true
join_relationship :credited_posts
source_attribute_on_join_resource :author_id
destination_attribute_on_join_resource :post_id
filter expr(parent(credited_posts.role) == :writer)
public?(true)
join_relationship(:credited_posts)
source_attribute_on_join_resource(:author_id)
destination_attribute_on_join_resource(:post_id)
filter(expr(parent(credited_posts.role) == :writer))
end

many_to_many :editor_of, AshPostgres.Test.Post do
public? true
join_relationship :credited_posts
source_attribute_on_join_resource :author_id
destination_attribute_on_join_resource :post_id
filter expr(parent(credited_posts.role) == :editor)
public?(true)
join_relationship(:credited_posts)
source_attribute_on_join_resource(:author_id)
destination_attribute_on_join_resource(:post_id)
filter(expr(parent(credited_posts.role) == :editor))
end

many_to_many :cancelled_co_authored_posts, AshPostgres.Test.Post do
public? true
join_relationship :credited_posts
filter expr(not is_nil(parent(credited_posts.was_cancelled_at)))
public?(true)
join_relationship(:credited_posts)
filter(expr(not is_nil(parent(credited_posts.was_cancelled_at))))
end
end

Expand Down
34 changes: 17 additions & 17 deletions test/support/resources/co_authored_post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,51 @@ defmodule AshPostgres.Test.CoAuthorPost do

attributes do
attribute :role, :atom do
allow_nil? false
public? true
allow_nil?(false)
public?(true)

constraints one_of: [:editor, :writer, :proof_reader]
constraints(one_of: [:editor, :writer, :proof_reader])
end

attribute :was_cancelled_at, :datetime do
allow_nil? true
public? true
allow_nil?(true)
public?(true)
end
end

actions do
default_accept :*
default_accept(:*)

defaults [:read, :update, :destroy]
defaults([:read, :update, :destroy])

create :create do
end

update :cancel_author do
change set_attribute(:was_cancelled_at, DateTime.utc_now())
change(set_attribute(:was_cancelled_at, DateTime.utc_now()))
end

update :uncancel_author do
change set_attribute(:was_cancelled_at, nil)
change(set_attribute(:was_cancelled_at, nil))
end
end

code_interface do
define :cancel, action: :cancel_author
define :uncancel, action: :uncancel_author
define(:cancel, action: :cancel_author)
define(:uncancel, action: :uncancel_author)
end

relationships do
belongs_to :author, AshPostgres.Test.Author do
primary_key? true
public? true
allow_nil? false
primary_key?(true)
public?(true)
allow_nil?(false)
end

belongs_to :post, AshPostgres.Test.Post do
primary_key? true
public? true
allow_nil? false
primary_key?(true)
public?(true)
allow_nil?(false)
end
end
end
14 changes: 7 additions & 7 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -449,21 +449,21 @@ defmodule AshPostgres.Test.Post do
end

has_many :co_author_posts, AshPostgres.Test.CoAuthorPost do
public? true
public?(true)

destination_attribute :post_id
destination_attribute(:post_id)
end

many_to_many :co_authors, AshPostgres.Test.Author do
public? true
join_relationship :co_author_posts
public?(true)
join_relationship(:co_author_posts)

filter expr(is_nil(parent(co_author_posts.was_cancelled_at)))
filter(expr(is_nil(parent(co_author_posts.was_cancelled_at))))
end

many_to_many :co_authors_unfiltered, AshPostgres.Test.Author do
public? true
join_relationship :co_author_posts
public?(true)
join_relationship(:co_author_posts)
end

has_many :posts_with_matching_title, __MODULE__ do
Expand Down

0 comments on commit d934ef4

Please sign in to comment.