Skip to content

Commit

Permalink
update source/db.py for generic insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
CarduCaldeira committed Jun 2, 2024
1 parent 20e08ce commit 8f8a89d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
3 changes: 2 additions & 1 deletion source/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from typing import Dict


def config():
def config() -> Dict[str,str]:

dict_param ={'dbname': os.getenv("DB_NAME"),
'user': os.getenv("DB_USER"),
Expand Down
22 changes: 5 additions & 17 deletions source/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,22 @@
from source.config import config


def create_database_connection():
def create_database_connection() -> psycopg2.extensions.connection:

params = config()
print('Connecting to the postgreSQL database ...')
connection = psycopg2.connect(**params)

return connection

def insert_list_database(data_list: List[Tuple]):
def insert_list_database(sql_query: str, data_list: List[Tuple]) -> None:

connection = create_database_connection()
cursor = connection.cursor()
inserted_ids = []

for data in data_list:
cursor.execute("""
INSERT INTO raw_db.news (
author, title, description, url, image_url, publication_date,
content, tags, source, query0, query1, query2
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING id;
""", data)
inserted_id = cursor.fetchone()[0]
inserted_ids.append(inserted_id)

cursor.executemany(sql_query, data_list)

connection.commit()

cursor.close()
connection.close()

return inserted_ids
connection.close()
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from faker import Faker
from typing import List, Tuple


from source.db import create_database_connection

Expand All @@ -14,7 +16,7 @@ def db_connection():

#scope = function inicializa create_fake_news para para cada função testado
@pytest.fixture(scope='function')
def create_fake_news():
def create_fake_news() -> List[Tuple]:

lists = []
news = []
Expand Down
25 changes: 18 additions & 7 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@
def test_insert_database(db_connection, create_fake_news):

fake_list = create_fake_news
id = insert_list_database(fake_list)[0]

sql_command = """
INSERT INTO raw_db.news (
author, title, description, url, image_url, publication_date,
content, tags, source, query0, query1, query2
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
"""

insert_list_database(sql_command, fake_list)

cursor = db_connection.cursor()
cursor.execute("""SELECT author, title, description,
url, image_url, publication_date,
content, tags, source, query0, query1, query2
FROM raw_db.news WHERE id = %s;""", (id,))
result = cursor.fetchall()
sql_select = "SELECT id FROM raw_db.news WHERE title IN %s;"

titles = tuple([fake_list[0][1]])

# Executando o comando 'SELECT'
cursor.execute(sql_select, (titles,))
id = cursor.fetchone()[0]

cursor.close()

assert result == fake_list
assert id is not None

0 comments on commit 8f8a89d

Please sign in to comment.