aboutsummaryrefslogtreecommitdiff
path: root/tests/test_model.py
blob: 6506cfc634b4ec25b15ac84d842f78a495f8efe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Copyright 2014 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Model tests

Unit tests for model utility methods.
"""
from __future__ import absolute_import

__author__ = 'jcgregorio@google.com (Joe Gregorio)'

import unittest2 as unittest

from googleapiclient.model import BaseModel
from googleapiclient.model import makepatch


TEST_CASES = [
    # (message, original, modified, expected)
    ("Remove an item from an object",
     {'a': 1, 'b': 2},  {'a': 1},         {'b': None}),
    ("Add an item to an object",
     {'a': 1},          {'a': 1, 'b': 2}, {'b': 2}),
    ("No changes",
     {'a': 1, 'b': 2},  {'a': 1, 'b': 2}, {}),
    ("Empty objects",
     {},  {}, {}),
    ("Modify an item in an object",
     {'a': 1, 'b': 2},  {'a': 1, 'b': 3}, {'b': 3}),
    ("Change an array",
     {'a': 1, 'b': [2, 3]},  {'a': 1, 'b': [2]}, {'b': [2]}),
    ("Modify a nested item",
     {'a': 1, 'b': {'foo':'bar', 'baz': 'qux'}},
     {'a': 1, 'b': {'foo':'bar', 'baz': 'qaax'}},
     {'b': {'baz': 'qaax'}}),
    ("Modify a nested array",
     {'a': 1, 'b': [{'foo':'bar', 'baz': 'qux'}]},
     {'a': 1, 'b': [{'foo':'bar', 'baz': 'qaax'}]},
     {'b': [{'foo':'bar', 'baz': 'qaax'}]}),
    ("Remove item from a nested array",
     {'a': 1, 'b': [{'foo':'bar', 'baz': 'qux'}]},
     {'a': 1, 'b': [{'foo':'bar'}]},
     {'b': [{'foo':'bar'}]}),
    ("Remove a nested item",
     {'a': 1, 'b': {'foo':'bar', 'baz': 'qux'}},
     {'a': 1, 'b': {'foo':'bar'}},
     {'b': {'baz': None}})
]


class TestPatch(unittest.TestCase):

  def test_patch(self):
    for (msg, orig, mod, expected_patch) in TEST_CASES:
      self.assertEqual(expected_patch, makepatch(orig, mod), msg=msg)


class TestBaseModel(unittest.TestCase):
    def test_build_query(self):
        model = BaseModel()

        test_cases = [
            ('hello', 'world', '?hello=world'),
            ('hello', u'world', '?hello=world'),
            ('hello', '세계', '?hello=%EC%84%B8%EA%B3%84'),
            ('hello', u'세계', '?hello=%EC%84%B8%EA%B3%84'),
            ('hello', 'こんにちは', '?hello=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'),
            ('hello', u'こんにちは', '?hello=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'),
            ('hello', '你好', '?hello=%E4%BD%A0%E5%A5%BD'),
            ('hello', u'你好', '?hello=%E4%BD%A0%E5%A5%BD'),
            ('hello', 'مرحبا', '?hello=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7'),
            ('hello', u'مرحبا', '?hello=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7')
        ]

        for case in test_cases:
            key, value, expect = case
            self.assertEqual(expect, model._build_query({key: value}))


if __name__ == '__main__':
  unittest.main()