|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module ProductTaxonomy |
| 6 | + module Serializers |
| 7 | + module Attribute |
| 8 | + module Data |
| 9 | + class DataSerializerTest < TestCase |
| 10 | + setup do |
| 11 | + @value = ProductTaxonomy::Value.new( |
| 12 | + id: 1, |
| 13 | + name: "Black", |
| 14 | + friendly_id: "color__black", |
| 15 | + handle: "color__black", |
| 16 | + ) |
| 17 | + |
| 18 | + @base_attribute = ProductTaxonomy::Attribute.new( |
| 19 | + id: 1, |
| 20 | + name: "Color", |
| 21 | + description: "Defines the primary color or pattern, such as blue or striped", |
| 22 | + friendly_id: "color", |
| 23 | + handle: "color", |
| 24 | + values: [@value], |
| 25 | + ) |
| 26 | + |
| 27 | + @extended_attribute = ProductTaxonomy::ExtendedAttribute.new( |
| 28 | + name: "Clothing Color", |
| 29 | + description: "Color of the clothing", |
| 30 | + friendly_id: "clothing_color", |
| 31 | + handle: "clothing_color", |
| 32 | + values_from: @base_attribute |
| 33 | + ) |
| 34 | + |
| 35 | + ProductTaxonomy::Value.add(@value) |
| 36 | + ProductTaxonomy::Attribute.add(@base_attribute) |
| 37 | + ProductTaxonomy::Attribute.add(@extended_attribute) |
| 38 | + end |
| 39 | + |
| 40 | + test "serialize returns the expected data structure for base attribute" do |
| 41 | + expected = { |
| 42 | + "id" => 1, |
| 43 | + "name" => "Color", |
| 44 | + "description" => "Defines the primary color or pattern, such as blue or striped", |
| 45 | + "friendly_id" => "color", |
| 46 | + "handle" => "color", |
| 47 | + "values" => ["color__black"] |
| 48 | + } |
| 49 | + |
| 50 | + assert_equal expected, DataSerializer.serialize(@base_attribute) |
| 51 | + end |
| 52 | + |
| 53 | + test "serialize returns the expected data structure for extended attribute" do |
| 54 | + expected = { |
| 55 | + "name" => "Clothing Color", |
| 56 | + "handle" => "clothing_color", |
| 57 | + "description" => "Color of the clothing", |
| 58 | + "friendly_id" => "clothing_color", |
| 59 | + "values_from" => "color" |
| 60 | + } |
| 61 | + |
| 62 | + assert_equal expected, DataSerializer.serialize(@extended_attribute) |
| 63 | + end |
| 64 | + |
| 65 | + test "serialize_all returns all attributes in data format" do |
| 66 | + expected = { |
| 67 | + "base_attributes" => [{ |
| 68 | + "id" => 1, |
| 69 | + "name" => "Color", |
| 70 | + "description" => "Defines the primary color or pattern, such as blue or striped", |
| 71 | + "friendly_id" => "color", |
| 72 | + "handle" => "color", |
| 73 | + "values" => ["color__black"] |
| 74 | + }], |
| 75 | + "extended_attributes" => [{ |
| 76 | + "name" => "Clothing Color", |
| 77 | + "handle" => "clothing_color", |
| 78 | + "description" => "Color of the clothing", |
| 79 | + "friendly_id" => "clothing_color", |
| 80 | + "values_from" => "color" |
| 81 | + }] |
| 82 | + } |
| 83 | + |
| 84 | + assert_equal expected, DataSerializer.serialize_all |
| 85 | + end |
| 86 | + |
| 87 | + test "serialize includes sorting field when attribute is manually sorted" do |
| 88 | + @base_attribute = ProductTaxonomy::Attribute.new( |
| 89 | + id: 2, |
| 90 | + name: "Size", |
| 91 | + description: "Defines the size of the product", |
| 92 | + friendly_id: "size", |
| 93 | + handle: "size", |
| 94 | + values: [@value], |
| 95 | + is_manually_sorted: true |
| 96 | + ) |
| 97 | + ProductTaxonomy::Attribute.add(@base_attribute) |
| 98 | + |
| 99 | + expected = { |
| 100 | + "id" => 2, |
| 101 | + "name" => "Size", |
| 102 | + "description" => "Defines the size of the product", |
| 103 | + "friendly_id" => "size", |
| 104 | + "handle" => "size", |
| 105 | + "sorting" => "custom", |
| 106 | + "values" => ["color__black"] |
| 107 | + } |
| 108 | + |
| 109 | + assert_equal expected, DataSerializer.serialize(@base_attribute) |
| 110 | + end |
| 111 | + |
| 112 | + test "serialize_all sorts base attributes by ID" do |
| 113 | + second_base_attribute = ProductTaxonomy::Attribute.new( |
| 114 | + id: 2, |
| 115 | + name: "Size", |
| 116 | + description: "Defines the size of the product", |
| 117 | + friendly_id: "size", |
| 118 | + handle: "size", |
| 119 | + values: [] |
| 120 | + ) |
| 121 | + third_base_attribute = ProductTaxonomy::Attribute.new( |
| 122 | + id: 3, |
| 123 | + name: "Material", |
| 124 | + description: "Defines the material of the product", |
| 125 | + friendly_id: "material", |
| 126 | + handle: "material", |
| 127 | + values: [] |
| 128 | + ) |
| 129 | + # Add attributes in random order to ensure sorting is by ID |
| 130 | + ProductTaxonomy::Attribute.add(third_base_attribute) |
| 131 | + ProductTaxonomy::Attribute.add(second_base_attribute) |
| 132 | + |
| 133 | + result = DataSerializer.serialize_all |
| 134 | + base_attributes = result["base_attributes"] |
| 135 | + |
| 136 | + assert_equal 3, base_attributes.length |
| 137 | + assert_equal "color", base_attributes[0]["friendly_id"] |
| 138 | + assert_equal "size", base_attributes[1]["friendly_id"] |
| 139 | + assert_equal "material", base_attributes[2]["friendly_id"] |
| 140 | + end |
| 141 | + |
| 142 | + test "serialize_all preserves extended attributes order" do |
| 143 | + second_extended_attribute = ProductTaxonomy::ExtendedAttribute.new( |
| 144 | + name: "Shoe Color", |
| 145 | + description: "Color of the shoe", |
| 146 | + friendly_id: "shoe_color", |
| 147 | + handle: "shoe_color", |
| 148 | + values_from: @base_attribute |
| 149 | + ) |
| 150 | + third_extended_attribute = ProductTaxonomy::ExtendedAttribute.new( |
| 151 | + name: "Accessory Color", |
| 152 | + description: "Color of the accessory", |
| 153 | + friendly_id: "accessory_color", |
| 154 | + handle: "accessory_color", |
| 155 | + values_from: @base_attribute |
| 156 | + ) |
| 157 | + # Add extended attributes in specific order |
| 158 | + ProductTaxonomy::Attribute.add(second_extended_attribute) |
| 159 | + ProductTaxonomy::Attribute.add(third_extended_attribute) |
| 160 | + |
| 161 | + result = DataSerializer.serialize_all |
| 162 | + extended_attributes = result["extended_attributes"] |
| 163 | + |
| 164 | + assert_equal 3, extended_attributes.length |
| 165 | + assert_equal "clothing_color", extended_attributes[0]["friendly_id"] |
| 166 | + assert_equal "shoe_color", extended_attributes[1]["friendly_id"] |
| 167 | + assert_equal "accessory_color", extended_attributes[2]["friendly_id"] |
| 168 | + end |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | +end |
0 commit comments