def test_from_dict_wrong_type_specifier(self): with self.assertRaises(ValueError) as cm: descriptor_dict = dict(data_id='xyz', type_specifier='tsr') DatasetDescriptor.from_dict(descriptor_dict) self.assertEqual( 'type_specifier must satisfy type specifier "dataset", but was "tsr"', f'{cm.exception}')
def test_from_dict_no_data_id(self): try: descriptor_dict = dict() DatasetDescriptor.from_dict(descriptor_dict) self.fail('Exception expected') except ValueError: pass
def test_from_dict_var_descriptors_as_dict(self): descriptor_dict = dict( data_id='xyz', data_type='dataset', crs='EPSG:4326', data_vars=dict( A=dict( name='A', dtype='float32', dims=('time', 'lat', 'lon'), chunks=(2, 3), attrs=dict( ssd=4, zjgrhgu='dgfrf' ) ) ) ) descriptor = DatasetDescriptor.from_dict(descriptor_dict) self.assertEqual('xyz', descriptor.data_id) self.assertEqual('dataset', descriptor.data_type.alias) self.assertEqual('EPSG:4326', descriptor.crs) self.assertEqual(1, len(descriptor.data_vars)) self.assertTrue('A' in descriptor.data_vars) self.assertIsInstance(descriptor.data_vars.get('A'), VariableDescriptor)
def test_to_dict(self): var_descriptors = [ VariableDescriptor(name='xf', dtype='rj', dims=('dfjhrt', 'sg'), attrs=dict(ssd=4, zjgrhgu='dgfrf')) ] descriptor = DatasetDescriptor(data_id='xyz', type_specifier=TypeSpecifier( 'dataset', flags={'cube'}), crs='EPSG:9346', bbox=(10., 20., 30., 40.), spatial_res=20., time_range=('2017-06-05', '2017-06-27'), time_period='daily', dims=dict(x=1, y=2, z=3), data_vars=var_descriptors, attrs=dict(dzus=236, tgr7h='rt5', df='s8fd4w5')) descriptor_dict = descriptor.to_dict() self.assertEqual( dict(data_id='xyz', type_specifier='dataset[cube]', crs='EPSG:9346', bbox=(10., 20., 30., 40.), spatial_res=20., time_range=('2017-06-05', '2017-06-27'), time_period='daily', dims=dict(x=1, y=2, z=3), data_vars=[ dict(name='xf', dtype='rj', dims=('dfjhrt', 'sg'), ndim=2, attrs=dict(ssd=4, zjgrhgu='dgfrf')) ], attrs=dict(dzus=236, tgr7h='rt5', df='s8fd4w5')), descriptor_dict)
def test_to_dict(self): descriptor = DatasetDescriptor( data_id='xyz', crs='EPSG:9346', bbox=(10., 20., 30., 40.), spatial_res=20., time_range=('2017-06-05', '2017-06-27'), time_period='daily', open_params_schema=JsonObjectSchema( properties=dict( consolidated=JsonBooleanSchema(), ), additional_properties=False, ) ) descriptor_dict = descriptor.to_dict() self.assertEqual( { 'data_id': 'xyz', 'crs': 'EPSG:9346', 'data_type': 'dataset', 'bbox': [10.0, 20.0, 30.0, 40.0], 'spatial_res': 20.0, 'time_range': ['2017-06-05', '2017-06-27'], 'time_period': 'daily', 'open_params_schema': { 'type': 'object', 'properties': { 'consolidated': { 'type': 'boolean' } }, 'additionalProperties': False, }, }, descriptor_dict)
def test_from_dict_full(self): descriptor_dict = dict( data_id='xyz', crs='EPSG:9346', bbox=(10., 20., 30., 40.), spatial_res=20., time_range=('2017-06-05', '2017-06-27'), time_period='daily', dims=dict(x=1, y=2, z=3), data_vars=[ dict(name='xf', dtype='rj', dims=('dfjhrt', 'sg'), ndim=2, attrs=dict(ssd=4, zjgrhgu='dgfrf')) ], attrs=dict(dzus=236, tgr7h='rt5', df='s8fd4w5'), open_params_schema=dict( type="object", properties=dict(variable_names=dict( type='array', items=dict(type='string'))))) descriptor = DatasetDescriptor.from_dict(descriptor_dict) self.assertIsNotNone(descriptor) self.assertEqual('xyz', descriptor.data_id) self.assertEqual('dataset', descriptor.type_specifier) self.assertEqual('EPSG:9346', descriptor.crs) self.assertEqual((10., 20., 30., 40.), descriptor.bbox) self.assertEqual(20., descriptor.spatial_res) self.assertEqual(('2017-06-05', '2017-06-27'), descriptor.time_range) self.assertEqual('daily', descriptor.time_period) self.assertEqual(dict(x=1, y=2, z=3), descriptor.dims) self.assertEqual(1, len(descriptor.data_vars)) self.assertEqual(236, descriptor.attrs.get('dzus', None)) self.assertEqual('rt5', descriptor.attrs.get('tgr7h', None)) self.assertEqual('s8fd4w5', descriptor.attrs.get('df', None)) self.assertEqual('object', descriptor.open_params_schema.get('type', None))
def test_to_dict(self): coords = dict( rtdt=VariableDescriptor( name='rtdt', dtype='rj', dims=('rtdt',), chunks=(2,), attrs=dict( ssd=6, zjgrhgu='hgtr' ) ) ) var_descriptors = dict( xf=VariableDescriptor( name='xf', dtype='rj', dims=('dfjhrt', 'sg'), chunks=(2, 3), attrs=dict( ssd=4, zjgrhgu='dgfrf' ) ) ) descriptor = DatasetDescriptor( data_id='xyz', crs='EPSG:9346', bbox=(10., 20., 30., 40.), spatial_res=20., time_range=('2017-06-05', '2017-06-27'), time_period='daily', coords=coords, dims=dict( x=1, y=2, z=3 ), data_vars=var_descriptors, attrs=dict( dzus=236, tgr7h='rt5', df='s8fd4w5' ) ) descriptor_dict = descriptor.to_dict() self.assertEqual( dict( data_id='xyz', data_type='dataset', crs='EPSG:9346', bbox=[10., 20., 30., 40.], spatial_res=20., time_range=['2017-06-05', '2017-06-27'], time_period='daily', coords=dict( rtdt=dict( name='rtdt', dtype='rj', dims=['rtdt', ], chunks=[2, ], attrs=dict( ssd=6, zjgrhgu='hgtr' ) ) ), dims=dict(x=1, y=2, z=3), data_vars=dict( xf=dict( name='xf', dtype='rj', dims=['dfjhrt', 'sg'], chunks=[2, 3], attrs=dict( ssd=4, zjgrhgu='dgfrf' ) ) ), attrs=dict( dzus=236, tgr7h='rt5', df='s8fd4w5' ) ), descriptor_dict )
def test_from_dict_derived_type(self): descriptor_dict = dict(data_id='xyz', data_type='dataset') descriptor = DatasetDescriptor.from_dict(descriptor_dict) self.assertIsNotNone(descriptor) self.assertEqual('xyz', descriptor.data_id) self.assertEqual('dataset', descriptor.data_type.alias)
def test_from_dict_wrong_data_type(self): descriptor_dict = dict(data_id='xyz', data_type='tsr') with self.assertRaises(ValueError) as cm: DatasetDescriptor.from_dict(descriptor_dict) self.assertEqual("unknown data type 'tsr'", f'{cm.exception}')
def test_from_dict_no_data_id(self): descriptor_dict = dict() with self.assertRaises(jsonschema.exceptions.ValidationError): DatasetDescriptor.from_dict(descriptor_dict)
def test_get_schema(self): schema = DatasetDescriptor.get_schema() self.assertIsInstance(schema, JsonObjectSchema)
def test_from_dict_derived_type(self): descriptor_dict = dict(data_id='xyz', type_specifier='dataset[fegd]') descriptor = DatasetDescriptor.from_dict(descriptor_dict) self.assertIsNotNone(descriptor) self.assertEqual('xyz', descriptor.data_id) self.assertEqual('dataset[fegd]', descriptor.type_specifier)
def test_from_dict_basic(self): descriptor_dict = dict(data_id='xyz') descriptor = DatasetDescriptor.from_dict(descriptor_dict) self.assertIsNotNone(descriptor) self.assertEqual('xyz', descriptor.data_id) self.assertEqual('dataset', descriptor.type_specifier)