ENV['RAILS_ENV'] = 'test' require '../../../config/environment' require 'test/unit' require 'pgsql_stats' require 'test/test_helper' class TableTest < Test::Unit::TestCase include TestHelper def setup @table = PgsqlStats::Table.new(PgsqlStats::Collector.connection, { "relid" => "1", "schemaname" => "schema", "relname" => "test", "seq_scan" => "2", "seq_tup_read" => "3", "idx_scan" => "4", "idx_tup_fetch" => "5", "n_tup_ins" => "6", "n_tup_upd" => "7", "n_tup_del" => "8", "last_vacuum" => "2007-08-01 00:00:00.012345-04", "last_autovacuum" => "2007-08-01 01:00:00.012345-04", "last_analyze" => "2007-08-01 02:00:00.012345-04", "last_autoanalyze" => "2007-08-01 03:00:00.012345-04" }, { "heap_blks_read" => "10", "heap_blks_hit" => "90", "idx_blks_read" => "20", "idx_blks_hit" => "80", "toast_blks_read" => "30", "toast_blks_hit" => "70", "tidx_blks_read" => "40", "tidx_blks_hit" => "60" }) end def test_initialize assert_equal 1, @table.oid assert_equal "schema", @table.schema_name assert_equal "test", @table.name assert_equal 2, @table.sequential_scans assert_equal 3, @table.rows_fetched assert_equal 4, @table.index_scans assert_equal 5, @table.index_rows_fetched assert_equal 6, @table.inserts assert_equal 7, @table.updates assert_equal 8, @table.deletes assert_equal Time.parse("2007-08-01 00:00:00.012345-04"), @table.vacuumed_at assert_equal Time.parse("2007-08-01 01:00:00.012345-04"), @table.autovacuumed_at assert_equal Time.parse("2007-08-01 02:00:00.012345-04"), @table.analyzed_at assert_equal Time.parse("2007-08-01 03:00:00.012345-04"), @table.autoanalyzed_at assert_equal 10, @table.blocks_read assert_equal 90, @table.cache_hits assert_equal 20, @table.index_blocks_read assert_equal 80, @table.index_cache_hits assert_equal 30, @table.toast_blocks_read assert_equal 70, @table.toast_cache_hits assert_equal 40, @table.toast_index_blocks_read assert_equal 60, @table.toast_index_cache_hits end def test_refresh table = PgsqlStats::Collector.all_tables.first oid = table.oid scans = table.sequential_scans PgsqlStats::Collector.connection.select_all("SELECT * FROM #{table.name}") wait_for_statistics_report assert_nothing_raised { table.refresh } assert_equal oid, table.oid assert table.sequential_scans > scans end def test_indexes table = PgsqlStats::Collector.all_tables.first assert_nothing_raised { assert_not_nil table.indexes } end def test_cache_hit_rate assert_in_delta 90, @table.cache_hit_rate, 0.001 end def test_index_cache_hit_rate assert_in_delta 80, @table.index_cache_hit_rate, 0.001 end def test_toast_cache_hit_rate assert_in_delta 70, @table.toast_cache_hit_rate, 0.001 end def test_toast_index_cache_hit_rate assert_in_delta 60, @table.toast_index_cache_hit_rate, 0.001 end def test_pages table = PgsqlStats::Collector.all_tables.first assert_nothing_raised { table.pages } end end