def test_response_json(tests_path): """ Check that each JSON file can be converted into dictionaries that can be used to construct objects needed for a Calculator object. """ # pylint: disable=too-many-locals responses_path = os.path.join(tests_path, '..', 'responses', '*.json') for jpf in glob.glob(responses_path): # read contents of jpf (JSON parameter filename) jfile = open(jpf, 'r') jpf_text = jfile.read() # check that jpf_text can be used to construct objects response_file = ('"consumption"' in jpf_text and '"behavior"' in jpf_text and '"growdiff_baseline"' in jpf_text and '"growdiff_response"' in jpf_text and '"growmodel"' in jpf_text) if response_file: # pylint: disable=protected-access (con, beh, gdiff_base, gdiff_resp, grow_model) = Calculator._read_json_econ_assump_text(jpf_text) cons = Consumption() cons.update_consumption(con) behv = Behavior() behv.update_behavior(beh) growdiff_baseline = GrowDiff() growdiff_baseline.update_growdiff(gdiff_base) growdiff_response = GrowDiff() growdiff_response.update_growdiff(gdiff_resp) growmodel = GrowModel() growmodel.update_growmodel(grow_model) else: # jpf_text is not a valid JSON response assumption file print('test-failing-filename: ' + jpf) assert False
def test_incorrect_growmodel_instantiation(): with pytest.raises(ValueError): GrowModel(start_year=2012) with pytest.raises(ValueError): GrowModel(num_years=0) with pytest.raises(FloatingPointError): np.divide(1., 0.)
def test_incorrect_update_growmodel(): with pytest.raises(ValueError): GrowModel().update_growmodel([]) with pytest.raises(ValueError): GrowModel().update_growmodel({2013: {'_active': [2]}}) with pytest.raises(ValueError): GrowModel().update_growmodel({2013: {'_active': [0.2]}}) with pytest.raises(ValueError): GrowModel().update_growmodel({2013: {'_activexxx': [True]}}) # year in update must be no less than start year gmod = GrowModel(start_year=2014) with pytest.raises(ValueError): gmod.update_growmodel({2013: {'_active': [True]}}) # year in update must be no less than current year gmod = GrowModel(start_year=2014) gmod.set_year(2015) with pytest.raises(ValueError): gmod.update_growmodel({2014: {'_active': [True]}}) # year in update must be no greater than end_year with pytest.raises(ValueError): GrowModel().update_growmodel({2040: {'_active': [True]}}) # invalid start year with pytest.raises(ValueError): GrowModel().update_growmodel({'notayear': {'_active': [True]}})
def test_correct_update_growmodel(): gmod = GrowModel() gmod.update_growmodel({}) assert not gmod.is_ever_active() start_cyr = gmod.start_year active_cyr = 2018 gmod.update_growmodel({active_cyr: {'_active': [True]}}) for cyr in range(start_cyr, active_cyr): assert not gmod._active[cyr - start_cyr] for cyr in range(active_cyr, gmod.end_year + 1): assert gmod._active[cyr - start_cyr] assert gmod.is_ever_active() gmod.set_year(active_cyr - 1) assert not gmod.is_active()
def test_growmodel_default_data(): paramdata = GrowModel.default_data() assert paramdata['_active'] == [False]