blob: bbc52b063c253301e79b94f60eca353ab2ba2fa6 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2020 The Chromium Authors
Copybara854996b2021-09-07 19:36:02 +00002# 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()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010037 _, kwargs = get_client_mock().create_task.call_args
38 self.assertEqual(kwargs['task'], task)
Copybara854996b2021-09-07 19:36:02 +000039
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
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010056 _, kwargs = get_client_mock().create_task.call_args
Copybara854996b2021-09-07 19:36:02 +000057 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',
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010069 'body': b'a=a&b=b',
Copybara854996b2021-09-07 19:36:02 +000070 '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',
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010082 'body': b'',
Copybara854996b2021-09-07 19:36:02 +000083 'headers': {
84 'Content-type': 'application/x-www-form-urlencoded'
85 }
86 }
87 }
88 self.assertEqual(actual, expected)