def test_fetch_data_gsheet_success(self, mock_get_authorized_service): data = {'name': 'John', 'age': 40} mock_credential_obj = Mock(OAuth2Credentials) mock_get_authorized_service.return_value = Mock() gviews_v4._get_authorized_service().spreadsheets().values().get().\ execute().get.return_value = data values, error = gviews_v4._fetch_data_gsheet( mock_credential_obj, 1234, 'Syria Security Incidences') self.assertIsNone(error) self.assertEqual(values, data)
def test_get_gsheet_metadata_success(self, mock_get_authorized_service): mock_service_execute = Mock() mock_credential_obj = Mock(OAuth2Credentials) mock_get_authorized_service.return_value = Mock() gviews_v4._get_authorized_service().spreadsheets().\ get().execute.return_value = mock_service_execute spreadsheet, error = gviews_v4._get_gsheet_metadata( mock_credential_obj, 1234, self.tola_user.user) self.assertIsNone(error) self.assertEqual(spreadsheet, mock_service_execute)
def test_get_gsheet_metadata_exception(self): exception = Exception() exception.__setattr__( 'content', '{"error": {"status": "ERROR", "message": "Msg Test"}}') mock_credential_obj = Mock(OAuth2Credentials) mock_service_execute = Mock(side_effect=exception) gviews_v4._get_authorized_service = Mock() gviews_v4._get_authorized_service().spreadsheets = mock_service_execute spreadsheet, error = gviews_v4._get_gsheet_metadata( mock_credential_obj, 1234, self.tola_user.user) expected_error = {'msg': 'ERROR: Msg Test', 'level': messages.ERROR} self.assertIsNotNone(error) self.assertIsNone(spreadsheet) self.assertEqual(error, expected_error)
def test_get_gsheet_metadata_refresh_error(self, mock_get_credential_obj): refresh_exception = HttpAccessTokenRefreshError() mock_credential_obj = Mock(OAuth2Credentials) mock_new_credential_obj = Mock(OAuth2Credentials) mock_service_execute = Mock(side_effect=refresh_exception) mock_get_credential_obj.return_value = mock_new_credential_obj gviews_v4._get_authorized_service = Mock() gviews_v4._get_authorized_service().spreadsheets = mock_service_execute spreadsheet, error = gviews_v4._get_gsheet_metadata( mock_credential_obj, 1234, self.tola_user.user) expected_error = {'credential': [mock_new_credential_obj]} self.assertIsNotNone(error) self.assertIsNone(spreadsheet) self.assertEqual(error, expected_error)
def test_fetch_data_gsheet_exception(self): exception = Exception('Deu ruim') mock_credential_obj = Mock(OAuth2Credentials) mock_service_execute = Mock(side_effect=exception) gviews_v4._get_authorized_service = Mock() gviews_v4._get_authorized_service().spreadsheets = mock_service_execute values, error = gviews_v4._fetch_data_gsheet( mock_credential_obj, 1234, 'Syria Security Incidences') expected_error = { 'level': messages.ERROR, 'msg': 'Something went wrong 22: Deu ruim', 'redirect': None } self.assertIsNotNone(error) self.assertIsNone(values) self.assertEqual(error, expected_error)