Skip to content

Commit

Permalink
Fix assert 'str' in str(ex) for pytest 5
Browse files Browse the repository at this point in the history
See pytest-dev/pytest#5579 for change
  • Loading branch information
tomalrussell committed Jul 30, 2019
1 parent fd56f2f commit 4a3f70d
Show file tree
Hide file tree
Showing 19 changed files with 119 additions and 119 deletions.
4 changes: 2 additions & 2 deletions tests/controller/test_modelrun.py
Expand Up @@ -131,7 +131,7 @@ def test_builder_scenario_sosmodelrun_not_in_sosmodel(self, config_data):
with raises(SmifModelRunError) as ex:
builder.finish()
assert "ScenarioSets {'population'} are selected in the ModelRun " \
"configuration but not found in the SosModel configuration" in str(ex)
"configuration but not found in the SosModel configuration" in str(ex.value)


class TestModelRun:
Expand All @@ -152,7 +152,7 @@ def test_run_timesteps(self, config_data):
store = Mock()
with raises(SmifModelRunError) as ex:
model_run.run(store)
assert 'No timesteps specified' in str(ex)
assert 'No timesteps specified' in str(ex.value)

def test_serialize(self, config_data):
"""Serialise back to config dict
Expand Down
16 changes: 8 additions & 8 deletions tests/convert/test_interval.py
Expand Up @@ -497,10 +497,10 @@ def test_equality(self):

def test_invalid_set_interval(self):
interval = Interval('test', ('PT0H', 'PT1H'))
with raises(ValueError) as excinfo:
with raises(ValueError) as ex:
interval.interval = None
msg = "A time interval must add either a single tuple or a list of tuples"
assert msg in str(excinfo)
assert msg in str(ex.value)

def test_empty_interval_tuple(self):
with raises(ValueError):
Expand Down Expand Up @@ -747,19 +747,19 @@ def test_validate_intervals_passes(self, remap_months):
def test_validate_intervals_fails(self, remap_months):
data = remap_months
data.append({'name': '5', 'interval': [('PT0H', 'PT1H')]})
with raises(ValueError) as excinfo:
with raises(ValueError) as ex:
IntervalSet('remap_months', data)
assert "Duplicate entry for hour 0 in interval set remap_months." in str(excinfo.value)
assert "Duplicate entry for hour 0 in interval set remap_months." in str(ex.value)

def test_time_interval_start_before_end(self):
with raises(ValueError) as excinfo:
with raises(ValueError) as ex:
Interval('backwards', ('P1Y', 'P3M'))
assert "A time interval must not end before it starts" in str(excinfo)
assert "A time interval must not end before it starts" in str(ex.value)

interval = Interval('starts_ok', ('P0Y', 'P1M'))
with raises(ValueError) as excinfo:
with raises(ValueError) as ex:
interval.interval = ('P2M', 'P1M')
assert "A time interval must not end before it starts" in str(excinfo)
assert "A time interval must not end before it starts" in str(ex.value)


class TestIntersection:
Expand Down
4 changes: 2 additions & 2 deletions tests/convert/test_region.py
Expand Up @@ -235,7 +235,7 @@ def test_must_have_unique_names(self):
}
])

assert 'Region set must have uniquely named regions' in str(ex)
assert 'Region set must have uniquely named regions' in str(ex.value)


class TestRegionRegister():
Expand All @@ -247,7 +247,7 @@ def test_create(self):

with raises(ValueError) as ex:
register.get_entry('nonexistent')
assert "ResolutionSet 'nonexistent' not registered" in str(ex)
assert "ResolutionSet 'nonexistent' not registered" in str(ex.value)

def test_convert_equal(self, register):
data = np.ones(1)
Expand Down
32 changes: 16 additions & 16 deletions tests/data_layer/test_config_store_yaml.py
Expand Up @@ -48,7 +48,7 @@ def test_model_run_write_twice(self, model_run, config_handler):

with raises(SmifDataExistsError) as ex:
config_handler.write_model_run(model_run1)
assert "Model_run 'unique' already exists" in str(ex)
assert "Model_run 'unique' already exists" in str(ex.value)

def test_model_run_read_one(self, model_run, config_handler):
"""Test reading a single model_run.
Expand All @@ -70,7 +70,7 @@ def test_model_run_read_missing(self, config_handler):
"""
with raises(SmifDataNotFoundError) as ex:
config_handler.read_model_run('missing_name')
assert "Model_run 'missing_name' not found" in str(ex)
assert "Model_run 'missing_name' not found" in str(ex.value)

def test_model_run_update(self, model_run, config_handler):
"""Test updating a model_run description
Expand All @@ -95,7 +95,7 @@ def test_model_run_update_mismatch(self, model_run, config_handler):
model_run['name'] = 'model_run'
with raises(SmifDataMismatchError) as ex:
config_handler.update_model_run('model_run2', model_run)
assert "name 'model_run2' must match 'model_run'" in str(ex)
assert "name 'model_run2' must match 'model_run'" in str(ex.value)

