|
2 | 2 | from unittest.mock import MagicMock |
3 | 3 |
|
4 | 4 | import pytest |
| 5 | +from fastapi import Response |
5 | 6 |
|
6 | 7 | from app.db.connector import DB # ajusta el import a tu ruta real |
7 | 8 |
|
@@ -82,3 +83,53 @@ def test_create_table_and_store_chat(tmp_path): |
82 | 83 | ) |
83 | 84 | # Commit y cierre |
84 | 85 | fake_pool.putconn.assert_called_with(conn) |
| 86 | + |
| 87 | + |
| 88 | +def test_change_votes_success_up(): |
| 89 | + db = DB() |
| 90 | + resp = db.change_votes( |
| 91 | + chat_id="11111111-1111-1111-1111-111111111111", |
| 92 | + up=True, |
| 93 | + down=False, |
| 94 | + equal=False, |
| 95 | + user_id=uuid.UUID(int=0), |
| 96 | + comment="Comentario", |
| 97 | + ) |
| 98 | + |
| 99 | + # 1) Verifica la respuesta |
| 100 | + assert isinstance(resp, Response) |
| 101 | + assert resp.status_code == 204 |
| 102 | + |
| 103 | + # 2) Inspecciona los calls al cursor |
| 104 | + fake_conn = db.pool.getconn() |
| 105 | + fake_cursor = fake_conn.cursor_obj # MagicMock |
| 106 | + |
| 107 | + # Debió llamarse execute al menos una vez |
| 108 | + calls = fake_cursor.execute.call_args_list |
| 109 | + assert calls, "No se llamó a cursor.execute()" |
| 110 | + |
| 111 | + # Parámetros esperados |
| 112 | + expected_params = ( |
| 113 | + 1, |
| 114 | + "Comentario", |
| 115 | + "11111111-1111-1111-1111-111111111111", |
| 116 | + str(uuid.UUID(int=0)), |
| 117 | + ) |
| 118 | + |
| 119 | + # Busca alguna llamada cuyo SQL contenga "UPDATE chats" |
| 120 | + # y cuyos params coincidan |
| 121 | + found = False |
| 122 | + for call in calls: |
| 123 | + sql_arg = call.args[0].strip() |
| 124 | + params_arg = call.args[1] |
| 125 | + if "UPDATE chats" in sql_arg and params_arg == expected_params: |
| 126 | + found = True |
| 127 | + break |
| 128 | + |
| 129 | + assert found, ( |
| 130 | + "No se encontró un call a execute() con UPDATE chats y " |
| 131 | + f"params={expected_params}. Calls realizados: {calls}" |
| 132 | + ) |
| 133 | + |
| 134 | + # 3) Verifica que devolvió la conexión al pool |
| 135 | + assert db.pool.putconn.called, "No se llamó a pool.putconn()" |
0 commit comments