|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module ProductTaxonomy |
| 6 | + class AddAttributeCommandTest < TestCase |
| 7 | + setup do |
| 8 | + @base_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 | + |
| 24 | + Attribute.add(@base_attribute) |
| 25 | + Value.add(@base_attribute.values.first) |
| 26 | + |
| 27 | + AddAttributeCommand.any_instance.stubs(:load_taxonomy) |
| 28 | + DumpAttributesCommand.any_instance.stubs(:load_taxonomy) |
| 29 | + DumpValuesCommand.any_instance.stubs(:load_taxonomy) |
| 30 | + SyncEnLocalizationsCommand.any_instance.stubs(:load_taxonomy) |
| 31 | + GenerateDocsCommand.any_instance.stubs(:load_taxonomy) |
| 32 | + end |
| 33 | + |
| 34 | + test "execute successfully adds a new base attribute with values" do |
| 35 | + DumpAttributesCommand.any_instance.expects(:execute).once |
| 36 | + DumpValuesCommand.any_instance.expects(:execute).once |
| 37 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 38 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 39 | + |
| 40 | + AddAttributeCommand.new( |
| 41 | + name: "Size", |
| 42 | + description: "Product size information", |
| 43 | + values: "Small, Medium, Large", |
| 44 | + ).execute |
| 45 | + |
| 46 | + new_attribute = Attribute.find_by(friendly_id: "size") |
| 47 | + assert_not_nil new_attribute |
| 48 | + assert_equal 2, new_attribute.id # Since id 1 already exists |
| 49 | + assert_equal "Size", new_attribute.name |
| 50 | + assert_equal "Product size information", new_attribute.description |
| 51 | + assert_equal "size", new_attribute.friendly_id |
| 52 | + assert_equal "size", new_attribute.handle |
| 53 | + |
| 54 | + assert_equal 3, new_attribute.values.size |
| 55 | + value_names = new_attribute.values.map(&:name) |
| 56 | + assert_includes value_names, "Small" |
| 57 | + assert_includes value_names, "Medium" |
| 58 | + assert_includes value_names, "Large" |
| 59 | + |
| 60 | + value_friendly_ids = new_attribute.values.map(&:friendly_id) |
| 61 | + assert_includes value_friendly_ids, "size__small" |
| 62 | + assert_includes value_friendly_ids, "size__medium" |
| 63 | + assert_includes value_friendly_ids, "size__large" |
| 64 | + end |
| 65 | + |
| 66 | + test "execute successfully adds an extended attribute" do |
| 67 | + DumpAttributesCommand.any_instance.expects(:execute).once |
| 68 | + DumpValuesCommand.any_instance.expects(:execute).once |
| 69 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 70 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 71 | + |
| 72 | + AddAttributeCommand.new( |
| 73 | + name: "Clothing Color", |
| 74 | + description: "Color of clothing items", |
| 75 | + base_attribute_friendly_id: "color", |
| 76 | + ).execute |
| 77 | + |
| 78 | + new_attribute = Attribute.find_by(friendly_id: "clothing_color") |
| 79 | + assert_not_nil new_attribute |
| 80 | + assert_instance_of ExtendedAttribute, new_attribute |
| 81 | + assert_equal "Clothing Color", new_attribute.name |
| 82 | + assert_equal "Color of clothing items", new_attribute.description |
| 83 | + assert_equal "clothing_color", new_attribute.friendly_id |
| 84 | + assert_equal "clothing-color", new_attribute.handle |
| 85 | + |
| 86 | + assert_equal @base_attribute, new_attribute.base_attribute |
| 87 | + |
| 88 | + assert_equal @base_attribute.values.size, new_attribute.values.size |
| 89 | + assert_equal @base_attribute.values.first.name, new_attribute.values.first.name |
| 90 | + end |
| 91 | + |
| 92 | + test "execute raises error when creating base attribute without values" do |
| 93 | + assert_raises(RuntimeError) do |
| 94 | + AddAttributeCommand.new( |
| 95 | + name: "Material", |
| 96 | + description: "Product material information", |
| 97 | + values: "", |
| 98 | + ).execute |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + test "execute raises error when creating extended attribute with values" do |
| 103 | + assert_raises(RuntimeError) do |
| 104 | + AddAttributeCommand.new( |
| 105 | + name: "Clothing Color", |
| 106 | + description: "Color of clothing items", |
| 107 | + base_attribute_friendly_id: "color", |
| 108 | + values: "Red, Blue", |
| 109 | + ).execute |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + test "execute raises error when base attribute for extended attribute doesn't exist" do |
| 114 | + assert_raises(ActiveModel::ValidationError) do |
| 115 | + AddAttributeCommand.new( |
| 116 | + name: "Clothing Material", |
| 117 | + description: "Material of clothing items", |
| 118 | + base_attribute_friendly_id: "nonexistent", |
| 119 | + ).execute |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + test "execute raises error when attribute with same friendly_id already exists" do |
| 124 | + assert_raises(ActiveModel::ValidationError) do |
| 125 | + AddAttributeCommand.new( |
| 126 | + name: "Color", # This will generate the same friendly_id as @base_attribute |
| 127 | + description: "Another color attribute", |
| 128 | + values: "Red, Blue", |
| 129 | + ).execute |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + test "execute creates values with correct friendly_ids and handles" do |
| 134 | + DumpAttributesCommand.any_instance.stubs(:execute) |
| 135 | + DumpValuesCommand.any_instance.stubs(:execute) |
| 136 | + SyncEnLocalizationsCommand.any_instance.stubs(:execute) |
| 137 | + GenerateDocsCommand.any_instance.stubs(:execute) |
| 138 | + |
| 139 | + AddAttributeCommand.new( |
| 140 | + name: "Material Type", |
| 141 | + description: "Type of material used", |
| 142 | + values: "Cotton, Polyester", |
| 143 | + ).execute |
| 144 | + |
| 145 | + new_attribute = Attribute.find_by(friendly_id: "material_type") |
| 146 | + assert_not_nil new_attribute |
| 147 | + |
| 148 | + cotton_value = new_attribute.values.find { |v| v.name == "Cotton" } |
| 149 | + assert_not_nil cotton_value |
| 150 | + assert_equal "material_type__cotton", cotton_value.friendly_id |
| 151 | + assert_equal "material-type__cotton", cotton_value.handle |
| 152 | + |
| 153 | + polyester_value = new_attribute.values.find { |v| v.name == "Polyester" } |
| 154 | + assert_not_nil polyester_value |
| 155 | + assert_equal "material_type__polyester", polyester_value.friendly_id |
| 156 | + assert_equal "material-type__polyester", polyester_value.handle |
| 157 | + end |
| 158 | + |
| 159 | + test "execute reuses existing values with same friendly_id" do |
| 160 | + existing_value = Value.new( |
| 161 | + id: 2, |
| 162 | + name: "Existing Cotton", |
| 163 | + friendly_id: "material_type__cotton", |
| 164 | + handle: "material_type__cotton", |
| 165 | + ) |
| 166 | + Value.add(existing_value) |
| 167 | + |
| 168 | + DumpAttributesCommand.any_instance.stubs(:execute) |
| 169 | + DumpValuesCommand.any_instance.stubs(:execute) |
| 170 | + SyncEnLocalizationsCommand.any_instance.stubs(:execute) |
| 171 | + GenerateDocsCommand.any_instance.stubs(:execute) |
| 172 | + |
| 173 | + AddAttributeCommand.new( |
| 174 | + name: "Material Type", |
| 175 | + description: "Type of material used", |
| 176 | + values: "Cotton, Polyester", |
| 177 | + ).execute |
| 178 | + |
| 179 | + new_attribute = Attribute.find_by(friendly_id: "material_type") |
| 180 | + assert_not_nil new_attribute |
| 181 | + |
| 182 | + cotton_value = new_attribute.values.find { |v| v.friendly_id == "material_type__cotton" } |
| 183 | + assert_not_nil cotton_value |
| 184 | + assert_equal existing_value, cotton_value |
| 185 | + assert_equal "Existing Cotton", cotton_value.name # Name from existing value |
| 186 | + |
| 187 | + polyester_value = new_attribute.values.find { |v| v.name == "Polyester" } |
| 188 | + assert_not_nil polyester_value |
| 189 | + assert_equal "material_type__polyester", polyester_value.friendly_id |
| 190 | + end |
| 191 | + |
| 192 | + test "execute updates data files after creating attribute" do |
| 193 | + DumpAttributesCommand.any_instance.expects(:execute).once |
| 194 | + DumpValuesCommand.any_instance.expects(:execute).once |
| 195 | + SyncEnLocalizationsCommand.expects(:new).with(targets: "attributes,values").returns(stub(execute: true)) |
| 196 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 197 | + |
| 198 | + AddAttributeCommand.new( |
| 199 | + name: "Size", |
| 200 | + description: "Product size information", |
| 201 | + values: "Small, Medium, Large", |
| 202 | + ).execute |
| 203 | + end |
| 204 | + end |
| 205 | +end |
0 commit comments