def test_model_run_update_missing(self, model_run, config_handler):
"""Test that updating a nonexistent model_run should fail
Expand All @@ -105,7 +105,7 @@ def test_model_run_update_missing(self, model_run, config_handler):

with raises(SmifDataNotFoundError) as ex:
config_handler.update_model_run('missing_name', model_run)
assert "Model_run 'missing_name' not found" in str(ex)
assert "Model_run 'missing_name' not found" in str(ex.value)

def test_model_run_delete(self, model_run, config_handler):
"""Test that updating a nonexistent model_run should fail
Expand All @@ -126,7 +126,7 @@ def test_model_run_delete_missing(self, model_run, config_handler):
"""
with raises(SmifDataNotFoundError) as ex:
config_handler.delete_model_run('missing_name')
assert "Model_run 'missing_name' not found" in str(ex)
assert "Model_run 'missing_name' not found" in str(ex.value)


class TestSosModel:
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_sos_model_write_twice(self, get_sos_model, config_handler):

with raises(SmifDataExistsError) as ex:
config_handler.write_sos_model(sos_model1)
assert "Sos_model 'unique' already exists" in str(ex)
assert "Sos_model 'unique' already exists" in str(ex.value)

def test_sos_model_read_one(self, get_sos_model, config_handler):
"""Test reading a single sos_model.
Expand All @@ -184,7 +184,7 @@ def test_sos_model_read_missing(self, config_handler):
"""
with raises(SmifDataNotFoundError) as ex:
config_handler.read_sos_model('missing_name')
assert "Sos_model 'missing_name' not found" in str(ex)
assert "Sos_model 'missing_name' not found" in str(ex.value)

def test_sos_model_update(self, get_sos_model, config_handler):
"""Test updating a sos_model description
Expand All @@ -209,7 +209,7 @@ def test_sos_model_update_mismatch(self, get_sos_model, config_handler):
sos_model['name'] = 'sos_model'
with raises(SmifDataMismatchError) as ex:
config_handler.update_sos_model('sos_model2', sos_model)
assert "name 'sos_model2' must match 'sos_model'" in str(ex)
assert "name 'sos_model2' must match 'sos_model'" in str(ex.value)

def test_sos_model_update_missing(self, get_sos_model, config_handler):
"""Test that updating a nonexistent sos_model should fail
Expand All @@ -219,7 +219,7 @@ def test_sos_model_update_missing(self, get_sos_model, config_handler):

with raises(SmifDataNotFoundError) as ex:
config_handler.update_sos_model('missing_name', sos_model)
assert "Sos_model 'missing_name' not found" in str(ex)
assert "Sos_model 'missing_name' not found" in str(ex.value)

def test_sos_model_delete(self, get_sos_model, config_handler):
"""Test that updating a nonexistent sos_model should fail
Expand All @@ -240,7 +240,7 @@ def test_sos_model_delete_missing(self, get_sos_model, config_handler):
"""
with raises(SmifDataNotFoundError) as ex:
config_handler.delete_sos_model('missing_name')
assert "Sos_model 'missing_name' not found" in str(ex)
assert "Sos_model 'missing_name' not found" in str(ex.value)


class TestSectorModel:
Expand Down Expand Up @@ -278,7 +278,7 @@ def test_sector_model_write_twice(self, get_sector_model, config_handler):

with raises(SmifDataExistsError) as ex:
config_handler.write_model(sector_model1)
assert "Sector_model 'unique' already exists" in str(ex)
assert "Sector_model 'unique' already exists" in str(ex.value)

def test_sector_model_read_one(self, get_sector_model, config_handler):
"""Test reading a single sector_model.
Expand All @@ -300,7 +300,7 @@ def test_sector_model_read_missing(self, config_handler):
"""
with raises(SmifDataNotFoundError) as ex:
config_handler.read_model('missing_name')
assert "Sector_model 'missing_name' not found" in str(ex)
assert "Sector_model 'missing_name' not found" in str(ex.value)

def test_sector_model_update(self, get_sector_model, config_handler):
"""Test updating a sector_model description
Expand All @@ -327,7 +327,7 @@ def test_sector_model_update_mismatch(self, get_sector_model, config_handler):
sector_model['name'] = 'sector_model'
with raises(SmifDataMismatchError) as ex:
config_handler.update_model('sector_model2', sector_model)
assert "name 'sector_model2' must match 'sector_model'" in str(ex)
assert "name 'sector_model2' must match 'sector_model'" in str(ex.value)

