def test_set_pdf_bin_new(mock_init_sample, name): bib = bm.find(key='ShowmanEtal2009apjRadGCM') bin_pdf = b'file.pdf' pm.set_pdf(bib, bin_pdf=bin_pdf, filename=name) filename = 'Showman2009_ApJ_699_564.pdf' if name is None else name assert filename in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf == filename
def test_set_pdf_bin_overwrite_rename(mock_init_sample, mock_input): bib = bm.find(key='ShowmanEtal2009apjRadGCM') bin_pdf = b'file.pdf' name = 'new.pdf' pathlib.Path(f"{u.BM_PDF()}{name}").touch() pm.set_pdf(bib, bin_pdf=bin_pdf, filename=name) assert 'new_new.pdf' in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf == 'new_new.pdf'
def test_set_pdf_in_new(mock_init_sample, name): bib = bm.find(key='ShowmanEtal2009apjRadGCM') pdf = u.BM_PDF() + 'file.pdf' pathlib.Path(pdf).touch() pm.set_pdf(bib, pdf=pdf, filename=name) filename = 'file.pdf' if name is None else name assert filename in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf == filename
def test_set_pdf_overwrite_no(mock_init_sample, mock_input): bib = bm.find(key='ShowmanEtal2009apjRadGCM') pdf = 'file.pdf' name = 'new.pdf' pathlib.Path(pdf).touch() pathlib.Path(f"{u.BM_PDF()}{name}").touch() pm.set_pdf(bib, pdf=pdf, filename=name) assert name in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf is None
def test_set_pdf_bin_replace_ask_no(mock_init_sample, mock_input): bib = bm.find(key='Slipher1913lobAndromedaRarialVelocity') bin_pdf = b'file.pdf' old = f"{u.BM_PDF()}{bib.pdf}" name = 'new.pdf' pathlib.Path(old).touch() pm.set_pdf(bib, bin_pdf=bin_pdf, filename=name) assert name not in os.listdir(u.BM_PDF()) assert os.path.basename(old) in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf == os.path.basename(old)
def test_set_pdf_rename(mock_init_sample, replace): bib = bm.find(key='Slipher1913lobAndromedaRarialVelocity') old = f"{u.BM_PDF()}{bib.pdf}" pdf = old name = 'new.pdf' pathlib.Path(old).touch() pm.set_pdf(bib, pdf=pdf, filename=name, replace=replace) assert name in os.listdir(u.BM_PDF()) assert old not in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf == name
def test_set_pdf_bin_replace(mock_init_sample): bib = bm.find(key='Slipher1913lobAndromedaRarialVelocity') bin_pdf = b'file.pdf' pdf = 'file.pdf' name = 'new.pdf' old = f"{u.BM_PDF()}{bib.pdf}" pathlib.Path(old).touch() pm.set_pdf(bib, bin_pdf=bin_pdf, filename=name, replace=True) filename = 'file.pdf' if name is None else name assert filename in os.listdir(u.BM_PDF()) assert os.path.basename(old) not in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf == filename
def test_set_pdf_overwrite_yes(mock_init_sample, mock_input): bib = bm.find(key='ShowmanEtal2009apjRadGCM') pdf = 'file.pdf' name = 'new.pdf' pathlib.Path(pdf).touch() pathlib.Path(f"{u.BM_PDF()}{name}").touch() out_filename = pm.set_pdf(bib, pdf=pdf, filename=name) assert name in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf == name # TBD: Check if previous file belongs to another entry? assert out_filename is not None assert out_filename == f"{u.BM_PDF()}{name}"
def test_set_pdf_replace_ask_yes(mock_init_sample, mock_input): bib = bm.find(key='Slipher1913lobAndromedaRarialVelocity') pdf = 'file.pdf' name = 'new.pdf' old = f"{u.BM_PDF()}{bib.pdf}" pathlib.Path(old).touch() pathlib.Path(pdf).touch() out_filename = pm.set_pdf(bib, pdf=pdf, filename=name) filename = 'file.pdf' if name is None else name assert filename in os.listdir(u.BM_PDF()) assert os.path.basename(old) not in os.listdir(u.BM_PDF()) bib = bm.find(key=bib.key) assert bib.pdf == filename assert out_filename is not None assert out_filename == f"{u.BM_PDF()}{filename}"
def test_find_key_bibcode(mock_init_sample): key = 'AASteamHendrickson2018aastex62' bibcode = '2013A&A...558A..33A' bib = bm.find(key=key, bibcode=bibcode) assert bib is not None assert bib.key == key assert bib.bibcode != bibcode
def test_set_pdf_duplicate_pdf(mock_init_sample): bib = bm.find(key='ShowmanEtal2009apjRadGCM') pdf = 'file.pdf' bin_pdf = b'file.pdf' with pytest.raises(ValueError, match='Exactly one of pdf or bin_pdf must be not None'): pm.set_pdf(bib, pdf=pdf, bin_pdf=bin_pdf)
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
def test_set_pdf_str_bib(mock_init_sample, bib): pdf = 'file.pdf' name = 'new.pdf' pathlib.Path(pdf).touch() pm.set_pdf(bib, pdf=pdf, filename=name) assert name in os.listdir(u.BM_PDF()) bib = bm.find(key='ShowmanEtal2009apjRadGCM') assert bib.pdf == name
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
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
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())
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
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
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
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())
def test_find_no_arguments(mock_init_sample): with pytest.raises( ValueError, match="Either key or bibcode arguments must be specified."): bib = bm.find()
def test_find_bibs(bibs): key = 'StoddenEtal2009ciseRRlegal' my_bibs = [bibs["beaulieu_apj"], bibs["stodden"]] bib = bm.find(key=key, bibs=my_bibs) assert bib is not None assert bib.key == key
def test_find_bibcode_not_found(mock_init_sample): bib = bm.find(bibcode='non_existing_bibcode') assert bib is None
def test_find_key_not_found(mock_init_sample): bib = bm.find(key='non_existing_key') assert bib is None
def test_find_bibcode(mock_init_sample): bibcode = '2013A&A...558A..33A' bib = bm.find(bibcode=bibcode) assert bib is not None assert bib.bibcode == bibcode
def test_find_key(mock_init_sample): key = 'AASteamHendrickson2018aastex62' bib = bm.find(key=key) assert bib is not None assert bib.key == key
def test_set_pdf_invalid_extension(mock_init_sample): bib = bm.find(key='ShowmanEtal2009apjRadGCM') bin_pdf = b'file.pdf' with pytest.raises(ValueError, match='Invalid filename, must have a .pdf extension'): pm.set_pdf(bib, bin_pdf=bin_pdf, filename='pdf_file')
def test_set_pdf_pathed_filename(mock_init_sample): bib = bm.find(key='ShowmanEtal2009apjRadGCM') bin_pdf = b'file.pdf' with pytest.raises(ValueError, match='filename must not have a path'): pm.set_pdf(bib, bin_pdf=bin_pdf, filename='./file.pdf')
def test_set_pdf_undefined_pdf(mock_init_sample): bib = bm.find(key='ShowmanEtal2009apjRadGCM') with pytest.raises(ValueError, match='Exactly one of pdf or bin_pdf must be not None'): pm.set_pdf(bib)