Skip to content

Commit

Permalink
Atualiza test_record.py
Browse files Browse the repository at this point in the history
  • Loading branch information
viniman27 authored Aug 18, 2024
1 parent 6474888 commit 7fb52f5
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions tests/test_record.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@

import pytest
import sys
import os
import uuid


sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src')))


from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from src.database import Base, get_db
from src.main import app


# Crie um banco de dados de teste em memória
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


# Dependência para usar o banco de dados de teste
def override_get_db():
try:
Expand All @@ -28,80 +25,72 @@ def override_get_db():
finally:
db.close()


app.dependency_overrides[get_db] = override_get_db


client = TestClient(app)


@pytest.fixture(scope="module")
def setup_database():
Base.metadata.create_all(bind=engine)
yield
Base.metadata.drop_all(bind=engine)


def test_add_to_record(setup_database):
user_id = str(uuid.uuid4())
video_id = str(uuid.uuid4())
timestamp = "2024-08-14 12:00:00"

response = client.post("/api/record/", json={"user_id": user_id, "videos": {video_id: timestamp}})
assert response.status_code == 200

# Verificar se o vídeo foi adicionado ao histórico
response = client.get("/api/record/get_record/", params={"user_id": user_id})
assert response.status_code == 200
assert "videos" in response.json()
assert response.json()["videos"] == {video_id: timestamp}


def test_toggle_tracking(setup_database):
user_id = str(uuid.uuid4())


# Desabilitar o rastreamento
response = client.post("/api/record/toggle_tracking/", params={"user_id": user_id, "track": "false"})
assert response.status_code == 200
assert response.json()["message"] in [
"Rastreamento desabilitado",
"Rastreamento desabilitado e novo registro criado"
]

# Verificar o status do rastreamento
response = client.get("/api/record/get_tracking_status/", params={"user_id": user_id})
assert response.status_code == 200
assert response.json()["track_enabled"] is False


# Habilitar o rastreamento novamente
response = client.post("/api/record/toggle_tracking/", params={"user_id": user_id, "track": "true"})
assert response.status_code == 200
assert response.json()["message"] in [
"Rastreamento habilitado",
"Rastreamento habilitado e novo registro criado"
]

# Verificar o status do rastreamento novamente
response = client.get("/api/record/get_tracking_status/", params={"user_id": user_id})
assert response.status_code == 200
assert response.json()["track_enabled"] is True


def test_get_record_sorted(setup_database):
user_id = str(uuid.uuid4())

# Adicionar múltiplos vídeos ao registro com diferentes timestamps
# Adicionar múltiplos vídeos ao registro com diferentes timestamps
videos = {
str(uuid.uuid4()): "2024-08-14 12:00:00",
str(uuid.uuid4()): "2024-08-14 13:00:00",
str(uuid.uuid4()): "2024-08-14 14:00:00"
}
for video_id, timestamp in videos.items():
client.post("/api/record/", json={"user_id": user_id, "videos": {video_id: timestamp}})

# Obter o registro ordenado de forma ascendente
# Obter o registro ordenado de forma ascendente
response = client.get("/api/record/get_record_sorted/", params={"user_id": user_id, "ascending": "true"})
assert response.status_code == 200
sorted_videos = response.json()["videos"]
Expand All @@ -120,6 +109,8 @@ def test_get_record_sorted(setup_database):
expected_order = sorted(videos.items(), key=lambda x: x[1], reverse=True)
sorted_keys = list(sorted_videos.keys())
assert sorted_keys == [k for k, v in expected_order] # Checa se está em ordem descendente



'''
def test_get_tracking_status(setup_database):
Expand Down

0 comments on commit 7fb52f5

Please sign in to comment.