def test_sector_model_update_missing(self, get_sector_model, config_handler):
"""Test that updating a nonexistent sector_model should fail
Expand All @@ -337,7 +337,7 @@ def test_sector_model_update_missing(self, get_sector_model, config_handler):

with raises(SmifDataNotFoundError) as ex:
config_handler.update_model('missing_name', sector_model)
assert "Sector_model 'missing_name' not found" in str(ex)
assert "Sector_model 'missing_name' not found" in str(ex.value)

def test_sector_model_delete(self, get_sector_model, config_handler):
"""Test that updating a nonexistent sector_model should fail
Expand All @@ -354,7 +354,7 @@ def test_sector_model_delete_missing(self, get_sector_model, config_handler):
"""
with raises(SmifDataNotFoundError) as ex:
config_handler.delete_model('missing_name')
assert "Sector_model 'missing_name' not found" in str(ex)
assert "Sector_model 'missing_name' not found" in str(ex.value)


class TestScenarios:
Expand All @@ -374,4 +374,4 @@ def test_read_scenario_missing(self, config_handler):
"""
with raises(SmifDataNotFoundError) as ex:
config_handler.read_scenario('missing')
assert "Scenario 'missing' not found" in str(ex)
assert "Scenario 'missing' not found" in str(ex.value)
21 changes: 10 additions & 11 deletions tests/data_layer/test_data_array.py
Expand Up @@ -7,7 +7,7 @@
from numpy.testing import assert_array_equal
from pytest import fixture, raises
from smif.data_layer.data_array import DataArray, show_null
from smif.exception import SmifDataNotFoundError, SmifDataMismatchError
from smif.exception import SmifDataMismatchError
from smif.metadata import Spec


