Skip to content

Business intelligence made simple

Added more tests

Andrew Kaneb3b9edf20c3b
diff --git a/Gemfile b/Gemfile
index d9be9cec..15e69b13 100644
--- a/Gemfile
+++ b/Gemfile
@@ -12,6 +12,7 @@ gem "sprockets-rails"
# data sources
gem "elasticsearch"
gem "mysql2"
+gem "sqlite3"
# anomaly detection and forecasting
gem "prophet-rb" if ENV["TEST_PROPHET"]
diff --git a/test/adapters/mysql_test.rb b/test/adapters/mysql_test.rb
new file mode 100644
index 00000000..cb136731
--- /dev/null
+++ b/test/adapters/mysql_test.rb
@@ -0,0 +1,13 @@
+require_relative "../test_helper"
+
+class MysqlTest < ActionDispatch::IntegrationTest
+ def test_run
+ run_query "SELECT 1", data_source: "mysql"
+ end
+
+ def test_tables
+ get blazer.tables_queries_path(data_source: "mysql")
+ assert_response :success
+ assert_empty JSON.parse(response.body)
+ end
+end
diff --git a/test/adapters/sqlite_test.rb b/test/adapters/sqlite_test.rb
new file mode 100644
index 00000000..24a27a0d
--- /dev/null
+++ b/test/adapters/sqlite_test.rb
@@ -0,0 +1,13 @@
+require_relative "../test_helper"
+
+class SqliteTest < ActionDispatch::IntegrationTest
+ def test_run
+ run_query "SELECT 1", data_source: "sqlite"
+ end
+
+ def test_tables
+ get blazer.tables_queries_path(data_source: "sqlite")
+ assert_response :success
+ assert_empty JSON.parse(response.body)
+ end
+end
diff --git a/test/archive_test.rb b/test/archive_test.rb
index 29197533..e97bee4b 100644
--- a/test/archive_test.rb
+++ b/test/archive_test.rb
@@ -17,5 +17,10 @@ class ArchiveTest < ActionDispatch::IntegrationTest
assert_equal "archived", query.status
query2.reload
assert_equal "active", query2.status
+
+ get blazer.query_path(query)
+ assert_response :success
+ query.reload
+ assert_equal "active", query.status
end
end
diff --git a/test/cache_test.rb b/test/cache_test.rb
new file mode 100644
index 00000000..0476d166
--- /dev/null
+++ b/test/cache_test.rb
@@ -0,0 +1,34 @@
+require_relative "test_helper"
+
+class CacheTest < ActionDispatch::IntegrationTest
+ def setup
+ Rails.cache.clear
+ end
+
+ def test_all
+ Blazer.data_sources["main"].stub(:cache, {"mode" => "all"}) do
+ run_query "SELECT 1"
+ refute_match "Cached", response.body
+ run_query "SELECT 1"
+ assert_match "Cached", response.body
+ end
+ end
+
+ def test_slow_under_threshold
+ Blazer.data_sources["main"].stub(:cache, {"mode" => "slow"}) do
+ run_query "SELECT 1"
+ refute_match "Cached", response.body
+ run_query "SELECT 1"
+ refute_match "Cached", response.body
+ end
+ end
+
+ def test_slow_over_threshold
+ Blazer.data_sources["main"].stub(:cache, {"mode" => "slow", "slow_threshold" => 0.01}) do
+ run_query "SELECT pg_sleep(0.01)::text"
+ refute_match "Cached", response.body
+ run_query "SELECT pg_sleep(0.01)::text"
+ assert_match "Cached", response.body
+ end
+ end
+end
diff --git a/test/charts_test.rb b/test/charts_test.rb
index afab6f83..317b3342 100644
--- a/test/charts_test.rb
+++ b/test/charts_test.rb
@@ -1,11 +1,16 @@
require_relative "test_helper"
class ChartsTest < ActionDispatch::IntegrationTest
- def test_line_chart
+ def test_line_chart_format1
run_query "SELECT NOW(), 1"
assert_match "LineChart", response.body
end
+ def test_line_chart_format2
+ run_query "SELECT NOW(), 'Label', 1"
+ assert_match "LineChart", response.body
+ end
+
def test_column_chart_format1
run_query "SELECT 'Label', 1"
assert_match "ColumnChart", response.body
diff --git a/test/checks_test.rb b/test/checks_test.rb
index 87beef87..9ce9cd10 100644
--- a/test/checks_test.rb
+++ b/test/checks_test.rb
@@ -70,6 +70,23 @@ class ChecksTest < ActionDispatch::IntegrationTest
end
end
+ def test_slack
+ query = create_query
+ check = create_check(query: query, check_type: "bad_data", slack_channels: "#general")
+
+ assert_slack_messages 0 do
+ Blazer.send_failing_checks
+ end
+
+ assert_slack_messages 1 do
+ Blazer.run_checks(schedule: "5 minutes")
+ end
+
+ assert_slack_messages 1 do
+ Blazer.send_failing_checks
+ end
+ end
+
def create_check(**attributes)
Blazer::Check.create!(schedule: "5 minutes", **attributes)
end
@@ -95,4 +112,12 @@ class ChecksTest < ActionDispatch::IntegrationTest
Blazer.anomaly_checks = previous_value
end
end
+
+ def assert_slack_messages(expected)
+ count = 0
+ Blazer::SlackNotifier.stub :post_api, ->(*) { count += 1 } do
+ yield
+ end
+ assert_equal expected, count
+ end
end
diff --git a/test/dashboards_test.rb b/test/dashboards_test.rb
index 8e83b8c1..d5cec2db 100644
--- a/test/dashboards_test.rb
+++ b/test/dashboards_test.rb
@@ -5,4 +5,20 @@ class DashboardsTest < ActionDispatch::IntegrationTest
get blazer.new_dashboard_path
assert_response :success
end
+
+ def test_show
+ dashboard = create_dashboard
+ get blazer.dashboard_path(dashboard)
+ assert_response :success
+ end
+
+ def test_destroy
+ dashboard = create_dashboard
+ delete blazer.dashboard_path(dashboard)
+ assert_response :redirect
+ end
+
+ def create_dashboard
+ Blazer::Dashboard.create!(name: "Test")
+ end
end
diff --git a/test/internal/config/blazer.yml b/test/internal/config/blazer.yml
index 475a600d..b7861944 100644
--- a/test/internal/config/blazer.yml
+++ b/test/internal/config/blazer.yml
@@ -27,10 +27,11 @@ data_sources:
# status: {0: "Active", 1: "Archived"}
linked_columns:
- # user_id: "/admin/users/{value}"
+ user_id: "/admin/users/{value}"
smart_columns:
# user_id: "SELECT id, name FROM users WHERE id IN {value}"
+ status: {0: "Active", 1: "Archived"}
elasticsearch:
adapter: elasticsearch
@@ -39,6 +40,9 @@ data_sources:
mysql:
url: mysql2://localhost:3306/blazer_test
+ sqlite:
+ url: "sqlite3::memory:"
+
# create audits
audit: true
@@ -61,7 +65,7 @@ audit: true
from_email: [email protected]
# webhook for Slack
-# slack_webhook_url:
+slack_webhook_url: http://localhost:5000
check_schedules:
- "1 day"
diff --git a/test/queries_test.rb b/test/queries_test.rb
index 4dd2dc0b..96821b2b 100644
--- a/test/queries_test.rb
+++ b/test/queries_test.rb
@@ -27,6 +27,12 @@ class QueriesTest < ActionDispatch::IntegrationTest
assert_equal query.data_source, audit.data_source
end
+ def test_destroy
+ query = create_query
+ delete blazer.query_path(query)
+ assert_response :redirect
+ end
+
def test_rollback
create_query
run_query "DELETE FROM blazer_queries"
@@ -49,4 +55,14 @@ class QueriesTest < ActionDispatch::IntegrationTest
get blazer.docs_queries_path(data_source: "main")
assert_response :success
end
+
+ def test_linked_columns
+ run_query "SELECT 123 AS user_id"
+ assert_match "/admin/users/123", response.body
+ end
+
+ def test_smart_columns
+ run_query "SELECT 0 AS status"
+ assert_match "Active", response.body
+ end
end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index c31a728d..c10cfeb3 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -10,8 +10,11 @@ Combustion.path = "test/internal"
Combustion.initialize! :active_record, :action_controller, :action_mailer, :sprockets do
config.action_controller.logger = logger
config.active_record.logger = logger
+ config.cache_store = :memory_store
end
+Rails.cache.logger = logger
+
ActiveSupport.on_load(:active_record) do
connection.execute("CREATE SCHEMA IF NOT EXISTS uploads")
end
diff --git a/test/uploads_test.rb b/test/uploads_test.rb
index d08f8726..8fe9c0b2 100644
--- a/test/uploads_test.rb
+++ b/test/uploads_test.rb
@@ -5,6 +5,16 @@ class UploadsTest < ActionDispatch::IntegrationTest
Blazer::Upload.delete_all
end
+ def test_index
+ get blazer.uploads_path
+ assert_response :success
+ end
+
+ def test_new
+ get blazer.new_upload_path
+ assert_response :success
+ end
+
def test_create
post blazer.uploads_path, params: {upload: {table: "line_items", description: "Billing line items", file: fixture_file_upload("test/support/line_items.csv", "text/csv")}}
assert_response :redirect