|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "tmpdir" |
| 5 | + |
| 6 | +module ProductTaxonomy |
| 7 | + class SyncEnLocalizationsCommandTest < 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/localizations/categories", @tmp_base_path)) |
| 13 | + FileUtils.mkdir_p(File.expand_path("data/localizations/attributes", @tmp_base_path)) |
| 14 | + FileUtils.mkdir_p(File.expand_path("data/localizations/values", @tmp_base_path)) |
| 15 | + ProductTaxonomy.stubs(:data_path).returns(File.expand_path("data", @tmp_base_path)) |
| 16 | + |
| 17 | + Command.any_instance.stubs(:load_taxonomy) |
| 18 | + end |
| 19 | + |
| 20 | + teardown do |
| 21 | + FileUtils.remove_entry(@tmp_base_path) |
| 22 | + end |
| 23 | + |
| 24 | + test "initialize sets targets to all permitted targets when not specified" do |
| 25 | + command = SyncEnLocalizationsCommand.new({}) |
| 26 | + assert_equal ["categories", "attributes", "values"], command.instance_variable_get(:@targets) |
| 27 | + end |
| 28 | + |
| 29 | + test "initialize accepts custom targets" do |
| 30 | + command = SyncEnLocalizationsCommand.new(targets: "categories,attributes") |
| 31 | + assert_equal ["categories", "attributes"], command.instance_variable_get(:@targets) |
| 32 | + end |
| 33 | + |
| 34 | + test "initialize raises error for invalid target" do |
| 35 | + assert_raises(RuntimeError) do |
| 36 | + SyncEnLocalizationsCommand.new(targets: "invalid,categories") |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + test "execute syncs categories localizations" do |
| 41 | + mock_localizations = { "test" => "data" } |
| 42 | + Serializers::Category::Data::LocalizationsSerializer.stubs(:serialize_all) |
| 43 | + .returns(mock_localizations) |
| 44 | + |
| 45 | + command = SyncEnLocalizationsCommand.new(targets: "categories") |
| 46 | + command.execute |
| 47 | + |
| 48 | + expected_path = File.expand_path("data/localizations/categories/en.yml", @tmp_base_path) |
| 49 | + assert File.exist?(expected_path) |
| 50 | + expected_content = "# This file is auto-generated. Do not edit directly.\n---\ntest: data\n" |
| 51 | + assert_equal expected_content, File.read(expected_path) |
| 52 | + end |
| 53 | + |
| 54 | + test "execute syncs attributes localizations" do |
| 55 | + mock_localizations = { "test" => "attribute_data" } |
| 56 | + Serializers::Attribute::Data::LocalizationsSerializer.stubs(:serialize_all) |
| 57 | + .returns(mock_localizations) |
| 58 | + |
| 59 | + command = SyncEnLocalizationsCommand.new(targets: "attributes") |
| 60 | + command.execute |
| 61 | + |
| 62 | + expected_path = File.expand_path("data/localizations/attributes/en.yml", @tmp_base_path) |
| 63 | + assert File.exist?(expected_path) |
| 64 | + expected_content = "# This file is auto-generated. Do not edit directly.\n---\ntest: attribute_data\n" |
| 65 | + assert_equal expected_content, File.read(expected_path) |
| 66 | + end |
| 67 | + |
| 68 | + test "execute syncs values localizations" do |
| 69 | + mock_localizations = { "test" => "value_data" } |
| 70 | + Serializers::Value::Data::LocalizationsSerializer.stubs(:serialize_all) |
| 71 | + .returns(mock_localizations) |
| 72 | + |
| 73 | + command = SyncEnLocalizationsCommand.new(targets: "values") |
| 74 | + command.execute |
| 75 | + |
| 76 | + expected_path = File.expand_path("data/localizations/values/en.yml", @tmp_base_path) |
| 77 | + assert File.exist?(expected_path) |
| 78 | + expected_content = "# This file is auto-generated. Do not edit directly.\n---\ntest: value_data\n" |
| 79 | + assert_equal expected_content, File.read(expected_path) |
| 80 | + end |
| 81 | + |
| 82 | + test "execute syncs all targets when none specified" do |
| 83 | + Serializers::Category::Data::LocalizationsSerializer.stubs(:serialize_all) |
| 84 | + .returns({ "category" => "data" }) |
| 85 | + Serializers::Attribute::Data::LocalizationsSerializer.stubs(:serialize_all) |
| 86 | + .returns({ "attribute" => "data" }) |
| 87 | + Serializers::Value::Data::LocalizationsSerializer.stubs(:serialize_all) |
| 88 | + .returns({ "value" => "data" }) |
| 89 | + |
| 90 | + command = SyncEnLocalizationsCommand.new({}) |
| 91 | + command.execute |
| 92 | + |
| 93 | + categories_path = File.expand_path("data/localizations/categories/en.yml", @tmp_base_path) |
| 94 | + attributes_path = File.expand_path("data/localizations/attributes/en.yml", @tmp_base_path) |
| 95 | + values_path = File.expand_path("data/localizations/values/en.yml", @tmp_base_path) |
| 96 | + |
| 97 | + assert File.exist?(categories_path) |
| 98 | + assert File.exist?(attributes_path) |
| 99 | + assert File.exist?(values_path) |
| 100 | + |
| 101 | + expected_categories = "# This file is auto-generated. Do not edit directly.\n---\ncategory: data\n" |
| 102 | + expected_attributes = "# This file is auto-generated. Do not edit directly.\n---\nattribute: data\n" |
| 103 | + expected_values = "# This file is auto-generated. Do not edit directly.\n---\nvalue: data\n" |
| 104 | + |
| 105 | + assert_equal expected_categories, File.read(categories_path) |
| 106 | + assert_equal expected_attributes, File.read(attributes_path) |
| 107 | + assert_equal expected_values, File.read(values_path) |
| 108 | + end |
| 109 | + end |
| 110 | +end |
0 commit comments