def test_set_home_none_success(tmp_path, mock_init): new_home = f'{tmp_path}/bm' cm.set('home', new_home) # These files/folders stay: assert set(os.listdir(u.HOME)) == set(["config", "examples", "pdf"]) # These files have been moved/created: assert os.listdir(str(new_home)) == ['pdf']
def test_search_unauthorized(reqs, mock_init): cm.set("ads_token", "None") query = 'author:"^fortney, j" year:2000-2018 property:refereed' with pytest.raises(ValueError, match='Unauthorized access to ADS. ' 'Check that the ADS token is valid.'): results, nmatch = am.search(query)
def test_set_editor(mock_init): # This is the only way to make sure vi is valid I can think of: if shutil.which("vi") is not None: cm.set("text_editor", "vi") assert cm.get("text_editor") == "vi" else: pass
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
def test_set_style_raises(mock_init): # Value must be a valid pygments style: with pytest.raises( ValueError, match= "'invalid_pygment' is not a valid style option. Available options are:" ): cm.set("style", "invalid_pygment")
def test_set_home_overwrite(tmp_path, mock_init_sample): new_home = f'{tmp_path}/bm' pathlib.Path(f"{u.BM_PDF()}Slipher1913.pdf").touch() os.mkdir(f'{new_home}') os.mkdir(f'{new_home}/pdf') pathlib.Path(f"{u.BM_HISTORY_SEARCH()}").touch() pathlib.Path(f"{new_home}/history_search").touch() pathlib.Path(f"{new_home}/pdf/Slipher1913.pdf").touch() cm.set('home', new_home)
def test_manager_query_caching(capsys, reqs, ads_entries, mock_init): cm.set('ads_display', '2') captured = capsys.readouterr() am.search.__defaults__ = 0, 4, 'pubdate+desc' query = 'author:"^fortney, j" year:2000-2018 property:refereed' am.manager(query) captured = capsys.readouterr() assert os.path.exists(u.BM_CACHE()) assert captured.out == f"""
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"""
def test_manager_from_cache(capsys, reqs, ads_entries, mock_init): cm.set('ads_display', '2') captured = capsys.readouterr() am.search.__defaults__ = 0, 4, 'pubdate+desc' query = 'author:"^fortney, j" year:2000-2018 property:refereed' am.manager(query) captured = capsys.readouterr() am.manager(None) captured = capsys.readouterr() assert captured.out == expected_output2 + 'Showing entries 3--4 out of 26 matches. To show the next set, execute:\nbibm ads-search -n\n'
def test_manager_cache_trigger_search(capsys, reqs, ads_entries, mock_init): cm.set('ads_display', '2') am.search.__defaults__ = 0, 4, 'pubdate+desc' query = 'author:"^fortney, j" year:2000-2018 property:refereed' am.manager(query) am.manager(None) captured = capsys.readouterr() am.manager(None) captured = capsys.readouterr() assert captured.out == f"""
def test_manager_cache_trigger_search(capsys, reqs, ads_entries, mock_init): cm.set('ads_display', '2') am.search.__defaults__ = 0, 4, 'pubdate+desc' query = 'author:"^fortney, j" year:2000-2018 property:refereed' am.manager(query) am.manager(None) captured = capsys.readouterr() am.manager(None) captured = capsys.readouterr() expected_output = "\x1b[0m\x1b[?7h\x1b[0m\r\n\x1b[0;38;5;33mTitle\x1b[0m: \x1b[0;38;5;130mDiscovery and Atmospheric Characterization of Giant Planet Kepler-12b:\r\n An Inflated Radius Outlier\x1b[0m\r\n\x1b[0;38;5;33mAuthors\x1b[0m: \x1b[0;38;5;130mFortney, Jonathan J.; et al.\x1b[0m\r\n\x1b[0;38;5;33mADS URL\x1b[0m: \x1b[0;38;5;130mhttps://ui.adsabs.harvard.edu/abs/2011ApJS..197....9F\x1b[0m\r\n\x1b[0;38;5;33mbibcode\x1b[0m: \x1b[0;38;5;142m2011ApJS..197....9F\x1b[0m\r\n\x1b[0m\x1b[0m\x1b[?7h\x1b[0m\r\n\x1b[0;38;5;33mTitle\x1b[0m: \x1b[0;38;5;130mSelf-consistent Model Atmospheres and the Cooling of the Solar System's\r\n Giant Planets\x1b[0m\r\n\x1b[0;38;5;33mAuthors\x1b[0m: \x1b[0;38;5;130mFortney, J. J.; et al.\x1b[0m\r\n\x1b[0;38;5;33mADS URL\x1b[0m: \x1b[0;38;5;130mhttps://ui.adsabs.harvard.edu/abs/2011ApJ...729...32F\x1b[0m\r\n\x1b[0;38;5;33mbibcode\x1b[0m: \x1b[0;38;5;142m2011ApJ...729...32F\x1b[0m\r\n\x1b[0m\nShowing entries 5--6 out of 26 matches. To show the next set, execute:\nbibm ads-search -n\n" assert captured.out == expected_output
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"])
def test_set_home_pdf_success(tmp_path, mock_init_sample): new_home = f'{tmp_path}/bm' old_pdf_home = u.BM_PDF() pathlib.Path(f"{u.BM_PDF()}Rubin1980.pdf").touch() pathlib.Path(f"{u.BM_PDF()}Slipher1913.pdf").touch() cm.set('home', new_home) # Not registered in database, not moved: assert 'Rubin1980.pdf' in os.listdir(f'{old_pdf_home}') # Moved: assert 'Slipher1913.pdf' in os.listdir(f'{new_home}/pdf/') assert 'Slipher1913.pdf' in os.listdir(u.BM_PDF())
def test_manager_query_caching(capsys, reqs, ads_entries, mock_init): cm.set('ads_display', '2') captured = capsys.readouterr() am.search.__defaults__ = 0, 4, 'pubdate+desc' query = 'author:"^fortney, j" year:2000-2018 property:refereed' am.manager(query) captured = capsys.readouterr() assert os.path.exists(u.BM_CACHE()) assert captured.out == ( expected_output1 + 'Showing entries 1--2 out of 26 matches. To show the next set, ' 'execute:\nbibm ads-search -n\n')
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"])
def test_set_home_merge(tmp_path, bibs, mock_init): new_home = f'{tmp_path}/bm' os.mkdir(f'{new_home}') bib1, bib2 = bibs["beaulieu_apj"], bibs["stodden"] bib1.pdf, bib2.pdf = 'file1.pdf', 'file2.pdf' bm.save([bib1]) shutil.copy(u.BM_DATABASE(), f'{new_home}/bm_database.pickle') bm.export([bib1], f'{new_home}/bm_bibliography.bib', meta=True) bm.export([bib2], f'{new_home}/other.bib', meta=True) bm.init(f'{new_home}/other.bib') cm.set('home', new_home) # Check DBs are merged: bibs = bm.load() assert len(bibs) == 2 assert bibs[0].content == bib1.content assert bibs[1].content == bib2.content # Check both meta exist: assert bibs[0].pdf == 'file1.pdf' assert bibs[1].pdf == 'file2.pdf'
def test_set_home_success(capsys, tmp_path, mock_init_sample): new_home = f'{tmp_path}/bm' cm.set('home', new_home) assert cm.get('home') == new_home + '/' # 'constants' now point to new home: assert u.BM_DATABASE() == f'{new_home}/bm_database.pickle' assert u.BM_BIBFILE() == f'{new_home}/bm_bibliography.bib' assert u.BM_TMP_BIB() == f'{new_home}/tmp_bibliography.bib' assert u.BM_CACHE() == f'{new_home}/cached_ads_query.pickle' assert u.BM_HISTORY_SEARCH() == f'{new_home}/history_search' assert u.BM_HISTORY_ADS() == f'{new_home}/history_ads_search' assert u.BM_PDF() == f'{new_home}/pdf/' captured = capsys.readouterr() assert captured.out == f'home updated to: {new_home}/.\n' # These files/folders stay: assert set(os.listdir(u.HOME)) == set(["config", "examples", "pdf"]) # These files have been moved/created: assert set(os.listdir(str(new_home))) == \ set(['pdf', 'bm_bibliography.bib', 'bm_database.pickle'])
def test_set_home_and_edit(tmp_path, mock_init, bibs, reqs): new_home = f'{tmp_path}/bm' cm.set('home', new_home) assert cm.get('home') == new_home + '/' # Test merge: bm.merge(new=[bibs["beaulieu_apj"]], take='new') # Test search: matches = bm.search(authors="beaulieu") assert len(matches) == 1 assert 'BeaulieuEtal2011apjGJ436bMethane' in matches[0].key # Test ADS add: am.add_bibtex(['1925PhDT.........1P'], ['Payne1925phdStellarAtmospheres']) # Test load: current_bibs = bm.load() assert len(current_bibs) == 2 assert 'Payne1925phdStellarAtmospheres' in current_bibs[1].key # These files/folders stay: assert set(os.listdir(u.HOME)) == set(["config", "examples", "pdf"]) # These files have been moved/created: assert set(os.listdir(str(new_home))) == \ set(['pdf', 'bm_bibliography.bib', 'bm_database.pickle'])
def test_set_home_no_parent(mock_init_sample): with pytest.raises( ValueError, match="The home value must have an existing parent folder"): cm.set("home", "fake_parent/some_dir")
def test_search_unauthorized(reqs): cm.set("ads_token", "None") querry = 'author:"^fortney, j" year:2000-2018 property:refereed' with pytest.raises(ValueError, match="Invalid ADS request: Unauthorized," \ " check you have a valid ADS token."): results, nmatch = am.search(querry)
def test_set_ads_display_raises(mock_init): with pytest.raises( ValueError, match="The ads_display value must be a positive integer."): cm.set("ads_display", "fifty")
def test_set_ads_display(mock_init): cm.set("ads_display", "50") assert cm.get("ads_display") == "50"
def test_set_ads_token(mock_init): cm.set("ads_token", "abc12345") assert cm.get("ads_token") == "abc12345"
def test_set_paper(mock_init): cm.set("paper", "A4") assert cm.get("paper") == "A4"
def test_set_editor_raises(mock_init): # Value must be a valid pygments style: with pytest.raises(ValueError, match="'invalid_editor' is not a valid text editor."): cm.set("text_editor", "invalid_editor")
def test_set_home_file_extension(mock_init_sample): with pytest.raises(ValueError, match="The home value cannot have a file extension"): cm.set("home", "./new.home")
def test_set_style(mock_init): cm.set("style", "fruity") assert cm.get("style") == "fruity"
def test_set_raises(mock_init): with pytest.raises( ValueError, match="'invalid_param' is not a valid bibmanager config parameter." ): cm.set("invalid_param", "value")
def test_set_editor_default(mock_init): cm.set("text_editor", "default") assert cm.get("text_editor") == "default"