Expand Down Expand Up @@ -226,7 +226,7 @@ def test_multi_dim_order(self):
dtype='float'
)
data = numpy.array([
#4 2
# 4 2
[1, 2], # c
[5, 6], # a
[9, 0] # b
Expand Down Expand Up @@ -264,7 +264,7 @@ def test_match_metadata(self):
"['region'], instead got data columns ['other'] and index names ['region']"
with raises(SmifDataMismatchError) as ex:
DataArray.from_df(spec, df)
assert msg in str(ex)
assert msg in str(ex.value)

# may not be indexed, if columns are otherwise all okay
df = pd.DataFrame([
Expand All @@ -280,7 +280,7 @@ def test_match_metadata(self):
"['region'], instead got data columns ['test'] and index names [None]"
with raises(SmifDataMismatchError) as ex:
DataArray.from_df(spec, df)
assert msg in str(ex)
assert msg in str(ex.value)

# must not have dimension labels outside of the spec dimension
df = pd.DataFrame([
Expand All @@ -291,7 +291,7 @@ def test_match_metadata(self):
"dimension 'region': ['extra']"
with raises(SmifDataMismatchError) as ex:
DataArray.from_df(spec, df)
assert msg in str(ex)
assert msg in str(ex.value)

def test_scalar(self):
# should handle zero-dimensional case (numpy array as scalar)
Expand Down Expand Up @@ -327,7 +327,7 @@ def test_error_duplicate_rows_single_index(self):
DataArray.from_df(spec, df)

msg = "Data for 'test' contains duplicate values at [{'a': 1}]"
assert msg in str(ex)
assert msg in str(ex.value)

def test_error_duplicate_rows_multi_index(self):
spec = Spec(
Expand All @@ -349,7 +349,7 @@ def test_error_duplicate_rows_multi_index(self):

msg = "Data for 'test' contains duplicate values at [{'a': 2, 'b': 4}]"
msg_alt = "Data for 'test' contains duplicate values at [{'b': 4, 'a': 2}]"
assert msg in str(ex) or msg_alt in str(ex)
assert msg in str(ex.value) or msg_alt in str(ex.value)


class TestMissingData:
Expand All @@ -366,7 +366,7 @@ def test_missing_data_raises(self, small_da):

msg = "Data for 'test_data' had missing values - read 20 but expected 24 in " + \
"total, from dims of length {a: 2, b: 3, c: 4}"
assert msg in str(ex)
assert msg in str(ex.value)

def test_missing_data_message(self, small_da):
"""Should check for NaNs and raise SmifDataError
Expand All @@ -380,7 +380,7 @@ def test_missing_data_message(self, small_da):

expected = "Data for 'test_data' had missing values - read 22 but expected 24 in " + \
"total, from dims of length {a: 2, b: 3, c: 4}"
assert expected in str(ex)
assert expected in str(ex.value)

def test_missing_data_message_non_numeric(self, small_da_non_numeric):
"""Should check for NaNs and raise SmifDataError
Expand All @@ -394,7 +394,7 @@ def test_missing_data_message_non_numeric(self, small_da_non_numeric):

expected = "Data for 'test_data' had missing values - read 22 but expected 24 in " + \
"total, from dims of length {a: 2, b: 3, c: 4}"
assert expected in str(ex)
assert expected in str(ex.value)

def test_no_missing_data(self, small_da):

Expand Down Expand Up @@ -439,7 +439,6 @@ def test_missing_data_non_numeric(self, small_da_non_numeric):
except TypeError:
index = pd.MultiIndex(levels=levels, labels=codes, names=names)


expected = pd.DataFrame(data=numpy.array([[None]], dtype=numpy.object),
index=index,
columns=['test_data'])
Expand Down
8 changes: 4 additions & 4 deletions tests/data_layer/test_data_handle.py
Expand Up @@ -469,7 +469,7 @@ def test_set_data_wrong_shape(self, mock_store, mock_model_with_conversion):

msg = "Data shape (1, 2) does not match spec " \
"(2, 1)"
assert msg in str(ex)
assert msg in str(ex.value)

def test_set_data_with_square_brackets(self, mock_store, mock_model):
"""should allow dict-like write access to output data
Expand Down Expand Up @@ -499,10 +499,10 @@ def test_set_data_with_square_brackets_raises(self, mock_store, mock_model):
spec = mock_model.outputs['gas_demand']
da = DataArray(spec, data)

with raises(TypeError) as err:
with raises(TypeError) as ex:
data_handle["gas_demand"] = da

assert "Pass in a numpy array" in str(err)
assert "Pass in a numpy array" in str(ex.value)


class TestDataHandleState():
Expand Down Expand Up @@ -624,7 +624,7 @@ def test_previous_timestep_error(self, mock_model, mock_store):
data_handle = DataHandle(mock_store, 1, 2015, [2015, 2020], mock_model)
with raises(SmifTimestepResolutionError) as ex:
data_handle.previous_timestep
assert 'no previous timestep' in str(ex)
assert 'no previous timestep' in str(ex.value)


class TestDataHandleGetResults:
Expand Down
2 changes: 1 addition & 1 deletion tests/data_layer/test_data_store.py
Expand Up @@ -119,7 +119,7 @@ def test_read_data_array_missing_timestep(self, handler, scenario):
msg = "not found for timestep 2011"
with raises(SmifDataNotFoundError) as ex:
handler.read_scenario_variant_data('mortality.csv', spec, 2011)
assert msg in str(ex)
assert msg in str(ex.value)

def test_string_data(self, handler):
spec = Spec(
Expand Down

0 comments on commit 4a3f70d

Please sign in to comment.