Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. |
| 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 | """Model tests |
| 18 | |
| 19 | Unit tests for model utility methods. |
| 20 | """ |
| 21 | |
| 22 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 23 | |
| 24 | import httplib2 |
| 25 | import unittest |
| 26 | |
| 27 | from apiclient.model import makepatch |
| 28 | |
| 29 | |
| 30 | TEST_CASES = [ |
| 31 | # (message, original, modified, expected) |
| 32 | ("Remove an item from an object", |
| 33 | {'a': 1, 'b': 2}, {'a': 1}, {'b': None}), |
| 34 | ("Add an item to an object", |
| 35 | {'a': 1}, {'a': 1, 'b': 2}, {'b': 2}), |
| 36 | ("No changes", |
| 37 | {'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {}), |
| 38 | ("Empty objects", |
| 39 | {}, {}, {}), |
| 40 | ("Modify an item in an object", |
| 41 | {'a': 1, 'b': 2}, {'a': 1, 'b': 3}, {'b': 3}), |
| 42 | ("Change an array", |
| 43 | {'a': 1, 'b': [2, 3]}, {'a': 1, 'b': [2]}, {'b': [2]}), |
| 44 | ("Modify a nested item", |
| 45 | {'a': 1, 'b': {'foo':'bar', 'baz': 'qux'}}, |
| 46 | {'a': 1, 'b': {'foo':'bar', 'baz': 'qaax'}}, |
| 47 | {'b': {'baz': 'qaax'}}), |
| 48 | ("Modify a nested array", |
| 49 | {'a': 1, 'b': [{'foo':'bar', 'baz': 'qux'}]}, |
| 50 | {'a': 1, 'b': [{'foo':'bar', 'baz': 'qaax'}]}, |
| 51 | {'b': [{'foo':'bar', 'baz': 'qaax'}]}), |
| 52 | ("Remove item from a nested array", |
| 53 | {'a': 1, 'b': [{'foo':'bar', 'baz': 'qux'}]}, |
| 54 | {'a': 1, 'b': [{'foo':'bar'}]}, |
| 55 | {'b': [{'foo':'bar'}]}), |
| 56 | ("Remove a nested item", |
| 57 | {'a': 1, 'b': {'foo':'bar', 'baz': 'qux'}}, |
| 58 | {'a': 1, 'b': {'foo':'bar'}}, |
| 59 | {'b': {'baz': None}}) |
| 60 | ] |
| 61 | |
| 62 | |
| 63 | class TestPatch(unittest.TestCase): |
| 64 | |
| 65 | def test_patch(self): |
| 66 | for (msg, orig, mod, expected_patch) in TEST_CASES: |
| 67 | self.assertEqual(expected_patch, makepatch(orig, mod), msg=msg) |
| 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | unittest.main() |