|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module ProductTaxonomy |
| 6 | + class AddValueCommandTest < TestCase |
| 7 | + setup do |
| 8 | + @attribute = Attribute.new( |
| 9 | + id: 1, |
| 10 | + name: "Color", |
| 11 | + description: "Defines the primary color or pattern", |
| 12 | + friendly_id: "color", |
| 13 | + handle: "color", |
| 14 | + values: [] |
| 15 | + ) |
| 16 | + @extended_attribute = ExtendedAttribute.new( |
| 17 | + name: "Clothing Color", |
| 18 | + description: "Color of the clothing", |
| 19 | + friendly_id: "clothing_color", |
| 20 | + handle: "clothing_color", |
| 21 | + values_from: @attribute |
| 22 | + ) |
| 23 | + @existing_value = Value.new( |
| 24 | + id: 1, |
| 25 | + name: "Black", |
| 26 | + friendly_id: "color__black", |
| 27 | + handle: "color__black" |
| 28 | + ) |
| 29 | + @attribute.add_value(@existing_value) |
| 30 | + |
| 31 | + Attribute.add(@attribute) |
| 32 | + Attribute.add(@extended_attribute) |
| 33 | + Value.add(@existing_value) |
| 34 | + |
| 35 | + AddValueCommand.any_instance.stubs(:load_taxonomy) |
| 36 | + DumpAttributesCommand.any_instance.stubs(:load_taxonomy) |
| 37 | + DumpValuesCommand.any_instance.stubs(:load_taxonomy) |
| 38 | + SyncEnLocalizationsCommand.any_instance.stubs(:load_taxonomy) |
| 39 | + GenerateDocsCommand.any_instance.stubs(:load_taxonomy) |
| 40 | + end |
| 41 | + |
| 42 | + test "execute successfully adds a new value to an attribute" do |
| 43 | + DumpAttributesCommand.any_instance.expects(:execute).once |
| 44 | + DumpValuesCommand.any_instance.expects(:execute).once |
| 45 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 46 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 47 | + |
| 48 | + AddValueCommand.new(name: "Blue", attribute_friendly_id: "color").execute |
| 49 | + |
| 50 | + new_value = @attribute.values.find { |v| v.name == "Blue" } |
| 51 | + assert_not_nil new_value |
| 52 | + assert_equal 2, new_value.id # Since id 1 already exists |
| 53 | + assert_equal "Blue", new_value.name |
| 54 | + assert_equal "color__blue", new_value.friendly_id |
| 55 | + assert_equal "color__blue", new_value.handle |
| 56 | + assert_not_nil Value.find_by(friendly_id: "color__blue") |
| 57 | + end |
| 58 | + |
| 59 | + test "execute raises error when attribute not found" do |
| 60 | + assert_raises(RuntimeError) do |
| 61 | + AddValueCommand.new(name: "Blue", attribute_friendly_id: "nonexistent").execute |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + test "execute raises error when trying to add value to extended attribute" do |
| 66 | + assert_raises(RuntimeError) do |
| 67 | + AddValueCommand.new(name: "Blue", attribute_friendly_id: "clothing_color").execute |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + test "execute raises error when value already exists" do |
| 72 | + assert_raises(ActiveModel::ValidationError) do |
| 73 | + AddValueCommand.new(name: "Black", attribute_friendly_id: "color").execute |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + test "execute warns when attribute has custom sorting" do |
| 78 | + @attribute.stubs(:manually_sorted?).returns(true) |
| 79 | + |
| 80 | + DumpAttributesCommand.any_instance.stubs(:execute) |
| 81 | + DumpValuesCommand.any_instance.stubs(:execute) |
| 82 | + SyncEnLocalizationsCommand.any_instance.stubs(:execute) |
| 83 | + GenerateDocsCommand.any_instance.stubs(:execute) |
| 84 | + |
| 85 | + logger = mock |
| 86 | + logger.expects(:info).once |
| 87 | + logger.expects(:warn).once.with(regexp_matches(/custom sorting/)) |
| 88 | + |
| 89 | + command = AddValueCommand.new(name: "Blue", attribute_friendly_id: "color") |
| 90 | + command.stubs(:logger).returns(logger) |
| 91 | + command.execute |
| 92 | + |
| 93 | + new_value = @attribute.values.find { |v| v.name == "Blue" } |
| 94 | + assert_not_nil new_value |
| 95 | + end |
| 96 | + |
| 97 | + test "execute generates correct friendly_id and handle" do |
| 98 | + DumpAttributesCommand.any_instance.stubs(:execute) |
| 99 | + DumpValuesCommand.any_instance.stubs(:execute) |
| 100 | + SyncEnLocalizationsCommand.any_instance.stubs(:execute) |
| 101 | + GenerateDocsCommand.any_instance.stubs(:execute) |
| 102 | + |
| 103 | + IdentifierFormatter.expects(:format_friendly_id).with("color__Multi Word").returns("color__multi_word") |
| 104 | + IdentifierFormatter.expects(:format_handle).with("color__multi_word").returns("color__multi_word") |
| 105 | + |
| 106 | + AddValueCommand.new(name: "Multi Word", attribute_friendly_id: "color").execute |
| 107 | + |
| 108 | + new_value = @attribute.values.find { |v| v.name == "Multi Word" } |
| 109 | + assert_not_nil new_value |
| 110 | + assert_equal "color__multi_word", new_value.friendly_id |
| 111 | + assert_equal "color__multi_word", new_value.handle |
| 112 | + end |
| 113 | + |
| 114 | + test "execute calls update_data_files! with correct options" do |
| 115 | + options = { name: "Blue", attribute_friendly_id: "color" } |
| 116 | + |
| 117 | + DumpAttributesCommand.expects(:new).with(options).returns(stub(execute: true)) |
| 118 | + DumpValuesCommand.expects(:new).with(options).returns(stub(execute: true)) |
| 119 | + SyncEnLocalizationsCommand.expects(:new).with(targets: "values").returns(stub(execute: true)) |
| 120 | + GenerateDocsCommand.expects(:new).with({}).returns(stub(execute: true)) |
| 121 | + |
| 122 | + AddValueCommand.new(options).execute |
| 123 | + end |
| 124 | + |
| 125 | + test "execute assigns sequential IDs correctly" do |
| 126 | + DumpAttributesCommand.any_instance.stubs(:execute) |
| 127 | + DumpValuesCommand.any_instance.stubs(:execute) |
| 128 | + SyncEnLocalizationsCommand.any_instance.stubs(:execute) |
| 129 | + GenerateDocsCommand.any_instance.stubs(:execute) |
| 130 | + |
| 131 | + AddValueCommand.new(name: "Blue", attribute_friendly_id: "color").execute |
| 132 | + AddValueCommand.new(name: "Green", attribute_friendly_id: "color").execute |
| 133 | + |
| 134 | + new_values = @attribute.values.select { |v| v.name != "Black" }.sort_by(&:id) |
| 135 | + assert_equal 2, new_values.size |
| 136 | + assert_equal 2, new_values[0].id |
| 137 | + assert_equal 3, new_values[1].id |
| 138 | + assert_equal "Blue", new_values[0].name |
| 139 | + assert_equal "Green", new_values[1].name |
| 140 | + assert_not_nil Value.find_by(friendly_id: "color__blue") |
| 141 | + assert_not_nil Value.find_by(friendly_id: "color__green") |
| 142 | + end |
| 143 | + end |
| 144 | +end |
0 commit comments