def test_must_return_none_if_file_not_found(self, yaml_parse_mock): expected = "parsed result" yaml_parse_mock.return_value = expected reader = SwaggerReader(definition_uri="somepath") actual = reader._download_swagger("abcdefgh.txt") self.assertIsNone(actual) yaml_parse_mock.assert_not_called()
def test_read_from_definition_uri(self): uri = "./file.txt" expected = {"some": "value"} reader = SwaggerReader(definition_uri=uri) reader._download_swagger = Mock() reader._download_swagger.return_value = expected actual = reader.read() self.assertEquals(actual, expected) reader._download_swagger.assert_called_with(uri)
def test_must_skip_non_s3_dictionaries(self, yaml_parse_mock): location = {"some": "value"} reader = SwaggerReader(definition_uri=location) reader._download_from_s3 = Mock() actual = reader._download_swagger(location) self.assertIsNone(actual) reader._download_from_s3.assert_not_called() yaml_parse_mock.assert_not_called()
def test_must_work_with_include_transform(self, parse_mock): body = {'this': 'swagger'} expected = {'k': 'v'} location = "some location" reader = SwaggerReader(definition_body=body) reader._download_swagger = Mock() reader._download_swagger.return_value = expected parse_mock.return_value = location actual = reader._read_from_definition_body() self.assertEquals(actual, expected) parse_mock.assert_called_with(body)
def test_must_read_first_from_definition_body(self): body = {"this is": "swagger"} uri = "./file.txt" expected = {"some": "value"} reader = SwaggerReader(definition_body=body, definition_uri=uri) reader._download_swagger = Mock() reader._read_from_definition_body = Mock() reader._read_from_definition_body.return_value = expected actual = reader.read() self.assertEquals(actual, expected) reader._read_from_definition_body.assert_called_with() reader._download_swagger.assert_not_called()
def test_must_use_definition_uri_if_body_does_not_exist(self): body = {"this is": "swagger"} uri = "./file.txt" expected = {"some": "value"} reader = SwaggerReader(definition_body=body, definition_uri=uri) reader._download_swagger = Mock() reader._download_swagger.return_value = expected # Set the output of reading the definition body to be None reader._read_from_definition_body = Mock() reader._read_from_definition_body.return_value = None actual = reader.read() self.assertEquals(actual, expected) reader._read_from_definition_body.assert_called_with() reader._download_swagger.assert_called_with(uri)
def test_must_read_from_local_file_without_working_directory( self, yaml_parse_mock): data = {"some": "value"} expected = "parsed result" yaml_parse_mock.return_value = expected with tempfile.NamedTemporaryFile(mode='w', delete=False) as fp: filepath = fp.name json.dump(data, fp) fp.flush() reader = SwaggerReader(definition_uri=filepath) actual = reader._download_swagger(filepath) self.assertEquals(actual, expected) yaml_parse_mock.assert_called_with( '{"some": "value"}' ) # data was read back from the file as JSON string
def test_must_download_from_s3_for_s3_locations(self, yaml_parse_mock): location = { "Bucket": "mybucket", "Key": "swagger.yaml", "Version": "versionId" } swagger_str = "some swagger str" expected = "some data" reader = SwaggerReader(definition_uri=location) reader._download_from_s3 = Mock() reader._download_from_s3.return_value = swagger_str yaml_parse_mock.return_value = expected actual = reader._download_swagger(location) self.assertEquals(actual, expected) reader._download_from_s3.assert_called_with(location["Bucket"], location["Key"], location["Version"]) yaml_parse_mock.assert_called_with(swagger_str)
def test_with_invalid_location(self): reader = SwaggerReader(definition_uri="something") actual = reader._download_swagger({}) self.assertIsNone(actual)