|
| 1 | +"""Plugin integration tests. |
| 2 | +
|
| 3 | +These tests verify we can load plugins and that plugin behavior works against a real Postgres backend. |
| 4 | +""" |
| 5 | + |
| 6 | +import collections |
| 7 | +import importlib |
| 8 | + |
| 9 | +import psycopg2 |
| 10 | +import pytest |
| 11 | + |
| 12 | +import plugins.tableau_hll as hll |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture() |
| 16 | +def plugin_context(postgres_settings, monkeypatch): |
| 17 | + # plugin's internal psycopg2 connection does not pass password, so provide it via libpq env var |
| 18 | + monkeypatch.setenv("PGPASSWORD", postgres_settings["password"]) |
| 19 | + |
| 20 | + with psycopg2.connect(sslmode="disable", **postgres_settings) as conn: |
| 21 | + conn.autocommit = True |
| 22 | + with conn.cursor() as cur: |
| 23 | + cur.execute('CREATE SCHEMA IF NOT EXISTS "crm_dim";') |
| 24 | + cur.execute('DROP TABLE IF EXISTS "crm_dim"."crm_data_source";') |
| 25 | + cur.execute( |
| 26 | + """ |
| 27 | + DO $$ |
| 28 | + BEGIN |
| 29 | + IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'hll' AND typtype = 'd') THEN |
| 30 | + DROP DOMAIN hll; |
| 31 | + END IF; |
| 32 | + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'hll') THEN |
| 33 | + CREATE TYPE hll AS (v text); |
| 34 | + END IF; |
| 35 | + END $$; |
| 36 | + """ |
| 37 | + ) |
| 38 | + cur.execute( |
| 39 | + 'CREATE TABLE "crm_dim"."crm_data_source" (' |
| 40 | + '"Set of Customers" hll, ' |
| 41 | + '"Campaign Name" text);' |
| 42 | + ) |
| 43 | + |
| 44 | + InstanceConfig = collections.namedtuple("InstanceConfig", "redirect") |
| 45 | + Redirect = collections.namedtuple("Redirect", "name host port") |
| 46 | + return { |
| 47 | + "instance_config": InstanceConfig( |
| 48 | + redirect=Redirect( |
| 49 | + name="postgres", |
| 50 | + host=postgres_settings["host"], |
| 51 | + port=postgres_settings["port"], |
| 52 | + ) |
| 53 | + ), |
| 54 | + "connect_params": { |
| 55 | + "user": postgres_settings["user"], |
| 56 | + "database": postgres_settings["dbname"], |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | +def test_rewrite_query_for_hll_column(plugin_context): |
| 62 | + src = ( |
| 63 | + 'SELECT COUNT(DISTINCT "crm_data_source"."Set of Customers") AS "ctd:Set of Customers:ok"\n' |
| 64 | + 'FROM "crm_dim"."crm_data_source" "crm_data_source"\n' |
| 65 | + "HAVING (COUNT(1) > 0);" |
| 66 | + ) |
| 67 | + |
| 68 | + res = hll.rewrite_query(src, plugin_context) |
| 69 | + assert "hll_cardinality(hll_union_agg" in res |
| 70 | + |
| 71 | + |
| 72 | +def test_plugin_module_loads_and_exposes_rewriter(): |
| 73 | + module = importlib.import_module("plugins.tableau_hll") |
| 74 | + assert hasattr(module, "rewrite_query") |
| 75 | + assert callable(module.rewrite_query) |
| 76 | + |
| 77 | + |
| 78 | +def test_does_not_rewrite_non_hll_column(plugin_context): |
| 79 | + src = ( |
| 80 | + 'SELECT COUNT(DISTINCT "crm_data_source"."Campaign Name") AS "ctd:Campaign Name:ok"\n' |
| 81 | + 'FROM "crm_dim"."crm_data_source" "crm_data_source"\n' |
| 82 | + "HAVING (COUNT(1) > 0);" |
| 83 | + ) |
| 84 | + |
| 85 | + res = hll.rewrite_query(src, plugin_context) |
| 86 | + assert "hll_cardinality(hll_union_agg" not in res |
0 commit comments