test commands (#151)

* added twitter error handling

* added facebook tests

* added header format test

* added multiple newlines check

* added test list command

* fixed commands structure
This commit is contained in:
Simone Robutti 2022-03-02 08:59:57 +01:00 committed by GitHub
parent 420f823dd4
commit f04942eefe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 142 additions and 19 deletions

View File

@ -7,12 +7,15 @@ from mobilizon_reshare.cli import safe_execution
from mobilizon_reshare.cli.commands.format.format import format_event
from mobilizon_reshare.cli.commands.list.list_event import list_events
from mobilizon_reshare.cli.commands.list.list_publication import list_publications
from mobilizon_reshare.cli.commands.recap.main import main as recap_main
from mobilizon_reshare.cli.commands.start.main import main as start_main
from mobilizon_reshare.cli.commands.recap.main import recap_command as recap_main
from mobilizon_reshare.cli.commands.start.main import start_command as start_main
from mobilizon_reshare.config.config import current_version
from mobilizon_reshare.config.publishers import publisher_names
from mobilizon_reshare.event.event import EventPublicationStatus
from mobilizon_reshare.main.retry import retry, retry_publication
from mobilizon_reshare.cli.commands.retry.main import (
retry_event_command,
retry_publication_command,
)
from mobilizon_reshare.models.publication import PublicationStatus
status_name_to_enum = {
@ -138,13 +141,13 @@ def format(
@event.command(name="retry", help="Retries all the failed publications")
@click.argument("event-id", type=click.UUID)
def event_retry(event_id):
safe_execution(functools.partial(retry, event_id),)
safe_execution(functools.partial(retry_event_command, event_id),)
@publication.command(name="retry", help="Retries a specific publication")
@click.argument("publication-id", type=click.UUID)
def publication_retry(publication_id):
safe_execution(functools.partial(retry_publication, publication_id),)
safe_execution(functools.partial(retry_publication_command, publication_id),)
if __name__ == "__main__":

View File

@ -50,8 +50,13 @@ async def list_events(
events = await list_unpublished_events(frm=frm, to=to)
else:
events = await events_with_status([status], from_date=frm, to_date=to)
events = list(events)
if events:
show_events(events)
else:
click.echo(f"No event found with status: {status.name}")
message = (
f"No event found with status: {status.name}"
if status is not None
else "No event found"
)
click.echo(message)

View File

@ -36,6 +36,11 @@ async def list_publications(
publications = await publications_with_status(status, from_date=frm, to_date=to)
if publications:
show_publications(list(publications))
show_publications(publications)
else:
click.echo(f"No publication found with status: {status.name}")
message = (
f"No publication found with status: {status.name}"
if status is not None
else "No publication found"
)
click.echo(message)

View File

@ -5,7 +5,7 @@ from mobilizon_reshare.main.recap import recap
logger = logging.getLogger(__name__)
async def main():
async def recap_command():
reports = await recap()
return 0 if reports and reports.successful else 1

View File

@ -0,0 +1,11 @@
from mobilizon_reshare.main.retry import retry_publication, retry_event
async def retry_event_command(event_id):
reports = await retry_event(event_id)
return 0 if reports and reports.successful else 1
async def retry_publication_command(publication_id):
reports = await retry_publication(publication_id)
return 0 if reports and reports.successful else 1

View File

@ -1,7 +1,7 @@
from mobilizon_reshare.main.start import start
async def main():
async def start_command():
"""
STUB
:return:

View File

@ -37,7 +37,7 @@ async def retry_publication(publication_id):
return PublisherCoordinator([publication]).run()
async def retry(mobilizon_event_id: UUID = None):
async def retry_event(mobilizon_event_id: UUID = None):
if mobilizon_event_id is None:
raise NotImplementedError(
"Autonomous retry not implemented yet, please specify an event_id"

View File

@ -0,0 +1,99 @@
import pytest
from mobilizon_reshare.cli.commands.list.list_event import list_events
from mobilizon_reshare.cli.commands.list.list_publication import list_publications
from mobilizon_reshare.event.event import EventPublicationStatus
from mobilizon_reshare.models.publication import PublicationStatus
spec = {
# We need three events since recap will print only
# future events, but the 0th event happens at today + 0.
"event": 3,
"publications": [
{"event_idx": 1, "publisher_idx": 0, "status": PublicationStatus.COMPLETED},
{"event_idx": 2, "publisher_idx": 0, "status": PublicationStatus.FAILED},
],
"publisher": ["zulip"],
}
def clean_output(output):
return list(map(lambda x: x.strip(), output.out.rstrip().split("\n")))
@pytest.mark.asyncio
async def test_list_events(capsys, generate_models):
await generate_models(spec)
await list_events()
output = capsys.readouterr()
assert clean_output(output) == [
"event_0 WAITING 00000000-0000-0000-0000-000000000000"
" 2021-06-06T05:00:00+02:00 2021-06-06T07:00:00+02:00",
"event_1 COMPLETED 00000000-0000-0000-0000-000000000001"
" 2021-06-07T05:00:00+02:00 2021-06-07T07:00:00+02:00",
"event_2 FAILED 00000000-0000-0000-0000-000000000002"
" 2021-06-08T05:00:00+02:00 2021-06-08T07:00:00+02:00",
]
@pytest.mark.asyncio
async def test_list_events_with_status(capsys, generate_models):
await generate_models(spec)
await list_events(status=EventPublicationStatus.WAITING)
output = capsys.readouterr()
assert clean_output(output) == [
"event_0 WAITING 00000000-0000-0000-0000-000000000000"
" 2021-06-06T05:00:00+02:00 2021-06-06T07:00:00+02:00"
]
@pytest.mark.asyncio
async def test_list_publications(capsys, generate_models):
await generate_models(spec)
await list_publications()
output = capsys.readouterr()
assert clean_output(output) == [
"00000000-0000-0000-0000-000000000000 2021-06-06T03:00:00+00:00 "
"COMPLETED zulip 00000000-0000-0000-0000-000000000001",
"00000000-0000-0000-0000-000000000001 2021-06-06T04:00:00+00:00 "
"FAILED zulip 00000000-0000-0000-0000-000000000002",
]
@pytest.mark.asyncio
async def test_list_publications_with_status(capsys, generate_models):
await generate_models(spec)
await list_publications(status=PublicationStatus.FAILED)
output = capsys.readouterr()
assert clean_output(output) == [
"00000000-0000-0000-0000-000000000001 2021-06-06T04:00:00+00:00 "
"FAILED zulip 00000000-0000-0000-0000-000000000002"
]
@pytest.mark.asyncio
async def test_list_events_empty(capsys, generate_models):
await list_events()
output = capsys.readouterr()
assert clean_output(output) == ["No event found"]
@pytest.mark.asyncio
async def test_list_publications_empty(capsys, generate_models):
await list_publications()
output = capsys.readouterr()
assert clean_output(output) == ["No publication found"]
@pytest.mark.asyncio
async def test_list_events_empty_with_status(capsys, generate_models):
await list_events(status=EventPublicationStatus.FAILED)
output = capsys.readouterr()
assert clean_output(output) == ["No event found with status: FAILED"]
@pytest.mark.asyncio
async def test_list_publications_empty_with_status(capsys, generate_models):
await list_publications(status=PublicationStatus.FAILED)
output = capsys.readouterr()
assert clean_output(output) == ["No publication found with status: FAILED"]

View File

@ -2,7 +2,7 @@ from logging import DEBUG
import pytest
from mobilizon_reshare.main.recap import recap
from mobilizon_reshare.cli.commands.recap.main import recap
from mobilizon_reshare.models.publication import PublicationStatus
spec = {

View File

@ -3,14 +3,14 @@ from logging import INFO
import pytest
from mobilizon_reshare.main.retry import retry
from mobilizon_reshare.main.retry import retry_event
from mobilizon_reshare.models.publication import PublicationStatus, Publication
@pytest.mark.asyncio
async def test_retry_decision():
with pytest.raises(NotImplementedError):
await retry()
await retry_event()
@pytest.mark.parametrize(
@ -24,7 +24,7 @@ async def test_retry(
failed_publication,
):
assert failed_publication.status == PublicationStatus.FAILED
await retry(event_with_failed_publication.mobilizon_id)
await retry_event(event_with_failed_publication.mobilizon_id)
p = await Publication.filter(id=failed_publication.id).first()
assert p.status == PublicationStatus.COMPLETED, p.id
assert len(message_collector) == 1
@ -39,7 +39,7 @@ async def test_retry_no_publications(
stored_event, mock_publisher_config, message_collector, caplog
):
with caplog.at_level(INFO):
await retry(stored_event.mobilizon_id)
await retry_event(stored_event.mobilizon_id)
assert "No failed publications found" in caplog.text
assert len(message_collector) == 0
@ -51,7 +51,7 @@ async def test_retry_no_publications(
async def test_retry_missing_event(mock_publisher_config, message_collector, caplog):
event_id = uuid.uuid4()
with caplog.at_level(INFO):
await retry(event_id)
await retry_event(event_id)
assert f"Event with id {event_id} not found" in caplog.text
assert len(message_collector) == 0
@ -76,7 +76,7 @@ async def test_retry_mixed_publications(
await p.save()
assert failed_publication.status == PublicationStatus.FAILED
await retry(event_with_failed_publication.mobilizon_id)
await retry_event(event_with_failed_publication.mobilizon_id)
p = await Publication.filter(id=failed_publication.id).first()
assert p.status == PublicationStatus.COMPLETED, p.id
assert len(message_collector) == 1