2023-10-22 15:37:46 +13:00
|
|
|
defmodule AshJsonApiWrapper.Petstore.Test do
|
|
|
|
use ExUnit.Case
|
|
|
|
require Ash.Query
|
|
|
|
@moduletag :petstore
|
|
|
|
|
|
|
|
defmodule TestingTesla do
|
|
|
|
use Tesla
|
|
|
|
# plug Tesla.Middleware.Logger
|
|
|
|
end
|
|
|
|
|
2023-10-26 15:08:42 +13:00
|
|
|
defmodule Petstore do
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: AshJsonApiWrapper.DataLayer,
|
2024-05-25 23:07:39 +12:00
|
|
|
domain: AshJsonApiWrapper.Petstore.Test.Domain,
|
|
|
|
validate_domain_inclusion?: false
|
2023-10-22 15:37:46 +13:00
|
|
|
|
|
|
|
json_api_wrapper do
|
|
|
|
tesla(TestingTesla)
|
|
|
|
|
|
|
|
endpoints do
|
|
|
|
base("https://petstore3.swagger.io/api/v3")
|
|
|
|
|
2023-10-26 15:08:42 +13:00
|
|
|
endpoint [:find_pets_by_status, :by_status] do
|
2023-10-22 15:37:46 +13:00
|
|
|
path("/pet/findByStatus")
|
|
|
|
|
|
|
|
field :status do
|
|
|
|
filter_handler(:simple)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get_endpoint :pet, :id do
|
|
|
|
path("/pet/:id")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
fields do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
|
|
|
read(:find_pets_by_status) do
|
2023-10-26 15:08:42 +13:00
|
|
|
primary? false
|
2023-10-22 15:37:46 +13:00
|
|
|
end
|
|
|
|
|
2023-10-26 15:08:42 +13:00
|
|
|
read(:by_status) do
|
|
|
|
primary? true
|
2023-10-22 15:37:46 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
read(:pet) do
|
|
|
|
primary? false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
attribute :id, :integer do
|
|
|
|
primary_key?(true)
|
|
|
|
allow_nil?(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
# attribute(:category, :string)
|
|
|
|
attribute(:name, :string)
|
|
|
|
attribute(:photo_urls, :string)
|
|
|
|
|
|
|
|
attribute :status, :atom do
|
|
|
|
constraints(one_of: [:available, :pending, :sold])
|
|
|
|
end
|
|
|
|
|
|
|
|
# attribute(:tags, :string)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-25 23:07:39 +12:00
|
|
|
defmodule Domain do
|
|
|
|
use Ash.Domain, validate_config_inclusion?: false
|
2023-10-22 15:37:46 +13:00
|
|
|
|
|
|
|
resources do
|
|
|
|
allow_unregistered?(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it works" do
|
2023-10-26 15:08:42 +13:00
|
|
|
Petstore
|
2023-10-22 15:37:46 +13:00
|
|
|
|> Ash.Query.for_read(:find_pets_by_status)
|
|
|
|
|> Ash.Query.filter(status == "pending")
|
2024-05-25 23:07:39 +12:00
|
|
|
|> Ash.read!()
|
2023-10-22 15:37:46 +13:00
|
|
|
|
2023-10-26 15:08:42 +13:00
|
|
|
Petstore
|
|
|
|
|> Ash.Query.for_read(:by_status)
|
2023-10-22 15:37:46 +13:00
|
|
|
|> Ash.Query.filter(status == "available")
|
2024-05-25 23:07:39 +12:00
|
|
|
|> Ash.read!()
|
2023-10-22 15:37:46 +13:00
|
|
|
|
2023-10-26 15:08:42 +13:00
|
|
|
Petstore
|
2023-10-22 15:37:46 +13:00
|
|
|
|> Ash.Query.for_read(:pet)
|
2023-10-26 15:08:42 +13:00
|
|
|
|> Ash.Query.filter(id == 10)
|
2024-05-25 23:07:39 +12:00
|
|
|
|> Ash.read!()
|
2023-10-22 15:37:46 +13:00
|
|
|
end
|
|
|
|
end
|