|
| 1 | +# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +defmodule AshPostgres.DataLayerMetadataTest do |
| 6 | + use AshPostgres.RepoCase, async: false |
| 7 | + alias AshPostgres.Test.Post |
| 8 | + |
| 9 | + require Ash.Query |
| 10 | + |
| 11 | + test "update_query sets bulk metadata on returned records" do |
| 12 | + _post1 = Ash.create!(Post, %{title: "title1"}) |
| 13 | + _post2 = Ash.create!(Post, %{title: "title2"}) |
| 14 | + _post3 = Ash.create!(Post, %{title: "title3"}) |
| 15 | + |
| 16 | + ash_query = Ash.Query.new(Post) |
| 17 | + {:ok, query} = Ash.Query.data_layer_query(ash_query) |
| 18 | + |
| 19 | + ref = make_ref() |
| 20 | + |
| 21 | + changeset = %Ash.Changeset{ |
| 22 | + resource: Post, |
| 23 | + action_type: :update, |
| 24 | + data: %Post{}, |
| 25 | + attributes: %{title: "updated"}, |
| 26 | + atomics: [], |
| 27 | + filter: nil, |
| 28 | + context: %{bulk_update: %{index: 0, ref: ref}}, |
| 29 | + domain: AshPostgres.Test.Domain, |
| 30 | + tenant: nil, |
| 31 | + timeout: :infinity |
| 32 | + } |
| 33 | + |
| 34 | + {:ok, results} = |
| 35 | + AshPostgres.DataLayer.update_query(query, changeset, Post, return_records?: true) |
| 36 | + |
| 37 | + assert is_list(results) |
| 38 | + assert length(results) > 0 |
| 39 | + |
| 40 | + Enum.each(results, fn result -> |
| 41 | + assert is_integer(result.__metadata__.bulk_update_index) |
| 42 | + assert result.__metadata__.bulk_action_ref == ref |
| 43 | + end) |
| 44 | + end |
| 45 | + |
| 46 | + test "destroy_query sets bulk metadata on returned records" do |
| 47 | + _post1 = Ash.create!(Post, %{title: "title1"}) |
| 48 | + _post2 = Ash.create!(Post, %{title: "title2"}) |
| 49 | + _post3 = Ash.create!(Post, %{title: "title3"}) |
| 50 | + |
| 51 | + ash_query = Ash.Query.new(Post) |
| 52 | + {:ok, query} = Ash.Query.data_layer_query(ash_query) |
| 53 | + |
| 54 | + ref = make_ref() |
| 55 | + |
| 56 | + changeset = %Ash.Changeset{ |
| 57 | + resource: Post, |
| 58 | + action_type: :destroy, |
| 59 | + data: %Post{}, |
| 60 | + attributes: %{}, |
| 61 | + atomics: [], |
| 62 | + filter: nil, |
| 63 | + context: %{bulk_destroy: %{index: 0, ref: ref}}, |
| 64 | + domain: AshPostgres.Test.Domain, |
| 65 | + tenant: nil, |
| 66 | + timeout: :infinity |
| 67 | + } |
| 68 | + |
| 69 | + {:ok, results} = |
| 70 | + AshPostgres.DataLayer.destroy_query(query, changeset, Post, return_records?: true) |
| 71 | + |
| 72 | + assert is_list(results) |
| 73 | + assert length(results) > 0 |
| 74 | + |
| 75 | + Enum.each(results, fn result -> |
| 76 | + assert is_integer(result.__metadata__.bulk_destroy_index) |
| 77 | + assert result.__metadata__.bulk_action_ref == ref |
| 78 | + end) |
| 79 | + end |
| 80 | +end |
0 commit comments