Skip to content

Commit

Permalink
Merge pull request #2 from mild-blue/improve-reporting
Browse files Browse the repository at this point in the history
Support scalar type definitions
  • Loading branch information
tomaspavlin authored Feb 9, 2021
2 parents 52fe1fd + 861efbe commit cbdf8d3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 15 deletions.
30 changes: 17 additions & 13 deletions swagger_unittest/swagger_parser/swagger_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,12 @@ def validate_additional_properties(self, valid_response, response):
except Exception:
return False

def validate_definition(self, definition_name: str, dict_to_test: Dict, definition: Dict = None):
def validate_definition(self, definition_name: str, expected_value: Dict, definition: Dict = None):
"""Validate the given dict according to the given definition.
Args:
definition_name: name of the the definition.
dict_to_test: dict to test.
expected_value: dict or scalar value to test.
definition: definition
Returns:
Expand All @@ -507,19 +507,23 @@ def validate_definition(self, definition_name: str, dict_to_test: Dict, definiti

# Check all required in dict_to_test
spec_def = definition or self.specification['definitions'][definition_name]
all_required_keys_present = all(req in dict_to_test.keys() for req in spec_def.get('required', {}))
if 'required' in spec_def and not all_required_keys_present:
return False
if isinstance(expected_value, dict):
all_required_keys_present = all(req in expected_value.keys() for req in spec_def.get('required', {}))
if 'required' in spec_def and not all_required_keys_present:
return False

# Check no extra arg & type
properties_dict = spec_def.get('properties', {})
for key, value in dict_to_test.items():
if value is not None:
if key not in properties_dict: # Extra arg
return False
else: # Check type
if not self._validate_type(properties_dict[key], value):
# Check no extra arg & type
properties_dict = spec_def.get('properties', {})
for key, value in expected_value.items():
if value is not None:
if key not in properties_dict: # Extra arg
return False
else: # Check type
if not self._validate_type(properties_dict[key], value):
return False
else:
# Definition corresponds to scalar value (string for instance)
return self.check_type(expected_value, spec_def['type'])

return True

Expand Down
8 changes: 6 additions & 2 deletions swagger_unittest/swagger_tester/swagger_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ def validate_definition(swagger_parser, valid_response, response):
# Check if there is a definition that match body and response
valid_definition = swagger_parser.get_dict_definition(valid_response, get_list=True)
actual_definition = swagger_parser.get_dict_definition(response, get_list=True)
assert len(set(valid_definition).intersection(actual_definition)) >= 1, \
f'Responses {valid_response} and {response} not compatible'
assert len(set(valid_definition).intersection(actual_definition)) >= 1, (
f'Responses are not compatible. Definition of valid response is {valid_definition} which is different '
f'than definition of actual response which is {actual_definition}. \n'
f'Valid response: {valid_response} \n'
f'Actual response: {response}'
)


def parse_parameters(url, action, path, request_args, swagger_parser):
Expand Down
5 changes: 5 additions & 0 deletions tests/swagger_parser/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def inline_parser():
return SwaggerParser('data/inline.yaml')


@pytest.fixture
def swagger_enum_parser():
return SwaggerParser('data/enum.yaml')


@pytest.fixture(scope='module',
params=['data/no_properties.yaml',
'data/object_no_schema.yaml',
Expand Down
21 changes: 21 additions & 0 deletions tests/swagger_parser/data/enum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
swagger: '2.0'
info:
version: '2016-01-19T11:24:50Z'
title: authentication tests
schemes:
- http
paths:
/test:
get:
description: Get something
responses:
'200':
description: OK
schema:
$ref: '#/definitions/my_enum'
definitions:
my_enum:
type: string
enum:
- OPTION_A
- OPTION_B
11 changes: 11 additions & 0 deletions tests/swagger_parser/test_swagger_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ def test_validate_definition(swagger_parser, pet_definition_example):
pet_definition_example['name'] = 2
assert not swagger_parser.validate_definition('Pet', pet_definition_example)

# Check string instead of dict_to_test
assert not swagger_parser.validate_definition('Pet', 'foo')


def test_validate_enum_definition(swagger_enum_parser):
# Check string instead of dict_to_test
assert swagger_enum_parser.validate_definition('my_enum', 'OPTION_A')

# TODO: also check for correct enum value (currently enum is treated as string)
assert swagger_enum_parser.validate_definition('my_enum', 'FOO_BAR')


def test_get_paths_data(swagger_parser, post_put_path_data, get_path_data):
swagger_parser.get_paths_data()
Expand Down
27 changes: 27 additions & 0 deletions tests/swagger_parser/test_swagger_tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding:utf-8 -*-
import pytest

from swagger_unittest.swagger_tester import swagger_tester


def test_validate_definition(swagger_parser, pet_definition_example):
valid_response = swagger_parser.definitions_example['Pet']

# Valid Pet is valid definition
swagger_tester.validate_definition(swagger_parser, valid_response, valid_response)

# Changing list with emtpy list is valid definition of Pet
response = valid_response.copy()
assert len(response['tags']) > 0
response['tags'] = []
swagger_tester.validate_definition(swagger_parser, valid_response, response)

# Changing list with string is not valid definition of Pet
with pytest.raises(AssertionError):
response = valid_response.copy()
response['tags'] = 'foo'
swagger_tester.validate_definition(swagger_parser, valid_response, response)

# Empty dict is not valid Pet
with pytest.raises(AssertionError, match=r".*Responses are not compatible\. Definition of valid response is.*"):
swagger_tester.validate_definition(swagger_parser, valid_response, {})

0 comments on commit cbdf8d3

Please sign in to comment.