-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Timescale backend teething troubles, take 2 (#243)
* add support for ssl connections to the ql db init container. * log pg conn data and test reading from env. * cater for new columns when exporting data from crate. * modularise env config and make it unit testable. * refactor tests to avoid nasty env var side-effects. * keep code climate happy. * implement pr feedback.
- Loading branch information
Showing
21 changed files
with
709 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pytest | ||
|
||
from translators.timescale import PostgresConnectionData | ||
|
||
|
||
def assert_conn_param(getter, env_var_name, env_var_value, | ||
expected_param_value=None): | ||
conn_data = PostgresConnectionData() | ||
default_value = getter(conn_data) | ||
conn_data.read_env(env={env_var_name: env_var_value}) | ||
|
||
if expected_param_value: | ||
assert getter(conn_data) == expected_param_value | ||
else: | ||
assert getter(conn_data) == default_value | ||
|
||
|
||
def get_host(c: PostgresConnectionData): return c.host | ||
|
||
|
||
def get_port(c: PostgresConnectionData): return c.port | ||
|
||
|
||
def get_ssl(c: PostgresConnectionData): return c.use_ssl | ||
|
||
|
||
def get_db_name(c: PostgresConnectionData): return c.db_name | ||
|
||
|
||
def get_db_user(c: PostgresConnectionData): return c.db_user | ||
|
||
|
||
def get_db_pass(c: PostgresConnectionData): return c.db_pass | ||
|
||
|
||
@pytest.mark.parametrize('getter, env_var_name', [ | ||
(get_host, 'POSTGRES_HOST'), | ||
(get_port, 'POSTGRES_PORT'), | ||
(get_ssl, 'POSTGRES_USE_SSL'), | ||
(get_db_name, 'POSTGRES_DB_NAME'), | ||
(get_db_user, 'POSTGRES_DB_USER'), | ||
(get_db_pass, 'POSTGRES_DB_PASS') | ||
]) | ||
def test_param_default(getter, env_var_name): | ||
assert_conn_param(getter, env_var_name, '') | ||
|
||
|
||
@pytest.mark.parametrize('getter, env_var_name, env_value, expected_param', [ | ||
(get_host, 'POSTGRES_HOST', ' my.host ', 'my.host'), | ||
(get_port, 'POSTGRES_PORT', ' 5432 ', 5432), | ||
(get_ssl, 'POSTGRES_USE_SSL', ' no ', False), | ||
(get_ssl, 'POSTGRES_USE_SSL', ' yes ', True), | ||
(get_db_name, 'POSTGRES_DB_NAME', 'quantumleap\n', 'quantumleap'), | ||
(get_db_user, 'POSTGRES_DB_USER', '\tquantumleap', 'quantumleap'), | ||
(get_db_pass, 'POSTGRES_DB_PASS', ' p4ss ', 'p4ss') | ||
]) | ||
def test_param(getter, env_var_name, env_value, expected_param): | ||
assert_conn_param(getter, env_var_name, env_value, expected_param) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,32 @@ | ||
import os | ||
import pytest | ||
import tempfile | ||
|
||
from translators.crate import CrateTranslator | ||
from translators.timescale import PostgresTranslator | ||
from translators.factory import QL_CONFIG_ENV_VAR, translator_for | ||
|
||
|
||
def write_config() -> str: | ||
temp = tempfile.TemporaryFile(mode='w+t') | ||
try: | ||
temp.writelines('') | ||
return temp.name | ||
finally: | ||
temp.close() | ||
from translators.factory import translator_for | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def with_config(): | ||
path = os.path.join(os.path.dirname(__file__), 'ql-config.yml') | ||
os.environ[QL_CONFIG_ENV_VAR] = path | ||
pg_port_var = 'POSTGRES_PORT' | ||
os.environ[pg_port_var] = '54320' | ||
yield {} | ||
os.environ[QL_CONFIG_ENV_VAR] = '' | ||
os.environ[pg_port_var] = '' | ||
# NOTE. Config file location set by run_tests.sh: | ||
# QL_CONFIG='src/translators/tests/ql-config.yml' | ||
|
||
|
||
def test_tenant1(with_config): | ||
def test_tenant1(): | ||
with translator_for('t1') as t: | ||
assert isinstance(t, PostgresTranslator) | ||
|
||
|
||
def test_tenant2(with_config): | ||
def test_tenant2(): | ||
with translator_for('t2') as t: | ||
assert isinstance(t, CrateTranslator) | ||
|
||
|
||
def test_tenant3(with_config): | ||
def test_tenant3(): | ||
with translator_for('t3') as t: | ||
assert isinstance(t, PostgresTranslator) | ||
|
||
|
||
def test_unknown_tenant(with_config): | ||
def test_unknown_tenant(): | ||
with translator_for('not-in-config') as t: | ||
assert isinstance(t, CrateTranslator) | ||
|
||
|
||
def test_no_tenant(with_config): | ||
def test_no_tenant(): | ||
with translator_for(None) as t: | ||
assert isinstance(t, CrateTranslator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.