203 lines
7.1 KiB
Python
203 lines
7.1 KiB
Python
import pytest
|
|
from datetime import date, datetime, timedelta
|
|
from app import db
|
|
from app.models.career import Career
|
|
from app.models.classroom import Classroom
|
|
from app.models.subject import Subject, Commission
|
|
from app.models.reservation import Reservation
|
|
from app.models.building import Building
|
|
from app.models.user import User
|
|
from app.services.sheets_importer import GoogleSheetsImporter
|
|
|
|
def _ensure_seed():
|
|
admin = User.query.filter_by(email='admin@edu-space.com').first()
|
|
if not admin:
|
|
admin = User(email='admin@edu-space.com', name='Admin', role='ADMIN', is_active=True)
|
|
admin.set_password('admin123')
|
|
db.session.add(admin)
|
|
b = Building.query.filter_by(name='Edificio Central').first()
|
|
if not b:
|
|
b = Building(name='Edificio Central', code='CENTRAL', floors=4)
|
|
db.session.add(b)
|
|
db.session.flush()
|
|
c = Classroom.query.filter_by(code='101').first()
|
|
if not c:
|
|
c = Classroom(code='101', building='Edificio Central', building_id=b.id, floor='1', capacity=35, is_active=True)
|
|
db.session.add(c)
|
|
db.session.commit()
|
|
|
|
def test_sync_single_date_only_creates_one_reservation(init_database, app_context):
|
|
_ensure_seed()
|
|
importer = GoogleSheetsImporter()
|
|
row = {
|
|
'dia': 'Lunes',
|
|
'codigo': 'TEST-001',
|
|
'asignatura': 'Materia Test Puntual',
|
|
'aula': '101',
|
|
'carrera': 'DATOS (CCC)',
|
|
'horario': '08:00 a 10:00hs',
|
|
'turno': 'Mañana'
|
|
}
|
|
manual_dt_str = '2026-10-19'
|
|
stats = importer.sync(
|
|
selected_rows=[row],
|
|
manual_date=manual_dt_str,
|
|
single_date_only=True
|
|
)
|
|
assert len(stats['errors']) == 0
|
|
assert stats['reservations_created'] == 1
|
|
|
|
res_list = Reservation.query.filter(Reservation.purpose.like('%TEST-001%')).all()
|
|
assert len(res_list) == 1
|
|
assert res_list[0].start_time.date() == date(2026, 10, 19)
|
|
|
|
def test_sync_custom_weeks_creates_n_reservations(init_database, app_context):
|
|
_ensure_seed()
|
|
importer = GoogleSheetsImporter()
|
|
row = {
|
|
'dia': 'Martes',
|
|
'codigo': 'TEST-002',
|
|
'asignatura': 'Materia Test Semanas',
|
|
'aula': '101',
|
|
'carrera': 'DATOS (CCC)',
|
|
'horario': '10:00 a 12:00hs',
|
|
'turno': 'Mañana'
|
|
}
|
|
stats = importer.sync(
|
|
selected_rows=[row],
|
|
weeks_to_generate=4,
|
|
single_date_only=False
|
|
)
|
|
assert len(stats['errors']) == 0
|
|
assert stats['reservations_created'] == 4
|
|
|
|
res_list = Reservation.query.filter(Reservation.purpose.like('%TEST-002%')).order_by(Reservation.start_time.asc()).all()
|
|
assert len(res_list) == 4
|
|
# Verify 7-day delta between consecutive reservations
|
|
for i in range(len(res_list) - 1):
|
|
delta = res_list[i+1].start_time.date() - res_list[i].start_time.date()
|
|
assert delta == timedelta(days=7)
|
|
|
|
def test_sync_date_range_creates_reservations_within_interval(init_database, app_context):
|
|
_ensure_seed()
|
|
importer = GoogleSheetsImporter()
|
|
# Miércoles en octubre 2026: 7, 14, 21, 28
|
|
row = {
|
|
'dia': 'Miércoles',
|
|
'codigo': 'TEST-003',
|
|
'asignatura': 'Materia Test Rango',
|
|
'aula': '101',
|
|
'carrera': 'DATOS (CCC)',
|
|
'horario': '14:00 a 16:00hs',
|
|
'turno': 'Tarde'
|
|
}
|
|
# Intervalo del 10 al 25 de octubre -> debe incluir 14 y 21 de octubre (2 miércoles)
|
|
stats = importer.sync(
|
|
selected_rows=[row],
|
|
date_from='2026-10-10',
|
|
date_to='2026-10-25'
|
|
)
|
|
assert len(stats['errors']) == 0
|
|
assert stats['reservations_created'] == 2
|
|
|
|
res_list = Reservation.query.filter(Reservation.purpose.like('%TEST-003%')).order_by(Reservation.start_time.asc()).all()
|
|
assert len(res_list) == 2
|
|
dates = [r.start_time.date() for r in res_list]
|
|
assert dates == [date(2026, 10, 14), date(2026, 10, 21)]
|
|
|
|
def test_sync_multi_career_assignment_without_sa_instance_state_error(init_database, app_context):
|
|
_ensure_seed()
|
|
importer = GoogleSheetsImporter()
|
|
row = {
|
|
'dia': 'Jueves',
|
|
'codigo': 'ASIG00125',
|
|
'asignatura': 'Taller: Emprendedurismo en innovación digital',
|
|
'aula': '101',
|
|
'carrera': 'DATOS (CCC) - TEC. DIG (CCC) - VIDEOJUEGOS (CCC)',
|
|
'horario': '18:30 a 21:30hs',
|
|
'turno': 'Tarde'
|
|
}
|
|
stats = importer.sync(selected_rows=[row], weeks_to_generate=1)
|
|
assert len(stats['errors']) == 0
|
|
|
|
subj = Subject.query.filter_by(code='ASIG00125').first()
|
|
assert subj is not None
|
|
assert len(subj.careers) == 3
|
|
c_names = [c.name for c in subj.careers]
|
|
assert 'DATOS (CCC)' in c_names
|
|
assert 'TEC. DIG. (CCC)' in c_names or 'TEC. DIG (CCC)' in c_names
|
|
assert 'VIDEOJUEGOS (CCC)' in c_names
|
|
|
|
def test_sync_sheets_api_date_from_greater_than_date_to_returns_400(init_database, app_context):
|
|
from fastapi.testclient import TestClient
|
|
from main import app as fastapi_app
|
|
from app.services.jwt_service import JWTService
|
|
_ensure_seed()
|
|
|
|
admin = User.query.filter_by(role='ADMIN').first()
|
|
assert admin is not None
|
|
token = JWTService.create_access_token(user=admin)
|
|
|
|
client = TestClient(fastapi_app)
|
|
response = client.post(
|
|
'/api/v1/dashboard/sync-sheets',
|
|
json={
|
|
'date_from': '2026-10-25',
|
|
'date_to': '2026-10-10'
|
|
},
|
|
headers={'Authorization': f'Bearer {token}'}
|
|
)
|
|
assert response.status_code == 400
|
|
assert 'La fecha de inicio (date_from) no puede ser posterior a la fecha de fin (date_to).' in response.json()['detail']
|
|
|
|
|
|
def test_sync_with_ddmmaaaa_dates(init_database, app_context):
|
|
"""Verifica que fechas provistas en formato DD/MM/AAAA sean parseadas correctamente."""
|
|
_ensure_seed()
|
|
importer = GoogleSheetsImporter()
|
|
row = {
|
|
'dia': 'Miércoles',
|
|
'codigo': 'TEST-DDMMAAAA-01',
|
|
'asignatura': 'Materia Test DD/MM/AAAA',
|
|
'aula': '101',
|
|
'carrera': 'DATOS (CCC)',
|
|
'horario': '08:30 a 11:30hs',
|
|
'turno': 'Mañana'
|
|
}
|
|
stats = importer.sync(
|
|
selected_rows=[row],
|
|
manual_date='21/10/2026',
|
|
single_date_only=True
|
|
)
|
|
assert len(stats['errors']) == 0
|
|
assert stats['reservations_created'] == 1
|
|
|
|
res = Reservation.query.filter(Reservation.purpose.like('%TEST-DDMMAAAA-01%')).first()
|
|
assert res is not None
|
|
assert res.start_time.date() == date(2026, 10, 21)
|
|
|
|
|
|
def test_sync_with_row_date_ddmmaaaa(init_database, app_context):
|
|
"""Verifica que fechas en filas individuales en formato DD/MM/AAAA se respeten."""
|
|
_ensure_seed()
|
|
importer = GoogleSheetsImporter()
|
|
row = {
|
|
'dia': 'Jueves',
|
|
'codigo': 'TEST-DDMMAAAA-ROW',
|
|
'asignatura': 'Materia Row DD/MM/AAAA',
|
|
'aula': '101',
|
|
'carrera': 'DATOS (CCC)',
|
|
'horario': '18:30 a 21:30hs',
|
|
'turno': 'Tarde',
|
|
'date': '22/10/2026'
|
|
}
|
|
stats = importer.sync(selected_rows=[row])
|
|
assert len(stats['errors']) == 0
|
|
assert stats['reservations_created'] == 1
|
|
|
|
res = Reservation.query.filter(Reservation.purpose.like('%TEST-DDMMAAAA-ROW%')).first()
|
|
assert res is not None
|
|
assert res.start_time.date() == date(2026, 10, 22)
|
|
|
|
|