Skip to content

Commit 1e964bd

Browse files
rafaelweingartnerpriteau
authored andcommitted
Fix broken lower-constraints job
Despite no Flask update on stable branches, the lower-constraints job is broken similarly to when Flask was bumped to 2.0.1. The broken test cases were mocking the root object "flask.request". Instead of mocking the root object, we address the issue by mocking only the needed methods and attributes. This facilitates the understanding of the unit test, and also helps people to pin-point problems right away. Change-Id: I8703c7d3e69f35ef3e85234c27b4743242111f3d (cherry picked from commit 885c9f0)
1 parent c8f095c commit 1e964bd

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

cloudkitty/api/app.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
from oslo_config import cfg
2121
from oslo_log import log
2222
from paste import deploy
23-
try:
24-
from werkzeug.middleware import dispatcher
25-
# In case we have werkzeug<0.15
26-
except ImportError:
27-
from werkzeug import wsgi as dispatcher
23+
24+
from werkzeug.middleware import dispatcher
2825

2926
from cloudkitty.api import root as api_root
3027
from cloudkitty.api.v1 import get_api_app as get_v1_app

cloudkitty/tests/api/v2/dataframes/test_dataframes.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
#
15+
import flask
16+
1517
from unittest import mock
1618

1719
from cloudkitty.api.v2.dataframes import dataframes
@@ -28,12 +30,15 @@ def setUp(self):
2830

2931
def test_non_admin_request_is_filtered_on_project_id(self):
3032
policy_mock = mock.patch('cloudkitty.common.policy.authorize')
33+
34+
flask.request.context = mock.Mock()
35+
flask.request.context.project_id = 'test-project'
36+
flask.request.context.is_admin = False
37+
3138
with mock.patch.object(self.endpoint._storage, 'retrieve') as ret_mock:
32-
with policy_mock, mock.patch('flask.request') as fmock:
39+
with policy_mock, mock.patch('flask.request.args.lists') as fmock:
3340
ret_mock.return_value = {'total': 42, 'dataframes': []}
34-
fmock.args.lists.return_value = []
35-
fmock.context.is_admin = False
36-
fmock.context.project_id = 'test-project'
41+
fmock.return_value = []
3742
self.endpoint.get()
3843
ret_mock.assert_called_once_with(
3944
begin=tzutils.get_month_start(),

cloudkitty/tests/api/v2/summary/test_summary.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
#
15+
import flask
16+
import uuid
17+
1518
from unittest import mock
1619

1720
from cloudkitty.api.v2.summary import summary
@@ -28,10 +31,15 @@ def setUp(self):
2831

2932
def test_type_filter_is_passed_separately(self):
3033
policy_mock = mock.patch('cloudkitty.common.policy.authorize')
34+
35+
flask.request.context = mock.Mock()
36+
flask.request.context.project_id = str(uuid.uuid4())
37+
flask.request.context.is_admin = True
38+
3139
with mock.patch.object(self.endpoint._storage, 'total') as total_mock:
32-
with policy_mock, mock.patch('flask.request') as fmock:
40+
with policy_mock, mock.patch('flask.request.args.lists') as fmock:
3341
total_mock.return_value = {'total': 0, 'results': []}
34-
fmock.args.lists.return_value = [
42+
fmock.return_value = [
3543
('filters', 'a:b,type:awesome')]
3644
self.endpoint.get()
3745
total_mock.assert_called_once_with(

cloudkitty/tests/api/v2/test_utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ def test_func(self, arg_one=None):
243243
self.assertEqual(
244244
list(test_func.input_schema.schema.keys())[0], 'arg_one')
245245

246-
with mock.patch('flask.request') as m:
247-
m.get_json.return_value = {}
246+
with mock.patch('flask.request.get_json') as m:
247+
m.return_value = {}
248248
test_func(self)
249249

250-
with mock.patch('flask.request') as m:
251-
m.get_json.return_value = {'arg_one': 'one'}
250+
with mock.patch('flask.request.get_json') as m:
251+
m.return_value = {'arg_one': 'one'}
252252
test_func(self)
253253

254254
def _test_multiple_add_input_schema_x(self, location):

0 commit comments

Comments
 (0)