def test_for_service_illegal_state(self): code_config = CodeConfig.from_code(INLINE_CODE) code_config.inline_code = None with self.assertRaises(RuntimeError) as e: code_config.for_service() self.assertEqual( ('for_service() failed due to an invalid CodeConfig state', ), e.exception.args)
def test_from_code_string(self): code_config = CodeConfig.from_code( INLINE_CODE, module_name='user_code_1', callable_params={'text': 'good bye'}) self.assertIsInstance(code_config, CodeConfig) self.assertEqual(INLINE_CODE, code_config.inline_code) self.assertEqual({'text': 'good bye'}, code_config.callable_params) self.assertEqual('user_code_1:process_dataset', code_config.callable_ref) self.assertTrue(callable(code_config.get_callable()))
def test_for_local_from_inline_code(self): user_code_config = CodeConfig.from_code(INLINE_CODE) local_code_config = user_code_config.for_local() self.assertIsInstance(local_code_config, CodeConfig) self.assertIsInstance(local_code_config.callable_ref, str) self.assertIsNone(local_code_config._callable) self.assertIsNone(local_code_config.inline_code) self.assertIsInstance(local_code_config.file_set, FileSet) self.assertTrue(local_code_config.file_set.is_local_dir()) self.assertRegex(os.path.basename(local_code_config.file_set.path), 'xcube-byoa-*.') self.assertRegex(local_code_config.callable_ref, 'xcube_byoa_*.:process_dataset') # CodeConfigs from for_local() shall be able to load callable self.assertTrue(callable(local_code_config.get_callable()))
def test_to_dict(self): d = CodeConfig.from_callable(modify_dataset).to_dict() # Shall we raise instead? self.assertEqual({}, d) d = CodeConfig.from_code(INLINE_CODE, module_name='user_code').to_dict() self.assertEqual( { 'callable_ref': 'user_code:process_dataset', 'inline_code': INLINE_CODE, }, d) d = CodeConfig.from_file_set( FileSet('github://*****:*****@v0.8.2.dev0', includes='*.py'), callable_ref=('test.core.byoa.test_config:' 'modify_dataset')).to_dict() self.assertIsInstance(d.get('file_set'), dict) self.assertEqual(('test.core.byoa.test_config:' 'modify_dataset'), d.get('callable_ref'))
def test_from_code_with_function_refs(self): code_config = CodeConfig.from_code( 'import xarray as xr\n', modify_dataset, transform_dataset, module_name='user_code_2', callable_params={'text': 'good bye'}) self.assertIsInstance(code_config, CodeConfig) self.assertEqual( ('import xarray as xr\n' '\n' '\n' 'def modify_dataset(ds, text="Hello"):\n' ' return transform_dataset(ds, text)\n' '\n' '\n' 'def transform_dataset(ds, text):\n' ' return ds.assign_attrs(comment=f"xcube says {text}")\n'), code_config.inline_code) self.assertIsNone(code_config.file_set) self.assertEqual({'text': 'good bye'}, code_config.callable_params) self.assertEqual('user_code_2:modify_dataset', code_config.callable_ref) self.assertTrue(callable(code_config.get_callable()))