mirror of
https://github.com/we-promise/sure.git
synced 2026-04-09 07:14:47 +00:00
- Added server-side validation to Category model to enforce 6-digit hex format for colors. - Added HTML pattern attribute to category form for client-side validation. - Updated tests to cover validation and fixed existing tests using shorthand hex colors.
59 lines
2.1 KiB
Ruby
59 lines
2.1 KiB
Ruby
require "test_helper"
|
|
|
|
class CategoryTest < ActiveSupport::TestCase
|
|
def setup
|
|
@family = families(:dylan_family)
|
|
end
|
|
|
|
test "replacing and destroying" do
|
|
transactions = categories(:food_and_drink).transactions.to_a
|
|
|
|
categories(:food_and_drink).replace_and_destroy!(categories(:income))
|
|
|
|
assert_equal categories(:income), transactions.map { |t| t.reload.category }.uniq.first
|
|
end
|
|
|
|
test "replacing with nil should nullify the category" do
|
|
transactions = categories(:food_and_drink).transactions.to_a
|
|
|
|
categories(:food_and_drink).replace_and_destroy!(nil)
|
|
|
|
assert_nil transactions.map { |t| t.reload.category }.uniq.first
|
|
end
|
|
|
|
test "subcategory can only be one level deep" do
|
|
category = categories(:subcategory)
|
|
|
|
error = assert_raises(ActiveRecord::RecordInvalid) do
|
|
category.subcategories.create!(name: "Invalid category", family: @family)
|
|
end
|
|
|
|
assert_equal "Validation failed: Parent can't have more than 2 levels of subcategories", error.message
|
|
end
|
|
|
|
test "all_investment_contributions_names returns all locale variants" do
|
|
names = Category.all_investment_contributions_names
|
|
|
|
assert_includes names, "Investment Contributions" # English
|
|
assert_includes names, "Contributions aux investissements" # French
|
|
assert_includes names, "Investeringsbijdragen" # Dutch
|
|
assert names.all? { |name| name.is_a?(String) }
|
|
assert_equal names, names.uniq # No duplicates
|
|
end
|
|
|
|
test "should accept valid 6-digit hex colors" do
|
|
[ "#FFFFFF", "#000000", "#123456", "#ABCDEF", "#abcdef" ].each do |color|
|
|
category = Category.new(name: "Category #{color}", color: color, lucide_icon: "shapes", family: @family)
|
|
assert category.valid?, "#{color} should be valid"
|
|
end
|
|
end
|
|
|
|
test "should reject invalid colors" do
|
|
[ "invalid", "#123", "#1234567", "#GGGGGG", "red", "ffffff", "#ffff", "" ].each do |color|
|
|
category = Category.new(name: "Category #{color}", color: color, lucide_icon: "shapes", family: @family)
|
|
assert_not category.valid?, "#{color} should be invalid"
|
|
assert_includes category.errors[:color], "is invalid"
|
|
end
|
|
end
|
|
end
|