def test_data(): sc.heading('Data loading') data = cv.load_data(os.path.join(sc.thisdir(__file__), 'example_data.csv')) sc.pp(data) # Check that it is looking for the right file with pytest.raises(FileNotFoundError): data = cv.load_data(datafile='file_not_found.csv') return data
def test_misc(): sc.heading('Testing miscellaneous functions') sim_path = 'test_misc.sim' json_path = 'test_misc.json' gitinfo_path = 'test_misc.gitinfo' fig_path = 'test_misc.png' fig_comments = 'Test comment' # Data loading cv.load_data(csv_file) cv.load_data(xlsx_file) with pytest.raises(NotImplementedError): cv.load_data('example_data.unsupported_extension') with pytest.raises(ValueError): cv.load_data(xlsx_file, columns=['missing_column']) # Dates d1 = cv.date('2020-04-04') d2 = cv.date(sc.readdate('2020-04-04')) ds = cv.date('2020-04-04', d2) assert d1 == d2 assert d2 == ds[0] with pytest.raises(ValueError): cv.date([(2020, 4, 4)]) # Raises a TypeError which raises a ValueError with pytest.raises(ValueError): cv.date('Not a date') cv.daydiff('2020-04-04') # Run sim for more investigations sim = cv.Sim(pop_size=500, verbose=0) sim.run() sim.plot(do_show=False) # Saving and loading cv.savefig(fig_path, comments=fig_comments) cv.save(filename=sim_path, obj=sim) cv.load(filename=sim_path) # Version checks cv.check_version('0.0.0') # Nonsense version print('↑ Should complain about version') with pytest.raises(ValueError): cv.check_version('0.0.0', die=True) # Git checks cv.git_info(json_path) cv.git_info(json_path, check=True) # Poisson tests c1 = 5 c2 = 8 for alternative in ['two-sided', 'larger', 'smaller']: cv.poisson_test(c1, c2, alternative=alternative) for method in ['score', 'wald', 'sqrt', 'exact-cond']: cv.poisson_test(c1, c2, method=method) with pytest.raises(ValueError): cv.poisson_test(c1, c2, method='not a method') # Test locations for location in [None, 'viet-nam']: cv.data.show_locations(location) # Test versions with pytest.raises(ValueError): cv.check_save_version('1.3.2', die=True) cv.check_save_version(cv.__version__, filename=gitinfo_path, comments='Test') # Test PNG try: metadata = cv.get_png_metadata(fig_path, output=True) assert metadata['Covasim version'] == cv.__version__ assert metadata['Covasim comments'] == fig_comments except ImportError as E: print( f'Cannot test PNG function since pillow not installed ({str(E)}), skipping' ) # Tidy up remove_files(sim_path, json_path, fig_path, gitinfo_path) return
def test_misc(): sc.heading('Testing miscellaneous functions') sim_path = 'test_misc.sim' json_path = 'test_misc.json' # Data loading cv.load_data(csv_file) cv.load_data(xlsx_file) with pytest.raises(NotImplementedError): cv.load_data('example_data.unsupported_extension') with pytest.raises(ValueError): cv.load_data(xlsx_file, columns=['missing_column']) # Dates d1 = cv.date('2020-04-04') d2 = cv.date(sc.readdate('2020-04-04')) ds = cv.date('2020-04-04', d2) assert d1 == d2 assert d2 == ds[0] with pytest.raises(ValueError): cv.date([(2020, 4, 4)]) # Raises a TypeError which raises a ValueError with pytest.raises(ValueError): cv.date('Not a date') cv.daydiff('2020-04-04') # Saving and loading sim = cv.Sim() cv.save(filename=sim_path, obj=sim) cv.load(filename=sim_path) # Version checks cv.check_version('0.0.0') # Nonsense version print('↑ Should complain about version') with pytest.raises(ValueError): cv.check_version('0.0.0', die=True) # Git checks cv.git_info(json_path) cv.git_info(json_path, check=True) # Poisson tests c1 = 5 c2 = 8 for alternative in ['two-sided', 'larger', 'smaller']: cv.poisson_test(c1, c2, alternative=alternative) for method in ['score', 'wald', 'sqrt', 'exact-cond']: cv.poisson_test(c1, c2, method=method) with pytest.raises(ValueError): cv.poisson_test(c1, c2, method='not a method') # Tidy up remove_files(sim_path, json_path) return