def test_merge_io_formats_wps_complements_cwl(): wps_fmt = [Format(CONTENT_TYPE_APP_JSON, encoding="utf-8")] cwl_fmt = [Format(CONTENT_TYPE_APP_JSON)] res_fmt = merge_io_formats(wps_fmt, cwl_fmt) assert isinstance(res_fmt, list) assert_formats_equal_any_order( res_fmt, [Format(CONTENT_TYPE_APP_JSON, encoding="utf-8")])
def test_format_class(self): """Test pywps.formats.Format class """ frmt = Format('mimetype', schema='halloworld', encoding='asdf', validate=self.validate) self.assertEqual(frmt.mime_type, 'mimetype') self.assertEqual(frmt.schema, 'halloworld') self.assertEqual(frmt.encoding, 'asdf') self.assertTrue(frmt.validate('the input', 1)) describeel = frmt.describe_xml() self.assertEqual('Format', describeel.tag) mimetype = xpath_ns(describeel, '/Format/MimeType') encoding = xpath_ns(describeel, '/Format/Encoding') schema = xpath_ns(describeel, '/Format/Schema') self.assertTrue(mimetype) self.assertTrue(encoding) self.assertTrue(schema) self.assertEqual(mimetype[0].text, 'mimetype') self.assertEqual(encoding[0].text, 'asdf') self.assertEqual(schema[0].text, 'halloworld') frmt2 = get_format('GML') self.assertFalse(frmt.same_as(frmt2))
def from_json(cls, json_input): instance = cls( identifier=json_input['identifier'], title=json_input.get('title'), abstract=json_input.get('abstract'), keywords=json_input.get('keywords', []), workdir=json_input.get('workdir'), metadata=[ Metadata.from_json(data) for data in json_input.get('metadata', []) ], data_format=Format( schema=json_input['data_format'].get('schema'), extension=json_input['data_format'].get('extension'), mime_type=json_input['data_format']['mime_type'], encoding=json_input['data_format'].get('encoding')), supported_formats=[ Format(schema=infrmt.get('schema'), extension=infrmt.get('extension'), mime_type=infrmt['mime_type'], encoding=infrmt.get('encoding')) for infrmt in json_input['supported_formats'] ], mode=json_input.get('mode', MODE.NONE)) instance.as_reference = json_input.get('asreference', False) if json_input.get('file'): instance.file = json_input['file'] elif json_input.get('href'): instance.url = json_input['href'] elif json_input.get('data'): instance.data = json_input['data'] return instance
def test_get_format(): assert f.get_format(f.ContentType.APP_JSON) == Format( f.ContentType.APP_JSON) # basic assert f.get_format(f.ContentType.APP_JSON + "; charset=UTF-8") == Format( f.ContentType.APP_JSON) assert f.get_format(f.ContentType.APP_GEOJSON) == Format( f.ContentType.APP_GEOJSON) # pywps vendor MIME-type assert f.get_format(f.ContentType.APP_NETCDF ).encoding == "base64" # extra encoding data available
def test_merge_io_formats_both_wps_and_cwl(): wps_fmt = [Format(CONTENT_TYPE_APP_NETCDF)] cwl_fmt = [Format(CONTENT_TYPE_APP_JSON)] res_fmt = merge_io_formats(wps_fmt, cwl_fmt) assert isinstance(res_fmt, list) assert_formats_equal_any_order( res_fmt, [Format(CONTENT_TYPE_APP_NETCDF), Format(CONTENT_TYPE_APP_JSON)])
def test_wps2json_io_default_format(): # must create object with matching data/supported formats or error otherwise wps_io = ComplexInput("test", "", supported_formats=[DEFAULT_FORMAT], data_format=DEFAULT_FORMAT) # then simulate that a merging/resolution replaced the supported formats wps_io.supported_formats = [] # conversion should employ the default format since none specified json_io = wps2json_io(wps_io) default = { "default": True, "mediaType": DEFAULT_FORMAT.mime_type, "extension": DEFAULT_FORMAT.extension, "encoding": DEFAULT_FORMAT.encoding, "schema": DEFAULT_FORMAT.schema, } assert json_io["formats"] == [default] # do the same thing again, but 'default' should be False because default plain/text # format is not defined as 'data_format' (used as default specifier) json_fmt = Format(ContentType.APP_JSON) wps_io = ComplexInput("test", "", supported_formats=[json_fmt], data_format=json_fmt) wps_io.supported_formats = [] json_io = wps2json_io(wps_io) default = { "default": False, # <-- this is what must be different "mediaType": DEFAULT_FORMAT.mime_type, "extension": DEFAULT_FORMAT.extension, "encoding": DEFAULT_FORMAT.encoding, "schema": DEFAULT_FORMAT.schema, } assert json_io["formats"] == [default]
def test_json_in(self): """Test json import """ injson = {} injson['schema'] = 'elcepelce' injson['extension'] = '.gml' injson['mime_type'] = 'application/gml+xml' injson['encoding'] = 'utf-8' frmt = Format(injson['mime_type']) frmt.json = injson self.assertEqual(injson['schema'], frmt.schema) self.assertEqual(injson['extension'], frmt.extension) self.assertEqual(injson['mime_type'], frmt.mime_type) self.assertEqual(injson['encoding'], frmt.encoding)
def test_get_format_media_type_no_extension(): for ctype in [ f.ContentType.APP_OCTET_STREAM, f.ContentType.APP_FORM, f.ContentType.MULTI_PART_FORM, ]: fmt = f.get_format(ctype) assert fmt == Format(ctype, extension=None) assert fmt.extension == ""
def test_merge_io_formats_wps_overlaps_cwl(): wps_fmt = [ Format(ContentType.APP_JSON, encoding="utf-8"), # complements CWL details Format(ContentType.APP_NETCDF), # duplicated in CWL (but different index) Format(ContentType.TEXT_PLAIN) # extra (but not default) ] cwl_fmt = [ Format(ContentType.APP_JSON), # overridden by WPS version Format(ContentType.APP_XML), # extra preserved Format(ContentType.APP_NETCDF), # duplicated with WPS, merged ] res_fmt = merge_io_formats(wps_fmt, cwl_fmt) assert isinstance(res_fmt, list) assert_formats_equal_any_order(res_fmt, [ Format(ContentType.APP_JSON, encoding="utf-8"), Format(ContentType.APP_NETCDF), Format(ContentType.APP_XML), Format(ContentType.TEXT_PLAIN), ])
def test_get_format_default_no_extension(): for val in ["", None]: for ctype in [ f.ContentType.APP_OCTET_STREAM, f.ContentType.APP_FORM, f.ContentType.MULTI_PART_FORM, ]: fmt = f.get_format(val, default=ctype) assert fmt == Format(ctype, extension=None) assert fmt.extension == ""
def test_format_class(self): """Test pywps.formats.Format class """ frmt = Format('mimetype', schema='halloworld', encoding='asdf', validate=self.validate) self.assertEqual(frmt.mime_type, 'mimetype') self.assertEqual(frmt.schema, 'halloworld') self.assertEqual(frmt.encoding, 'asdf') self.assertTrue(frmt.validate('the input', 1)) describeel = frmt.json self.assertEqual(describeel["mime_type"], 'mimetype') self.assertEqual(describeel["encoding"], 'asdf') self.assertEqual(describeel["schema"], 'halloworld') frmt2 = get_format('GML') self.assertFalse(frmt.same_as(frmt2))
def get_format(mime_type, default=None): # type: (str, Optional[str]) -> Format """ Obtains a :class:`Format` with predefined extension and encoding details from known MIME-types. """ ctype = clean_mime_type_format(mime_type, strip_parameters=True) fmt = _CONTENT_TYPE_FORMAT_MAPPING.get(mime_type) if fmt is not None: return fmt if default is not None: ctype = default return Format(ctype, extension=get_extension(ctype))
def test_any2cwl_io_from_wps(): fmt = Format(ContentType.APP_NETCDF) wps_io = ComplexInput("test", "", supported_formats=[fmt], data_format=fmt) # use 'json' to obtain 'type' field, but otherwise fields are named the same as if using the object wps_as_json = wps_io.json cwl_io, cwl_ns = any2cwl_io(wps_as_json, "input") assert cwl_io == { "id": "test", "type": "File", "format": f"edam:{EDAM_MAPPING[ContentType.APP_NETCDF]}" } assert cwl_ns == EDAM_NAMESPACE_DEFINITION # retry by manually injecting the type to validate that # pre-resolved type can also be converted directly from object # since the object is used rather than its JSON representation, # potentially more field definitions are available to extract CWL equivalents setattr(wps_io, "type", "complex") cwl_io, cwl_ns = any2cwl_io(wps_io, "input") assert cwl_io == { "id": "test", "type": "File", "format": f"edam:{EDAM_MAPPING[ContentType.APP_NETCDF]}", "default": None, } assert cwl_ns == EDAM_NAMESPACE_DEFINITION wps_io.min_occurs = 10 wps_io.max_occurs = 20 # use 'json' to obtain 'type' field, but otherwise fields are named the same as if using the object cwl_io, cwl_ns = any2cwl_io(wps_io, "input") assert cwl_io == { "id": "test", "type": {"type": "array", "items": "File"}, "format": f"edam:{EDAM_MAPPING[ContentType.APP_NETCDF]}", "default": None, } assert cwl_ns == EDAM_NAMESPACE_DEFINITION
def get_format(mime_type): # type: (str) -> Format """Obtains a :class:`Format` with predefined extension and encoding details from known MIME-types.""" ctype = clean_mime_type_format(mime_type, strip_parameters=True) return _CONTENT_TYPE_FORMAT_MAPPING.get( mime_type, Format(ctype, extension=get_extension(ctype)))
def json(self, value): """init this request from json back again :param value: the json (not string) representation """ self.operation = value['operation'] self.version = value['version'] self.language = value['language'] self.identifier = value['identifier'] self.identifiers = value['identifiers'] self.store_execute = value['store_execute'] self.status = value['status'] self.lineage = value['lineage'] self.outputs = value['outputs'] self.raw = value['raw'] self.inputs = {} for identifier in value['inputs']: inpt = None inpt_defs = value['inputs'][identifier] for inpt_def in inpt_defs: if inpt_def['type'] == 'complex': inpt = ComplexInput( identifier=inpt_def['identifier'], title=inpt_def.get('title'), abstract=inpt_def.get('abstract'), workdir=inpt_def.get('workdir'), data_format=Format( schema=inpt_def['data_format'].get('schema'), extension=inpt_def['data_format'].get('extension'), mime_type=inpt_def['data_format']['mime_type'], encoding=inpt_def['data_format'].get('encoding')), supported_formats=[ Format(schema=infrmt.get('schema'), extension=infrmt.get('extension'), mime_type=infrmt['mime_type'], encoding=infrmt.get('encoding')) for infrmt in inpt_def['supported_formats'] ], mode=MODE.NONE) inpt.file = inpt_def['file'] elif inpt_def['type'] == 'literal': allowed_values = [] for allowed_value in inpt_def['allowed_values']: if allowed_value['type'] == 'anyvalue': allowed_values.append(AnyValue()) elif allowed_value['type'] == 'novalue': allowed_values.append(NoValue()) elif allowed_value['type'] == 'valuesreference': allowed_values.append(ValuesReference()) elif allowed_value['type'] == 'allowedvalue': allowed_values.append( AllowedValue( allowed_type=allowed_value['allowed_type'], value=allowed_value['value'], minval=allowed_value['minval'], maxval=allowed_value['maxval'], spacing=allowed_value['spacing'], range_closure=allowed_value[ 'range_closure'])) inpt = LiteralInput(identifier=inpt_def['identifier'], title=inpt_def.get('title'), abstract=inpt_def.get('abstract'), data_type=inpt_def.get('data_type'), workdir=inpt_def.get('workdir'), allowed_values=AnyValue, uoms=inpt_def.get('uoms'), mode=inpt_def.get('mode')) inpt.uom = inpt_def.get('uom') inpt.data = inpt_def.get('data') elif inpt_def['type'] == 'bbox': inpt = BBoxInput(identifier=inpt_def['identifier'], title=inpt_def['title'], abstract=inpt_def['abstract'], crss=inpt_def['crs'], dimensions=inpt_def['dimensions'], workdir=inpt_def['workdir'], mode=inpt_def['mode']) inpt.ll = inpt_def['bbox'][0] inpt.ur = inpt_def['bbox'][1] if identifier in self.inputs: self.inputs[identifier].append(inpt) else: self.inputs[identifier] = [inpt]
def test_merge_io_formats_with_wps_and_default_cwl(): wps_fmt = [Format(ContentType.APP_NETCDF)] cwl_fmt = [DEFAULT_FORMAT] res_fmt = merge_io_formats(wps_fmt, cwl_fmt) assert isinstance(res_fmt, list) assert_formats_equal_any_order(res_fmt, [Format(ContentType.APP_NETCDF)])