def test_readwrite_from_subclass_complete_info(self, cosmo_cls, cosmo, tmpdir, format): """ Test writing from an instance and reading from that class, when there's full information saved. """ fname = str(tmpdir / f"{cosmo.name}.{format}") cosmo.write(fname, format=format) # read with the same class that wrote. got = cosmo_cls.read(fname, format=format) assert got == cosmo assert got.meta == cosmo.meta # this should be equivalent to got = Cosmology.read(fname, format=format, cosmology=cosmo_cls) assert got == cosmo assert got.meta == cosmo.meta # and also got = Cosmology.read(fname, format=format, cosmology=cosmo_cls.__qualname__) assert got == cosmo assert got.meta == cosmo.meta
def test_from_subclass_complete_info(self, tmpdir, instance, format): """ Test writing from an instance and reading from that class, when there's full information saved. """ cosmo = getattr(cosmology.realizations, instance) fname = tmpdir / f"{instance}.{format}" cosmo.write(str(fname), format=format) # read with the same class that wrote. got = cosmo.__class__.read(fname, format=format) assert got == cosmo assert got.meta == cosmo.meta # this should be equivalent to got = Cosmology.read(fname, format=format, cosmology=cosmo.__class__) assert got == cosmo assert got.meta == cosmo.meta # and also got = Cosmology.read(fname, format=format, cosmology=cosmo.__class__.__qualname__) assert got == cosmo assert got.meta == cosmo.meta
def test_readwrite_from_subclass_partial_info(self, instance, tmpdir): """ Test writing from an instance and reading from that class. This requires partial information. .. todo:: remove when fix method in super """ cosmo = getattr(cosmology.realizations, instance) format = "json" fname = tmpdir / f"{cosmo.name}.{format}" cosmo.write(str(fname), format=format) # partial information with open(fname, "r") as file: L = file.readlines()[0] L = L[:L.index('"cosmology":')] + L[L.index(", ") + 2:] # remove cosmology i = L.index('"Tcmb0":') # delete Tcmb0 L = L[:i] + L[L.index(", ", L.index(", ", i) + 1) + 2:] # second occurence tempfname = tmpdir / f"{cosmo.name}_temp.{format}" with open(tempfname, "w") as file: file.writelines([L]) # read with the same class that wrote fills in the missing info with # the default value got = cosmo.__class__.read(tempfname, format=format) got2 = Cosmology.read(tempfname, format=format, cosmology=cosmo.__class__) got3 = Cosmology.read(tempfname, format=format, cosmology=cosmo.__class__.__qualname__) assert (got == got2) and (got2 == got3) # internal consistency # not equal, because Tcmb0 is changed, which also changes m_nu assert got != cosmo assert got.Tcmb0 == cosmo.__class__._init_signature.parameters[ "Tcmb0"].default assert got.clone(name=cosmo.name, Tcmb0=cosmo.Tcmb0, m_nu=cosmo.m_nu) == cosmo # but the metadata is the same assert got.meta == cosmo.meta
def test_readwrite_reader_class_mismatch(self, cosmo, tmpdir, format): """Test when the reader class doesn't match the file.""" fname = tmpdir / f"{cosmo.name}.{format}" cosmo.write(str(fname), format=format) # class mismatch # when reading directly with pytest.raises(TypeError, match="missing 1 required"): w0wzCDM.read(fname, format=format) with pytest.raises(TypeError, match="missing 1 required"): Cosmology.read(fname, format=format, cosmology=w0wzCDM) # when specifying the class with pytest.raises(ValueError, match="`cosmology` must be either"): w0wzCDM.read(fname, format=format, cosmology="FlatLambdaCDM")
def test_from_subclass_partial_info(self, tmpdir, instance): """ Test writing from an instance and reading from that class. This requires partial information. .. todo:: generalize over all save formats for this test. """ format = "json" cosmo = getattr(cosmology.realizations, instance) fname = tmpdir / f"{instance}.{format}" cosmo.write(str(fname), format=format) # partial information with open(fname, "r") as file: L = file.readlines() L[0] = L[0][:L[0].index('"cosmology":')] + L[0][L[0].index(', ') + 2:] i = L[0].index('"Tcmb0":') # delete Tcmb0 L[0] = L[0][:i] + L[0][L[0].index(', ', i) + 2:] tempfname = tmpdir / f"{instance}_temp.{format}" with open(tempfname, "w") as file: file.writelines(L) # read with the same class that wrote fills in the missing info with # the default value got = cosmo.__class__.read(tempfname, format=format) got2 = Cosmology.read(tempfname, format=format, cosmology=cosmo.__class__) got3 = Cosmology.read(tempfname, format=format, cosmology=cosmo.__class__.__qualname__) assert (got == got2) and (got2 == got3) # internal consistency # not equal, because Tcmb0 is changed assert got != cosmo assert got.Tcmb0 == cosmo.__class__._init_signature.parameters[ "Tcmb0"].default assert got.clone(name=cosmo.name, Tcmb0=cosmo.Tcmb0.value) == cosmo # but the metadata is the same assert got.meta == cosmo.meta
def test_roundtrip_from_astropy(self, tmp_path, instance, format): cosmo = getattr(cosmology.realizations, instance) fname = tmp_path / f"{instance}.{format}" # write to file cosmo.write(str(fname), format=format) # also test kwarg "overwrite" assert os.path.exists(str(fname)) # file exists with pytest.raises(IOError): cosmo.write(str(fname), format=format, overwrite=False) assert os.path.exists(str(fname)) # overwrite file existing file cosmo.write(str(fname), format=format, overwrite=True) # Read back got = Cosmology.read(fname, format=format) # test round-tripped as expected assert got == cosmo # tests immutable parameters, e.g. H0
def test_readwrite_complete_info(self, cosmo, tmpdir, format): """ Test writing from an instance and reading from the base class. This requires full information. """ fname = tmpdir / f"{cosmo.name}.{format}" cosmo.write(str(fname), format=format) # Also test kwarg "overwrite" assert os.path.exists(str(fname)) # file exists with pytest.raises(IOError): cosmo.write(str(fname), format=format, overwrite=False) assert os.path.exists(str(fname)) # overwrite file existing file cosmo.write(str(fname), format=format, overwrite=True) # Read back got = Cosmology.read(fname, format=format) assert got == cosmo assert got.meta == cosmo.meta
def test_readwrite_complete_info(self, cosmo, tmpdir, format): """ Test writing from an instance and reading from the base class. This requires full information. The round-tripped metadata can be in a different order, so the OrderedDict must be converted to a dict before testing equality. """ fname = str(tmpdir / f"{cosmo.name}.{format}") cosmo.write(fname, format=format) # Also test kwarg "overwrite" assert os.path.exists(fname) # file exists with pytest.raises(IOError): cosmo.write(fname, format=format, overwrite=False) assert os.path.exists(fname) # overwrite file existing file cosmo.write(fname, format=format, overwrite=True) # Read back got = Cosmology.read(fname, format=format) assert got == cosmo assert dict(got.meta) == dict(cosmo.meta)