Пример #1
0
    def test_get_response_json_gets_expected_request_dict(self, mock_return):
        """ Verify get_response_json.request is set to the correct dict."""

        # We don't want to make an actual request and requests_mock is quite slow, so we don't want to use it
        # unnecessarily. Since this isn't actually used as part of the test, I'm just returning an empty string.
        mock_return.request.return_value = ""

        for test, params in self.test_params.items():
            session_parameters = {
                'method': params['method'],
                'session_id': SESSION_ID,
                'metric': params['metric'],
                'auth_token': AUTH_TOKEN,
                "params": params['params'],
                "data": params['data']
            }
            self.retriever = ResponseRetriever(**session_parameters)
            self.retriever.get_response_json()

            expected = {
                "method": params['method'],
                "url": params['url'],
                "attributes": params['attributes']
            }
            actual = self.retriever.request

            self.assertEqual(
                expected,
                actual,
                self.error_message.format(
                    test,
                    expected,
                    actual
                )
            )
Пример #2
0
    def test_get_response_json_creates_expected_json_with_time_series_data(
            self, mock_return):
        """ Verify the json we are expecting is returned from the Adapter."""

        for test, params in self.test_params.items():
            session_parameters = {
                'method': params['method'],
                'session_id': params['session_id'],
                'metric': params['metric'],
                'auth_token': AUTH_TOKEN,
                "params": params['params'],
                "data": params['data']
            }
            expected_request_parameters = {
                "method": params['method'],
                "url": params['url'],
                "attributes": params['attributes']
            }

            adapter = RequestsAdapter(**expected_request_parameters)
            mock_return.request(params['method'],
                                params['url'],
                                json=self.time_series_json["response"])

            expected_json = adapter.request().json()
            expected = expected_json['samples']

            self.retriever = ResponseRetriever(**session_parameters)
            actual_json = self.retriever.get_response_json()
            actual = actual_json['samples']

            self.assertEqual(expected, actual,
                             self.error_message.format(test, expected, actual))
Пример #3
0
class GenericMediator(ResponseMediator):
    """ Concrete Mediator for generic objects to use."""
    def __init__(self, **request_parameters):
        super().__init__(**request_parameters)
        self.retriever = ResponseRetriever(**request_parameters)

    def get_results(self):
        """ Sets JSON data into a Pandas DataFrame.

            :return: DataFrame of the JSON data from a response.
        """

        response_json = self.retriever.get_response_json()

        return to_dataframe(response_json)
Пример #4
0
class SessionDetailMediator(ResponseMediator):
    """ Concrete Mediator for session detail objects to use."""
    def __init__(self, **request_parameters):
        super().__init__(**request_parameters)
        self.retriever = ResponseRetriever(**request_parameters['response'])

    def get_results(self):
        """ Sets JSON data into a Pandas DataFrame.

            :return: DataFrame of the JSON data from a response.
        """

        response_json = self.retriever.get_response_json()
        session_dict = {
            'metric': self.request_parameters['metric'],
            'response_json': response_json
        }

        return session_detail_to_dataframe(**session_dict)
Пример #5
0
class TestResponseRetriever(TestCase):
    """ Tests for the ResponseMediator."""

    def setUp(self):
        self.error_message = "\nTest: {} \nExpected: {}\nActual:   {}\n"
        with open(os.path.join(
                PARENT_DIR + API_SAMPLES + "cpu_multiple_sessions.json")) as \
                json_data:
            self.time_series_json = json.load(json_data)
        with open(os.path.join(
                PARENT_DIR + API_SAMPLES + "sessionid.json")) as json_data:
            self.session_id_json = json.load(json_data)
        self.test_params = {
            "metric": {
                'method': 'GET',
                'metric': '/cpu',
                "params": 'test_params',
                "url": DEFAULT_SESSION_URL,
                "data": {'test_data': 'test_data'},
                "headers": DEFAULT_SESSION_HEADERS,
                "attributes": {
                    'params': 'test_params',
                    'headers': DEFAULT_SESSION_HEADERS,
                    'data': {'test_data': 'test_data'}
                }
            },
            "no_metric": {
                'method': 'POST',
                'metric': '',
                "params": 'test_params',
                'url': "https://api.production.gamebench.net/v1/sessions/session_id",
                "data": {'test_data': 'test_data'},
                "headers": NO_METRIC_HEADERS,
                "attributes": {
                    'params': 'test_params',
                    'headers': NO_METRIC_HEADERS,
                    'data': {'test_data': 'test_data'}
                }
            }
        }

    @patch('gamebench_api_client.api.response.response_retriever.RequestsAdapter')
    def test_get_response_json_gets_expected_request_dict(self, mock_return):
        """ Verify get_response_json.request is set to the correct dict."""

        # We don't want to make an actual request and requests_mock is quite slow, so we don't want to use it
        # unnecessarily. Since this isn't actually used as part of the test, I'm just returning an empty string.
        mock_return.request.return_value = ""

        for test, params in self.test_params.items():
            session_parameters = {
                'method': params['method'],
                'session_id': SESSION_ID,
                'metric': params['metric'],
                'auth_token': AUTH_TOKEN,
                "params": params['params'],
                "data": params['data']
            }
            self.retriever = ResponseRetriever(**session_parameters)
            self.retriever.get_response_json()

            expected = {
                "method": params['method'],
                "url": params['url'],
                "attributes": params['attributes']
            }
            actual = self.retriever.request

            self.assertEqual(
                expected,
                actual,
                self.error_message.format(
                    test,
                    expected,
                    actual
                )
            )

    @requests_mock.Mocker()
    def test_get_response_json_creates_expected_json_with_time_series_data(self, mock_return):
        """ Verify the json we are expecting is returned from the Adapter."""

        for test, params in self.test_params.items():
            session_parameters = {
                'method': params['method'],
                'session_id': SESSION_ID,
                'metric': params['metric'],
                'auth_token': AUTH_TOKEN,
                "params": params['params'],
                "data": params['data']
            }
            expected_request_parameters = {
                "method": params['method'],
                "url": params['url'],
                "attributes": params['attributes']
            }

            adapter = RequestsAdapter(**expected_request_parameters)
            mock_return.request(
                params['method'],
                params['url'],
                json=self.time_series_json["response"]
            )

            expected_json = adapter.request().json()
            expected = expected_json['samples']

            self.retriever = ResponseRetriever(**session_parameters)
            actual_json = self.retriever.get_response_json()
            actual = actual_json['samples']

            self.assertEqual(
                expected,
                actual,
                self.error_message.format(
                    test,
                    expected,
                    actual
                )
            )
Пример #6
0
 def __init__(self, **request_parameters):
     super().__init__(**request_parameters)
     self.retriever = ResponseRetriever(**request_parameters)