Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions tests/unit/test_db_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import uuid
from unittest.mock import MagicMock

import pytest
from fastapi.responses import Response

# Adjust import to real path
from app.db.connector import DB


# Fake connection and pool classes as per provided tests
class FakeConn:
def __init__(self):
self.cursor_obj = MagicMock()

def cursor(self):
return self.cursor_obj

def commit(self):
pass

def close(self):
pass


class FakePool:
def __init__(self, minconn, maxconn, dsn):
# can assert on minconn, maxconn, dsn if desired
self._conn = FakeConn()
self.getconn = MagicMock(return_value=self._conn)
self.putconn = MagicMock()
self.closeall = MagicMock()


@pytest.fixture(autouse=True)
def fake_pool(monkeypatch):
# Patch environment and pool initializer
monkeypatch.setenv("DATABASE_URL", "postgres://fake")
monkeypatch.setattr(
"app.db.connector.pool.SimpleConnectionPool",
lambda minconn, maxconn, dsn: FakePool(minconn, maxconn, dsn),
)
# Reset singleton
DB._instance = None
yield


def test_create_table_and_store_chat():
# Instantiate DB so __init__ runs create_table
db = DB(minconn=2, maxconn=5)
fake_pool = db.pool
conn = fake_pool.getconn()
# Check that DDL for chats table was executed
expected_ddl = """
CREATE TABLE IF NOT EXISTS chats (
uid UUID PRIMARY KEY,
question TEXT NOT NULL,
answer TEXT NOT NULL,
votes INTEGER DEFAULT 0,
comment TEXT,
user_id UUID
);
"""
conn.cursor_obj.execute.assert_any_call(expected_ddl)
# putconn called for initialization
fake_pool.putconn.assert_called_with(conn)

# Now test store_chat
fake_pool.getconn.reset_mock()
fake_pool.putconn.reset_mock()
conn.cursor_obj.reset_mock()

test_uid = str(uuid.uuid4())
test_user = uuid.uuid4()
db.store_chat("¿Hola?", "¡Hola!", test_uid, test_user)

# Verify INSERT called with correct SQL and params
conn.cursor_obj.execute.assert_called_once_with(
"""
INSERT into chats (uid, question, answer, votes, user_id) values (%s, %s, %s, %s, %s);
""",
(test_uid, "¿Hola?", "¡Hola!", 0, str(test_user)),
)
fake_pool.putconn.assert_called_with(conn)


def test_change_votes_success_up():
db = DB()
# Prepare inputs
chat_id = "11111111-1111-1111-1111-111111111111"
user_id = uuid.UUID(int=0)
# Call change_votes to upvote
response = db.change_votes(
chat_id=chat_id,
up=True,
down=False,
equal=False,
user_id=user_id,
comment="Comentario",
)

assert isinstance(response, Response)
assert response.status_code == 204

# Inspect cursor calls
fake_conn = db.pool.getconn()
fake_cursor = fake_conn.cursor_obj
calls = fake_cursor.execute.call_args_list
assert calls, "cursor.execute was not called"

expected_params = (
1,
"Comentario",
chat_id,
str(user_id),
)
# Find call with UPDATE chats and expected params
found = any(
"UPDATE chats" in call.args[0] and call.args[1] == expected_params
for call in calls
)
assert found, f"No UPDATE chats call with params {expected_params}, calls: {calls}"
# Ensure connection returned
assert db.pool.putconn.called