Back to Repositories

Validating Category Management Operations in Maybe Finance

This test suite validates category management functionality in the Maybe Finance application, focusing on category replacement, deletion, and hierarchical constraints. The tests ensure proper handling of transaction categories and enforce subcategory depth limitations.

Test Coverage Overview

The test suite provides comprehensive coverage of category management operations:

  • Category replacement and deletion scenarios
  • Transaction category reassignment
  • Subcategory hierarchy validation
  • Edge cases for null category handling

Implementation Analysis

The testing approach utilizes Minitest’s ActiveSupport::TestCase framework for Ruby on Rails. It implements fixtures for test data setup and employs assertion patterns to verify category manipulation outcomes. The tests leverage Rails’ built-in validation mechanisms and exception handling.

Technical Details

  • Testing Framework: Minitest
  • Test Environment: Rails ActiveSupport::TestCase
  • Fixtures: Used for family and category data
  • Assertions: assert_equal, assert_nil, assert_raises
  • Model Testing: ActiveRecord validations and relationships

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Rails applications:

  • Proper test setup using fixtures
  • Isolated test cases for specific functionality
  • Clear validation error message verification
  • Comprehensive edge case coverage
  • Efficient use of ActiveRecord relationships

maybe-finance/maybe

test/models/category_test.rb

            
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", color: "#000", family: @family)
    end

    assert_equal "Validation failed: Parent can't have more than 2 levels of subcategories", error.message
  end
end