def test_binary_file(self): contents = 'This is a test' filename = self.files.create_file('foo', contents) prefixed_filename = 'fileb://' + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, b'This is a test') self.assertIsInstance(data, six.binary_type)
def _check_for_uri_param(param, value): if isinstance(value, list) and len(value) == 1: value = value[0] try: return get_paramfile(value) except ResourceLoadingError as e: raise ParamError(param.cli_name, six.text_type(e))
def test_text_file(self): contents = 'This is a test' filename = self.files.create_file('foo', contents) prefixed_filename = 'file://' + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, contents) self.assertIsInstance(data, six.string_types)
def add_to_call_parameters(self, call_parameters, parsed_args): # Check if ``--cli-input-json`` was specified in the command line. input_json = getattr(parsed_args, 'cli_input_json', None) if input_json is not None: # Retrieve the JSON from the file if needed. retrieved_json = get_paramfile(input_json) # Nothing was retrieved from the file. So assume the argument # is already a JSON string. if retrieved_json is None: retrieved_json = input_json try: # Try to load the JSON string into a python dictionary input_data = json.loads(retrieved_json) except ValueError as e: raise ParamError( self.name, "Invalid JSON: %s\nJSON received: %s" % (e, retrieved_json)) # We run the ParamFileVisitor over the input data to resolve any # paramfile references in it. input_data = ParamFileVisitor().visit( input_data, self._operation_model.input_shape) # Add the members from the input JSON to the call parameters. self._update_call_parameters(call_parameters, input_data) return True
def test_non_200_raises_error(self): self.response.status_code = 500 with self.assertRaisesRegexp(ResourceLoadingError, 'foo\\.bar\\.baz'): get_paramfile('https://foo.bar.baz')
def test_resource_from_https(self): self.response.text = 'http contents' loaded = get_paramfile('https://foo.bar.baz') self.assertEqual(loaded, 'http contents') self.requests_mock.get.assert_called_with('https://foo.bar.baz')
def test_non_string_type_returns_none(self): self.assertIsNone(get_paramfile(100))
def test_no_match_uris_returns_none(self): self.assertIsNone(get_paramfile('foobar://somewhere.bar'))
def test_file_does_not_exist_raises_error(self): with self.assertRaises(ResourceLoadingError): get_paramfile('file://file/does/not/existsasdf.txt')
def test_cannot_load_text_file(self): contents = b'\xbfX\xac\xbe' filename = self.files.create_file('foo', contents, mode='wb') prefixed_filename = 'file://' + filename with self.assertRaises(ResourceLoadingError): get_paramfile(prefixed_filename)
def test_connection_error_raises_error(self): self.requests_mock.get.side_effect = Exception("Connection error.") with self.assertRaisesRegexp(ResourceLoadingError, 'foo\\.bar\\.baz'): get_paramfile('https://foo.bar.baz')