Back to Repositories

Testing PostgreSQL Space Management Implementation in PgHero

This test suite validates PostgreSQL database space management functionality in PgHero, focusing on database size monitoring, relation space statistics, and space growth tracking. The tests ensure proper measurement and management of database storage metrics.

Test Coverage Overview

The test suite provides comprehensive coverage of PostgreSQL space management features, including database size calculations, relation size tracking, and space statistics capture.

  • Tests database size measurement functionality
  • Validates relation size calculations for tables, indexes, and materialized views
  • Verifies space growth monitoring capabilities
  • Tests space statistics capture and cleanup operations

Implementation Analysis

The implementation uses Minitest framework for Ruby-based testing of PgHero’s space management features. The tests follow a straightforward approach with individual test methods for each space-related functionality.

  • Uses assertion-based testing methodology
  • Implements database interaction through a database helper object
  • Employs direct PostgreSQL system table queries
  • Includes state management for space statistics

Technical Details

  • Testing Framework: Minitest
  • Database Interface: PgHero database wrapper
  • Test Helper Integration: Custom test_helper.rb
  • Database Features: PostgreSQL system tables and statistics
  • Space Stats Model: PgHero::SpaceStats

Best Practices Demonstrated

The test suite demonstrates strong testing practices for database management tools, with clear separation of concerns and comprehensive feature coverage.

  • Isolated test cases for each space management feature
  • Proper cleanup of test data
  • Verification of both positive and negative scenarios
  • Clear test method naming conventions
  • Efficient use of database resources during testing

ankane/pghero

test/space_test.rb

            
require_relative "test_helper"

class SpaceTest < Minitest::Test
  def test_database_size
    assert database.database_size
  end

  def test_relation_sizes
    relation_sizes = database.relation_sizes
    assert relation_sizes.find { |r| r[:relation] == "users" && r[:type] == "table" }
    assert relation_sizes.find { |r| r[:relation] == "users_pkey" && r[:type] == "index" }
    assert relation_sizes.find { |r| r[:relation] == "all_users" && r[:type] == "matview" }
  end

  def test_table_sizes
    assert database.table_sizes
  end

  def test_space_growth
    assert database.space_growth
  end

  def test_relation_space_stats
    assert database.relation_space_stats("cities")
  end

  def test_capture_space_stats
    PgHero::SpaceStats.delete_all
    refute PgHero::SpaceStats.any?
    assert database.capture_space_stats
    assert PgHero::SpaceStats.any?
  end

  def test_clean_space_stats
    assert database.clean_space_stats
  end

  def test_space_stats_enabled
    assert database.space_stats_enabled?
  end
end