Skip to content

Commit

Permalink
base_image: add localized attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Binetti <[email protected]>
  • Loading branch information
rbino committed May 24, 2024
1 parent ab64b8e commit 0c07802
Show file tree
Hide file tree
Showing 4 changed files with 386 additions and 4 deletions.
73 changes: 71 additions & 2 deletions backend/lib/edgehog/base_images/base_image/base_image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Edgehog.BaseImages.BaseImage do
]

alias Edgehog.BaseImages.BaseImage.Changes
alias Edgehog.Localization
alias Edgehog.Validations

resource do
Expand All @@ -39,6 +40,9 @@ defmodule Edgehog.BaseImages.BaseImage do
graphql do
type :base_image

field_names localized_description: :description,
localized_release_display_name: :release_display_name

queries do
get :base_image, :get
end
Expand Down Expand Up @@ -70,8 +74,23 @@ defmodule Edgehog.BaseImages.BaseImage do
allow_nil? false
end

argument :localized_descriptions, {:array, Localization.LocalizedAttribute} do
description "A list of descriptions in different languages."
end

argument :localized_release_display_names, {:array, Localization.LocalizedAttribute} do
description "A list of release display names in different languages."
end

change Changes.HandleFileUpload
change manage_relationship(:base_image_collection_id, :base_image_collection, type: :append)

change {Localization.Changes.UpsertLocalizedAttribute,
input_argument: :localized_descriptions, target_attribute: :description}

change {Localization.Changes.UpsertLocalizedAttribute,
input_argument: :localized_release_display_names,
target_attribute: :release_display_name}
end

create :create_fixture do
Expand All @@ -82,7 +101,22 @@ defmodule Edgehog.BaseImages.BaseImage do
allow_nil? false
end

argument :localized_descriptions, {:array, Localization.LocalizedAttribute} do
description "A list of descriptions in different languages."
end

argument :localized_release_display_names, {:array, Localization.LocalizedAttribute} do
description "A list of release display names in different languages."
end

change manage_relationship(:base_image_collection_id, :base_image_collection, type: :append)

change {Localization.Changes.UpsertLocalizedAttribute,
input_argument: :localized_descriptions, target_attribute: :description}

change {Localization.Changes.UpsertLocalizedAttribute,
input_argument: :localized_release_display_names,
target_attribute: :release_display_name}
end

read :get do
Expand All @@ -99,7 +133,26 @@ defmodule Edgehog.BaseImages.BaseImage do
description "Updates a base image."
primary? true

# Needed because UpsertLocalizedAttribute is not atomic
require_atomic? false

argument :localized_descriptions, {:array, Localization.LocalizedAttributeUpdateInput} do
description "A list of descriptions in different languages."
end

argument :localized_release_display_names,
{:array, Localization.LocalizedAttributeUpdateInput} do
description "A list of release display names in different languages."
end

accept [:starting_version_requirement]

change {Localization.Changes.UpsertLocalizedAttribute,
input_argument: :localized_descriptions, target_attribute: :description}

change {Localization.Changes.UpsertLocalizedAttribute,
input_argument: :localized_release_display_names,
target_attribute: :release_display_name}
end

destroy :destroy do
Expand Down Expand Up @@ -142,8 +195,8 @@ defmodule Edgehog.BaseImages.BaseImage do
allow_nil? false
end

# TODO: localized description
# TODO: localized release_display_name
attribute :description, :map
attribute :release_display_name, :map

create_timestamp :inserted_at
update_timestamp :updated_at
Expand All @@ -158,6 +211,22 @@ defmodule Edgehog.BaseImages.BaseImage do
end
end

calculations do
calculate :localized_descriptions, {:array, Localization.LocalizedAttribute} do
public? true
description "A list of descriptions in different languages."
calculation {Localization.Calculations.LocalizedAttribute, attribute: :description}
argument :preferred_language_tags, {:array, :string}
end

calculate :localized_release_display_names, {:array, Localization.LocalizedAttribute} do
public? true
description "A list of release display names in different languages."
calculation {Localization.Calculations.LocalizedAttribute, attribute: :release_display_name}
argument :preferred_language_tags, {:array, :string}
end
end

identities do
# These have to be named this way to match the existing unique indexes
# we already have. Ash uses identities to add a `unique_constraint` to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,52 @@ defmodule EdgehogWeb.Schema.Mutation.CreateBaseImageTest do
} = base_image
end

test "allows passing localized descriptions", %{tenant: tenant} do
localized_descriptions = [
%{"languageTag" => "en", "value" => "My Base Image"},
%{"languageTag" => "it", "value" => "La mia Base Image"}
]

result =
create_base_image_mutation(
tenant: tenant,
localized_descriptions: localized_descriptions
)

assert %{"localizedDescriptions" => localized_descriptions} = extract_result!(result)
assert length(localized_descriptions) == 2
assert %{"languageTag" => "en", "value" => "My Base Image"} in localized_descriptions
assert %{"languageTag" => "it", "value" => "La mia Base Image"} in localized_descriptions
end

