Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any pytest version take 2 #360

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions carsus/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,38 @@ def pytest_configure(config):


def pytest_addoption(parser):
parser.addoption("--runslow", action="store_true",
help="include running slow tests during run")
parser.addoption("--test-db", dest='test-db', default=None,
help="filename for the testing database")
parser.addoption("--refdata", dest='refdata', default=None,
help="carsus-refdata folder location")
parser.addoption(
"--runslow",
action="store_true",
default=False,
help="include running slow tests during run"
)
parser.addoption(
"--test-db",
dest="test-db",
default=None,
help="filename for the testing database",
)
parser.addoption(
"--refdata", dest="refdata", default=None, help="carsus-refdata folder location"
)


def pytest_collection_modifyitems(config, items):
skip_slow = pytest.mark.skip(reason="need --runslow option to run")
skip_not_with_refdata = pytest.mark.skip(
reason="carsus-refdata folder location not specified"
)
skip_not_with_testdb = pytest.mark.skip(
reason="filename for the testing database not specified"
)
for item in items:
if "slow" in item.keywords and not config.getoption("--runslow"):
item.add_marker(skip_slow)
if "with_refdata" in item.keywords and not config.getoption("--refdata"):
item.add_marker(skip_not_with_refdata)
if "with_test_db" in item.keywords and not config.getoption("--test-db"):
item.add_marker(skip_not_with_testdb)


@pytest.fixture
Expand Down
10 changes: 3 additions & 7 deletions carsus/io/tests/test_chianti.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
from carsus.model import Level, Ion, Line, ECollision


slow = pytest.mark.skipif(
not pytest.config.getoption("--runslow"), reason="need --runslow option to run"
)


@pytest.fixture
def ch_ingester(memory_session):
Expand Down Expand Up @@ -45,7 +41,7 @@ def test_chianti_reader_read_collisions(self, ch_ion_reader):
return ch_ion_reader.collisions


