def test_eq_spill_container_pair(uncertain): """ SpillContainerPair inherits from SpillContainer so it should compute __eq__ and __ne__ in the same way - test it here Incomplete - this doesn't currently work! Test fails if uncertainty is on whether particles are released or not This is because 'id' of uncertain spills do not match and one should not expect them to match. todo: remove 'id' property as a check for equality. This requires changes in persisting logic. Update persistence then revisit this test and simplify it """ (sp1, sp2) = get_eq_spills() # windages array will not match after elements are released so lets not # add any more types to data_arrays for this test. Just look at base # array_types for SpillContainer's and ensure the data matches for them #sp1.element_type = ElementType() #sp2.element_type = ElementType() scp1 = SpillContainerPair(uncertain) # uncertainty is on scp1.add(sp1) scp2 = SpillContainerPair(uncertain) if uncertain: u_sp1 = [ scp1.items()[1].spills[spill.id] for spill in scp1.items()[1].spills ][0] u_sp2 = copy.deepcopy(u_sp1) # deepcopy does not match ids! # for test, we need these to match so force them to be equal here u_sp2._id = u_sp1.id scp2.add((sp2, u_sp2)) else: scp2.add(sp2) for sc in zip(scp1.items(), scp2.items()): sc[0].prepare_for_model_run() sc[0].release_elements(360, sp1.release.release_time) sc[1].prepare_for_model_run() sc[1].release_elements(360, sp2.release.release_time) assert scp1 == scp2 assert scp2 == scp1 assert not (scp1 != scp2) assert not (scp2 != scp1)
def test_exceptions(output_filename): spill_pair = SpillContainerPair() # begin tests kmz = KMZOutput(output_filename) kmz.rewind() # delete temporary files with raises(TypeError): # need to pass in model start time kmz.prepare_for_model_run(num_time_steps=4) with raises(TypeError): # need to pass in model start time and spills kmz.prepare_for_model_run() with raises(ValueError): # need a cache object kmz.write_output(0) # Maybe add ability to specify which data later on.. # with raises(ValueError): # kmz.which_data = 'some random string' # changed renderer and netcdf ouputter to delete old files in # prepare_for_model_run() rather than rewind() # -- rewind() was getting called a lot # -- before there was time to change the output file names, etc. # So for this unit test, there should be no exception if we do it twice. kmz.prepare_for_model_run(model_start_time=datetime.now(), spills=spill_pair, num_time_steps=4) kmz.prepare_for_model_run(model_start_time=datetime.now(), spills=spill_pair, num_time_steps=4)
def test_to_dict(self, json_): c_spill = [ point_line_release_spill(self.num_elements, self.start_position, self.start_time) for i in range(2) ] u_spill = [ point_line_release_spill(self.num_elements, self.start_position2, self.start_time2) for i in range(2) ] scp = SpillContainerPair(True) for sp_tuple in zip(c_spill, u_spill): scp += sp_tuple toserial = scp.to_dict() assert 'spills' in toserial assert 'uncertain_spills' in toserial for key in ('spills', 'uncertain_spills'): if key == 'spills': check = c_spill else: check = u_spill alltrue = [check[ix].id == spill['id'] \ for ix, spill in enumerate(toserial[key])] assert all(alltrue) alltrue = [check[ix].obj_type_to_dict() == spill['obj_type'] \ for ix, spill in enumerate(toserial[key])] assert all(alltrue)
def test_exception_uncertainty(self): spill = point_line_release_spill(self.num_elements, self.start_position, self.start_time) sp2 = point_line_release_spill(self.num_elements, self.start_position2, self.start_time2) scp = SpillContainerPair(False) with raises(ValueError): scp += (spill, sp2)
def test_SpillContainerPair_uncertainty(self): 'test uncertainty property' u_scp = SpillContainerPair(True) u_scp.uncertain = False assert not u_scp.uncertain assert not hasattr(u_scp, '_u_spill_container') u_scp.uncertain = True assert u_scp.uncertain assert hasattr(u_scp, '_u_spill_container')
def test_add_spill(self): spill = [ point_line_release_spill(self.num_elements, self.start_position, self.start_time) for i in range(2) ] scp = SpillContainerPair(False) scp += (spill[0], ) scp += spill[1] for sp_ix in zip(scp._spill_container.spills, range(len(spill))): spill_ = sp_ix[0] index = sp_ix[1] assert spill_.id == spill[index].id
def test_exception_tuple(self): """ tests that spills can be added to SpillContainerPair object """ spill = point_line_release_spill(self.num_elements, self.start_position, self.start_time) sp2 = point_line_release_spill(self.num_elements, self.start_position2, self.start_time2) scp = SpillContainerPair(True) with raises(ValueError): scp += (spill, sp2, spill)
def test_spill_by_index(self): 'test spill_by_index returns the correct spill object' spill = [ point_line_release_spill(self.num_elements, self.start_position, self.start_time), point_line_release_spill(self.num_elements, self.start_position, self.start_time) ] scp = SpillContainerPair(True) scp += spill[0] scp += spill[1] for ix in range(2): assert scp.spill_by_index(ix) is spill[ix] u_spill = scp.spill_by_index(ix, uncertain=True) assert u_spill is not spill[ix] assert scp.items()[1].spills[ix] is u_spill
def test_exceptions(output_filename): spill_pair = SpillContainerPair() print "output_filename:", output_filename # begin tests netcdf = NetCDFOutput(output_filename, which_data='all') netcdf.rewind() # delete temporary files with raises(TypeError): # need to pass in model start time netcdf.prepare_for_model_run(num_time_steps=4) with raises(TypeError): # need to pass in model start time and spills netcdf.prepare_for_model_run() with raises(ValueError): # need a cache object netcdf.write_output(0) with raises(ValueError): netcdf.which_data = 'some random string' # changed renderer and netcdf ouputter to delete old files in # prepare_for_model_run() rather than rewind() # -- rewind() was getting called a lot # -- before there was time to change the ouput file names, etc. # So for this unit test, there should be no exception if we do it twice. netcdf.prepare_for_model_run(model_start_time=datetime.now(), spills=spill_pair, num_time_steps=4) netcdf.prepare_for_model_run(model_start_time=datetime.now(), spills=spill_pair, num_time_steps=4) with raises(AttributeError): 'cannot change after prepare_for_model_run has been called' netcdf.prepare_for_model_run(model_start_time=datetime.now(), spills=spill_pair, num_time_steps=4) netcdf.which_data = 'most'
def test_release_particles(self): '''test that the 'id' for uncertain spill container's data is starting from 0''' spill = [ point_line_release_spill(self.num_elements, self.start_position, self.start_time) for i in range(2) ] scp = SpillContainerPair(True) scp += spill[0] scp += spill[1] for sc in scp.items(): sc.prepare_for_model_run(windage_at) # model sets this for each step sc.current_time_stamp = self.start_time sc.release_elements(100, self.start_time) for key in ['id', 'spill_num', 'age']: c_val = scp.LE(key) u_val = scp.LE(key, 'uncertain') assert np.all(c_val == u_val)
def test_add_spillpair(self): c_spill = [ point_line_release_spill(self.num_elements, self.start_position, self.start_time) for i in range(2) ] u_spill = [ point_line_release_spill(self.num_elements, self.start_position2, self.start_time2) for i in range(2) ] scp = SpillContainerPair(True) for sp_tuple in zip(c_spill, u_spill): scp += sp_tuple for sp, idx in zip(scp._spill_container.spills, range(len(c_spill))): assert sp.id == c_spill[idx].id for sp, idx in zip(scp._u_spill_container.spills, range(len(c_spill))): assert sp.id == u_spill[idx].id
def test_rewind_change_spill_attribute(self): ''' check that if an attribute of forcast spillcontainer is updated, the uncertain spill container creates a new copy of uncertain spills ''' num_elements = 100 release_time = datetime(2012, 1, 1, 12) start_position = (23.0, -78.5, 0.0) scp = SpillContainerPair(uncertain=True) scp += point_line_release_spill(num_elements, start_position, release_time) (forecast_sc, uncertain_sc) = scp.items() assert forecast_sc.spills == uncertain_sc.spills forecast_sc.spills[0].release_time = release_time + timedelta(hours=1) (forecast_sc, uncertain_sc) = scp.items() assert forecast_sc.spills != uncertain_sc.spills scp.rewind() (forecast_sc, uncertain_sc) = scp.items() assert forecast_sc.spills == uncertain_sc.spills
def test_init_SpillContainerPair(self): 'All this does is test that it can be initialized' SpillContainerPair() SpillContainerPair(True) assert True
def init_scp(self): scp = SpillContainerPair(uncertain=True) for s in self.s0: scp += s return scp