2021-08-14 18:23:55 +02:00
|
|
|
from datetime import timedelta
|
|
|
|
from typing import Union
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.models.event import Event
|
|
|
|
from mobilizon_reshare.models.publication import PublicationStatus, Publication
|
|
|
|
from mobilizon_reshare.models.publisher import Publisher
|
2021-08-14 18:23:55 +02:00
|
|
|
from tests.storage import today
|
|
|
|
|
|
|
|
|
|
|
|
async def _generate_publishers(specification):
|
|
|
|
|
|
|
|
publishers = []
|
2021-08-27 23:45:24 +02:00
|
|
|
for i, publisher_name in enumerate(specification["publisher"]):
|
2021-08-14 18:23:55 +02:00
|
|
|
publisher = Publisher(
|
2021-08-27 23:45:24 +02:00
|
|
|
id=UUID(int=i), name=publisher_name, account_ref=f"account_ref_{i}"
|
2021-08-14 18:23:55 +02:00
|
|
|
)
|
|
|
|
publishers.append(publisher)
|
|
|
|
await publisher.save()
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-08-14 18:23:55 +02:00
|
|
|
return publishers
|
|
|
|
|
|
|
|
|
|
|
|
async def _generate_events(specification):
|
|
|
|
events = []
|
|
|
|
if "event" in specification.keys():
|
|
|
|
for i in range(specification["event"]):
|
|
|
|
begin_date = today + timedelta(days=i)
|
|
|
|
event = Event(
|
|
|
|
id=UUID(int=i),
|
|
|
|
name=f"event_{i}",
|
|
|
|
description=f"desc_{i}",
|
2021-08-29 14:51:52 +02:00
|
|
|
mobilizon_id=UUID(int=i),
|
2021-08-14 18:23:55 +02:00
|
|
|
mobilizon_link=f"moblink_{i}",
|
|
|
|
thumbnail_link=f"thumblink_{i}",
|
|
|
|
location=f"loc_{i}",
|
|
|
|
begin_datetime=begin_date,
|
|
|
|
end_datetime=begin_date + timedelta(hours=2),
|
|
|
|
)
|
|
|
|
events.append(event)
|
|
|
|
await event.save()
|
|
|
|
return events
|
|
|
|
|
|
|
|
|
|
|
|
async def _generate_publications(events, publishers, specification):
|
|
|
|
if "publications" in specification.keys():
|
2021-11-11 15:18:04 +01:00
|
|
|
for i, publication in enumerate(specification["publications"]):
|
|
|
|
status = publication.get("status", PublicationStatus.COMPLETED)
|
2021-08-14 18:23:55 +02:00
|
|
|
timestamp = publication.get("timestamp", today + timedelta(hours=i))
|
|
|
|
await Publication.create(
|
|
|
|
id=UUID(int=i),
|
|
|
|
status=status,
|
|
|
|
timestamp=timestamp,
|
|
|
|
event_id=events[publication["event_idx"]].id,
|
|
|
|
publisher_id=publishers[publication["publisher_idx"]].id,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def generate_models():
|
|
|
|
async def _generate_models(specification: dict[str, Union[int, list]]):
|
|
|
|
publishers = await _generate_publishers(specification)
|
|
|
|
events = await _generate_events(specification)
|
|
|
|
await _generate_publications(events, publishers, specification)
|
|
|
|
|
|
|
|
return _generate_models
|