def test_upload_file(self, resolwe_mock, sys_mock, requests_mock): # Example file: fn = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files', 'example.fastq') resolwe_mock.url = 'http://some/url' resolwe_mock.auth = MagicMock() # Supress upload progress messages: sys_mock.sys.stdout.write = MagicMock() sys_mock.sys.stdout.flush = MagicMock() # Immitate response form server - always status 200 requests_response = {'files': [{'temp': 'fake_name'}]} requests_mock.post.return_value = MagicMock(status_code=200, **{'json.return_value': requests_response}) resolwe = Resolwe._upload_file(resolwe_mock, fn) self.assertEqual(resolwe, 'fake_name') # Immitate response form server - always status 400 requests_mock.post.return_value = MagicMock(status_code=400) resolwe = Resolwe._upload_file(resolwe_mock, fn) self.assertIsNone(resolwe) # Immitate response form server - one status 400, but other 200 requests_response = {'files': [{'temp': 'fake_name'}]} response_ok = MagicMock(status_code=200, **{'json.return_value': requests_response}) response_fails = MagicMock(status_code=400) requests_mock.post.side_effect = [response_ok, response_fails, response_ok, response_ok] resolwe = Resolwe._upload_file(resolwe_mock, fn) self.assertEqual(resolwe, 'fake_name')
def test_always_bad(self, resolwe_mock, requests_mock): resolwe_mock.configure_mock(**self.config) # Immitate response form server - always status 400 requests_mock.post.return_value = MagicMock(status_code=400) response = Resolwe._upload_file(resolwe_mock, self.file_path) self.assertIsNone(response) self.assertEqual(resolwe_mock.logger.warning.call_count, 4)
def test_always_bad(self, resolwe_mock, requests_mock): resolwe_mock.configure_mock(**self.config) # Immitate response form server - always status 400 requests_mock.post.return_value = MagicMock(status_code=400) response = Resolwe._upload_file(resolwe_mock, self.fn) self.assertIsNone(response) self.assertEqual(resolwe_mock.logger.warning.call_count, 4)
def test_always_ok(self, resolwe_mock, requests_mock): resolwe_mock.configure_mock(**self.config) # Immitate response form server - always status 200: requests_response = {'files': [{'temp': 'fake_name'}]} requests_mock.post.return_value = MagicMock(status_code=200, **{'json.return_value': requests_response}) response = Resolwe._upload_file(resolwe_mock, self.fn) self.assertEqual(response, 'fake_name')
def test_always_ok(self, resolwe_mock, requests_mock): resolwe_mock.configure_mock(**self.config) # Immitate response form server - always status 200: requests_response = {'files': [{'temp': 'fake_name'}]} requests_mock.post.return_value = MagicMock( status_code=200, **{'json.return_value': requests_response}) response = Resolwe._upload_file(resolwe_mock, self.file_path) self.assertEqual(response, 'fake_name')
def test_always_ok(self, resolwe_mock): resolwe_mock.configure_mock(**self.config) # Immitate response form server - always status 200: requests_response = {"files": [{"temp": "fake_name"}]} resolwe_mock.session.post.return_value = MagicMock( status_code=200, **{"json.return_value": requests_response} ) response = Resolwe._upload_file(resolwe_mock, self.file_path) self.assertEqual(response, "fake_name")
def test_one_bad_other_ok(self, resolwe_mock, requests_mock): resolwe_mock.configure_mock(**self.config) requests_response = {'files': [{'temp': 'fake_name'}]} response_ok = MagicMock(status_code=200, **{'json.return_value': requests_response}) response_fails = MagicMock(status_code=400) # Immitate response form server - one status 400, but other 200: requests_mock.post.side_effect = [response_fails, response_ok, response_ok] response = Resolwe._upload_file(resolwe_mock, self.fn) self.assertEqual(response, 'fake_name') self.assertEqual(resolwe_mock.logger.warning.call_count, 1)
def test_one_bad_other_ok(self, resolwe_mock, requests_mock): resolwe_mock.configure_mock(**self.config) requests_response = {'files': [{'temp': 'fake_name'}]} response_ok = MagicMock(status_code=200, **{'json.return_value': requests_response}) response_fails = MagicMock(status_code=400) # Immitate response form server - one status 400, but other 200: requests_mock.post.side_effect = [response_fails, response_ok, response_ok] response = Resolwe._upload_file(resolwe_mock, self.file_path) self.assertEqual(response, 'fake_name') self.assertEqual(resolwe_mock.logger.warning.call_count, 1)