From 961e15242c386a0c8a5049117bfab9938fefd78c Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 26 Jan 2021 02:22:00 -0800 Subject: [PATCH] Added controller tests --- .gitignore | 2 +- Gemfile | 1 + gemfiles/activerecord50.gemfile | 1 + gemfiles/activerecord51.gemfile | 1 + gemfiles/activerecord52.gemfile | 1 + gemfiles/activerecord60.gemfile | 1 + test/controller_test.rb | 69 +++++++++++++++++++++ test/internal/app/assets/config/manifest.js | 0 test/internal/config/database.yml | 3 + test/internal/config/routes.rb | 3 + test/internal/db/schema.rb | 57 +++++++++++++++++ test/internal/public/favicon.ico | 0 test/support/migrations.rb | 57 ----------------- test/test_helper.rb | 11 ++-- 14 files changed, 144 insertions(+), 63 deletions(-) create mode 100644 test/controller_test.rb create mode 100644 test/internal/app/assets/config/manifest.js create mode 100644 test/internal/config/database.yml create mode 100644 test/internal/config/routes.rb create mode 100644 test/internal/db/schema.rb create mode 100644 test/internal/public/favicon.ico delete mode 100644 test/support/migrations.rb diff --git a/.gitignore b/.gitignore index 4cfd62aed..9ebe29b1b 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,4 @@ tmp *.so *.o *.a -mkmf.log +*.log diff --git a/Gemfile b/Gemfile index e153185bf..5e9190972 100644 --- a/Gemfile +++ b/Gemfile @@ -7,5 +7,6 @@ gem "rake" gem "activerecord", "~> 6.1.0" gem "activerecord-import" +gem "combustion" gem "pg" gem "pg_query" diff --git a/gemfiles/activerecord50.gemfile b/gemfiles/activerecord50.gemfile index a98ef335d..b098be4cb 100644 --- a/gemfiles/activerecord50.gemfile +++ b/gemfiles/activerecord50.gemfile @@ -7,5 +7,6 @@ gem "rake" gem "activerecord", "~> 5.0.0" gem "activerecord-import" +gem "combustion" gem "pg" gem "pg_query", RUBY_VERSION.to_f < 2.5 ? "< 1.3.0" : nil diff --git a/gemfiles/activerecord51.gemfile b/gemfiles/activerecord51.gemfile index a4e3eae1d..563cf6589 100644 --- a/gemfiles/activerecord51.gemfile +++ b/gemfiles/activerecord51.gemfile @@ -7,5 +7,6 @@ gem "rake" gem "activerecord", "~> 5.1.0" gem "activerecord-import" +gem "combustion" gem "pg" gem "pg_query" diff --git a/gemfiles/activerecord52.gemfile b/gemfiles/activerecord52.gemfile index 10e3ade07..dabe4066b 100644 --- a/gemfiles/activerecord52.gemfile +++ b/gemfiles/activerecord52.gemfile @@ -7,5 +7,6 @@ gem "rake" gem "activerecord", "~> 5.2.0" gem "activerecord-import" +gem "combustion" gem "pg" gem "pg_query" diff --git a/gemfiles/activerecord60.gemfile b/gemfiles/activerecord60.gemfile index 43bf90717..bc5b0b653 100644 --- a/gemfiles/activerecord60.gemfile +++ b/gemfiles/activerecord60.gemfile @@ -7,5 +7,6 @@ gem "rake" gem "activerecord", "~> 6.0.0" gem "activerecord-import" +gem "combustion" gem "pg" gem "pg_query" diff --git a/test/controller_test.rb b/test/controller_test.rb new file mode 100644 index 000000000..a8c96d276 --- /dev/null +++ b/test/controller_test.rb @@ -0,0 +1,69 @@ +require_relative "test_helper" + +class ControllerTest < ActionDispatch::IntegrationTest + def test_index + get pg_hero.root_path + assert_response :success + end + + def test_space + get pg_hero.space_path + assert_response :success + end + + def test_relation_space + get pg_hero.relation_space_path(relation: "users") + assert_response :success + end + + def test_index_bloat + get pg_hero.index_bloat_path + assert_response :success + end + + def test_live_queries + get pg_hero.live_queries_path + assert_response :success + end + + def test_queries + get pg_hero.queries_path + assert_response :success + end + + def test_show_query + get pg_hero.show_query_path(query_hash: 123) + assert_response :success + end + + def test_system + get pg_hero.system_path + assert_response :success + end + + def test_explain + get pg_hero.explain_path + assert_response :success + end + + def test_tune + get pg_hero.tune_path + assert_response :success + end + + def test_connections + get pg_hero.connections_path + assert_response :success + end + + def test_maintenance + get pg_hero.maintenance_path + assert_response :success + end + + def test_kill + # prevent warning for now + # post pg_hero.kill_path(pid: 1_000_000_000) + # assert_redirected_to "/" + end +end diff --git a/test/internal/app/assets/config/manifest.js b/test/internal/app/assets/config/manifest.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/internal/config/database.yml b/test/internal/config/database.yml new file mode 100644 index 000000000..030b4bb9c --- /dev/null +++ b/test/internal/config/database.yml @@ -0,0 +1,3 @@ +test: + adapter: postgresql + database: pghero_test diff --git a/test/internal/config/routes.rb b/test/internal/config/routes.rb new file mode 100644 index 000000000..1515678a4 --- /dev/null +++ b/test/internal/config/routes.rb @@ -0,0 +1,3 @@ +Rails.application.routes.draw do + mount PgHero::Engine, at: "/" +end diff --git a/test/internal/db/schema.rb b/test/internal/db/schema.rb new file mode 100644 index 000000000..316acea3c --- /dev/null +++ b/test/internal/db/schema.rb @@ -0,0 +1,57 @@ +ActiveRecord::Schema.define do + enable_extension "pg_stat_statements" + enable_extension "pg_trgm" + enable_extension "ltree" + + create_table :pghero_query_stats, force: true do |t| + t.text :database + t.text :user + t.text :query + t.integer :query_hash, limit: 8 + t.float :total_time + t.integer :calls, limit: 8 + t.timestamp :captured_at + t.index [:database, :captured_at] + end + + create_table :pghero_space_stats, force: true do |t| + t.text :database + t.text :schema + t.text :relation + t.integer :size, limit: 8 + t.timestamp :captured_at + t.index [:database, :captured_at] + end + + create_table :cities, force: true do |t| + t.string :name + end + + create_table :states, force: true do |t| + t.string :name + end + + create_table :users, force: true do |t| + t.integer :city_id + t.integer :login_attempts + t.string :email + t.string :zip_code + t.boolean :active + t.string :country + t.column :tree_path, :ltree + t.column :range, :int4range + t.column :last_known_ip, :inet + t.column :metadata, :jsonb + t.timestamp :created_at + t.timestamp :updated_at + t.index :id # duplicate index + t.index :updated_at + t.index :login_attempts, using: :hash + t.index "country gist_trgm_ops", using: :gist + t.index :tree_path, using: :gist + t.index :range, using: :gist + t.index :created_at, using: :brin + t.index "last_known_ip inet_ops", using: :gist + t.index :metadata, using: :gin + end +end diff --git a/test/internal/public/favicon.ico b/test/internal/public/favicon.ico new file mode 100644 index 000000000..e69de29bb diff --git a/test/support/migrations.rb b/test/support/migrations.rb deleted file mode 100644 index 563d3510a..000000000 --- a/test/support/migrations.rb +++ /dev/null @@ -1,57 +0,0 @@ -ActiveRecord::Migration.verbose = ENV["VERBOSE"] - -ActiveRecord::Migration.enable_extension "pg_stat_statements" -ActiveRecord::Migration.enable_extension "pg_trgm" -ActiveRecord::Migration.enable_extension "ltree" - -ActiveRecord::Migration.create_table :pghero_query_stats, force: true do |t| - t.text :database - t.text :user - t.text :query - t.integer :query_hash, limit: 8 - t.float :total_time - t.integer :calls, limit: 8 - t.timestamp :captured_at - t.index [:database, :captured_at] -end - -ActiveRecord::Migration.create_table :pghero_space_stats, force: true do |t| - t.text :database - t.text :schema - t.text :relation - t.integer :size, limit: 8 - t.timestamp :captured_at - t.index [:database, :captured_at] -end - -ActiveRecord::Migration.create_table :cities, force: true do |t| - t.string :name -end - -ActiveRecord::Migration.create_table :states, force: true do |t| - t.string :name -end - -ActiveRecord::Migration.create_table :users, force: true do |t| - t.integer :city_id - t.integer :login_attempts - t.string :email - t.string :zip_code - t.boolean :active - t.string :country - t.column :tree_path, :ltree - t.column :range, :int4range - t.column :last_known_ip, :inet - t.column :metadata, :jsonb - t.timestamp :created_at - t.timestamp :updated_at - t.index :id # duplicate index - t.index :updated_at - t.index :login_attempts, using: :hash - t.index "country gist_trgm_ops", using: :gist - t.index :tree_path, using: :gist - t.index :range, using: :gist - t.index :created_at, using: :brin - t.index "last_known_ip inet_ops", using: :gist - t.index :metadata, using: :gin -end diff --git a/test/test_helper.rb b/test/test_helper.rb index a6cb3ac28..5e00467b3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,9 +1,9 @@ require "bundler/setup" +require "combustion" Bundler.require(:default) require "minitest/autorun" require "minitest/pride" require "pg_query" -require "active_record" require "activerecord-import" class Minitest::Test @@ -13,11 +13,12 @@ def database end logger = ActiveSupport::Logger.new(ENV["VERBOSE"] ? STDERR : nil) -ActiveRecord::Base.logger = logger -ActiveRecord::Base.establish_connection adapter: "postgresql", database: "pghero_test" - -require_relative "support/migrations" +Combustion.path = "test/internal" +Combustion.initialize! :active_record, :action_controller do + config.action_controller.logger = logger + config.active_record.logger = logger +end class City < ActiveRecord::Base end