Skip to content

Commit

Permalink
Fix dist sort order for localized Values
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpgross committed Jan 16, 2025
1 parent 4f11191 commit 8ee4086
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
14 changes: 10 additions & 4 deletions dev/lib/product_taxonomy/models/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def load_from_source(source_data)
def to_json(version:, locale: "en")
{
"version" => version,
"values" => all_values_sorted(locale:).map { _1.to_json(locale:) },
"values" => all_values_sorted.map { _1.to_json(locale:) },
}
end

Expand All @@ -54,7 +54,7 @@ def to_txt(version:, locale: "en", padding: longest_gid_length)
HEADER
[
header,
*all_values_sorted(locale:).map { _1.to_txt(padding:, locale:) },
*all_values_sorted.map { _1.to_txt(padding:, locale:) },
].join("\n")
end

Expand Down Expand Up @@ -86,8 +86,14 @@ def longest_gid_length
find_by(id: largest_id).gid.length
end

def all_values_sorted(locale: "en")
all.sort_by { |value| [value.name(locale:) == "Other" ? 1 : 0, value.name(locale:), value.id] }
def all_values_sorted
all.sort_by do |value|
[
value.name(locale: "en").downcase == "other" ? 1 : 0,
value.name(locale: "en"),
value.id,
]
end
end
end

Expand Down
88 changes: 83 additions & 5 deletions dev/test/models/value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ class ValueTest < TestCase
assert_equal expected_json, Value.to_json(version: "1.0")
end

test "Value.to_json returns the JSON representation of all values sorted by name" do
test "Value.to_json returns the JSON representation of all values sorted by name with 'Other' at the end" do
add_values_for_sorting
expected_json = {
"version" => "1.0",
"values" => [
{
"id" => "gid://shopify/TaxonomyValue/3",
"id" => "gid://shopify/TaxonomyValue/4",
"name" => "Aaaa",
"handle" => "color__aaa",
},
Expand All @@ -241,11 +241,48 @@ class ValueTest < TestCase
"name" => "Cccc",
"handle" => "color__ccc",
},
{
"id" => "gid://shopify/TaxonomyValue/3",
"name" => "Other",
"handle" => "color__other",
},
],
}
assert_equal expected_json, Value.to_json(version: "1.0")
end

test "Value.to_json sorts localized values according to their sort order in English" do
add_values_for_sorting
stub_localizations_for_sorting

expected_json = {
"version" => "1.0",
"values" => [
{
"id" => "gid://shopify/TaxonomyValue/4",
"name" => "Zzzz",
"handle" => "color__aaa",
},
{
"id" => "gid://shopify/TaxonomyValue/2",
"name" => "Xxxx",
"handle" => "color__bbb",
},
{
"id" => "gid://shopify/TaxonomyValue/1",
"name" => "Yyyy",
"handle" => "color__ccc",
},
{
"id" => "gid://shopify/TaxonomyValue/3",
"name" => "Autre",
"handle" => "color__other",
},
],
}
assert_equal expected_json, Value.to_json(version: "1.0", locale: "fr")
end

test "to_txt returns the text representation of the value" do
expected_txt = "gid://shopify/TaxonomyValue/1 : Black [Color]"
assert_equal expected_txt, @value.to_txt
Expand Down Expand Up @@ -273,20 +310,37 @@ class ValueTest < TestCase
assert_equal expected_txt.strip, Value.to_txt(version: "1.0")
end

test "Value.to_txt returns the text representation of all values sorted by name" do
test "Value.to_txt returns the text representation of all values sorted by name with 'Other' at the end" do
add_values_for_sorting

expected_txt = <<~TXT
# Shopify Product Taxonomy - Attribute Values: 1.0
# Format: {GID} : {Value name} [{Attribute name}]
gid://shopify/TaxonomyValue/3 : Aaaa [Color]
gid://shopify/TaxonomyValue/4 : Aaaa [Color]
gid://shopify/TaxonomyValue/2 : Bbbb [Color]
gid://shopify/TaxonomyValue/1 : Cccc [Color]
gid://shopify/TaxonomyValue/3 : Other [Color]
TXT
assert_equal expected_txt.strip, Value.to_txt(version: "1.0")
end

test "Value.to_txt sorts localized values according to their sort order in English" do
add_values_for_sorting
stub_localizations_for_sorting

expected_txt = <<~TXT
# Shopify Product Taxonomy - Attribute Values: 1.0
# Format: {GID} : {Value name} [{Attribute name}]
gid://shopify/TaxonomyValue/4 : Zzzz [Color]
gid://shopify/TaxonomyValue/2 : Xxxx [Color]
gid://shopify/TaxonomyValue/1 : Yyyy [Color]
gid://shopify/TaxonomyValue/3 : Autre [Color]
TXT
assert_equal expected_txt.strip, Value.to_txt(version: "1.0", locale: "fr")
end

test "Value.sort_by_localized_name sorts values alphanumerically" do
values = stub_color_values

Expand Down Expand Up @@ -345,10 +399,34 @@ def add_values_for_sorting
[
Value.new(id: 1, name: "Cccc", friendly_id: "color__ccc", handle: "color__ccc"),
Value.new(id: 2, name: "Bbbb", friendly_id: "color__bbb", handle: "color__bbb"),
Value.new(id: 3, name: "Aaaa", friendly_id: "color__aaa", handle: "color__aaa"),
Value.new(id: 3, name: "Other", friendly_id: "color__other", handle: "color__other"),
Value.new(id: 4, name: "Aaaa", friendly_id: "color__aaa", handle: "color__aaa"),
].each { Value.add(_1) }
end

def stub_localizations_for_sorting
fr_yaml = <<~YAML
fr:
values:
color__aaa:
name: "Zzzz"
color__bbb:
name: "Xxxx"
color__ccc:
name: "Yyyy"
color__other:
name: "Autre"
YAML
Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "values", "*.yml"))
.returns(["fake/path/fr.yml"])
YAML.stubs(:safe_load_file).with("fake/path/fr.yml").returns(YAML.safe_load(fr_yaml))

Dir.stubs(:glob)
.with(File.join(DATA_PATH, "localizations", "attributes", "*.yml"))
.returns([])
end

def stub_color_values
fr_yaml = <<~YAML
fr:
Expand Down

0 comments on commit 8ee4086

Please sign in to comment.