/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Martin Pool
  • Date: 2008-09-15 11:20:15 UTC
  • mto: (3697.2.6 261315-into-1.7)
  • mto: This revision was merged to the branch mainline in revision 3710.
  • Revision ID: mbp@sourcefrog.net-20080915112015-taqk86xpt7vhj71r
FakeClient can know what calls to expect

Show diffs side-by-side

added added

removed removed

Lines of Context:
177
177
        self.responses = []
178
178
        self._calls = []
179
179
        self.expecting_body = False
 
180
        # if non-None, this is the list of expected calls, with only the
 
181
        # method name and arguments included.  the body might be hard to
 
182
        # compute so is not included
 
183
        self._expected_calls = None
180
184
        _SmartClient.__init__(self, FakeMedium(self._calls, fake_medium_base))
181
185
 
 
186
    def add_expected_call(self, call_name, call_args, response_type,
 
187
        response_args, response_body=None):
 
188
        if self._expected_calls is None:
 
189
            self._expected_calls = []
 
190
        self._expected_calls.append((call_name, call_args))
 
191
        self.responses.append((response_types, response_args, response_body))
 
192
 
182
193
    def add_success_response(self, *args):
183
194
        self.responses.append(('success', args, None))
184
195
 
192
203
        self.responses.append(('unknown', verb))
193
204
 
194
205
    def _get_next_response(self):
195
 
        response_tuple = self.responses.pop(0)
 
206
        try:
 
207
            response_tuple = self.responses.pop(0)
 
208
        except IndexError, e:
 
209
            raise AssertionError("%r didn't expect any more calls"
 
210
                % (self,))
196
211
        if response_tuple[0] == 'unknown':
197
212
            raise errors.UnknownSmartMethod(response_tuple[1])
198
213
        elif response_tuple[0] == 'error':
199
214
            raise errors.ErrorFromSmartServer(response_tuple[1])
200
215
        return response_tuple
201
216
 
 
217
    def _check_call(self, method, args):
 
218
        if self._expected_calls is None:
 
219
            # the test should be updated to say what it expects
 
220
            return
 
221
        try:
 
222
            next_call = self._expected_calls.pop(0)
 
223
        except IndexError:
 
224
            raise AssertionError("%r didn't expect any more calls "
 
225
                "but got %r%r"
 
226
                % (method, args,))
 
227
        if method != next_call[0] or args != next_call[1]:
 
228
            raise AssertionError("%r expected %r%r "
 
229
                "but got %r%r"
 
230
                % (next_call[0], next_call[1], method, args,))
 
231
 
202
232
    def call(self, method, *args):
 
233
        self._check_call(method, args)
203
234
        self._calls.append(('call', method, args))
204
235
        return self._get_next_response()[1]
205
236
 
206
237
    def call_expecting_body(self, method, *args):
 
238
        self._check_call(method, args)
207
239
        self._calls.append(('call_expecting_body', method, args))
208
240
        result = self._get_next_response()
209
241
        self.expecting_body = True
210
242
        return result[1], FakeProtocol(result[2], self)
211
243
 
212
244
    def call_with_body_bytes_expecting_body(self, method, args, body):
 
245
        self._check_call(method, args)
213
246
        self._calls.append(('call_with_body_bytes_expecting_body', method,
214
247
            args, body))
215
248
        result = self._get_next_response()