def test_get_session_headers(self): """ Verify _get_session_headers returns the expected headers.""" error_message = "\nTest: {} \nExpected: {}\nActual: {}\n" test_params = { "no_metric": { "metric": "", "headers": NO_METRIC_HEADERS, 'session_id': '' }, "metric_present": { "metric": METRIC, "headers": DEFAULT_SESSION_HEADERS, 'session_id': SESSION_ID } } for test, params in test_params.items(): request_parameters = { 'metric': params['metric'], 'auth_token': AUTH_TOKEN, 'headers': params['headers'], 'session_id': params['session_id'] } attributes = Attributes(**request_parameters) expected = params["headers"] actual = attributes._get_session_headers() self.assertEqual(expected, actual, error_message.format(test, expected, actual))
def test_get_parameters_no_key(self): """ If 'params' not in dictionary it will be added.""" test_parameters = {'session_id': '1342312kj12'} attributes = Attributes(**test_parameters) actual = attributes.get_params() expected = '' self.assertEqual(actual, expected)
def test_get_data_without_username(self, mock_is_auth_request): """ Verify the correct data is returned if there is no username key.""" mock_is_auth_request.return_value = False test_parameters = {'session_id': '1234abc'} attributes = Attributes(**test_parameters) actual = attributes.get_data() expected = '' self.assertEqual(actual, expected)
def test_is_auth_request(self): """ Verify True is returned if parameters include 'username' key.""" test_parameters = { "username": "******", "password": "******" } attributes = Attributes(**test_parameters) actual = attributes._is_auth_request() self.assertTrue(actual)
def test_get_headers_for_auth(self): """ Verify correct headers are returned for an authentication call. If there is no 'metric' key in the parameters it is set as an auth call. """ test_parameters = {} self.attributes = Attributes(**test_parameters) actual = self.attributes.get_headers() expected = AUTH_HEADERS self.assertEqual(actual, expected)
def test_get_headers_includes_empty_metric(self): """ Verify the correct headers are returned if the metric key is empty.""" test_parameters = { "metric": "", "auth_token": AUTH_TOKEN, 'session_id': '' } attributes = Attributes(**test_parameters) actual = attributes.get_headers() expected = NO_METRIC_HEADERS self.assertEqual(actual, expected)
def test_get_params(self): """ Verify the correct parameters are returned. The test parameter used is an example of a query that could be used for calls to the GameBench API. When added to the URL it would return the last 15 entries that matched the search. """ test_parameters = {"params": "pageSize=15"} attributes = Attributes(**test_parameters) actual = attributes.get_params() expected = "pageSize=15" self.assertEqual(actual, expected)
def test_get_headers_includes_metric(self): """ Verify the correct headers are returned if a metric key is present.""" test_parameters = { "metric": METRIC, "auth_token": AUTH_TOKEN, 'session_id': SESSION_ID } attributes = Attributes(**test_parameters) actual = attributes.get_headers() expected = DEFAULT_SESSION_HEADERS self.assertEqual(actual, expected)
def __init__(self): """ Creates an instance of the Request object for construction. """ self.request = Request() self.attributes = Attributes() self.method = Method()
def test_get_data_with_username(self, mock_is_auth_request): """ Verify the correct data is returned if a username and password are present. If there is a username key that means it is an auth request so we return the username and password as the data variable in the request builder. """ mock_is_auth_request.return_value = True test_parameters = { "password": "******", "username": "******" } attributes = Attributes(**test_parameters) actual = attributes.get_data() expected = {"username": "******", "password": "******"} with self.subTest(): mock_is_auth_request.assert_called_once_with() self.assertEqual(actual, expected)
def __init__(self, **request_parameters): """ Creates an instance of the Request object for construction. :param request_parameters: session_id - id for the current session metric - specific category of data being requested (cpu, gpu, battery, etc.) auth_token - authorization token necessary for retrieving data. params - parameters appended to certain session requests (pageSize, timePushed, etc.) data - json body appended to certain session requests (example: {"apps" : [], "devices" : [], "manufacturers" : []}) """ self.request_parameters = request_parameters self.request = Request() self.attributes = Attributes(**self.request_parameters) self.method = Method(**self.request_parameters)
def setUp(self): self.attributes = Attributes()
class TestAttributes(TestCase): """ Unit tests for the Attributes class.""" def setUp(self): self.attributes = Attributes() def test_get_headers_includes_empty_metric(self): """ Verify the correct headers are returned if the metric key is empty.""" test_parameters = {"metric": "", "auth_token": AUTH_TOKEN} actual = self.attributes.get_headers(**test_parameters) expected = NO_METRIC_HEADERS self.assertEqual(actual, expected) def test_get_headers_includes_metric(self): """ Verify the correct headers are returned if a metric key is present.""" test_parameters = {"metric": METRIC, "auth_token": AUTH_TOKEN} actual = self.attributes.get_headers(**test_parameters) expected = DEFAULT_SESSION_HEADERS self.assertEqual(actual, expected) def test_get_headers_for_auth(self): """ Verify correct headers are returned for an authentication call. If there is no 'metric' key in the parameters it is set as an auth call. """ test_parameters = {} actual = self.attributes.get_headers(**test_parameters) expected = AUTH_HEADERS self.assertEqual(actual, expected) def test_get_params(self): """ Verify the correct parameters are returned. The test parameter used is an example of a query that could be used for calls to the GameBench API. When added to the URL it would return the last 15 entries that matched the search. """ test_parameters = {"params": "pageSize=15"} actual = self.attributes.get_params(**test_parameters) expected = "pageSize=15" self.assertEqual(actual, expected) @patch( 'gamebench_api_client.api.requests_retriever.builder.attributes.attributes_creator.Attributes' '._is_auth_request') def test_get_data_with_username(self, mock_is_auth_request): """ Verify the correct data is returned if a username and password are present. If there is a username key that means it is an auth request so we return the username and password as the data variable in the request builder. """ mock_is_auth_request.return_value = True test_parameters = { "password": "******", "username": "******" } actual = self.attributes.get_data(**test_parameters) expected = {"username": "******", "password": "******"} with self.subTest(): mock_is_auth_request.assert_called_once_with(**test_parameters) self.assertEqual(actual, expected) @patch( 'gamebench_api_client.api.requests_retriever.builder.attributes.attributes_creator.Attributes' '._is_auth_request') def test_get_data_without_username(self, mock_is_auth_request): """ Verify the correct data is returned if there is no username key.""" mock_is_auth_request.return_value = False test_parameters = { "data": "test_data", } actual = self.attributes.get_data(**test_parameters) expected = "test_data" self.assertEqual(actual, expected) def test_is_auth_request(self): """ Verify True is returned if parameters include 'username' key.""" test_parameters = { "username": "******", "password": "******" } actual = self.attributes._is_auth_request(**test_parameters) self.assertTrue(actual) def test_get_session_headers(self): """ Verify _get_session_headers returns the expected headers.""" error_message = "\nTest: {} \nExpected: {}\nActual: {}\n" test_params = { "no_metric": { "metric": "", "headers": NO_METRIC_HEADERS }, "metric_present": { "metric": METRIC, "headers": DEFAULT_SESSION_HEADERS } } for test, params in test_params.items(): request_parameters = { 'metric': params['metric'], 'auth_token': AUTH_TOKEN, 'headers': params['headers'] } expected = params["headers"] actual = self.attributes._get_session_headers(**request_parameters) self.assertEqual(expected, actual, error_message.format(test, expected, actual))