Skip to content

Commit 4440786

Browse files
authored
Migrate dump_values command to new tool (#575)
2 parents 231894c + f77a852 commit 4440786

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

dev/lib/product_taxonomy.rb

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def data_path = DATA_PATH
3939
require_relative "product_taxonomy/models/serializers/attribute/docs/search_serializer"
4040
require_relative "product_taxonomy/models/serializers/attribute/dist/json_serializer"
4141
require_relative "product_taxonomy/models/serializers/attribute/dist/txt_serializer"
42+
require_relative "product_taxonomy/models/serializers/value/data/data_serializer"
4243
require_relative "product_taxonomy/models/serializers/value/data/localizations_serializer"
4344
require_relative "product_taxonomy/models/serializers/value/dist/json_serializer"
4445
require_relative "product_taxonomy/models/serializers/value/dist/txt_serializer"
@@ -49,5 +50,6 @@ def data_path = DATA_PATH
4950
require_relative "product_taxonomy/commands/generate_release_command"
5051
require_relative "product_taxonomy/commands/dump_categories_command"
5152
require_relative "product_taxonomy/commands/dump_attributes_command"
53+
require_relative "product_taxonomy/commands/dump_values_command"
5254
require_relative "product_taxonomy/commands/sync_en_localizations_command"
5355
require_relative "product_taxonomy/commands/add_category_command"

dev/lib/product_taxonomy/cli.rb

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ def dump_attributes
5151
DumpAttributesCommand.new(options).run
5252
end
5353

54+
desc "dump_values", "Dump values to YAML file"
55+
def dump_values
56+
DumpValuesCommand.new(options).run
57+
end
58+
5459
desc "sync_en_localizations", "Sync English localizations for categories, attributes, and values"
5560
option :targets, type: :string, desc: "List of targets to sync. Valid targets are: categories, attributes, values"
5661
def sync_en_localizations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module ProductTaxonomy
4+
class DumpValuesCommand < Command
5+
def execute
6+
logger.info("Dumping values...")
7+
8+
load_taxonomy
9+
10+
path = File.expand_path("values.yml", ProductTaxonomy.data_path)
11+
FileUtils.mkdir_p(File.dirname(path))
12+
13+
data = Serializers::Value::Data::DataSerializer.serialize_all
14+
File.write(path, YAML.dump(data, line_width: -1))
15+
16+
logger.info("Updated `#{path}`")
17+
end
18+
end
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module ProductTaxonomy
4+
module Serializers
5+
module Value
6+
module Data
7+
module DataSerializer
8+
class << self
9+
def serialize_all
10+
ProductTaxonomy::Value.all.sort_by(&:id).map { serialize(_1) }
11+
end
12+
13+
# @param [Value] value
14+
# @return [Hash]
15+
def serialize(value)
16+
{
17+
"id" => value.id,
18+
"name" => value.name,
19+
"friendly_id" => value.friendly_id,
20+
"handle" => value.handle,
21+
}
22+
end
23+
end
24+
end
25+
end
26+
end
27+
end
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "tmpdir"
5+
6+
module ProductTaxonomy
7+
class DumpValuesCommandTest < TestCase
8+
setup do
9+
@tmp_base_path = Dir.mktmpdir
10+
@real_base_path = File.expand_path("..", ProductTaxonomy.data_path)
11+
12+
FileUtils.mkdir_p(File.expand_path("data", @tmp_base_path))
13+
ProductTaxonomy.stubs(:data_path).returns(File.expand_path("data", @tmp_base_path))
14+
15+
Command.any_instance.stubs(:load_taxonomy)
16+
end
17+
18+
teardown do
19+
FileUtils.remove_entry(@tmp_base_path)
20+
end
21+
22+
test "execute dumps values to YAML file" do
23+
mock_data = [{ "id" => 1, "name" => "Test Value", "friendly_id" => "test__value", "handle" => "test__value" }]
24+
Serializers::Value::Data::DataSerializer.stubs(:serialize_all)
25+
.returns(mock_data)
26+
27+
command = DumpValuesCommand.new({})
28+
command.execute
29+
30+
expected_path = File.expand_path("data/values.yml", @tmp_base_path)
31+
expected_content = "---\n- id: 1\n name: Test Value\n friendly_id: test__value\n handle: test__value\n"
32+
assert File.exist?(expected_path)
33+
assert_equal expected_content, File.read(expected_path)
34+
end
35+
end
36+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module ProductTaxonomy
6+
module Serializers
7+
module Value
8+
module Data
9+
class DataSerializerTest < TestCase
10+
setup do
11+
@value1 = ProductTaxonomy::Value.new(
12+
id: 1,
13+
name: "Black",
14+
friendly_id: "color__black",
15+
handle: "color__black"
16+
)
17+
@value2 = ProductTaxonomy::Value.new(
18+
id: 2,
19+
name: "Red",
20+
friendly_id: "color__red",
21+
handle: "color__red"
22+
)
23+
@value3 = ProductTaxonomy::Value.new(
24+
id: 3,
25+
name: "Blue",
26+
friendly_id: "color__blue",
27+
handle: "color__blue"
28+
)
29+
30+
ProductTaxonomy::Value.add(@value1)
31+
ProductTaxonomy::Value.add(@value2)
32+
ProductTaxonomy::Value.add(@value3)
33+
end
34+
35+
test "serialize returns the expected data structure for a value" do
36+
expected = {
37+
"id" => 1,
38+
"name" => "Black",
39+
"friendly_id" => "color__black",
40+
"handle" => "color__black"
41+
}
42+
43+
assert_equal expected, DataSerializer.serialize(@value1)
44+
end
45+
46+
test "serialize_all returns all values in data format" do
47+
expected = [
48+
{
49+
"id" => 1,
50+
"name" => "Black",
51+
"friendly_id" => "color__black",
52+
"handle" => "color__black"
53+
},
54+
{
55+
"id" => 2,
56+
"name" => "Red",
57+
"friendly_id" => "color__red",
58+
"handle" => "color__red"
59+
},
60+
{
61+
"id" => 3,
62+
"name" => "Blue",
63+
"friendly_id" => "color__blue",
64+
"handle" => "color__blue"
65+
}
66+
]
67+
68+
assert_equal expected, DataSerializer.serialize_all
69+
end
70+
71+
test "serialize_all sorts values by ID" do
72+
# Add values in random order to ensure sorting is by ID
73+
ProductTaxonomy::Value.reset
74+
ProductTaxonomy::Value.add(@value3)
75+
ProductTaxonomy::Value.add(@value1)
76+
ProductTaxonomy::Value.add(@value2)
77+
78+
result = DataSerializer.serialize_all
79+
80+
assert_equal 3, result.length
81+
assert_equal "color__black", result[0]["friendly_id"]
82+
assert_equal "color__red", result[1]["friendly_id"]
83+
assert_equal "color__blue", result[2]["friendly_id"]
84+
end
85+
end
86+
end
87+
end
88+
end
89+
end

0 commit comments

Comments
 (0)