|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module ProductTaxonomy |
| 6 | + class AddAttributesToCategoriesCommandTest < TestCase |
| 7 | + setup do |
| 8 | + @color_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 | + Value.new( |
| 16 | + id: 1, |
| 17 | + name: "Black", |
| 18 | + friendly_id: "color__black", |
| 19 | + handle: "color__black", |
| 20 | + ), |
| 21 | + ], |
| 22 | + ) |
| 23 | + @size_attribute = Attribute.new( |
| 24 | + id: 2, |
| 25 | + name: "Size", |
| 26 | + description: "Defines the size of the product", |
| 27 | + friendly_id: "size", |
| 28 | + handle: "size", |
| 29 | + values: [ |
| 30 | + Value.new( |
| 31 | + id: 2, |
| 32 | + name: "Small", |
| 33 | + friendly_id: "size__small", |
| 34 | + handle: "size__small", |
| 35 | + ), |
| 36 | + ], |
| 37 | + ) |
| 38 | + |
| 39 | + Attribute.add(@color_attribute) |
| 40 | + Attribute.add(@size_attribute) |
| 41 | + Value.add(@color_attribute.values.first) |
| 42 | + Value.add(@size_attribute.values.first) |
| 43 | + |
| 44 | + @root = Category.new(id: "aa", name: "Apparel & Accessories") |
| 45 | + @clothing = Category.new(id: "aa-1", name: "Clothing") |
| 46 | + @shirts = Category.new(id: "aa-1-1", name: "Shirts") |
| 47 | + @root.add_child(@clothing) |
| 48 | + @clothing.add_child(@shirts) |
| 49 | + |
| 50 | + Category.add(@root) |
| 51 | + Category.add(@clothing) |
| 52 | + Category.add(@shirts) |
| 53 | + |
| 54 | + AddAttributesToCategoriesCommand.any_instance.stubs(:load_taxonomy) |
| 55 | + DumpCategoriesCommand.any_instance.stubs(:load_taxonomy) |
| 56 | + SyncEnLocalizationsCommand.any_instance.stubs(:load_taxonomy) |
| 57 | + GenerateDocsCommand.any_instance.stubs(:load_taxonomy) |
| 58 | + end |
| 59 | + |
| 60 | + test "execute adds attributes to specified categories" do |
| 61 | + DumpCategoriesCommand.any_instance.expects(:execute).once |
| 62 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 63 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 64 | + |
| 65 | + AddAttributesToCategoriesCommand.new( |
| 66 | + attribute_friendly_ids: "color,size", |
| 67 | + category_ids: "aa-1", |
| 68 | + include_descendants: false |
| 69 | + ).execute |
| 70 | + |
| 71 | + assert_equal 2, @clothing.attributes.size |
| 72 | + assert_includes @clothing.attributes, @color_attribute |
| 73 | + assert_includes @clothing.attributes, @size_attribute |
| 74 | + |
| 75 | + assert_empty @root.attributes |
| 76 | + assert_empty @shirts.attributes |
| 77 | + end |
| 78 | + |
| 79 | + test "execute adds attributes to categories and their descendants when include_descendants is true" do |
| 80 | + DumpCategoriesCommand.any_instance.expects(:execute).once |
| 81 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 82 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 83 | + |
| 84 | + AddAttributesToCategoriesCommand.new( |
| 85 | + attribute_friendly_ids: "color", |
| 86 | + category_ids: "aa-1", |
| 87 | + include_descendants: true |
| 88 | + ).execute |
| 89 | + |
| 90 | + assert_equal 1, @clothing.attributes.size |
| 91 | + assert_includes @clothing.attributes, @color_attribute |
| 92 | + |
| 93 | + assert_equal 1, @shirts.attributes.size |
| 94 | + assert_includes @shirts.attributes, @color_attribute |
| 95 | + |
| 96 | + assert_empty @root.attributes |
| 97 | + end |
| 98 | + |
| 99 | + test "execute adds attributes to multiple categories" do |
| 100 | + DumpCategoriesCommand.any_instance.expects(:execute).once |
| 101 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 102 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 103 | + |
| 104 | + AddAttributesToCategoriesCommand.new( |
| 105 | + attribute_friendly_ids: "color", |
| 106 | + category_ids: "aa,aa-1", |
| 107 | + include_descendants: false |
| 108 | + ).execute |
| 109 | + |
| 110 | + assert_equal 1, @root.attributes.size |
| 111 | + assert_includes @root.attributes, @color_attribute |
| 112 | + |
| 113 | + assert_equal 1, @clothing.attributes.size |
| 114 | + assert_includes @clothing.attributes, @color_attribute |
| 115 | + |
| 116 | + assert_empty @shirts.attributes |
| 117 | + end |
| 118 | + |
| 119 | + test "execute skips adding attributes that are already present" do |
| 120 | + @clothing.add_attribute(@color_attribute) |
| 121 | + |
| 122 | + DumpCategoriesCommand.any_instance.expects(:execute).once |
| 123 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 124 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 125 | + |
| 126 | + AddAttributesToCategoriesCommand.new( |
| 127 | + attribute_friendly_ids: "color,size", |
| 128 | + category_ids: "aa-1", |
| 129 | + include_descendants: false |
| 130 | + ).execute |
| 131 | + |
| 132 | + assert_equal 2, @clothing.attributes.size |
| 133 | + assert_includes @clothing.attributes, @color_attribute |
| 134 | + assert_includes @clothing.attributes, @size_attribute |
| 135 | + |
| 136 | + assert_equal 1, @clothing.attributes.count { |attr| attr == @color_attribute } |
| 137 | + end |
| 138 | + |
| 139 | + test "execute raises error when attribute is not found" do |
| 140 | + assert_raises(RuntimeError) do |
| 141 | + AddAttributesToCategoriesCommand.new( |
| 142 | + attribute_friendly_ids: "nonexistent", |
| 143 | + category_ids: "aa-1", |
| 144 | + include_descendants: false |
| 145 | + ).execute |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + test "execute raises error when category is not found" do |
| 150 | + assert_raises(RuntimeError) do |
| 151 | + AddAttributesToCategoriesCommand.new( |
| 152 | + attribute_friendly_ids: "color", |
| 153 | + category_ids: "nonexistent", |
| 154 | + include_descendants: false |
| 155 | + ).execute |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + test "execute updates data files for all affected root categories" do |
| 160 | + # When adding attributes to categories from different verticals, |
| 161 | + # the command should update data files for all affected root categories |
| 162 | + @second_root = Category.new(id: "bb", name: "Business & Industrial") |
| 163 | + @equipment = Category.new(id: "bb-1", name: "Equipment") |
| 164 | + |
| 165 | + @second_root.add_child(@equipment) |
| 166 | + |
| 167 | + Category.add(@second_root) |
| 168 | + Category.add(@equipment) |
| 169 | + |
| 170 | + dump_command = mock |
| 171 | + dump_command.expects(:execute).once |
| 172 | + DumpCategoriesCommand.expects(:new).with(verticals: ["aa", "bb"]).returns(dump_command) |
| 173 | + |
| 174 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 175 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 176 | + |
| 177 | + AddAttributesToCategoriesCommand.new( |
| 178 | + attribute_friendly_ids: "color", |
| 179 | + category_ids: "aa-1,bb-1", |
| 180 | + include_descendants: false |
| 181 | + ).execute |
| 182 | + |
| 183 | + assert_equal 1, @clothing.attributes.size |
| 184 | + assert_includes @clothing.attributes, @color_attribute |
| 185 | + |
| 186 | + assert_equal 1, @equipment.attributes.size |
| 187 | + assert_includes @equipment.attributes, @color_attribute |
| 188 | + end |
| 189 | + end |
| 190 | +end |
0 commit comments