1#!/usr/bin/env python
2# -*- coding:utf-8 -*-
3#
4# Copyright 2014 Google Inc. All Rights Reserved.
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
20Unit tests for model utility methods.
21"""
22from __future__ import absolute_import
23
24__author__ = 'jcgregorio@google.com (Joe Gregorio)'
25
26import unittest2 as unittest
27
28from googleapiclient.model import BaseModel
29from googleapiclient.model import makepatch
30
31
32TEST_CASES = [
33    # (message, original, modified, expected)
34    ("Remove an item from an object",
35     {'a': 1, 'b': 2},  {'a': 1},         {'b': None}),
36    ("Add an item to an object",
37     {'a': 1},          {'a': 1, 'b': 2}, {'b': 2}),
38    ("No changes",
39     {'a': 1, 'b': 2},  {'a': 1, 'b': 2}, {}),
40    ("Empty objects",
41     {},  {}, {}),
42    ("Modify an item in an object",
43     {'a': 1, 'b': 2},  {'a': 1, 'b': 3}, {'b': 3}),
44    ("Change an array",
45     {'a': 1, 'b': [2, 3]},  {'a': 1, 'b': [2]}, {'b': [2]}),
46    ("Modify a nested item",
47     {'a': 1, 'b': {'foo':'bar', 'baz': 'qux'}},
48     {'a': 1, 'b': {'foo':'bar', 'baz': 'qaax'}},
49     {'b': {'baz': 'qaax'}}),
50    ("Modify a nested array",
51     {'a': 1, 'b': [{'foo':'bar', 'baz': 'qux'}]},
52     {'a': 1, 'b': [{'foo':'bar', 'baz': 'qaax'}]},
53     {'b': [{'foo':'bar', 'baz': 'qaax'}]}),
54    ("Remove item from a nested array",
55     {'a': 1, 'b': [{'foo':'bar', 'baz': 'qux'}]},
56     {'a': 1, 'b': [{'foo':'bar'}]},
57     {'b': [{'foo':'bar'}]}),
58    ("Remove a nested item",
59     {'a': 1, 'b': {'foo':'bar', 'baz': 'qux'}},
60     {'a': 1, 'b': {'foo':'bar'}},
61     {'b': {'baz': None}})
62]
63
64
65class TestPatch(unittest.TestCase):
66
67  def test_patch(self):
68    for (msg, orig, mod, expected_patch) in TEST_CASES:
69      self.assertEqual(expected_patch, makepatch(orig, mod), msg=msg)
70
71
72class TestBaseModel(unittest.TestCase):
73    def test_build_query(self):
74        model = BaseModel()
75
76        test_cases = [
77            ('hello', 'world', '?hello=world'),
78            ('hello', u'world', '?hello=world'),
79            ('hello', '세계', '?hello=%EC%84%B8%EA%B3%84'),
80            ('hello', u'세계', '?hello=%EC%84%B8%EA%B3%84'),
81            ('hello', 'こんにちは', '?hello=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'),
82            ('hello', u'こんにちは', '?hello=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'),
83            ('hello', '你好', '?hello=%E4%BD%A0%E5%A5%BD'),
84            ('hello', u'你好', '?hello=%E4%BD%A0%E5%A5%BD'),
85            ('hello', 'مرحبا', '?hello=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7'),
86            ('hello', u'مرحبا', '?hello=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7')
87        ]
88
89        for case in test_cases:
90            key, value, expect = case
91            self.assertEqual(expect, model._build_query({key: value}))
92
93
94if __name__ == '__main__':
95  unittest.main()
96