blob: 09ad2cd8001d93cc63f214a806137b6522a3a25e [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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
6from __future__ import absolute_import
7from __future__ import division
8from __future__ import print_function
9
10from google.api_core import exceptions
11
12import mock
13import unittest
14
15from framework import cloud_tasks_helpers
16import settings
17
18
19class 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)