| # Copyright 2019 Google Inc. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Unittests for download_testcases_csv.py.""" |
| |
| from __future__ import absolute_import |
| import unittest |
| import download_testcases_csv |
| import mock |
| |
| |
| class TestDownloadTestCasesCSV(unittest.TestCase): |
| |
| def setUp(self): |
| super(TestDownloadTestCasesCSV, self).setUp() |
| self.get_credentials = mock.patch('download_testcases_csv.get_credentials', |
| autospec=True).start() |
| self.discovery_build = mock.patch('googleapiclient.discovery.build', |
| autospec=True).start() |
| self.mock_print = mock.patch('sys.stdout', autospec=True).start() |
| |
| @mock.patch('download_testcases_csv.SaveResponseToCSV', autospec=True) |
| def testDownloadCSVFromGoogleSheet(self, save_response_mock): |
| response = mock.Mock(status=200) |
| self.discovery_build.return_value._http.request.return_value = ( |
| response, 'content') |
| save_response_mock.return_value = '/folder/workbook_1.csv' |
| |
| self.assertEqual(['/folder/workbook_1.csv'], |
| download_testcases_csv.DownloadCSVFromGoogleSheet( |
| None, 'spreadsheet_key', ['workbook_1'], '/folder')) |
| |
| def testDownloadCSVFromGoogleSheet_Failure(self): |
| response = mock.Mock(status=400) |
| self.discovery_build.return_value._http.request.return_value = ( |
| response, 'content') |
| |
| with self.assertRaises( |
| download_testcases_csv.GoogleDriveDownloadException) as context: |
| download_testcases_csv.DownloadCSVFromGoogleSheet(None, 'spreadsheet_key', |
| ['workbook_1'], '/tmp') |
| |
| self.assertIn('Did not download any spreadsheet for', |
| str(context.exception)) |
| |
| |
| if __name__ == '__main__': |
| unittest.main() |