Back to Repositories

Testing Single Table Inheritance Version Tracking in Paper Trail

This test suite examines the Single Table Inheritance (STI) behavior in the PaperTrail gem, specifically focusing on management and customer models. It verifies how version tracking handles inheritance relationships when the base class lacks a type column, ensuring proper version history maintenance across model hierarchies.

Test Coverage Overview

The test suite provides comprehensive coverage of Single Table Inheritance (STI) version tracking behavior.

Key areas tested include:
  • Inheritance column configuration verification
  • Version creation for base and derived classes
  • Item type and subtype tracking across model hierarchies
  • CRUD operations version tracking for inherited models

Implementation Analysis

The testing approach uses RSpec to validate PaperTrail’s version tracking mechanism for STI models.

Technical implementation details:
  • Direct model manipulation through create/update/destroy operations
  • Version count verification using PaperTrail::Version queries
  • Explicit type column absence validation
  • Inheritance relationship verification between Management and Customer classes

Technical Details

Testing environment configuration:
  • RSpec as the testing framework
  • PaperTrail gem integration
  • Model-level versioning enabled
  • Type-specific version querying
  • Base class inheritance validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices for versioning systems.

Notable practices include:
  • Isolated test scenarios with clear setup and verification
  • Explicit expectation setting for version counts
  • Comprehensive CRUD operation coverage
  • Clear separation of concerns between base and derived classes
  • Proper version attribute verification

paper-trail-gem/paper_trail

spec/models/management_spec.rb

            
# frozen_string_literal: true

require "spec_helper"

::RSpec.describe(::Management, type: :model, versioning: true) do
  it "utilises the base_class for STI classes having no type column" do
    expect(Management.inheritance_column).to eq("type")
    expect(Management.columns.map(&:name)).not_to include("type")

    # Create, update, and destroy a Management and a Customer
    customer1 = Customer.create(name: "Cust 1")
    customer2 = Management.create(name: "Cust 2")
    customer1.update(name: "Cust 1a")
    customer2.update(name: "Cust 2a")
    customer1.destroy
    customer2.destroy

    # All versions end up with an `item_type` of Customer
    expect(
      PaperTrail::Version.where(item_type: "Customer").count
    ).to eq(6)
    expect(
      PaperTrail::Version.where(item_type: "Management").count
    ).to eq(0)

    # The item_subtype, on the other hand, is 3 and 3
    expect(
      PaperTrail::Version.where(item_subtype: "Customer").count
    ).to eq(3)
    expect(
      PaperTrail::Version.where(item_subtype: "Management").count
    ).to eq(3)
  end
end