Esempio n. 1
0
def test_merge_error(capsys, mock_init):
    # Simulate user input:
    sys.argv = f"bibm merge fake_file.bib".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out \
           == "\nError: Input BibTeX file 'fake_file.bib' does not exist.\n"
Esempio n. 2
0
def test_cli_fetch_invalid_name(capsys, mock_init_sample, reqs):
    sys.argv = "bibm fetch 1957RvMP...29..547B pdf_file".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == (
        "Fetching PDF file from Journal website:\n"
        "\nError: Invalid filename, must have a .pdf extension\n")
Esempio n. 3
0
def test_cli_open_keycode_invalid(capsys, mock_init_sample, mock_call):
    pathlib.Path(f"{u.BM_PDF()}Slipher1913.pdf").touch()
    sys.argv = f"bibm open bad_keycode".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == ('\nError: Input is no key, bibcode, or PDF '
                            'of any entry in Bibmanager database\n')
Esempio n. 4
0
def test_cli_search_year_invalid(capsys, mock_init_sample,
                                 mock_prompt_session):
    sys.argv = "bibm search".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == ("(Press 'tab' for autocomplete)\n\n"
                            "\nInvalid format for input year: 1984a\n")
Esempio n. 5
0
def test_cli_pdf_set_pathed_filename(capsys, mock_init_sample):
    sys.argv = "bibm pdf 1957RvMP...29..547B file.pdf ./new.pdf".split()
    pathlib.Path("file.pdf").touch()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == "\nError: filename must not have a path\n"
    bib = bm.find(bibcode='1957RvMP...29..547B')
    assert bib.pdf is None
Esempio n. 6
0
def test_version(capsys):
    sys.argv = "bibm --version".split()
    try:
        cli.main()
    except SystemExit:
        pass
    captured = capsys.readouterr()
    assert captured.out == f"bibmanager version {bibmanager.__version__}\n"
Esempio n. 7
0
def test_help(capsys):
    sys.argv = "bibm -h".split()
    try:
        cli.main()
    except:
        pass
    captured = capsys.readouterr()
    assert captured.out == main_description
Esempio n. 8
0
def test_ads_search(capsys, reqs, mock_prompt_session):
    cm.set('ads_display', '2')
    am.search.__defaults__ = 0, 2, 'pubdate+desc'
    sys.argv = "bibm ads-search".split()
    captured = capsys.readouterr()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == f"""(Press 'tab' for autocomplete)\n
Esempio n. 9
0
def test_cli_pdf_set_missing_pdf(capsys, mock_init_sample):
    sys.argv = f"bibm pdf 1957RvMP...29..547B".split()
    pathlib.Path(f"file.pdf").touch()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == "\nError: Path to PDF file is missing.\n"
    bib = bm.find(bibcode='1957RvMP...29..547B')
    assert bib.pdf is None
Esempio n. 10
0
def test_merge_default(capsys, mock_init):
    bibfile = u.HOME + "examples/sample.bib"
    # Simulate user input:
    sys.argv = f"bibm merge {bibfile}".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out  == "\nMerged 17 new entries.\n\n" \
                  f"Merged BibTeX file '{bibfile}' into bibmanager database.\n"
Esempio n. 11
0
def test_cli_pdf_set_renamed(capsys, mock_init_sample, mock_call):
    sys.argv = f"bibm pdf 1957RvMP...29..547B file.pdf new.pdf".split()
    pathlib.Path(f"file.pdf").touch()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == f"Saved PDF to: '{u.BM_PDF()}new.pdf'.\n"
    bib = bm.find(bibcode='1957RvMP...29..547B')
    assert bib.pdf == 'new.pdf'
    assert 'new.pdf' in os.listdir(u.BM_PDF())
Esempio n. 12
0
def test_cli_pdf_set_invalid_name(capsys, mock_init_sample):
    sys.argv = "bibm pdf 1957RvMP...29..547B file.pdf pdf_file".split()
    pathlib.Path("file.pdf").touch()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == \
        "\nError: Invalid filename, must have a .pdf extension\n"
    bib = bm.find(bibcode='1957RvMP...29..547B')
    assert bib.pdf is None
Esempio n. 13
0
def test_cli_pdf_set_missing_pdf_file(capsys, mock_init_sample):
    sys.argv = "bibm pdf 1957RvMP...29..547B file.pdf".split()
    with u.ignored(OSError):
        os.remove('file.pdf')
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == "\nError: input PDF file does not exist.\n"
    bib = bm.find(bibcode='1957RvMP...29..547B')
    assert bib.pdf is None
Esempio n. 14
0
def test_cli_pdf_set_missing_bib(capsys, mock_init_sample):
    sys.argv = f"bibm pdf 1957RvMP...00..000X file.pdf".split()
    pathlib.Path("file.pdf").touch()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == \
        "\nError: BibTex entry is not in Bibmanager database.\n"
    bib = bm.find(bibcode='1957RvMP...29..547B')
    assert bib.pdf is None
Esempio n. 15
0
def test_reset_config(capsys, mock_init_sample):
    pathlib.Path(u.BM_BIBFILE).touch()
    # Simulate user input:
    sys.argv = "bibm reset -c".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == "Resetting config parameters.\n"
    assert filecmp.cmp(u.HOME + "config", u.ROOT + "config")
    assert set(os.listdir(u.HOME)) == \
        set(["bm_database.pickle", "bm_bibliography.bib", "config", "examples"])
Esempio n. 16
0
def test_config_help(capsys, mock_init_sample):
    sys.argv = "bibm config paper".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == (
        "\nThe 'paper' parameter sets the default paper "
        "format for latex compilation outputs\n"
        "(not for pdflatex, which is automatic).  Typical options are 'letter'\n"
        "(e.g., for ApJ articles) or 'A4' (e.g., for A&A).\n\n"
        "The current paper format is: 'letter'.\n")
Esempio n. 17
0
def test_cli_ads_search_next(capsys, reqs, mock_prompt_session, mock_init):
    cm.set('ads_display', '2')
    am.search.__defaults__ = 0, 2, 'pubdate+desc'
    sys.argv = "bibm ads-search".split()
    cli.main()
    captured = capsys.readouterr()
    sys.argv = "bibm ads-search -n".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == f"""
