async def test_start_event_from_db(
    mock_mobilizon_success_answer,
    mobilizon_answer,
    caplog,
    mock_publisher_config,
    message_collector,
    event_generator,
):
    event = event_generator()
    event_model = event_to_model(event)
    await event_model.save()

    with caplog.at_level(DEBUG):
        # calling the start command
        assert await start() is not None

        # since the db contains at least one event, this has to be picked and published
        assert "Event to publish found" in caplog.text
        assert message_collector == [
            "test event|description of the event",
        ]

        await event_model.fetch_related("publications",
                                        "publications__publisher")
        # it should create a publication for each publisher
        publications = event_model.publications
        assert len(publications) == 1, publications

        # all the publications for the first event should be saved as COMPLETED
        for p in publications:
            assert p.status == PublicationStatus.COMPLETED

        # the derived status for the event should be COMPLETED
        assert event_from_model(
            event_model).status == EventPublicationStatus.COMPLETED
示例#2
0
async def test_format_event(runner, event, capsys, publisher_name):
    event_model = event_to_model(event)
    await event_model.save()
    await format_event(event_id=str(event_model.mobilizon_id),
                       publisher_name=publisher_name)

    assert (capsys.readouterr().out.strip() == get_formatter_class(
        publisher_name)().get_message_from_event(event).strip())
async def test_mobilizon_event_to_model(event):
    event_model = event_to_model(event)
    await event_model.save()

    event_db = await Event.all().first()
    begin_date_utc = arrow.Arrow(year=2021, month=1, day=1, hour=10, minute=30)
    begin_date_utc = begin_date_utc.astimezone(
        tortoise.timezone.get_default_timezone())

    assert event_db.name == "test event"
    assert event_db.description == "description of the event"
    assert event_db.begin_datetime == begin_date_utc
    assert event_db.end_datetime == begin_date_utc + timedelta(hours=1)
    assert event_db.mobilizon_link == "http://some_link.com/123"
    assert event_db.mobilizon_id == UUID(int=12345)
    assert event_db.thumbnail_link == "http://some_link.com/123.jpg"
    assert event_db.location == "location"
async def test_start_publisher_failure(
    mock_mobilizon_success_answer,
    mobilizon_answer,
    caplog,
    mock_publisher_config,
    message_collector,
    event_generator,
    mock_notifier_config,
):
    event = event_generator()
    event_model = event_to_model(event)
    await event_model.save()

    with caplog.at_level(DEBUG):
        # calling the start command
        assert await start() is not None

        # since the db contains at least one event, this has to be picked and published

        await event_model.fetch_related("publications",
                                        "publications__publisher")
        # it should create a publication for each publisher
        publications = event_model.publications
        assert len(publications) == 1, publications

        # all the publications for event should be saved as FAILED
        for p in publications:
            assert p.status == PublicationStatus.FAILED
            assert p.reason == "credentials error"

        assert "Event to publish found" in caplog.text
        assert message_collector == [
            f"Publication {p.id} failed with status: 0."
            f"\nReason: credentials error\nPublisher: mock\nEvent: test event"
            for p in publications for _ in range(2)
        ]  # 2 publications failed * 2 notifiers
        # the derived status for the event should be FAILED
        assert event_from_model(
            event_model).status == EventPublicationStatus.FAILED
async def mock_publications(
    num_publications: int,
    test_event: MobilizonEvent,
    mock_publisher_valid,
    mock_formatter_valid,
):
    result = []
    for i in range(num_publications):
        event = event_to_model(test_event)
        await event.save()
        publisher = Publisher(name="telegram")
        await publisher.save()
        publication = PublicationModel(
            id=UUID(int=i + 1),
            event=event,
            publisher=publisher,
            timestamp=today + timedelta(hours=i),
            reason=None,
        )
        publication = publication_from_orm(publication, test_event)
        publication.publisher = mock_publisher_valid
        publication.formatter = mock_formatter_valid
        result.append(publication)
    return result
示例#6
0
async def stored_event(event) -> Event:
    model = event_to_model(event)
    await model.save()
    await model.fetch_related("publications")
    return model