Esempio n. 1
0
    def test_extend_validators_with_dict(self):
        def_validators = [{'eq': ["a", {"v": 1}]}, {'eq': [{"b": 1}, 200]}]
        current_validators = [{'len_eq': ['s3', 12]}, {'eq': [{"b": 1}, 201]}]
        def_validators = [
            validator.uniform_validator(_validator)
            for _validator in def_validators
        ]
        ref_validators = [
            validator.uniform_validator(_validator)
            for _validator in current_validators
        ]

        extended_validators = validator.extend_validators(
            def_validators, ref_validators)
        self.assertEqual(len(extended_validators), 3)
        self.assertIn(
            {
                'check': {
                    'b': 1
                },
                'expect': 201,
                'comparator': 'equals'
            }, extended_validators)
        self.assertNotIn(
            {
                'check': {
                    'b': 1
                },
                'expect': 200,
                'comparator': 'equals'
            }, extended_validators)
Esempio n. 2
0
    def test_extend_validators(self):
        def_validators = [{
            'eq': ['v1', 200]
        }, {
            "check": "s2",
            "expect": 16,
            "comparator": "len_eq"
        }]
        current_validators = [{
            "check": "v1",
            "expect": 201
        }, {
            'len_eq': ['s3', 12]
        }]
        def_validators = [
            validator.uniform_validator(_validator)
            for _validator in def_validators
        ]
        ref_validators = [
            validator.uniform_validator(_validator)
            for _validator in current_validators
        ]

        extended_validators = validator.extend_validators(
            def_validators, ref_validators)
        self.assertIn({
            "check": "v1",
            "expect": 201,
            "comparator": "equals"
        }, extended_validators)
        self.assertIn(
            {
                "check": "s2",
                "expect": 16,
                "comparator": "length_equals"
            }, extended_validators)
        self.assertIn(
            {
                "check": "s3",
                "expect": 12,
                "comparator": "length_equals"
            }, extended_validators)
Esempio n. 3
0
def _extend_with_api(test_dict, api_def_dict):
    """ extend test with api definition, test will merge and override api definition.

    Args:
        test_dict (dict): test block, this will override api_def_dict
        api_def_dict (dict): api definition

    Examples:
        >>> api_def_dict = {
                "name": "get token 1",
                "request": {...},
                "validate": [{'eq': ['status_code', 200]}]
            }
        >>> test_dict = {
                "name": "get token 2",
                "extract": {"token": "content.token"},
                "validate": [{'eq': ['status_code', 201]}, {'len_eq': ['content.token', 16]}]
            }
        >>> _extend_with_api(test_dict, api_def_dict)
        >>> print(test_dict)
            {
                "name": "get token 2",
                "request": {...},
                "extract": {"token": "content.token"},
                "validate": [{'eq': ['status_code', 201]}, {'len_eq': ['content.token', 16]}]
            }

    """
    # override api name
    test_dict.setdefault("name", api_def_dict.pop("name",
                                                  "api name undefined"))

    # override variables
    def_variables = api_def_dict.pop("variables", [])
    test_dict["variables"] = utils.extend_variables(
        def_variables, test_dict.get("variables", {}))

    # merge & override validators TODO: relocate
    def_raw_validators = api_def_dict.pop("validate", [])
    def_validators = [
        validator.uniform_validator(_validator)
        for _validator in def_raw_validators
    ]
    ref_validators = test_dict.pop("validate", [])
    test_dict["validate"] = validator.extend_validators(
        def_validators, ref_validators)

    # merge & override extractors
    def_extrators = api_def_dict.pop("extract", {})
    test_dict["extract"] = utils.extend_variables(def_extrators,
                                                  test_dict.get("extract", {}))

    # merge & override request
    test_dict["request"] = api_def_dict.pop("request", {})

    # base_url & verify: priority api_def_dict > test_dict
    if api_def_dict.get("base_url"):
        test_dict["base_url"] = api_def_dict["base_url"]

    if "verify" in api_def_dict:
        test_dict["request"]["verify"] = api_def_dict["verify"]

    # merge & override setup_hooks
    def_setup_hooks = api_def_dict.pop("setup_hooks", [])
    ref_setup_hooks = test_dict.get("setup_hooks", [])
    extended_setup_hooks = list(set(def_setup_hooks + ref_setup_hooks))
    if extended_setup_hooks:
        test_dict["setup_hooks"] = extended_setup_hooks
    # merge & override teardown_hooks
    def_teardown_hooks = api_def_dict.pop("teardown_hooks", [])
    ref_teardown_hooks = test_dict.get("teardown_hooks", [])
    extended_teardown_hooks = list(set(def_teardown_hooks +
                                       ref_teardown_hooks))
    if extended_teardown_hooks:
        test_dict["teardown_hooks"] = extended_teardown_hooks

    # TODO: extend with other api definition items, e.g. times
    test_dict.update(api_def_dict)