@slow
@pytest.mark.slow
@pytest.mark.parametrize(
"atomic_number, ion_charge, levels_count", [(10, 1, 138), (17, 3, 5)]
)
Expand All @@ -59,7 +55,7 @@ def test_chianti_ingest_levels_count(
assert len(ion.levels) == levels_count


@slow
@pytest.mark.slow
@pytest.mark.parametrize("atomic_number, ion_charge, lines_count", [(10, 1, 1999)])
def test_chianti_ingest_lines_count(
memory_session, ch_ingester, atomic_number, ion_charge, lines_count
Expand All @@ -77,7 +73,7 @@ def test_chianti_ingest_lines_count(
assert cnt == lines_count


@slow
@pytest.mark.slow
@pytest.mark.parametrize("atomic_number, ion_charge, e_col_count", [(10, 1, 9453)])
def test_chianti_ingest_e_col_count(
memory_session, ch_ingester, atomic_number, ion_charge, e_col_count
Expand Down
27 changes: 12 additions & 15 deletions carsus/io/tests/test_cmfgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@

from carsus.io.cmfgen.util import *

with_refdata = pytest.mark.skipif(
not pytest.config.getoption("--refdata"), reason="--refdata folder not specified"
)
data_dir = os.path.join(os.path.dirname(__file__), "data")


@with_refdata
@pytest.mark.with_refdata
@pytest.fixture()
def si1_reader():
return CMFGENReader.from_config(
Expand All @@ -40,7 +37,7 @@ def cmfgen_refdata_fname(refdata_path, path):
return os.path.join(refdata_path, "cmfgen", subdirectory, fname)


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
@pytest.mark.parametrize(
"path",
Expand All @@ -55,7 +52,7 @@ def test_CMFGENEnergyLevelsParser(cmfgen_refdata_fname):
return parser.base


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
@pytest.mark.parametrize(
"path",
Expand All @@ -72,7 +69,7 @@ def test_CMFGENOscillatorStrengthsParser(cmfgen_refdata_fname):
return parser.base


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
@pytest.mark.parametrize(
"path",
Expand All @@ -86,7 +83,7 @@ def test_CMFGENCollisionalStrengthsParser(cmfgen_refdata_fname):
return parser.base


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.parametrize(
"path",
[
Expand All @@ -102,7 +99,7 @@ def test_CMFGENPhoCrossSectionsParser(cmfgen_refdata_fname):
return parser.base[0]


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
@pytest.mark.parametrize(
"path",
Expand All @@ -116,7 +113,7 @@ def test_CMFGENHydLParser(cmfgen_refdata_fname):
return parser.base


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
@pytest.mark.parametrize(
"path",
Expand All @@ -130,31 +127,31 @@ def test_CMFGENHydGauntBfParser(cmfgen_refdata_fname):
return parser.base


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
def test_reader_lines(si1_reader):
return si1_reader.lines


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
def test_reader_levels(si1_reader):
return si1_reader.levels


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
def test_reader_collisions(si1_reader):
return si1_reader.collisions


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
def test_reader_cross_sections_squeeze(si1_reader):
return si1_reader.cross_sections


@with_refdata
@pytest.mark.with_refdata
@pytest.mark.array_compare(file_format="pd_hdf")
def test_reader_ionization_energies(si1_reader):
return si1_reader.ionization_energies.to_frame()
Expand Down
41 changes: 19 additions & 22 deletions carsus/io/tests/test_output_tardis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
from carsus.io.output.tardis_ import AtomData
from carsus.model import DataSource, Ion

with_test_db = pytest.mark.skipif(
not pytest.config.getoption("--test-db"),
reason="--testing database was not specified"
)


@pytest.fixture
def atom_data(test_session, chianti_short_name):
Expand Down Expand Up @@ -115,13 +112,13 @@ def test_atom_data_chianti_ions_subset(memory_session):
chianti_ions="He 1; N 5; Si 1")


@with_test_db
@pytest.mark.with_test_db
def test_atom_data_wo_chianti_ions_attributes(atom_data_be, test_session):
assert atom_data_be.chianti_ions == list()
assert test_session.query(atom_data_be.chianti_ions_table).count() == 0


@with_test_db
@pytest.mark.with_test_db
def test_atom_data_only_be(atom_data_be):
assert all([atomic_number == 4 for atomic_number in
atom_data_be.atom_masses["atomic_number"].values.tolist()])
Expand All @@ -133,7 +130,7 @@ def test_atom_data_only_be(atom_data_be):
atom_data_be.lines["atomic_number"].values.tolist()])


@with_test_db
@pytest.mark.with_test_db
def test_atom_data_join_on_chianti_ions_table(test_session, atom_data):

# This join operation leads to an empty chianti_ions list
Expand All @@ -148,7 +145,7 @@ def test_atom_data_join_on_chianti_ions_table(test_session, atom_data):
assert set(chianti_ions) == set([(2,1), (7,5)])


@with_test_db
@pytest.mark.with_test_db
def test_atom_data_two_instances_same_session(
test_session,
chianti_short_name):
Expand All @@ -167,7 +164,7 @@ def test_atom_data_two_instances_same_session(
atom_data2.chianti_ions_table


@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number, exp_mass", [
(2, 4.002602 * u.u),
(4, 9.0121831 * u.u),
Expand All @@ -184,7 +181,7 @@ def test_create_atom_masses(atom_masses, atomic_number, exp_mass):
)


@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number, ion_number, exp_ioniz_energy", [
(2, 1, 54.41776311 * u.eV),
(4, 2, 153.896198 * u.eV),
Expand All @@ -201,7 +198,7 @@ def test_create_ionizatinon_energies(ionization_energies, atomic_number, ion_num
)


@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number, ion_number, ionization_energy",[
(2, 1, 54.41776311 * u.eV),
(4, 2, 153.896198 * u.eV),
Expand All @@ -216,7 +213,7 @@ def test_create_levels_filter_auto_ionizing_levels(levels, atomic_number, ion_nu
assert all(levels_energies < ionization_energy)


@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number, ion_number, level_number, exp_energy, exp_g, exp_metastable_flag",[
# Kurucz levels
(4, 2, 0, 0.0 * u.Unit("cm-1"), 1, True),
Expand Down Expand Up @@ -248,7 +245,7 @@ def test_create_levels(levels, atomic_number, ion_number, level_number,
assert metastable_flag == exp_metastable_flag


@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number, ion_number, level_number_lower, level_number_upper, exp_wavelength, exp_loggf",[
# Kurucz lines
(14, 1, 0, 57, 81.8575 * u.Unit("nm"), -1.92),
Expand All @@ -270,7 +267,7 @@ def test_create_lines(lines, atomic_number, ion_number, level_number_lower, leve
assert_almost_equal(loggf, exp_loggf)


@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number, ion_number, level_number_lower, level_number_upper, exp_wavelength, exp_loggf",[
# Kurucz lines with wavelength above 2000 Angstrom
(14, 1, 8, 83, 2015.574293 * u.Unit("angstrom"), -0.120),
Expand All @@ -287,7 +284,7 @@ def test_create_lines_convert_air2vacuum(lines, atomic_number, ion_number, level
assert_quantity_allclose(wavelength, exp_wavelength)
assert_almost_equal(loggf, exp_loggf)

@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number, ion_number, level_number_lower, level_number_upper", [
# Default loggf_threshold = -3
# Kurucz lines
Expand All @@ -304,7 +301,7 @@ def test_create_lines_loggf_treshold(lines, atomic_number, ion_number, level_num
assert lines.loc[(atomic_number, ion_number, level_number_lower, level_number_upper)]


@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number", [2, 14, 30])
def test_levels_create_artificial_fully_ionized(levels, atomic_number):
levels = levels.set_index(["atomic_number", "ion_number", "level_number"])
Expand All @@ -314,22 +311,22 @@ def test_levels_create_artificial_fully_ionized(levels, atomic_number):
assert metastable

# ToDo: Implement real tests
@with_test_db
@pytest.mark.with_test_db
def test_create_collisions(collisions):
assert True


@with_test_db
@pytest.mark.with_test_db
def test_create_macro_atom(macro_atom):
assert True


@with_test_db
@pytest.mark.with_test_db
def test_create_macro_atom_ref(macro_atom_references):
assert True


@with_test_db
@pytest.mark.with_test_db
@pytest.mark.parametrize("atomic_number, ion_number, source_level_number", [
(2, 2, 0),
(5, 5, 0),
Expand All @@ -347,12 +344,12 @@ def test_create_macro_atom_references_levels_wo_lines(macro_atom_references, ato
assert all([count == 0 for count in [count_up, count_down, count_total]])


@with_test_db
@pytest.mark.with_test_db
def test_create_zeta_data(zeta_data):
assert True


@with_test_db
@pytest.mark.with_test_db
def test_atom_data_to_hdf(atom_data, hdf5_path):
atom_data.to_hdf(hdf5_path, store_atom_masses=True, store_ionization_energies=True,
store_levels=True, store_lines=True, store_macro_atom=True,
Expand Down
2 changes: 1 addition & 1 deletion carsus_env3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ dependencies:
- pandoc

# Test/Coverage
- pytest =4
- pytest
- pytest-cov
- pytest-astropy
- epassaro::pytest-arraydiff
Expand Down
5 changes: 5 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
with_refdata: mark tests as needing refdata path to run
with_test_db: mark tests as needing test_db path to run
Loading