Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | # Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | """Tests for the cloud tasks helper module.""" |
| 5 | |
| 6 | from __future__ import absolute_import |
| 7 | from __future__ import division |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | from google.api_core import exceptions |
| 11 | |
| 12 | import mock |
| 13 | import unittest |
| 14 | |
| 15 | from framework import cloud_tasks_helpers |
| 16 | import settings |
| 17 | |
| 18 | |
| 19 | class CloudTasksHelpersTest(unittest.TestCase): |
| 20 | |
| 21 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 22 | def test_create_task(self, get_client_mock): |
| 23 | |
| 24 | queue = 'somequeue' |
| 25 | task = { |
| 26 | 'app_engine_http_request': |
| 27 | { |
| 28 | 'http_method': 'GET', |
| 29 | 'relative_uri': '/some_url' |
| 30 | } |
| 31 | } |
| 32 | cloud_tasks_helpers.create_task(task, queue=queue) |
| 33 | |
| 34 | get_client_mock().queue_path.assert_called_with( |
| 35 | settings.app_id, settings.CLOUD_TASKS_REGION, queue) |
| 36 | get_client_mock().create_task.assert_called_once() |
| 37 | ((_parent, called_task), _kwargs) = get_client_mock().create_task.call_args |
| 38 | self.assertEqual(called_task, task) |
| 39 | |
| 40 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 41 | def test_create_task_raises(self, get_client_mock): |
| 42 | task = {'app_engine_http_request': {}} |
| 43 | |
| 44 | get_client_mock().create_task.side_effect = exceptions.GoogleAPICallError( |
| 45 | 'oh no!') |
| 46 | |
| 47 | with self.assertRaises(exceptions.GoogleAPICallError): |
| 48 | cloud_tasks_helpers.create_task(task) |
| 49 | |
| 50 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 51 | def test_create_task_retries(self, get_client_mock): |
| 52 | task = {'app_engine_http_request': {}} |
| 53 | |
| 54 | cloud_tasks_helpers.create_task(task) |
| 55 | |
| 56 | (_args, kwargs) = get_client_mock().create_task.call_args |
| 57 | self.assertEqual(kwargs.get('retry'), cloud_tasks_helpers._DEFAULT_RETRY) |
| 58 | |
| 59 | def test_generate_simple_task(self): |
| 60 | actual = cloud_tasks_helpers.generate_simple_task( |
| 61 | '/alphabet/letters', { |
| 62 | 'a': 'a', |
| 63 | 'b': 'b' |
| 64 | }) |
| 65 | expected = { |
| 66 | 'app_engine_http_request': |
| 67 | { |
| 68 | 'relative_uri': '/alphabet/letters', |
| 69 | 'body': 'a=a&b=b', |
| 70 | 'headers': { |
| 71 | 'Content-type': 'application/x-www-form-urlencoded' |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | self.assertEqual(actual, expected) |
| 76 | |
| 77 | actual = cloud_tasks_helpers.generate_simple_task('/alphabet/letters', {}) |
| 78 | expected = { |
| 79 | 'app_engine_http_request': |
| 80 | { |
| 81 | 'relative_uri': '/alphabet/letters', |
| 82 | 'body': '', |
| 83 | 'headers': { |
| 84 | 'Content-type': 'application/x-www-form-urlencoded' |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | self.assertEqual(actual, expected) |