Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2014 Google Inc. All Rights Reserved.
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 """Mock tests
     18 
     19 Unit tests for the Mocks.
     20 """
     21 from __future__ import absolute_import
     22 
     23 __author__ = 'jcgregorio (at] google.com (Joe Gregorio)'
     24 
     25 import httplib2
     26 import os
     27 import unittest2 as unittest
     28 
     29 from googleapiclient.errors import HttpError
     30 from googleapiclient.errors import UnexpectedBodyError
     31 from googleapiclient.errors import UnexpectedMethodError
     32 from googleapiclient.discovery import build
     33 from googleapiclient.http import RequestMockBuilder
     34 from googleapiclient.http import HttpMock
     35 
     36 
     37 DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
     38 
     39 def datafile(filename):
     40   return os.path.join(DATA_DIR, filename)
     41 
     42 
     43 class Mocks(unittest.TestCase):
     44   def setUp(self):
     45     self.http = HttpMock(datafile('plus.json'), {'status': '200'})
     46     self.zoo_http = HttpMock(datafile('zoo.json'), {'status': '200'})
     47 
     48   def test_default_response(self):
     49     requestBuilder = RequestMockBuilder({})
     50     plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
     51     activity = plus.activities().get(activityId='tag:blah').execute()
     52     self.assertEqual({}, activity)
     53 
     54   def test_simple_response(self):
     55     requestBuilder = RequestMockBuilder({
     56         'plus.activities.get': (None, '{"foo": "bar"}')
     57         })
     58     plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
     59 
     60     activity = plus.activities().get(activityId='tag:blah').execute()
     61     self.assertEqual({"foo": "bar"}, activity)
     62 
     63   def test_unexpected_call(self):
     64     requestBuilder = RequestMockBuilder({}, check_unexpected=True)
     65 
     66     plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
     67 
     68     try:
     69       plus.activities().get(activityId='tag:blah').execute()
     70       self.fail('UnexpectedMethodError should have been raised')
     71     except UnexpectedMethodError:
     72       pass
     73 
     74   def test_simple_unexpected_body(self):
     75     requestBuilder = RequestMockBuilder({
     76         'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', None)
     77         })
     78     zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
     79 
     80     try:
     81       zoo.animals().insert(body='{}').execute()
     82       self.fail('UnexpectedBodyError should have been raised')
     83     except UnexpectedBodyError:
     84       pass
     85 
     86   def test_simple_expected_body(self):
     87     requestBuilder = RequestMockBuilder({
     88         'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', '{}')
     89         })
     90     zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
     91 
     92     try:
     93       zoo.animals().insert(body='').execute()
     94       self.fail('UnexpectedBodyError should have been raised')
     95     except UnexpectedBodyError:
     96       pass
     97 
     98   def test_simple_wrong_body(self):
     99     requestBuilder = RequestMockBuilder({
    100         'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
    101                                     '{"data": {"foo": "bar"}}')
    102         })
    103     zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
    104 
    105     try:
    106       zoo.animals().insert(
    107           body='{"data": {"foo": "blah"}}').execute()
    108       self.fail('UnexpectedBodyError should have been raised')
    109     except UnexpectedBodyError:
    110       pass
    111 
    112   def test_simple_matching_str_body(self):
    113     requestBuilder = RequestMockBuilder({
    114         'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
    115                                     '{"data": {"foo": "bar"}}')
    116         })
    117     zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
    118 
    119     activity = zoo.animals().insert(
    120         body={'data': {'foo': 'bar'}}).execute()
    121     self.assertEqual({'foo': 'bar'}, activity)
    122 
    123   def test_simple_matching_dict_body(self):
    124     requestBuilder = RequestMockBuilder({
    125         'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
    126                                     {'data': {'foo': 'bar'}})
    127         })
    128     zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
    129 
    130     activity = zoo.animals().insert(
    131         body={'data': {'foo': 'bar'}}).execute()
    132     self.assertEqual({'foo': 'bar'}, activity)
    133 
    134   def test_errors(self):
    135     errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'})
    136     requestBuilder = RequestMockBuilder({
    137         'plus.activities.list': (errorResponse, b'{}')
    138         })
    139     plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
    140 
    141     try:
    142       activity = plus.activities().list(collection='public', userId='me').execute()
    143       self.fail('An exception should have been thrown')
    144     except HttpError as e:
    145       self.assertEqual(b'{}', e.content)
    146       self.assertEqual(500, e.resp.status)
    147       self.assertEqual('Server Error', e.resp.reason)
    148 
    149 
    150 if __name__ == '__main__':
    151   unittest.main()
    152