test "allows passing localized release display names", %{tenant: tenant} do
localized_release_display_names = [
%{"languageTag" => "en", "value" => "Initial version"},
%{"languageTag" => "it", "value" => "Versione iniziale"}
]

result =
create_base_image_mutation(
tenant: tenant,
localized_release_display_names: localized_release_display_names
)

assert %{"localizedReleaseDisplayNames" => localized_release_display_names} =
extract_result!(result)

assert length(localized_release_display_names) == 2

assert %{
"languageTag" => "en",
"value" => "Initial version"
} in localized_release_display_names

assert %{
"languageTag" => "it",
"value" => "Versione iniziale"
} in localized_release_display_names
end

test "returns error for non-existing base image collection", %{tenant: tenant} do
base_image_collection = base_image_collection_fixture(tenant: tenant)
base_image_collection_id = AshGraphql.Resource.encode_relay_id(base_image_collection)
Expand Down Expand Up @@ -193,6 +239,14 @@ defmodule EdgehogWeb.Schema.Mutation.CreateBaseImageTest do
id
version
url
localizedDescriptions {
languageTag
value
}
localizedReleaseDisplayNames {
languageTag
value
}
startingVersionRequirement
baseImageCollection {
id
Expand Down Expand Up @@ -220,6 +274,8 @@ defmodule EdgehogWeb.Schema.Mutation.CreateBaseImageTest do
input = %{
"baseImageCollectionId" => base_image_collection_id,
"version" => version,
"localizedDescriptions" => opts[:localized_descriptions],
"localizedReleaseDisplayNames" => opts[:localized_release_display_names],
"startingVersionRequirement" => opts[:starting_version_requirement],
"file" => file && "file"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,81 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateBaseImageTest do
} = base_image
end

test "allows updating localized descriptions", %{tenant: tenant} do
initial_localized_descriptions = [
%{language_tag: "en", value: "Description"},
%{language_tag: "it", value: "Descrizione"}
]

fixture =
base_image_fixture(
tenant: tenant,
localized_descriptions: initial_localized_descriptions
)

id = AshGraphql.Resource.encode_relay_id(fixture)

updated_localized_descriptions = [
# nil value, so it will be removed
%{"languageTag" => "en", "value" => nil},
%{"languageTag" => "it", "value" => "Nuova descrizione"},
%{"languageTag" => "bs", "value" => "Opis"}
]

result =
update_base_image_mutation(
tenant: tenant,
id: id,
localized_descriptions: updated_localized_descriptions
)

assert %{"localizedDescriptions" => localized_descriptions} = extract_result!(result)
assert length(localized_descriptions) == 2
assert %{"languageTag" => "it", "value" => "Nuova descrizione"} in localized_descriptions
assert %{"languageTag" => "bs", "value" => "Opis"} in localized_descriptions
end

test "allows updating localized release display names", %{tenant: tenant} do
initial_localized_release_display_names = [
%{language_tag: "en", value: "Release display name"},
%{language_tag: "it", value: "Nome del rilascio"}
]

fixture =
base_image_fixture(
tenant: tenant,
localized_release_display_names: initial_localized_release_display_names
)

id = AshGraphql.Resource.encode_relay_id(fixture)

updated_localized_release_display_names = [
# nil value, so it will be removed
%{"languageTag" => "en", "value" => nil},
%{"languageTag" => "it", "value" => "Nuovo nome del rilascio"},
%{"languageTag" => "bs", "value" => "Ime"}
]

result =
update_base_image_mutation(
tenant: tenant,
id: id,
localized_release_display_names: updated_localized_release_display_names
)

assert %{"localizedReleaseDisplayNames" => localized_release_display_names} =
extract_result!(result)

assert length(localized_release_display_names) == 2

assert %{
"languageTag" => "it",
"value" => "Nuovo nome del rilascio"
} in localized_release_display_names

assert %{"languageTag" => "bs", "value" => "Ime"} in localized_release_display_names
end

test "returns error for invalid starting version requirement", %{
tenant: tenant,
base_image: base_image,
Expand Down Expand Up @@ -94,6 +169,14 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateBaseImageTest do
id
version
url
localizedDescriptions {
languageTag
value
}
localizedReleaseDisplayNames {
languageTag
value
}
startingVersionRequirement
baseImageCollection {
id
Expand All @@ -109,7 +192,9 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateBaseImageTest do

input =
%{
"startingVersionRequirement" => opts[:starting_version_requirement]
"startingVersionRequirement" => opts[:starting_version_requirement],
"localizedDescriptions" => opts[:localized_descriptions],
"localizedReleaseDisplayNames" => opts[:localized_release_display_names]
}
|> Enum.filter(fn {_k, v} -> v != nil end)
|> Enum.into(%{})
Expand Down
Loading

0 comments on commit 0c07802

Please sign in to comment.