python-databases 0.1.2__tar.gz → 0.1.3__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.3}/PKG-INFO +1 -1
- python_databases-0.1.3/python_databases/postgresql_infrastructure/__init__.py +7 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/python_databases/postgresql_infrastructure/postgresql.py +2 -2
- {python_databases-0.1.2 → python_databases-0.1.3/python_databases.egg-info}/PKG-INFO +1 -1
- {python_databases-0.1.2 → python_databases-0.1.3}/python_databases.egg-info/SOURCES.txt +2 -1
- {python_databases-0.1.2 → python_databases-0.1.3}/setup.py +1 -1
- python_databases-0.1.3/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.3}/LICENSE +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/MANIFEST.in +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/README.md +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/pyproject.toml +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/python_databases/__init__.py +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/python_databases/elasticsearch_infrastructure/__init__.py +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/python_databases/elasticsearch_infrastructure/elasticsearch.py +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/python_databases.egg-info/dependency_links.txt +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/python_databases.egg-info/requires.txt +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/python_databases.egg-info/top_level.txt +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/requirements.txt +0 -0
- {python_databases-0.1.2 → python_databases-0.1.3}/setup.cfg +0 -0
|
@@ -42,7 +42,7 @@ class PostgreSQL:
|
|
|
42
42
|
self.create_connection()
|
|
43
43
|
return self._cursor
|
|
44
44
|
|
|
45
|
-
@retry(stop_max_attempt_number=3, wait_fixed=180000)
|
|
45
|
+
# @retry(stop_max_attempt_number=3, wait_fixed=180000)
|
|
46
46
|
def create_connection(self):
|
|
47
47
|
self.logger.info(f"Create connection to {self.host}:{self.port}")
|
|
48
48
|
|
|
@@ -53,7 +53,7 @@ class PostgreSQL:
|
|
|
53
53
|
user=self.username,
|
|
54
54
|
password=self.password,
|
|
55
55
|
)
|
|
56
|
-
self._cursor = self.
|
|
56
|
+
self._cursor = self._client.cursor(cursor_factory=extras.DictCursor)
|
|
57
57
|
|
|
58
58
|
def execute_query(self, query: str, params: Optional[Union[tuple, dict[str, Any]]] = None) -> None:
|
|
59
59
|
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
|
|
File without changes
|
{python_databases-0.1.2 → python_databases-0.1.3}/python_databases.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|