Craig Citro | 15744b1 | 2015-03-02 13:34:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Parkayun | 5bd38f3 | 2014-12-10 15:34:46 +0900 | [diff] [blame] | 2 | # -*- coding:utf-8 -*- |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 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 | """ |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 22 | from __future__ import absolute_import |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 23 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 24 | __author__ = "jcgregorio@google.com (Joe Gregorio)" |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 25 | |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 26 | import unittest2 as unittest |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 27 | |
Parkayun | 1c1da59 | 2014-12-10 13:32:46 +0900 | [diff] [blame] | 28 | from googleapiclient.model import BaseModel |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 29 | from googleapiclient.model import makepatch |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 30 | |
| 31 | |
| 32 | TEST_CASES = [ |
| 33 | # (message, original, modified, expected) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 34 | ("Remove an item from an object", {"a": 1, "b": 2}, {"a": 1}, {"b": None}), |
| 35 | ("Add an item to an object", {"a": 1}, {"a": 1, "b": 2}, {"b": 2}), |
| 36 | ("No changes", {"a": 1, "b": 2}, {"a": 1, "b": 2}, {}), |
| 37 | ("Empty objects", {}, {}, {}), |
| 38 | ("Modify an item in an object", {"a": 1, "b": 2}, {"a": 1, "b": 3}, {"b": 3}), |
| 39 | ("Change an array", {"a": 1, "b": [2, 3]}, {"a": 1, "b": [2]}, {"b": [2]}), |
| 40 | ( |
| 41 | "Modify a nested item", |
| 42 | {"a": 1, "b": {"foo": "bar", "baz": "qux"}}, |
| 43 | {"a": 1, "b": {"foo": "bar", "baz": "qaax"}}, |
| 44 | {"b": {"baz": "qaax"}}, |
| 45 | ), |
| 46 | ( |
| 47 | "Modify a nested array", |
| 48 | {"a": 1, "b": [{"foo": "bar", "baz": "qux"}]}, |
| 49 | {"a": 1, "b": [{"foo": "bar", "baz": "qaax"}]}, |
| 50 | {"b": [{"foo": "bar", "baz": "qaax"}]}, |
| 51 | ), |
| 52 | ( |
| 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 | ), |
| 58 | ( |
| 59 | "Remove a nested item", |
| 60 | {"a": 1, "b": {"foo": "bar", "baz": "qux"}}, |
| 61 | {"a": 1, "b": {"foo": "bar"}}, |
| 62 | {"b": {"baz": None}}, |
| 63 | ), |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 64 | ] |
| 65 | |
| 66 | |
| 67 | class TestPatch(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 68 | def test_patch(self): |
| 69 | for (msg, orig, mod, expected_patch) in TEST_CASES: |
| 70 | self.assertEqual(expected_patch, makepatch(orig, mod), msg=msg) |
Joe Gregorio | e98c232 | 2011-05-26 15:40:48 -0400 | [diff] [blame] | 71 | |
| 72 | |
Parkayun | 1c1da59 | 2014-12-10 13:32:46 +0900 | [diff] [blame] | 73 | class TestBaseModel(unittest.TestCase): |
| 74 | def test_build_query(self): |
| 75 | model = BaseModel() |
| 76 | |
| 77 | test_cases = [ |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 78 | ("hello", "world", "?hello=world"), |
| 79 | ("hello", u"world", "?hello=world"), |
| 80 | ("hello", "세계", "?hello=%EC%84%B8%EA%B3%84"), |
| 81 | ("hello", u"세계", "?hello=%EC%84%B8%EA%B3%84"), |
| 82 | ("hello", "こんにちは", "?hello=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF"), |
| 83 | ("hello", u"こんにちは", "?hello=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF"), |
| 84 | ("hello", "你好", "?hello=%E4%BD%A0%E5%A5%BD"), |
| 85 | ("hello", u"你好", "?hello=%E4%BD%A0%E5%A5%BD"), |
| 86 | ("hello", "مرحبا", "?hello=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7"), |
| 87 | ("hello", u"مرحبا", "?hello=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7"), |
Parkayun | 1c1da59 | 2014-12-10 13:32:46 +0900 | [diff] [blame] | 88 | ] |
| 89 | |
| 90 | for case in test_cases: |
| 91 | key, value, expect = case |
| 92 | self.assertEqual(expect, model._build_query({key: value})) |
| 93 | |
| 94 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 95 | if __name__ == "__main__": |
| 96 | unittest.main() |