|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module ProductTaxonomy |
| 6 | + class AddCategoryCommandTest < TestCase |
| 7 | + setup do |
| 8 | + @root_category = Category.new(id: "aa", name: "Root Category") |
| 9 | + @child_category = Category.new(id: "aa-1", name: "Child Category", parent: @root_category) |
| 10 | + @root_category.add_child(@child_category) |
| 11 | + |
| 12 | + Category.add(@root_category) |
| 13 | + Category.add(@child_category) |
| 14 | + end |
| 15 | + |
| 16 | + test "execute successfully adds a new category" do |
| 17 | + DumpCategoriesCommand.any_instance.expects(:execute).once |
| 18 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 19 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 20 | + |
| 21 | + AddCategoryCommand.new(name: "New Category", parent_id: "aa").execute |
| 22 | + |
| 23 | + new_category = @root_category.children.find { |c| c.name == "New Category" } |
| 24 | + assert_not_nil new_category |
| 25 | + assert_equal "aa-2", new_category.id # Since aa-1 already exists |
| 26 | + assert_equal "New Category", new_category.name |
| 27 | + assert_equal @root_category, new_category.parent |
| 28 | + assert_not_nil Category.find_by(id: "aa-2") |
| 29 | + end |
| 30 | + |
| 31 | + test "execute successfully adds a category with custom numeric ID" do |
| 32 | + DumpCategoriesCommand.any_instance.expects(:execute).once |
| 33 | + SyncEnLocalizationsCommand.any_instance.expects(:execute).once |
| 34 | + GenerateDocsCommand.any_instance.expects(:execute).once |
| 35 | + |
| 36 | + AddCategoryCommand.new(name: "Custom ID Category", parent_id: "aa", id: "aa-5").execute |
| 37 | + |
| 38 | + new_category = @root_category.children.find { |c| c.name == "Custom ID Category" } |
| 39 | + assert_not_nil new_category |
| 40 | + assert_equal "aa-5", new_category.id |
| 41 | + assert_equal "Custom ID Category", new_category.name |
| 42 | + assert_equal @root_category, new_category.parent |
| 43 | + assert_not_nil Category.find_by(id: "aa-5") |
| 44 | + end |
| 45 | + |
| 46 | + test "execute raises error when parent category not found" do |
| 47 | + assert_raises(RuntimeError) do |
| 48 | + AddCategoryCommand.new(name: "New Category", parent_id: "nonexistent").execute |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + test "execute raises error when category ID already exists" do |
| 53 | + assert_raises(RuntimeError) do |
| 54 | + AddCategoryCommand.new(name: "Duplicate ID", parent_id: "aa", id: "aa-1").execute |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + test "execute raises error when category ID format is invalid" do |
| 59 | + assert_raises(RuntimeError) do |
| 60 | + AddCategoryCommand.new(name: "Invalid ID", parent_id: "aa", id: "aa-custom").execute |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + test "execute raises error when category name is invalid" do |
| 65 | + assert_raises(RuntimeError) do |
| 66 | + AddCategoryCommand.new(name: "", parent_id: "aa").execute |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + test "execute updates correct vertical based on parent category" do |
| 71 | + DumpCategoriesCommand.expects(:new).with(verticals: ["aa"]).returns(stub(execute: true)) |
| 72 | + SyncEnLocalizationsCommand.any_instance.stubs(:execute) |
| 73 | + GenerateDocsCommand.any_instance.stubs(:execute) |
| 74 | + |
| 75 | + AddCategoryCommand.new(name: "New Category", parent_id: "aa").execute |
| 76 | + |
| 77 | + new_category = @root_category.children.find { |c| c.name == "New Category" } |
| 78 | + assert_not_nil new_category |
| 79 | + assert_equal @root_category, new_category.parent |
| 80 | + assert_not_nil Category.find_by(id: "aa-2") |
| 81 | + end |
| 82 | + |
| 83 | + test "execute generates sequential IDs correctly" do |
| 84 | + DumpCategoriesCommand.any_instance.stubs(:execute) |
| 85 | + SyncEnLocalizationsCommand.any_instance.stubs(:execute) |
| 86 | + GenerateDocsCommand.any_instance.stubs(:execute) |
| 87 | + |
| 88 | + AddCategoryCommand.new(name: "First New", parent_id: "aa").execute |
| 89 | + AddCategoryCommand.new(name: "Second New", parent_id: "aa").execute |
| 90 | + |
| 91 | + new_categories = @root_category.children.select { |c| c.name.include?("New") }.sort_by(&:id) |
| 92 | + assert_equal 2, new_categories.size |
| 93 | + assert_equal "aa-2", new_categories[0].id |
| 94 | + assert_equal "aa-3", new_categories[1].id |
| 95 | + assert_equal "First New", new_categories[0].name |
| 96 | + assert_equal "Second New", new_categories[1].name |
| 97 | + assert_not_nil Category.find_by(id: "aa-2") |
| 98 | + assert_not_nil Category.find_by(id: "aa-3") |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments