Back to Repositories

Testing Translation Version Tracking in PaperTrail

This test suite examines the versioning behavior of translations in the PaperTrail gem, specifically focusing on how different operations affect version tracking for US and non-US translations. The tests verify version creation under various conditions including draft status, language codes, and CRUD operations.

Test Coverage Overview

The test suite provides comprehensive coverage of translation versioning behavior across different scenarios.

Key areas tested include:
  • Non-US translation operations (create, update, destroy, touch)
  • US translation operations with draft status
  • US translation operations without draft status
  • Version count verification after each operation

Implementation Analysis

The testing approach uses RSpec’s context-based structure to organize test cases logically by translation type and status. The implementation leverages RSpec’s described_class pattern and expectations to verify version counts, demonstrating both model-level changes and their impact on the PaperTrail version count.

Technical patterns include:
  • Nested context blocks for clear test organization
  • Consistent version count verification
  • Direct model manipulation for state changes

Technical Details

Testing tools and configuration:
  • RSpec as the testing framework
  • PaperTrail gem for version tracking
  • Model-level specs with versioning: true flag
  • Translation model with language_code and draft_status attributes

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby and RSpec.

Notable practices include:
  • Consistent test structure and naming
  • Isolated test cases for each scenario
  • Clear separation of concerns between US and non-US translations
  • Thorough verification of version counts
  • Proper setup and teardown of test data

paper-trail-gem/paper_trail

spec/models/translation_spec.rb

            
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Translation, type: :model, versioning: true do
  context "with non-US translations" do
    it "not change the number of versions" do
      described_class.create!(headline: "Headline")
      expect(PaperTrail::Version.count).to(eq(0))
    end

    context "when after update" do
      it "not change the number of versions" do
        translation = described_class.create!(headline: "Headline")
        translation.update(content: "Content")
        expect(PaperTrail::Version.count).to(eq(0))
      end
    end

    context "when after destroy" do
      it "not change the number of versions" do
        translation = described_class.create!(headline: "Headline")
        translation.destroy
        expect(PaperTrail::Version.count).to(eq(0))
      end
    end

    context "when after touch" do
      it "not change the number of versions" do
        translation = described_class.create!(headline: "Headline")
        translation.touch
        expect(PaperTrail::Version.count).to(eq(0))
      end
    end
  end

  context "with US translations" do
    context "with drafts" do
      it "creation does not change the number of versions" do
        translation = described_class.new(headline: "Headline")
        translation.language_code = "US"
        translation.draft_status = "DRAFT"
        translation.save!
        expect(PaperTrail::Version.count).to(eq(0))
      end

      it "update does not change the number of versions" do
        translation = described_class.new(headline: "Headline")
        translation.language_code = "US"
        translation.draft_status = "DRAFT"
        translation.save!
        translation.update(content: "Content")
        expect(PaperTrail::Version.count).to(eq(0))
      end

      it "touch does not change the number of versions" do
        translation = described_class.new(headline: "Headline")
        translation.language_code = "US"
        translation.draft_status = "DRAFT"
        translation.save!
        translation.touch
        expect(PaperTrail::Version.count).to(eq(0))
      end
    end

    context "with non-drafts" do
      it "create changes the number of versions" do
        described_class.create!(headline: "Headline", language_code: "US")
        expect(PaperTrail::Version.count).to(eq(1))
      end

      it "update changes the number of versions" do
        translation = described_class.create!(headline: "Headline", language_code: "US")
        translation.update(content: "Content")
        expect(PaperTrail::Version.count).to(eq(2))
        expect(translation.versions.size).to(eq(2))
      end

      it "touch changes the number of versions" do
        translation = described_class.create!(headline: "Headline", language_code: "US")
        translation.touch
        expect(PaperTrail::Version.count).to(eq(2))
        expect(translation.versions.size).to(eq(2))
      end

      it "destroy changes the number of versions" do
        translation = described_class.new(headline: "Headline")
        translation.language_code = "US"
        translation.save!
        translation.destroy
        expect(PaperTrail::Version.count).to(eq(2))
        expect(translation.versions.size).to(eq(2))
      end
    end
  end
end