Пример #1
0
    def test_substitute_variables_with_variables(self):
        data = {
            'foo': '[[users.0.username]]',
            'baz':
            ['qux', '[[runners.1]]', None, 12, '[[integers.seventeen]]'],
            'flub': {
                'flux':
                'flare [[GET_DOCTOR.0.doctor.specialty]] [[{get_doctor.0}.doctor.name]] '
                '[[not.a.sub] [also.not.a.sub]] [[users.1.username]] with [[runners.0]]',
            },
        }

        substitute_variables(data, *self.sources)

        self.assertEqual(
            {
                'foo': 'beamer',
                'baz': ['qux', 'Rebecca', None, 12, 17],
                'flub': {
                    'flux':
                    'flare endocrinologist Jamie Rolling [[not.a.sub] [also.not.a.sub]] pumpkin with John',
                },
            },
            data,
        )
Пример #2
0
    def test_substitute_variables_no_sources(self):
        data = {
            'foo': '[[users.0.username]]',
            'baz': ['qux', 'flem'],
            'flub': {'flux': 'flare', 'flex': 'flue'},
        }

        with self.assertRaises(StatusError):
            substitute_variables(data)
Пример #3
0
    def test_substitute_variables_no_variables(self):
        data = {
            'foo': 'bar',
            'baz': ['qux', 'flem'],
            'flub': {'flux': 'flare', 'flex': 'flue'},
        }

        substitute_variables(data, *self.sources)  # type: ignore

        self.assertEqual(
            {
                'foo': 'bar',
                'baz': ['qux', 'flem'],
                'flub': {'flux': 'flare', 'flex': 'flue'},
            },
            data,
        )
Пример #4
0
    def _run_test_case(self, test_case, test_fixture, test_fixture_results, *_, **__):
        """
        This does the actual work of running the test case.

        :param test_case: The directive instructions to run and assert this specific test case
        :type test_case: dict
        :param test_fixture: List of test cases in this fixture
        :type test_fixture: list
        :param test_fixture_results: List of all action-call results in the entire fixture
        :type test_fixture_results: list
        """

        # noinspection PyUnusedLocal
        # This instructs the traceback manipulator that this frame belongs to _run_test_case, which is simpler than
        # having it analyze the code path details to determine the frame location.
        _run_test_case_frame = True  # noqa F841

        action_results = {}
        action_response_bodies = {}
        test_fixture_results.append(action_results)

        for action_path in test_case['actions']:
            action_name, action_index = action_path.split('.')
            action_case = test_case[action_path]

            if 'inputs' in action_case:
                substitute_variables(action_case['inputs'], action_response_bodies, self.model_constants)
            if 'job_control_inputs' in action_case:
                substitute_variables(action_case['job_control_inputs'], action_response_bodies, self.model_constants)
            if 'job_context_inputs' in action_case:
                substitute_variables(action_case['job_context_inputs'], action_response_bodies, self.model_constants)

            self.set_up_test_case_action(action_name, action_case, test_case, test_fixture)
            self._run_directive_hook('set_up_test_case_action', action_name, action_case, test_case, test_fixture)

            stub_context = self._WrapperContextManager()  # it's a no-op with no arguments
            if (
                action_name not in self.server_class.action_class_map and  # if the server doesn't have this action
                action_name not in ('status', 'introspect') and  # if the action isn't one of the built-in actions
                hasattr(self, '_process_stub_action_{}'.format(action_name))  # if the test job has a mock action
            ):
                # Hook for custom, test-only actions that are not real commands on the service.  Custom actions must
                # must work the same as side-effect functions on stub_action (must accept a request body dict and
                # return a response body dict, `ActionResponse`, or `JobResponse` or raise an `ActionError` or
                # `JobError`).
                stub_context = self._WrapperContextManager(
                    stub_action(self.server_class.service_name, action_name),
                    getattr(self, '_process_stub_action_{}'.format(action_name)),
                )

            with stub_context:
                job_response = self.client.call_actions(
                    service_name=self.server_class.service_name,
                    actions=[{'action': action_name, 'body': action_case.get('inputs', {})}],
                    raise_job_errors=False,
                    raise_action_errors=False,
                    context=action_case.get('job_context_inputs', {}),
                    control_extra=action_case.get('job_control_inputs', {}),
                )

            action_results[action_path] = job_response.actions[0] if job_response.actions else None
            action_response_bodies[action_path] = job_response.actions[0].body if job_response.actions else None

            substitute_variables(action_response_bodies, action_response_bodies, self.model_constants)
            substitute_variables(action_case, action_response_bodies, self.model_constants)

            try:
                self._run_directive_hook(
                    'assert_test_case_action_results',
                    action_name,
                    action_case,
                    test_case,
                    test_fixture,
                    action_results[action_path],
                    job_response,
                    action_path,
                )
            finally:
                try:
                    self._run_directive_hook(
                        'tear_down_test_case_action',
                        action_name,
                        action_case,
                        test_case,
                        test_fixture,
                    )
                finally:
                    self.tear_down_test_case_action(action_name, action_case, test_case, test_fixture)

        self._run_directive_hook('assert_test_case_results', action_results, test_case, test_fixture)