Esempio n. 18
0
def test_cli_open_prompt(capsys, mock_init_sample, mock_call,
                         mock_prompt_session):
    pathlib.Path(f"{u.BM_PDF()}Slipher1913.pdf").touch()
    sys.argv = f"bibm open".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == ("Syntax is:  key: KEY_VALUE\n"
                            "       or:  bibcode: BIBCODE_VALUE\n"
                            "       or:  pdf: PDF_VALUE\n"
                            "(Press 'tab' for autocomplete)\n\n")
Esempio n. 19
0
def test_cli_open_fetch(capsys, mock_init_sample, mock_call, reqs, mock_input):
    sys.argv = f"bibm open 1957RvMP...29..547B".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == (
        "\nError: Entry does not have a PDF in the database\n"
        "Fetch from ADS?\n"
        "[]yes [n]o\n\n"
        "Fetching PDF file from Journal website:\n"
        f"Saved PDF to: '{u.BM_PDF()}Burbidge1957_RvMP_29_547.pdf'.\n")
Esempio n. 20
0
def test_older_pickle(capsys, mock_init_sample, mock_prompt_session):
    # Mock pickle DB file with older version than bibmanager:
    with open(u.BM_DATABASE, 'wb') as handle:
        pickle.dump([], handle, protocol=pickle.HIGHEST_PROTOCOL)

    # Simulate user input:
    sys.argv = "bibm search".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == f"""Updating database file from version 0.0.0 to version {bibmanager.__version__}.
Esempio n. 21
0
def test_cli_reset_all(capsys, mock_init_sample):
    pathlib.Path(u.BM_BIBFILE()).touch()
    cm.set("ads_display", "10")
    captured = capsys.readouterr()
    # Simulate user input:
    sys.argv = "bibm reset".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == "Initializing new bibmanager database.\n" \
                           "Resetting config parameters.\n"
    assert set(os.listdir(u.HOME)) == set(["config", "examples", "pdf"])
Esempio n. 22
0
def test_config_display(capsys, mock_init_sample):
    sys.argv = "bibm config".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == ("\nbibmanager configuration file:\n"
                            "PARAMETER    VALUE\n"
                            "-----------  -----\n"
                            "style        autumn\n"
                            "text_editor  default\n"
                            "paper        letter\n"
                            "ads_token    None\n"
                            "ads_display  20\n")
Esempio n. 23
0
def test_reset_keep_database(capsys, mock_init_sample):
    bibfile = u.HOME + "examples/sample.bib"
    # Simulate user input:
    sys.argv = f"bibm reset {bibfile}".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == f"Initializing new bibmanager database with BibTeX file: '{bibfile}'.\n" \
                           "Resetting config parameters.\n"
    assert set(os.listdir(u.HOME)) == set(
        ["bm_database.pickle", "bm_bibliography.bib", "config", "examples"])
    bibs = bm.loadfile(bibfile)
    assert len(bibs) == 17
Esempio n. 24
0
def test_cli_pdf_set_prompt_missing_pdf(capsys, mock_init_sample,
                                        mock_prompt_session):
    sys.argv = f"bibm pdf".split()
    pathlib.Path(f"file.pdf").touch()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == (
        "Syntax is:  key: KEY_VALUE PDF_FILE FILENAME\n"
        "       or:  bibcode: BIBCODE_VALUE PDF_FILE FILENAME\n"
        "(output FILENAME is optional, set it to guess for automated naming)\n"
        "\n\nError: Path to PDF file is missing.\n")
    bib = bm.find(bibcode='1957RvMP...29..547B')
    assert bib.pdf is None
Esempio n. 25
0
def test_cli_pdf_set_prompt_guess(capsys, mock_init_sample, mock_call,
                                  mock_prompt_session):
    sys.argv = f"bibm pdf".split()
    pathlib.Path(f"file.pdf").touch()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == (
        "Syntax is:  key: KEY_VALUE PDF_FILE FILENAME\n"
        "       or:  bibcode: BIBCODE_VALUE PDF_FILE FILENAME\n"
        "(output FILENAME is optional, set it to guess for automated naming)\n"
        f"\nSaved PDF to: '{u.BM_PDF()}Burbidge1957_RvMP_29_547.pdf'.\n")
    bib = bm.find(bibcode='1957RvMP...29..547B')
    assert bib.pdf == 'Burbidge1957_RvMP_29_547.pdf'
    assert 'Burbidge1957_RvMP_29_547.pdf' in os.listdir(u.BM_PDF())
Esempio n. 26
0
def test_future_pickle(capsys, mock_init_sample):
    # Mock pickle DB file with later version than bibmanager:
    future_version = '2.0.0'
    with open(u.BM_DATABASE, 'wb') as handle:
        pickle.dump([], handle, protocol=pickle.HIGHEST_PROTOCOL)
        pickle.dump(future_version, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # Simulate user input:
    sys.argv = "bibm reset".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == \
       (f"Bibmanager version ({bibmanager.__version__}) is older "
        f"than saved database.  Please update to version {future_version}.\n")
Esempio n. 27
0
def test_reset_database(capsys, mock_init_sample):
    pathlib.Path(u.BM_BIBFILE).touch()
    cm.set("ads_display", "10")
    captured = capsys.readouterr()
    # Simulate user input:
    sys.argv = "bibm reset -d".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == "Initializing new bibmanager database.\n"
    # filecmp does not seem to work fine, will do manually:
    # assert not filecmp.cmp(u.HOME+"config", u.ROOT+"config")
    with open(u.ROOT + "config", "r") as f:
        rconfig = f.read()
    with open(u.HOME + "config", "r") as f:
        hconfig = f.read()
    assert rconfig != hconfig
    assert set(os.listdir(u.HOME)) == set(["config", "examples"])
Esempio n. 28
0
def test_config_invalid_value(capsys, mock_init_sample):
    sys.argv = "bibm config ads_display A224".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == \
        "\nError: The ads_display value must be a positive integer.\n"
Esempio n. 29
0
def test_ads_search_next_empty(capsys, reqs, mock_prompt_session, mock_init):
    sys.argv = "bibm ads-search -n".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == f"""There are no more entries for this querry.\n"""
Esempio n. 30
0
def test_ads_search_empty(capsys, reqs, mock_prompt_session, mock_init):
    sys.argv = "bibm ads-search".split()
    cli.main()
    captured = capsys.readouterr()
    assert captured.out == f"""(Press 'tab' for autocomplete)\n\n"""