Parkayun | 1c1da59 | 2014-12-10 13:32:46 +0900 | [diff] [blame^] | 1 | # -*- coding:utf-8 -*- |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 2 | #!/usr/bin/python2.4 |
| 3 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 4 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | """Model tests |
| 19 | |
| 20 | Unit tests for model utility methods. |
| 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 25 | import unittest |
| 26 | |
Parkayun | 1c1da59 | 2014-12-10 13:32:46 +0900 | [diff] [blame^] | 27 | from googleapiclient.model import BaseModel |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 28 | from googleapiclient.model import makepatch |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 29 | |
| 30 | |
| 31 | TEST_CASES = [ |
| 32 | # (message, original, modified, expected) |
| 33 | ("Remove an item from an object", |
| 34 | {'a': 1, 'b': 2}, {'a': 1}, {'b': None}), |
| 35 | ("Add an item to an object", |
| 36 | {'a': 1}, {'a': 1, 'b': 2}, {'b': 2}), |
| 37 | ("No changes", |
| 38 | {'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {}), |
| 39 | ("Empty objects", |
| 40 | {}, {}, {}), |
| 41 | ("Modify an item in an object", |
| 42 | {'a': 1, 'b': 2}, {'a': 1, 'b': 3}, {'b': 3}), |
| 43 | ("Change an array", |
| 44 | {'a': 1, 'b': [2, 3]}, {'a': 1, 'b': [2]}, {'b': [2]}), |
| 45 | ("Modify a nested item", |
| 46 | {'a': 1, 'b': {'foo':'bar', 'baz': 'qux'}}, |
| 47 | {'a': 1, 'b': {'foo':'bar', 'baz': 'qaax'}}, |
| 48 | {'b': {'baz': 'qaax'}}), |
| 49 | ("Modify a nested array", |
| 50 | {'a': 1, 'b': [{'foo':'bar', 'baz': 'qux'}]}, |
| 51 | {'a': 1, 'b': [{'foo':'bar', 'baz': 'qaax'}]}, |
| 52 | {'b': [{'foo':'bar', 'baz': 'qaax'}]}), |
| 53 | ("Remove item from a nested array", |
| 54 | {'a': 1, 'b': [{'foo':'bar', 'baz': 'qux'}]}, |
| 55 | {'a': 1, 'b': [{'foo':'bar'}]}, |
| 56 | {'b': [{'foo':'bar'}]}), |
| 57 | ("Remove a nested item", |
| 58 | {'a': 1, 'b': {'foo':'bar', 'baz': 'qux'}}, |
| 59 | {'a': 1, 'b': {'foo':'bar'}}, |
| 60 | {'b': {'baz': None}}) |
| 61 | ] |
| 62 | |
| 63 | |
| 64 | class TestPatch(unittest.TestCase): |
| 65 | |
| 66 | def test_patch(self): |
| 67 | for (msg, orig, mod, expected_patch) in TEST_CASES: |
| 68 | self.assertEqual(expected_patch, makepatch(orig, mod), msg=msg) |
| 69 | |
| 70 | |
Parkayun | 1c1da59 | 2014-12-10 13:32:46 +0900 | [diff] [blame^] | 71 | class TestBaseModel(unittest.TestCase): |
| 72 | def test_build_query(self): |
| 73 | model = BaseModel() |
| 74 | |
| 75 | test_cases = [ |
| 76 | ('hello', 'world', '?hello=world'), |
| 77 | ('hello', u'world', '?hello=world'), |
| 78 | ('hello', '세계', '?hello=%EC%84%B8%EA%B3%84'), |
| 79 | ('hello', u'세계', '?hello=%EC%84%B8%EA%B3%84'), |
| 80 | ('hello', 'こんにちは', '?hello=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'), |
| 81 | ('hello', u'こんにちは', '?hello=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'), |
| 82 | ('hello', '你好', '?hello=%E4%BD%A0%E5%A5%BD'), |
| 83 | ('hello', u'你好', '?hello=%E4%BD%A0%E5%A5%BD'), |
| 84 | ('hello', 'مرحبا', '?hello=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7'), |
| 85 | ('hello', u'مرحبا', '?hello=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7') |
| 86 | ] |
| 87 | |
| 88 | for case in test_cases: |
| 89 | key, value, expect = case |
| 90 | self.assertEqual(expect, model._build_query({key: value})) |
| 91 | |
| 92 | |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 93 | if __name__ == '__main__': |
| 94 | unittest.main() |