python-databases 0.1.2__tar.gz → 0.1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_databases-0.1.2/python_databases.egg-info → python_databases-0.1.4}/PKG-INFO +1 -1
- {python_databases-0.1.2 → python_databases-0.1.4}/python_databases/elasticsearch_infrastructure/elasticsearch.py +5 -4
- python_databases-0.1.4/python_databases/postgresql_infrastructure/__init__.py +7 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/python_databases/postgresql_infrastructure/postgresql.py +4 -3
- {python_databases-0.1.2 → python_databases-0.1.4/python_databases.egg-info}/PKG-INFO +1 -1
- {python_databases-0.1.2 → python_databases-0.1.4}/python_databases.egg-info/SOURCES.txt +2 -1
- {python_databases-0.1.2 → python_databases-0.1.4}/setup.py +1 -1
- python_databases-0.1.4/tests/test_postgres.py +46 -0
- python_databases-0.1.2/python_databases/postgresql_infrastructure/__init__.py +0 -3
- {python_databases-0.1.2 → python_databases-0.1.4}/LICENSE +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/MANIFEST.in +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/README.md +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/pyproject.toml +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/python_databases/__init__.py +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/python_databases/elasticsearch_infrastructure/__init__.py +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/python_databases.egg-info/dependency_links.txt +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/python_databases.egg-info/requires.txt +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/python_databases.egg-info/top_level.txt +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/requirements.txt +0 -0
- {python_databases-0.1.2 → python_databases-0.1.4}/setup.cfg +0 -0
|
@@ -6,6 +6,7 @@ from datetime import datetime, timezone
|
|
|
6
6
|
from enum import Enum
|
|
7
7
|
from typing import Optional, Any
|
|
8
8
|
import pandas as pd
|
|
9
|
+
from custom_python_logger import get_logger
|
|
9
10
|
from elasticsearch import Elasticsearch, helpers
|
|
10
11
|
from retrying import retry
|
|
11
12
|
|
|
@@ -25,7 +26,7 @@ class ElasticSearch(ABC):
|
|
|
25
26
|
username: Optional[str],
|
|
26
27
|
password: Optional[str]
|
|
27
28
|
):
|
|
28
|
-
self.logger =
|
|
29
|
+
self.logger = get_logger(self.__class__.__name__)
|
|
29
30
|
self._change_elasticsearch_logger()
|
|
30
31
|
|
|
31
32
|
self.protocol = protocol
|
|
@@ -43,7 +44,7 @@ class ElasticSearch(ABC):
|
|
|
43
44
|
self.elk_client = None
|
|
44
45
|
|
|
45
46
|
def _change_elasticsearch_logger(self):
|
|
46
|
-
tracer =
|
|
47
|
+
tracer = get_logger('elasticsearch')
|
|
47
48
|
tracer.setLevel(logging.CRITICAL) # or desired level
|
|
48
49
|
# tracer.addHandler(logging.FileHandler('indexer.log'))
|
|
49
50
|
|
|
@@ -260,7 +261,7 @@ class ElasticSearchOnPrem(ElasticSearch):
|
|
|
260
261
|
password=password
|
|
261
262
|
)
|
|
262
263
|
|
|
263
|
-
self.logger =
|
|
264
|
+
self.logger = get_logger(self.__class__.__name__)
|
|
264
265
|
|
|
265
266
|
self.connect_to_elasticsearch()
|
|
266
267
|
|
|
@@ -303,7 +304,7 @@ class ElasticSearchCloud(ElasticSearch):
|
|
|
303
304
|
password=password
|
|
304
305
|
)
|
|
305
306
|
|
|
306
|
-
self.logger =
|
|
307
|
+
self.logger = get_logger(self.__class__.__name__)
|
|
307
308
|
|
|
308
309
|
self.connect_to_elasticsearch()
|
|
309
310
|
|
|
@@ -2,6 +2,7 @@ import logging
|
|
|
2
2
|
import os
|
|
3
3
|
from typing import Optional, Union, Any
|
|
4
4
|
import psycopg2
|
|
5
|
+
from custom_python_logger import get_logger
|
|
5
6
|
from psycopg2 import extras
|
|
6
7
|
from retrying import retry
|
|
7
8
|
|
|
@@ -15,7 +16,7 @@ class PostgreSQL:
|
|
|
15
16
|
username: Optional[str] = None,
|
|
16
17
|
password: Optional[str] = None
|
|
17
18
|
):
|
|
18
|
-
self.logger =
|
|
19
|
+
self.logger = get_logger(self.__class__.__name__)
|
|
19
20
|
|
|
20
21
|
self.name = name or os.getenv("POSTGRESQL_NAME")
|
|
21
22
|
self.host = host or os.getenv("POSTGRESQL_HOST")
|
|
@@ -42,7 +43,7 @@ class PostgreSQL:
|
|
|
42
43
|
self.create_connection()
|
|
43
44
|
return self._cursor
|
|
44
45
|
|
|
45
|
-
@retry(stop_max_attempt_number=3, wait_fixed=180000)
|
|
46
|
+
# @retry(stop_max_attempt_number=3, wait_fixed=180000)
|
|
46
47
|
def create_connection(self):
|
|
47
48
|
self.logger.info(f"Create connection to {self.host}:{self.port}")
|
|
48
49
|
|
|
@@ -53,7 +54,7 @@ class PostgreSQL:
|
|
|
53
54
|
user=self.username,
|
|
54
55
|
password=self.password,
|
|
55
56
|
)
|
|
56
|
-
self._cursor = self.
|
|
57
|
+
self._cursor = self._client.cursor(cursor_factory=extras.DictCursor)
|
|
57
58
|
|
|
58
59
|
def execute_query(self, query: str, params: Optional[Union[tuple, dict[str, Any]]] = None) -> None:
|
|
59
60
|
self.logger.debug(f"Fetch rows from DB by query {query} with params {params}")
|
|
@@ -13,4 +13,5 @@ python_databases.egg-info/top_level.txt
|
|
|
13
13
|
python_databases/elasticsearch_infrastructure/__init__.py
|
|
14
14
|
python_databases/elasticsearch_infrastructure/elasticsearch.py
|
|
15
15
|
python_databases/postgresql_infrastructure/__init__.py
|
|
16
|
-
python_databases/postgresql_infrastructure/postgresql.py
|
|
16
|
+
python_databases/postgresql_infrastructure/postgresql.py
|
|
17
|
+
tests/test_postgres.py
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# test_postgresql.py
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from unittest.mock import patch, MagicMock
|
|
5
|
+
from python_databases.postgresql_infrastructure import PostgreSQL
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@pytest.fixture
|
|
9
|
+
def mock_connection():
|
|
10
|
+
with patch("psycopg2.connect") as mock_connect:
|
|
11
|
+
mock_conn = MagicMock()
|
|
12
|
+
mock_cursor = MagicMock()
|
|
13
|
+
mock_conn.cursor.return_value = mock_cursor
|
|
14
|
+
mock_connect.return_value = mock_conn
|
|
15
|
+
yield mock_conn, mock_cursor
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_connection_created(mock_connection):
|
|
19
|
+
conn, _ = mock_connection
|
|
20
|
+
db = PostgreSQL(name="test_db", host="localhost", port="5432", username="user", password="pass")
|
|
21
|
+
assert db.client == conn
|
|
22
|
+
assert conn.cursor.call_count == 2
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_execute_query_success(mock_connection):
|
|
26
|
+
_, cursor = mock_connection
|
|
27
|
+
db = PostgreSQL(name="test_db", host="localhost", port="5432", username="user", password="pass")
|
|
28
|
+
db.execute_query("SELECT 1", None)
|
|
29
|
+
cursor.execute.assert_called_with("SELECT 1", None)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_fetch_all_success(mock_connection):
|
|
33
|
+
_, cursor = mock_connection
|
|
34
|
+
cursor.fetchall.return_value = [{"id": 1}]
|
|
35
|
+
db = PostgreSQL(name="test_db", host="localhost", port="5432", username="user", password="pass")
|
|
36
|
+
results = db.fetch_all("SELECT id FROM test")
|
|
37
|
+
cursor.execute.assert_called_with("SELECT id FROM test", None)
|
|
38
|
+
assert results == [{"id": 1}]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_context_manager_closes_connection(mock_connection):
|
|
42
|
+
conn, cursor = mock_connection
|
|
43
|
+
with PostgreSQL(name="test_db", host="localhost", port="5432", username="user", password="pass") as db:
|
|
44
|
+
pass
|
|
45
|
+
cursor.close.assert_called_once()
|
|
46
|
+
conn.close.assert_called_once()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{python_databases-0.1.2 → python_databases-0.1.4}/python_databases.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|