You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by vi...@apache.org on 2021/01/15 14:51:15 UTC

[superset] branch master updated: test: /postgres engine spec test coverage (#12490)

This is an automated email from the ASF dual-hosted git repository.

villebro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new fc5e311  test: /postgres engine spec test coverage (#12490)
fc5e311 is described below

commit fc5e311842b8039da6ae897f53ca2434d893210f
Author: Karol Kostrzewa <ka...@gmail.com>
AuthorDate: Fri Jan 15 15:50:37 2021 +0100

    test: /postgres engine spec test coverage (#12490)
    
    * add tests for PostgresEngineSpec.estimate_statement_cost
    
    * add tests for PostgresEngineSpec.query_cost_formatter
    
    * fix docstring
    
    * fix dependency
    
    * fix raise SyntaxError
---
 tests/db_engine_specs/postgres_tests.py | 48 +++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/tests/db_engine_specs/postgres_tests.py b/tests/db_engine_specs/postgres_tests.py
index 4f362d0..6a9bd37 100644
--- a/tests/db_engine_specs/postgres_tests.py
+++ b/tests/db_engine_specs/postgres_tests.py
@@ -154,3 +154,51 @@ class TestPostgresDbEngineSpec(TestDbEngineSpec):
         connect_args = extras["engine_params"]["connect_args"]
         assert connect_args["sslmode"] == "verify-ca"
         assert "sslrootcert" in connect_args
+
+    def test_estimate_statement_cost_select_star(self):
+        """
+        DB Eng Specs (postgres): Test estimate_statement_cost select star
+        """
+
+        cursor = mock.Mock()
+        cursor.fetchone.return_value = (
+            "Seq Scan on birth_names  (cost=0.00..1537.91 rows=75691 width=46)",
+        )
+        sql = "SELECT * FROM birth_names"
+        results = PostgresEngineSpec.estimate_statement_cost(sql, cursor)
+        self.assertEqual(results, {"Start-up cost": 0.00, "Total cost": 1537.91,})
+
+    def test_estimate_statement_invalid_syntax(self):
+        """
+        DB Eng Specs (postgres): Test estimate_statement_cost invalid syntax
+        """
+        from psycopg2 import errors
+
+        cursor = mock.Mock()
+        cursor.execute.side_effect = errors.SyntaxError(
+            """
+            syntax error at or near "EXPLAIN"
+            LINE 1: EXPLAIN DROP TABLE birth_names
+                            ^
+            """
+        )
+        sql = "DROP TABLE birth_names"
+        with self.assertRaises(errors.SyntaxError):
+            PostgresEngineSpec.estimate_statement_cost(sql, cursor)
+
+    def test_query_cost_formatter_example_costs(self):
+        """
+        DB Eng Specs (postgres): Test test_query_cost_formatter example costs
+        """
+        raw_cost = [
+            {"Start-up cost": 0.00, "Total cost": 1537.91,},
+            {"Start-up cost": 10.00, "Total cost": 1537.00,},
+        ]
+        result = PostgresEngineSpec.query_cost_formatter(raw_cost)
+        self.assertEqual(
+            result,
+            [
+                {"Start-up cost": "0.0", "Total cost": "1537.91",},
+                {"Start-up cost": "10.0", "Total cost": "1537.0",},
+            ],
+        )