Beispiel #1
0
def test_unidify_stdin(monkeypatch: MonkeyPatch,
                       capsys: CaptureFixture) -> None:
    monkeypatch.setattr(
        sys,
        "stdin",
        StringIO("\n".join(("1115690002233556993", "1115690615612825601",
                            "1115691710657499137"))),
    )
    main("unidify")

    assert [
        "We encourage submissions of new, previously, or concurrently published "
        "research. The event should be a forum for researchers to exchange ideas, "
        "discuss work, and get feedback. We hope you'll consider submitting your work.",
        "We'll have talks from research leaders on the latest advances in NLP. "
        "@NandoDF will be giving the keynote and more speakers will be announced soon. "
        "https://t.co/SB3URxn6ab",
        "Registration will open soon. In the meantime, we'll hope you'll save the date "
        "and consider joining us for what should be a fun day of listening to "
        "stimulating talks, mingling with like-minded people, exchanging ideas, and "
        "maybe even striking up a collaboration.",
    ] == [
        Tweet(json.loads(line)).text
        for line in capsys.readouterr().out.strip().split("\n")
    ]
Beispiel #2
0
def test_correct_call(request_: Request, monkeypatch: MonkeyPatch,
                      capsys: CaptureFixture) -> None:
    mock_context: MockRequestContext = MockRequestContext()
    monkeypatch.setattr(type(request_), request_.request.__name__,
                        mock_context.mock_request)

    main(*_make_args(request_))

    assert mock_context.request == request_
    assert not mock_context.remaining_result_tweets
    assert capsys.readouterr().out == ""
Beispiel #3
0
def test_no_batch_file(tmp_path: Path) -> None:
    batch_file = tmp_path / "batch.jsonl"
    results_dir = tmp_path / "out"

    with pytest.raises(FileNotFoundError):
        main(
            "batch",
            "--batch-file",
            str(batch_file),
            "--results-dir",
            str(results_dir),
        )
Beispiel #4
0
def test_idify_indir(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
    mock_context = MockBatchResultsContext()
    monkeypatch.setattr(
        nasty._cli,
        nasty._cli.BatchResults.__name__,  # type: ignore
        mock_context.MockBatchResults,
    )

    main("idify", "--in-dir", str(tmp_path))

    assert mock_context.init_args == (tmp_path, )
    assert mock_context.idify_args == (tmp_path, )
    assert mock_context.unidify_args is None
Beispiel #5
0
def test_incorrect_calls(args_string: str, capsys: CaptureFixture) -> None:
    args = args_string.split(" ") if args_string != "" else []
    logger.debug("Raw arguments: {}".format(args))

    with pytest.raises(SystemExit) as excinfo:
        main(*args)

    assert excinfo.value.code == 2

    captured = capsys.readouterr().err
    logger.debug("Captured Error:")
    for line in captured.split("\n"):
        logger.debug("  " + line)
    assert "usage: nasty" in captured and ": error: " in captured
Beispiel #6
0
def test_idify_indir_outdir(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
    in_dir = tmp_path / "in"
    out_dir = tmp_path / "out"

    mock_context = MockBatchResultsContext()
    monkeypatch.setattr(
        nasty._cli,
        nasty._cli.BatchResults.__name__,  # type: ignore
        mock_context.MockBatchResults,
    )

    main("idify", "--in-dir", str(in_dir), "--out-dir", str(out_dir))

    assert mock_context.init_args == (in_dir, )
    assert mock_context.idify_args == (out_dir, )
    assert mock_context.unidify_args is None
Beispiel #7
0
def test_correct_call_to_batch(
    request_: Request,
    capsys: CaptureFixture,
    tmp_path: Path,
) -> None:
    batch_file = tmp_path / "batch.jsonl"

    main(*_make_args(request_, to_batch=batch_file))

    assert capsys.readouterr().out == ""
    batch = Batch()
    batch.load(batch_file)
    assert len(batch) == 1
    assert batch[0].request == request_
    assert batch[0].id
    assert batch[0].completed_at is None
    assert batch[0].exception is None
Beispiel #8
0
def test_correct_call_results(
    request_: Request,
    num_results: int,
    monkeypatch: MonkeyPatch,
    capsys: CaptureFixture,
) -> None:
    mock_context: MockRequestContext = MockRequestContext(
        num_results=num_results)
    monkeypatch.setattr(type(request_), request_.request.__name__,
                        mock_context.mock_request)

    main(*_make_args(request_))

    assert mock_context.request == request_
    assert not mock_context.remaining_result_tweets
    assert capsys.readouterr().out == (json.dumps(
        mock_context.RESULT_TWEET.to_json()) + "\n") * min(10, num_results)
Beispiel #9
0
def test_idify_stdin(monkeypatch: MonkeyPatch, capsys: CaptureFixture) -> None:
    tweet_json = tweet_jsons["1142944425502543875"]
    tweets = []
    for i in range(5):
        tweet = deepcopy(tweet_json)
        tweet["id"] = i
        tweet["id_str"] = str(i)
        tweets.append(tweet)

    monkeypatch.setattr(
        sys,
        "stdin",
        StringIO("\n".join(json.dumps(tweet) for tweet in tweets)),
    )
    main("idify")
    assert capsys.readouterr().out == "\n".join(str(i)
                                                for i in range(5)) + "\n"
Beispiel #10
0
def test_correct_call_to_batch_daily(capsys: CaptureFixture,
                                     tmp_path: Path) -> None:
    batch_file = tmp_path / "batch.jsonl"
    request = Search("trump", since=date(2019, 1, 1), until=date(2019, 2, 1))

    # Needed for type checking.
    assert request.until is not None and request.since is not None

    main(*_make_args(request, to_batch=batch_file, daily=True))

    assert capsys.readouterr().out == ""
    batch = Batch()
    batch.load(batch_file)
    assert len(batch) == (request.until - request.since).days
    for batch_entry, expected_request in zip(batch,
                                             request.to_daily_requests()):
        assert batch_entry.request == expected_request
        assert batch_entry.id
        assert batch_entry.completed_at is None
        assert batch_entry.exception is None
Beispiel #11
0
def test_correct_call(
    monkeypatch: MonkeyPatch,
    capsys: CaptureFixture,
    tmp_path: Path,
) -> None:
    mock_context = MockBatchContext()
    monkeypatch.setattr(
        nasty._cli,
        nasty._cli.Batch.__name__,  # type: ignore
        mock_context.MockBatch,
    )

    batch_file = tmp_path / "batch.jsonl"
    results_dir = tmp_path / "out"
    main("batch", "--batch-file", str(batch_file), "--results-dir",
         str(results_dir))

    assert mock_context.load_args == (batch_file, )
    assert mock_context.execute_args == (results_dir, )
    assert capsys.readouterr().out == ""
Beispiel #12
0
def test_correct_call_to_batch_exists(
    old_request: Request,
    new_request: Request,
    capsys: CaptureFixture,
    tmp_path: Path,
) -> None:
    batch_file = tmp_path / "batch.jsonl"
    batch = Batch()
    batch.append(old_request)
    batch.dump(batch_file)

    main(*_make_args(new_request, to_batch=batch_file))

    assert capsys.readouterr().out == ""
    batch = Batch()
    batch.load(batch_file)
    assert len(batch) == 2
    for batch_entry, expected_request in zip(batch,
                                             [old_request, new_request]):
        assert batch_entry.request == expected_request
        assert batch_entry.id
        assert batch_entry.completed_at is None
        assert batch_entry.exception is None