def test_init(self): # Test optimizations are chosen appropriately. grid = mock.create_autospec(Grid) grid.ndim = 2 grid.resolution_max = 10 self.assertIsInstance(grid, Grid) gridu = mock.create_autospec(GridUnstruct) gridu.resolution_max = None self.assertIsInstance(gridu, GridUnstruct) for g in [grid, gridu]: g._gc_initialize_ = mock.Mock() g.parent = mock.Mock() gs = GridChunker(gridu, grid, (3, 4), paths=self.fixture_paths) self.assertFalse(gs.optimized_bbox_subset) self.assertTrue(gs.eager) gs = GridChunker(gridu, grid, (3, 4), src_grid_resolution=1.0, dst_grid_resolution=2.0, paths=self.fixture_paths) self.assertTrue(gs.optimized_bbox_subset) self.assertFalse(gs.use_spatial_decomp) # Test spatial decomposition is chosen appropriately. gc = GridChunker(grid, gridu) self.assertTrue(gc.use_spatial_decomp) # Test ESMF keyword arguments. mock_ESMF = mock.Mock() with mock.patch.dict(sys.modules, {'ESMF': mock_ESMF}): esmf_kwargs = {'ignore_degenerate': True} gs = self.fixture_grid_chunker(genweights=True, esmf_kwargs=esmf_kwargs) self.assertGreaterEqual(len(gs.esmf_kwargs), 2) self.assertTrue(gs.genweights) self.assertTrue(gs.esmf_kwargs['ignore_degenerate'])
def test_prepare(self): coords = (-10.0, 40.0, 50.0, 50.0) bbox = box(*coords) gvar = GeometryVariable(value=bbox, dimensions='geom', crs=Spherical(), is_bbox=True, wrapped_state=WrappedState.UNWRAPPED) for _ in range(3): prepared = gvar.prepare() self.assertNotEqual(id(prepared), id(gvar)) actual = [] desired = [(-10.0, 40.0, 50.0, 50.0), (350.0, 40.0, 370.0, 50.0)] for g in prepared.get_value().flatten()[0]: actual.append(g.bounds) self.assertEqual(actual, desired) # Test updating the coordinate system. update_crs_call_count = {WGS84: 1, Spherical: 0, None: 1} wrapped_state = [None, WrappedState.WRAPPED, WrappedState.UNWRAPPED, WrappedState.UNKNOWN] keywords = dict(archetype_crs=update_crs_call_count.keys(), wrapped_state=wrapped_state) for k in self.iter_product_keywords(keywords): dgvar = deepcopy(gvar) dgvar.update_crs = mock.Mock() dgvar.deepcopy = mock.Mock(return_value=dgvar) dgvar.copy = mock.Mock() archetype = mock.create_autospec(GeometryVariable, spec_set=True) archetype.wrapped_state = k.wrapped_state try: archetype.crs = k.archetype_crs() archetype.crs.wrap_or_unwrap = mock.Mock() except TypeError: archetype.crs = None _ = dgvar.prepare(archetype=archetype) desired_count = update_crs_call_count[k.archetype_crs] self.assertEqual(dgvar.update_crs.call_count, desired_count) if desired_count > 0: dgvar.update_crs.assert_called_once_with(archetype.crs) if k.archetype_crs is not None and k.wrapped_state not in (WrappedState.UNKNOWN, None): archetype.crs.wrap_or_unwrap.assert_called_once_with(archetype.wrapped_state, dgvar) else: if k.archetype_crs is not None: archetype.crs.wrap_or_unwrap.assert_not_called() dgvar.deepcopy.assert_called_once() dgvar.copy.assert_not_called() # Test identical object is returned if nothing happens. gvar = GeometryVariable() gvar.deepcopy = mock.Mock(spec=GeometryVariable.deepcopy) gvar.copy = mock.Mock(spec=GeometryVariable.copy) actual = gvar.prepare() self.assertNotEqual(id(actual), id(gvar)) gvar.deepcopy.assert_not_called() gvar.copy.assert_called_once() # Test exception for more than one geometry. gvar = mock.create_autospec(GeometryVariable, spec_set=True) gvar.size = 2 with self.assertRaises(RequestableFeature): GeometryVariable.prepare(gvar)
def test_init(self): # Test optimizations are chosen appropriately. grid = mock.create_autospec(Grid) grid.ndim = 2 self.assertIsInstance(grid, Grid) gridu = mock.create_autospec(GridUnstruct) self.assertIsInstance(gridu, GridUnstruct) for g in [grid, gridu]: g._gs_initialize_ = mock.Mock() gs = GridSplitter(gridu, grid, (3, 4), paths=self.fixture_paths) self.assertFalse(gs.optimized_bbox_subset) gs = GridSplitter(gridu, grid, (3, 4), src_grid_resolution=1.0, dst_grid_resolution=2.0, paths=self.fixture_paths) self.assertTrue(gs.optimized_bbox_subset)
def test_start_new_thread_error(app): bill = mock.create_autospec(Bill) app.twitter_bot.start_new_thread.return_value = Exception prev_tweet_id = start_new_thread(bill, app.twitter_bot) assert prev_tweet_id is None
def mocked_api_client(self): from dmapiclient import DataAPIClient mocked_api_client = mock.create_autospec(DataAPIClient) with freeze_time("Jan 1st, 2018"): mocked_api_client.find_frameworks.return_value = { "frameworks": [ { "slug": "framework-expired-yesterday", "frameworkExpiresAtUTC": "2017-12-31T23:59:59.999999Z" }, { "slug": "framework-expired-almost-three-years-ago", "frameworkExpiresAtUTC": "2015-01-03T23:59:59.999999Z" }, { "slug": "framework-expired-three-years-ago", "frameworkExpiresAtUTC": "2015-01-02T00:00:00.000000Z" }, { "slug": "framework-expired-a-decade-ago", "frameworkExpiresAtUTC": "2008-01-04T23:59:59.999999Z" } ] } mocked_api_client.find_framework_suppliers_iter.side_effect = lambda *args, **kwargs: \ iter(self._supplier_framework_response()) return mocked_api_client
def test_init(self): rd = mock.create_autospec(RequestDataset) d = DriverNetcdfSCRIP(rd) self.assertIsInstance(d, DriverNetcdfSCRIP) field = self.fixture_driver_scrip_netcdf_field() self.assertIsInstance(field, Field)
def test_system_regrid_target_types(self): """Test grids are retrieved from the supported input regrid target types.""" mGrid = mock.create_autospec(Grid, spec_set=True, instance=True) mGrid.parent = mock.Mock() type(mGrid).ndim = PropertyMock(return_value=2) def _create_mField_(): mField = mock.create_autospec(Field, spec_set=True, instance=True) p_grid = PropertyMock(return_value=mGrid) type(mField).grid = p_grid return mField, p_grid def _create_mRequestDataset_(): ret = mock.create_autospec(RequestDataset, spec_set=True, instance=True) ret.create_field = mock.Mock(return_value=_create_mField_()[0]) return ret # Test with request datasets. source = _create_mRequestDataset_() destination = _create_mRequestDataset_() gs = GridChunker(source, destination, (1, 1)) for t in [source, destination]: t.create_field.assert_called_once() for t in [gs.src_grid, gs.dst_grid]: self.assertEqual(t, mGrid) # Test with fields. source, psource = _create_mField_() destination, pdestination = _create_mField_() gs = GridChunker(source, destination, (1, 1)) for t in [psource, pdestination]: t.assert_called_once_with() for t in [gs.src_grid, gs.dst_grid]: self.assertEqual(t, mGrid)
def mock_devices_available(): dev = create_autospec(device.Device) dev.path = '/dev/foo' dev.available_lvm = True dev.vg_size = [21474836480] dev.vg_free = dev.vg_size return [dev]
def setUp(self): super(TestCore, self).setUp() self.driver_mock = mock.create_autospec(drivers.Driver) mapping = {('my_pdu', 1): 'server_one'} self.core = core.Core(driver=self.driver_mock, mapping=mapping)
def test_system_regrid_target_types(self): """Test grids are retrieved from the supported input regrid target types.""" mGrid = mock.create_autospec(Grid, spec_set=True, instance=True) type(mGrid).ndim = PropertyMock(return_value=2) def _create_mField_(): mField = mock.create_autospec(Field, spec_set=True, instance=True) p_grid = PropertyMock(return_value=mGrid) type(mField).grid = p_grid return mField, p_grid def _create_mRequestDataset_(): ret = mock.create_autospec(RequestDataset, spec_set=True, instance=True) ret.create_field = mock.Mock(return_value=_create_mField_()[0]) return ret # Test with request datasets. source = _create_mRequestDataset_() destination = _create_mRequestDataset_() gs = GridSplitter(source, destination, (1, 1)) for t in [source, destination]: t.create_field.assert_called_once() for t in [gs.src_grid, gs.dst_grid]: self.assertEqual(t, mGrid) # Test with fields. source, psource = _create_mField_() destination, pdestination = _create_mField_() gs = GridSplitter(source, destination, (1, 1)) for t in [psource, pdestination]: t.assert_called_once_with() for t in [gs.src_grid, gs.dst_grid]: self.assertEqual(t, mGrid)
def test_tweet_threading(mock_get_prev_tweet_id, mock_continue_thread, mock_start_new_thread, mock_prev_tweet_id_is_missing, app): introductions = [] for i in range(6): bill = mock.create_autospec(Bill) if i <= 3: bill.date = '10/25/18' else: bill.date = '01/01/18' introductions.append(bill) app.bills.missing_tweet_id.return_value = introductions app.bills.get_prev_tweet_id.side_effect = list(range(len(introductions))) first_appearance = [False] * len(introductions) i = [i for i, b in enumerate(introductions) if b.date == '10/25/18'][0] first_appearance[i] = True i = [i for i, b in enumerate(introductions) if b.date == '01/01/18'][0] first_appearance[i] = True mock_prev_tweet_id_is_missing.side_effect = first_appearance mock_start_new_thread.side_effect = ['100', '500'] mock_continue_thread.return_value = {'10/25/18': '1'} tweet(app.bills, app.twitter_bot) assert app.bills.missing_tweet_id.call_count == 1 assert mock_get_prev_tweet_id.call_count == len(introductions) assert mock_start_new_thread.call_count == 2 assert mock_continue_thread.call_count == len(introductions)
def iter_dst(grid_splitter, yield_slice=False): for _ in range(3): yld = mock.create_autospec(GridUnstruct, instance=True) yld.wrapped_state = WrappedState.UNWRAPPED yld.is_empty = False yld.extent_global = (0, 0, 0, 0) parent = mock.create_autospec(Field, spec_set=True) parent.add_variable = mock.Mock() yld.parent = parent yld.shape_global = (25,) yld.extent_global = (1, 2, 3, 4) yld.parent.attrs = {} if yield_slice: yld = yld, None yield yld
def test_get_grid_object(self): obj = mock.create_autospec(AbstractGrid, instance=True) obj.parent = mock.Mock() _ = get_grid_object(obj) obj.parent.load.assert_called_once() obj.parent.reset_mock() _ = get_grid_object(obj, load=False) obj.parent.assert_not_called()
def setUp(self): super(TestTransitiveState, self).setUp() self.driver_mock = mock.create_autospec(drivers.Driver) mapping = { ('my_pdu', 1): 'server_one' } self.store = {} self.core = core.Core(driver=self.driver_mock, mapping=mapping, store=self.store, default_state=core.POWER_ON)
def test_start_new_thread(app): bill = mock.create_autospec(Bill) app.twitter_bot.start_new_thread.return_value = SUCCESS_RESPONSE prev_tweet_id = start_new_thread(bill, app.twitter_bot) app.twitter_bot.start_new_thread.assert_called_with(bill) assert app.twitter_bot.start_new_thread.call_count == 1 assert prev_tweet_id == SUCCESS_RESPONSE['id_str']
def mock_devices_available(): dev = create_autospec(device.Device) dev.path = '/dev/foo' dev.vg_name = 'vg_foo' dev.lv_name = 'lv_foo' dev.vgs = [lvm.VolumeGroup(vg_name=dev.vg_name, lv_name=dev.lv_name)] dev.available_lvm = True dev.vg_size = [21474836480] dev.vg_free = dev.vg_size return [dev]
def test_array_resolution_called(self): """Test the driver's array resolution method is called appropriately.""" m_DriverNetcdfSCRIP = mock.create_autospec(DriverNetcdfSCRIP) with mock.patch('ocgis.driver.registry.get_driver_class', return_value=m_DriverNetcdfSCRIP): x = Variable(name='x', value=[1, 2, 3], dimensions='dimx') y = Variable(name='y', value=[4, 5, 6], dimensions='dimy') pgc = PointGC(x=x, y=y) _ = pgc.resolution_x _ = pgc.resolution_y self.assertEqual(m_DriverNetcdfSCRIP.array_resolution.call_count, 2)
def test_continue_thread_error(app): bill = mock.create_autospec(Bill) prev_tweet_id = '123' app.twitter_bot.tweet_bill.side_effect = TwythonError('Error') no_prev_tweets = {} prev_tweets = continue_thread(bill, app.bills, prev_tweet_id, no_prev_tweets, app.twitter_bot) assert app.bills.insert.call_count == 0 assert prev_tweets == no_prev_tweets
def test_create_dimension_map_rotated_spherical(self): self.assertTrue(env.SET_GRID_AXIS_ATTRS) self.assertIsNone(env.COORDSYS_ACTUAL) rd = mock.create_autospec(RequestDataset) rd._has_assigned_coordinate_system = False rd.rotated_pole_priority = False driver = DriverNetcdfCF(rd) dmap = driver.create_dimension_map(self.fixture_rotated_spherical_metadata) self.assertFalse(env.SET_GRID_AXIS_ATTRS) self.assertIsInstance(env.COORDSYS_ACTUAL, CFRotatedPole) self.assertEqual(dmap.get_variable(DMK.X), 'lon') self.assertEqual(dmap.get_variable(DMK.Y), 'lat')
def test_get_features_object_file_geodatabase(self): ds = mock.create_autospec(ogr.DataSource, spec_set=True) m_Driver = mock.Mock() m_Driver.GetName = mock.Mock(return_value='OpenFileGDB') ds.GetDriver = mock.Mock(return_value=m_Driver) ds.GetLayerByName = mock.Mock() with self.assertRaises(ValueError): _ = GeomCabinet._get_features_object_(ds) desired = {'feature_class': 'foo'} _ = GeomCabinet._get_features_object_(ds, driver_kwargs=desired) ds.GetLayerByName.assert_called_once_with(desired['feature_class'])
def fixture_abstract_driver(self): rd = mock.create_autospec(RequestDataset, spec_set=True) group2 = {'variables': {'alone': {'dtype': np.int8}, 'friendly': {}}} group3 = { 'variables': { 'food': { 'dimensions': ('c', ) } }, 'dimensions': { 'c': { 'size': 5 } } } rd.metadata = { 'dimensions': { 'foo': { 'name': 'foo', 'size': 10 }, 'foobar': { 'name': 'foobar', 'size': 11 }, 'boundless': { 'name': 'boundless', 'size': 9, 'isunlimited': True } }, 'variables': { 'tester': { 'name': 'tester', 'dimensions': ('foo', 'foobar'), 'dtype': 'int' } }, 'groups': { 'group2': group2, 'group3': group3 } } driver = FakeAbstractDriver(rd) rd.driver = driver return driver
def test_continue_thread(app): bill = mock.create_autospec(Bill) prev_tweet_id = '123' bill_date = '10/01/18' bill.date = bill_date app.twitter_bot.tweet_bill.return_value = SUCCESS_RESPONSE prev_tweets = continue_thread(bill, app.bills, prev_tweet_id, {}, app.twitter_bot) app.twitter_bot.tweet_bill.assert_called_with(bill, prev_tweet_id) assert bill.tweet_id == '123' assert prev_tweets == {bill.date: bill.tweet_id} app.bills.insert.assert_called_with(bill)
def test_write_subsets(self): # Test a destination iterator. grid = mock.create_autospec(GridUnstruct) grid._gs_initialize_ = mock.Mock() grid.resolution = 10.0 grid.ndim = 1 grid.wrapped_state = WrappedState.UNWRAPPED grid.crs = Spherical() grid.get_intersects = mock.Mock(return_value=(grid, slice(0, 0))) grid.is_empty = False parent = mock.create_autospec(Field, spec_set=True) parent.add_variable = mock.Mock() grid.parent = parent grid.shape_global = (5,) def iter_dst(grid_splitter, yield_slice=False): for _ in range(3): yld = mock.create_autospec(GridUnstruct, instance=True) yld.wrapped_state = WrappedState.UNWRAPPED yld.is_empty = False yld.extent_global = (0, 0, 0, 0) parent = mock.create_autospec(Field, spec_set=True) parent.add_variable = mock.Mock() yld.parent = parent yld.shape_global = (25,) yld.extent_global = (1, 2, 3, 4) yld.parent.attrs = {} if yield_slice: yld = yld, None yield yld gs = GridSplitter(grid, grid, (100,), iter_dst=iter_dst, dst_grid_resolution=5.0, check_contains=False) gs.write_subsets()
def mock_lv(): size = 21474836480 dev = create_autospec(device.Device) dev.lv_name = 'lv' dev.vg_name = 'vg' dev.path = '{}/{}'.format(dev.vg_name, dev.lv_name) dev.used_by_ceph = False dev.vg_size = [size] dev.vg_free = dev.vg_size dev.lvs = [ lvm.Volume(vg_name=dev.vg_name, lv_name=dev.lv_name, lv_size=size, lv_tags='') ] return dev
def fixture_abstract_driver(self): rd = mock.create_autospec(RequestDataset, spec_set=True) group2 = {'variables': {'alone': {'dtype': np.int8}, 'friendly': {}}} group3 = {'variables': {'food': {'dimensions': ('c',)}}, 'dimensions': {'c': {'size': 5}}} rd.metadata = {'dimensions': {'foo': {'name': 'foo', 'size': 10}, 'foobar': {'name': 'foobar', 'size': 11}, 'boundless': {'name': 'boundless', 'size': 9, 'isunlimited': True}}, 'variables': {'tester': {'name': 'tester', 'dimensions': ('foo', 'foobar'), 'dtype': 'int'}}, 'groups': {'group2': group2, 'group3': group3}} driver = FakeAbstractDriver(rd) rd.driver = driver return driver
def test_get_crs(self): group_metadata = self.fixture_rotated_spherical_metadata rd = mock.create_autospec(RequestDataset) keywords = Dict() keywords.rpp = [True, False] keywords.with_spherical = [True, False] desired = Dict() desired.rpp[True] = CFRotatedPole desired.rpp[False] = CFSpherical for k in self.iter_product_keywords(keywords): cgm = deepcopy(group_metadata) if not k.with_spherical: cgm['variables'].pop('lat') cgm['variables'].pop('lon') rd.rotated_pole_priority = k.rpp actual = DriverNetcdfCF(rd).get_crs(cgm) if k.with_spherical: self.assertIsInstance(actual, desired.rpp[k.rpp]) else: self.assertIsInstance(actual, CFRotatedPole)
def test_gc_nchunks_dst(self): field = self.fixture_driver_scrip_netcdf_field() gc = mock.create_autospec(GridChunker, spec_set=True) gc.dst_grid = field.grid actual = field.grid._gc_nchunks_dst_(gc) self.assertEqual(actual, 10)
def twitter_bot(): twitter_bot = mock.create_autospec(TwitterBot) yield twitter_bot
def test_create_dimension_map(self): driver = mock.create_autospec(DriverVector, spec_set=True) driver.get_crs = mock.Mock(return_value=None) actual = DriverVector.create_dimension_map(driver, {}) self.assertEqual(actual.get_variable(DMK.GEOM), VariableName.GEOMETRY_VARIABLE)
def test_gc_nchunks_dst(self): pgc = self.fixture() gc = mock.create_autospec(GridChunker) actual = pgc._gc_nchunks_dst_(gc) self.assertIsNotNone(actual) self.assertEqual(actual, (100,))
def status(self): return mock.create_autospec(StatusTracker, instance=True)
def deployer(self): return mock.create_autospec(Deployer)
def data_api_client(): from dmapiclient import DataAPIClient return mock.create_autospec(DataAPIClient)
def bills_api(): bills_api = mock.create_autospec(BillsAPI) bills_api.get_bills.return_value = EXAMPLE_INTRODUCTIONS yield bills_api
def bills(): bills = mock.create_autospec(Bills) bills.exists.return_value = False yield bills
def bills_request_params(): bills_request_params = mock.create_autospec(BillsRequestParams) yield bills_request_params
def _create_mField_(): mField = mock.create_autospec(Field, spec_set=True, instance=True) p_grid = PropertyMock(return_value=mGrid) type(mField).grid = p_grid return mField, p_grid
def _create_mRequestDataset_(): ret = mock.create_autospec(RequestDataset, spec_set=True, instance=True) ret.create_field = mock.Mock(return_value=_create_mField_()[0]) return ret