2024-03-07 19:15:50 +01:00
|
|
|
require "test_helper"
|
|
|
|
|
2024-06-20 08:15:09 -04:00
|
|
|
class CategoryTest < ActiveSupport::TestCase
|
2024-03-07 19:15:50 +01:00
|
|
|
def setup
|
|
|
|
@family = families(:dylan_family)
|
|
|
|
end
|
|
|
|
|
2024-05-02 07:24:31 -06:00
|
|
|
test "replacing and destroying" do
|
2024-06-24 11:58:39 -04:00
|
|
|
transactions = categories(:food_and_drink).transactions.to_a
|
2024-05-02 07:24:31 -06:00
|
|
|
|
2024-06-20 08:15:09 -04:00
|
|
|
categories(:food_and_drink).replace_and_destroy!(categories(:income))
|
2024-05-02 07:24:31 -06:00
|
|
|
|
2024-06-20 08:15:09 -04:00
|
|
|
assert_equal categories(:income), transactions.map { |t| t.reload.category }.uniq.first
|
2024-05-02 07:24:31 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "replacing with nil should nullify the category" do
|
2024-06-20 08:15:09 -04:00
|
|
|
transactions = categories(:food_and_drink).transactions.to_a
|
2024-05-02 07:24:31 -06:00
|
|
|
|
2024-06-20 08:15:09 -04:00
|
|
|
categories(:food_and_drink).replace_and_destroy!(nil)
|
2024-05-02 07:24:31 -06:00
|
|
|
|
|
|
|
assert_nil transactions.map { |t| t.reload.category }.uniq.first
|
|
|
|
end
|
2024-12-20 11:37:26 -05:00
|
|
|
|
|
|
|
test "subcategory can only be one level deep" do
|
|
|
|
category = categories(:subcategory)
|
|
|
|
|
|
|
|
error = assert_raises(ActiveRecord::RecordInvalid) do
|
2025-01-30 16:49:31 -05:00
|
|
|
category.subcategories.create!(name: "Invalid category", family: @family)
|
2024-12-20 11:37:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Validation failed: Parent can't have more than 2 levels of subcategories", error.message
|
|
|
|
end
|
2024-03-07 19:15:50 +01:00
|
|
|
end
|