diff --git a/README.md b/README.md index b0d9412e..674bafe4 100644 --- a/README.md +++ b/README.md @@ -894,6 +894,10 @@ models: |----------------------------------|--------------------|-------------| | `DATACONTRACT_POSTGRES_USERNAME` | `postgres` | Username | | `DATACONTRACT_POSTGRES_PASSWORD` | `mysecretpassword` | Password | +| `DATACONTRACT_POSTGRES_HOST` | `localhost` | Hostname | +| `DATACONTRACT_POSTGRES_PORT` | `5432` | Port | +| `DATACONTRACT_POSTGRES_DATABASE` | `dbname` | Database | +| `DATACONTRACT_POSTGRES_SCHEMA` | `public` | Schema | #### Trino @@ -2157,4 +2161,4 @@ Created by [Stefan Negele](https://www.linkedin.com/in/stefan-negele-573153112/) - + \ No newline at end of file diff --git a/datacontract/engines/soda/connections/postgres.py b/datacontract/engines/soda/connections/postgres.py index 29b5c90c..cfc03f8d 100644 --- a/datacontract/engines/soda/connections/postgres.py +++ b/datacontract/engines/soda/connections/postgres.py @@ -8,14 +8,14 @@ def to_postgres_soda_configuration(server): soda_configuration = { f"data_source {server.type}": { "type": "postgres", - "host": server.host, - "port": str(server.port), + "host": os.getenv("DATACONTRACT_POSTGRES_HOST", server.host), + "port": os.getenv("DATACONTRACT_POSTGRES_PORT", str(server.port)), "username": os.getenv("DATACONTRACT_POSTGRES_USERNAME"), "password": os.getenv("DATACONTRACT_POSTGRES_PASSWORD"), - "database": server.database, - "schema": server.schema_, + "database": os.getenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + "schema": os.getenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), } } soda_configuration_str = yaml.dump(soda_configuration) - return soda_configuration_str + return soda_configuration_str \ No newline at end of file diff --git a/tests/test_test_postgres.py b/tests/test_test_postgres.py index d5dc573c..b164d4c4 100644 --- a/tests/test_test_postgres.py +++ b/tests/test_test_postgres.py @@ -24,6 +24,10 @@ def remove_container(): def test_test_postgres(postgres_container, monkeypatch): monkeypatch.setenv("DATACONTRACT_POSTGRES_USERNAME", postgres.username) monkeypatch.setenv("DATACONTRACT_POSTGRES_PASSWORD", postgres.password) + monkeypatch.setenv("DATACONTRACT_POSTGRES_HOST", server.host), + monkeypatch.setenv("DATACONTRACT_POSTGRES_PORT", server.port), + monkeypatch.setenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + monkeypatch.setenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), _init_sql("fixtures/postgres/data/data.sql") datacontract_file = "fixtures/postgres/datacontract.yaml" @@ -40,6 +44,10 @@ def test_test_postgres(postgres_container, monkeypatch): def test_test_postgres_odcs(postgres_container, monkeypatch): monkeypatch.setenv("DATACONTRACT_POSTGRES_USERNAME", postgres.username) monkeypatch.setenv("DATACONTRACT_POSTGRES_PASSWORD", postgres.password) + monkeypatch.setenv("DATACONTRACT_POSTGRES_HOST", server.host), + monkeypatch.setenv("DATACONTRACT_POSTGRES_PORT", server.port), + monkeypatch.setenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + monkeypatch.setenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), _init_sql("fixtures/postgres/data/data.sql") datacontract_file = "fixtures/postgres/odcs.yaml" @@ -56,6 +64,10 @@ def test_test_postgres_odcs(postgres_container, monkeypatch): def test_test_postgres_case_sensitive_table_name(postgres_container, monkeypatch): monkeypatch.setenv("DATACONTRACT_POSTGRES_USERNAME", postgres.username) monkeypatch.setenv("DATACONTRACT_POSTGRES_PASSWORD", postgres.password) + monkeypatch.setenv("DATACONTRACT_POSTGRES_HOST", server.host), + monkeypatch.setenv("DATACONTRACT_POSTGRES_PORT", server.port), + monkeypatch.setenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + monkeypatch.setenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), _init_sql("fixtures/postgres/data/data_case_sensitive.sql") datacontract_file = "fixtures/postgres/datacontract_case_sensitive.yaml" @@ -72,6 +84,10 @@ def test_test_postgres_case_sensitive_table_name(postgres_container, monkeypatch def test_test_postgres_case_sensitive_table_name_odcs(postgres_container, monkeypatch): monkeypatch.setenv("DATACONTRACT_POSTGRES_USERNAME", postgres.username) monkeypatch.setenv("DATACONTRACT_POSTGRES_PASSWORD", postgres.password) + monkeypatch.setenv("DATACONTRACT_POSTGRES_HOST", server.host), + monkeypatch.setenv("DATACONTRACT_POSTGRES_PORT", server.port), + monkeypatch.setenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + monkeypatch.setenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), _init_sql("fixtures/postgres/data/data_case_sensitive.sql") datacontract_file = "fixtures/postgres/odcs_case_sensitive.odcs.yaml" @@ -88,6 +104,10 @@ def test_test_postgres_case_sensitive_table_name_odcs(postgres_container, monkey def test_test_postgres_servicelevels_freshness_should_fail(postgres_container, monkeypatch): monkeypatch.setenv("DATACONTRACT_POSTGRES_USERNAME", postgres.username) monkeypatch.setenv("DATACONTRACT_POSTGRES_PASSWORD", postgres.password) + monkeypatch.setenv("DATACONTRACT_POSTGRES_HOST", server.host), + monkeypatch.setenv("DATACONTRACT_POSTGRES_PORT", server.port), + monkeypatch.setenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + monkeypatch.setenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), _init_sql("fixtures/postgres/data/data.sql") datacontract_file = "fixtures/postgres/datacontract_servicelevels.yaml" @@ -103,6 +123,10 @@ def test_test_postgres_servicelevels_freshness_should_fail(postgres_container, m def test_test_postgres_servicelevels_freshness_should_fail_odcs(postgres_container, monkeypatch): monkeypatch.setenv("DATACONTRACT_POSTGRES_USERNAME", postgres.username) monkeypatch.setenv("DATACONTRACT_POSTGRES_PASSWORD", postgres.password) + monkeypatch.setenv("DATACONTRACT_POSTGRES_HOST", server.host), + monkeypatch.setenv("DATACONTRACT_POSTGRES_PORT", server.port), + monkeypatch.setenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + monkeypatch.setenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), _init_sql("fixtures/postgres/data/data.sql") datacontract_file = "fixtures/postgres/servicelevels.odcs.yaml" @@ -149,4 +173,4 @@ def _init_sql(file_path): cursor.execute(sql_commands) connection.commit() cursor.close() - connection.close() + connection.close() \ No newline at end of file diff --git a/tests/test_test_quality.py b/tests/test_test_quality.py index 3fac59c1..61094305 100644 --- a/tests/test_test_quality.py +++ b/tests/test_test_quality.py @@ -21,6 +21,10 @@ def remove_container(): def test_test_quality_valid(postgres_container, monkeypatch): monkeypatch.setenv("DATACONTRACT_POSTGRES_USERNAME", postgres.username) monkeypatch.setenv("DATACONTRACT_POSTGRES_PASSWORD", postgres.password) + monkeypatch.setenv("DATACONTRACT_POSTGRES_HOST", server.host), + monkeypatch.setenv("DATACONTRACT_POSTGRES_PORT", server.port), + monkeypatch.setenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + monkeypatch.setenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), _init_sql("fixtures/quality/data/data.valid.sql") datacontract_file = "fixtures/quality/datacontract.yaml" data_contract_str = _setup_datacontract(datacontract_file) @@ -36,6 +40,10 @@ def test_test_quality_valid(postgres_container, monkeypatch): def test_test_quality_invalid(postgres_container, monkeypatch): monkeypatch.setenv("DATACONTRACT_POSTGRES_USERNAME", postgres.username) monkeypatch.setenv("DATACONTRACT_POSTGRES_PASSWORD", postgres.password) + monkeypatch.setenv("DATACONTRACT_POSTGRES_HOST", server.host), + monkeypatch.setenv("DATACONTRACT_POSTGRES_PORT", server.port), + monkeypatch.setenv("DATACONTRACT_POSTGRES_DATABASE", server.database), + monkeypatch.setenv("DATACONTRACT_POSTGRES_SCHEMA", server.schema_), _init_sql("fixtures/quality/data/data.invalid.sql") datacontract_file = "fixtures/quality/datacontract.yaml" data_contract_str = _setup_datacontract(datacontract_file) @@ -98,4 +106,4 @@ def _init_sql(file_path): cursor.execute(sql_commands) connection.commit() cursor.close() - connection.close() + connection.close() \ No newline at end of file