Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: third_party/google-endpoints/apitools/base/py/testing/mock_test.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #
2 # Copyright 2015 Google Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """Tests for apitools.base.py.testing.mock."""
17
18 import unittest2
19
20 from apitools.base.protorpclite import messages
21
22 import apitools.base.py as apitools_base
23 from apitools.base.py.testing import mock
24 from apitools.base.py.testing import testclient as fusiontables
25
26
27 class MockTest(unittest2.TestCase):
28
29 def testMockFusionBasic(self):
30 with mock.Client(fusiontables.FusiontablesV1) as client_class:
31 client_class.column.List.Expect(request=1, response=2)
32 client = fusiontables.FusiontablesV1(get_credentials=False)
33 self.assertEqual(client.column.List(1), 2)
34 with self.assertRaises(mock.UnexpectedRequestException):
35 client.column.List(3)
36
37 def testMockFusionException(self):
38 with mock.Client(fusiontables.FusiontablesV1) as client_class:
39 client_class.column.List.Expect(
40 request=1,
41 exception=apitools_base.HttpError({'status': 404}, '', ''))
42 client = fusiontables.FusiontablesV1(get_credentials=False)
43 with self.assertRaises(apitools_base.HttpError):
44 client.column.List(1)
45
46 def testMockFusionOrder(self):
47 with mock.Client(fusiontables.FusiontablesV1) as client_class:
48 client_class.column.List.Expect(request=1, response=2)
49 client_class.column.List.Expect(request=2, response=1)
50 client = fusiontables.FusiontablesV1(get_credentials=False)
51 self.assertEqual(client.column.List(1), 2)
52 self.assertEqual(client.column.List(2), 1)
53
54 def testMockFusionWrongOrder(self):
55 with mock.Client(fusiontables.FusiontablesV1) as client_class:
56 client_class.column.List.Expect(request=1, response=2)
57 client_class.column.List.Expect(request=2, response=1)
58 client = fusiontables.FusiontablesV1(get_credentials=False)
59 with self.assertRaises(mock.UnexpectedRequestException):
60 self.assertEqual(client.column.List(2), 1)
61 with self.assertRaises(mock.UnexpectedRequestException):
62 self.assertEqual(client.column.List(1), 2)
63
64 def testMockFusionTooMany(self):
65 with mock.Client(fusiontables.FusiontablesV1) as client_class:
66 client_class.column.List.Expect(request=1, response=2)
67 client = fusiontables.FusiontablesV1(get_credentials=False)
68 self.assertEqual(client.column.List(1), 2)
69 with self.assertRaises(mock.UnexpectedRequestException):
70 self.assertEqual(client.column.List(2), 1)
71
72 def testMockFusionTooFew(self):
73 with self.assertRaises(mock.ExpectedRequestsException):
74 with mock.Client(fusiontables.FusiontablesV1) as client_class:
75 client_class.column.List.Expect(request=1, response=2)
76 client_class.column.List.Expect(request=2, response=1)
77 client = fusiontables.FusiontablesV1(get_credentials=False)
78 self.assertEqual(client.column.List(1), 2)
79
80 def testFusionUnmock(self):
81 with mock.Client(fusiontables.FusiontablesV1):
82 client = fusiontables.FusiontablesV1(get_credentials=False)
83 mocked_service_type = type(client.column)
84 client = fusiontables.FusiontablesV1(get_credentials=False)
85 self.assertNotEqual(type(client.column), mocked_service_type)
86
87 def testClientUnmock(self):
88 mock_client = mock.Client(fusiontables.FusiontablesV1)
89 attributes = set(mock_client.__dict__.keys())
90 mock_client = mock_client.Mock()
91 self.assertTrue(set(mock_client.__dict__.keys()) - attributes)
92 mock_client.Unmock()
93 self.assertEqual(attributes, set(mock_client.__dict__.keys()))
94
95
96 class _NestedMessage(messages.Message):
97 nested = messages.StringField(1)
98
99
100 class _NestedListMessage(messages.Message):
101 nested_list = messages.MessageField(_NestedMessage, 1, repeated=True)
102
103
104 class _NestedNestedMessage(messages.Message):
105 nested = messages.MessageField(_NestedMessage, 1)
106
107
108 class UtilTest(unittest2.TestCase):
109
110 def testMessagesEqual(self):
111 self.assertFalse(mock._MessagesEqual(
112 _NestedNestedMessage(
113 nested=_NestedMessage(
114 nested='foo')),
115 _NestedNestedMessage(
116 nested=_NestedMessage(
117 nested='bar'))))
118
119 self.assertTrue(mock._MessagesEqual(
120 _NestedNestedMessage(
121 nested=_NestedMessage(
122 nested='foo')),
123 _NestedNestedMessage(
124 nested=_NestedMessage(
125 nested='foo'))))
126
127 def testListedMessagesEqual(self):
128 self.assertTrue(mock._MessagesEqual(
129 _NestedListMessage(
130 nested_list=[_NestedMessage(nested='foo')]),
131 _NestedListMessage(
132 nested_list=[_NestedMessage(nested='foo')])))
133
134 self.assertTrue(mock._MessagesEqual(
135 _NestedListMessage(
136 nested_list=[_NestedMessage(nested='foo'),
137 _NestedMessage(nested='foo2')]),
138 _NestedListMessage(
139 nested_list=[_NestedMessage(nested='foo'),
140 _NestedMessage(nested='foo2')])))
141
142 self.assertFalse(mock._MessagesEqual(
143 _NestedListMessage(
144 nested_list=[_NestedMessage(nested='foo')]),
145 _NestedListMessage(
146 nested_list=[_NestedMessage(nested='bar')])))
147
148 self.assertFalse(mock._MessagesEqual(
149 _NestedListMessage(
150 nested_list=[_NestedMessage(nested='foo')]),
151 _NestedListMessage(
152 nested_list=[_NestedMessage(nested='foo'),
153 _NestedMessage(nested='foo')])))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698