def test_eq(self): # Test the __eq__ method. # __eq__ is a shallow test and only compares ids. block1 = Block(7, 0, [shapes.Site(1.0, 1.0)]) block2 = Block(7, 0, [shapes.Site(1.0, 0.0)]) self.assertTrue(block1 == block2)
def test_not_eq(self): # Test __eq__ with 2 Blocks that should not be equal block1 = Block(7, 0, [shapes.Site(1.0, 1.0)]) block2 = Block(8, 0, [shapes.Site(1.0, 1.0)]) self.assertFalse(block1 == block2) block1 = Block(7, 0, [shapes.Site(1.0, 1.0)]) block2 = Block(7, 1, [shapes.Site(1.0, 1.0)]) self.assertFalse(block1 == block2)
def setUp(self): inputs = [("fragility", ""), ("exposure", "")] self.job = self.setup_classic_job(inputs=inputs) kvs.mark_job_as_current(self.job.id) kvs.cache_gc(self.job.id) self.site = Site(1.0, 1.0) block = Block(self.job.id, BLOCK_ID, [self.site]) block.to_kvs() # this region contains a single site, that is exactly # a site with longitude == 1.0 and latitude == 1.0 params = {"REGION_VERTEX": "1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0", "REGION_GRID_SPACING": "0.5", "BASE_PATH": ".", "OUTPUT_DIR": "."} self.job_ctxt = JobContext(params, self.job.id, oq_job=self.job) self.em = self._store_em() self._store_gmvs([0.40, 0.30, 0.45, 0.35, 0.40]) self.calculator = ScenarioDamageRiskCalculator(self.job_ctxt) # just stubbing out some preprocessing stuff... ScenarioDamageRiskCalculator.store_exposure_assets = lambda self: None ScenarioDamageRiskCalculator.store_fragility_model = lambda self: None ScenarioDamageRiskCalculator.partition = lambda self: None
def test_split_into_blocks(self): # Test a typical split case. # We will use a block size of 3, which will # give us 2 blocks of 3 sites and 1 block of 2 sites. expected = [ Block(self.job_id, 0, self.all_sites[:3]), Block(self.job_id, 1, self.all_sites[3:6]), Block(self.job_id, 2, self.all_sites[6:]) ] actual = [ block for block in general.split_into_blocks( self.job_id, self.all_sites, block_size=3) ] self.assertEqual(expected, actual)
def test_split_block_size_gt_site_list_size(self): # If the block size is greater than the input site list size, # the generator should just yield a single block containing all of the # sites. actual = [ block for block in general.split_into_blocks( self.job_id, self.all_sites, block_size=9) ] self.assertEqual([Block(self.job_id, 0, self.all_sites)], actual)
def test_split_block_size_eq_1(self): # Test splitting when block_size==1. expected = [ Block(self.job_id, i, [self.all_sites[i]]) for i in xrange(len(self.all_sites)) ] actual = [ block for block in general.split_into_blocks( self.job_id, self.all_sites, block_size=1) ] self.assertEqual(expected, actual)
def test_block_kvs_serialization(self): # Test that a Block is properly serialized/deserialized from the cache. job_id = 7 block_id = 0 expected_block = Block(job_id, block_id, [shapes.Site(1.0, 1.0), shapes.Site(2.0, 2.0)]) expected_block.to_kvs() actual_block = Block.from_kvs(job_id, block_id) self.assertEqual(expected_block, actual_block) # The sites are not compared in Block.__eq__; we need to check those # also. self.assertEqual(expected_block.sites, actual_block.sites)