-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from nrccua/ARCH-532-add-decorator-to-manage-d…
…b-connections Adding DB decorator for DAG usage primarily
- Loading branch information
Showing
12 changed files
with
231 additions
and
27 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
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,36 @@ | ||
"""Pyodbc functions for connecting and send queries.""" | ||
|
||
# pylint: disable=c-extension-no-member | ||
# pylint: disable=too-many-arguments | ||
|
||
import psycopg2 | ||
|
||
|
||
async def establish_psycopg2_connection( | ||
host: str, | ||
user: str, | ||
password: str, | ||
database: str, | ||
port: int=5432, | ||
is_audit: bool=False | ||
): | ||
"""Acquire the psycopg2 connection object. | ||
Args: | ||
host (str): Host | ||
user (str): User | ||
password (str): Password | ||
database (str): Database | ||
port (int, optional): Port. Defaults to 5432. | ||
is_audit (bool, optional): Audit queries. Defaults to False. | ||
Returns: | ||
pyscopg2 Connection object | ||
""" | ||
|
||
conn = psycopg2.connect(host=host, port=port, user=user, password=password, dbname=database) | ||
|
||
if is_audit: | ||
conn.autocommit=True | ||
|
||
return conn |
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
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,24 @@ | ||
"""pytest psycopg2 script.""" | ||
|
||
import json | ||
|
||
import pytest | ||
|
||
from aioradio.aws.secrets import get_secret | ||
from aioradio.psycopg2 import establish_psycopg2_connection | ||
|
||
pytestmark = pytest.mark.asyncio | ||
|
||
|
||
async def test_establish_psycopg2_connection(github_action, user): | ||
"""Test establish_psycopg2_connection.""" | ||
|
||
if github_action: | ||
pytest.skip('Skip test_establish_psycopg2_connection when running via Github Action') | ||
elif user != 'tim.reichard': | ||
pytest.skip('Skip test_establish_psycopg2_connection since user is not Tim Reichard') | ||
|
||
creds = json.loads(await get_secret('datalab/dev/classplanner_db', 'us-east-1')) | ||
conn = await establish_psycopg2_connection(**creds, database='student') | ||
assert conn.closed == 0 | ||
conn.close() |
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,4 +1,4 @@ | ||
"""pytest file_ingestion script.""" | ||
"""pytest pyodbc script.""" | ||
|
||
import os | ||
|
||
|
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.