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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
#!/usr/bin/env python
#
# Copyright (C) 2010 Google Inc.
#
# 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.
# This module is used for version 2 of the Google Data APIs.
__author__ = 'j.s@google.com (Jeff Scudder)'
import unittest
import gdata.core
import gdata.test_config as conf
PLAYLIST_EXAMPLE = (
'{"apiVersion": "2.0","data": {"totalResults": 347,"startIndex": 1,"it'
'emsPerPage": 2,"items": [{"id": "4DAEFAF23BB3CDD0","created": "2008-1'
'2-09T20:23:06.000Z","updated": "2010-01-04T02:56:19.000Z","author": "'
'GoogleDevelopers","title": "Google Web Toolkit Developers","descripti'
'on": "Developers talk about using Google Web Toolkit ...","tags": ["g'
'oogle","web","toolkit","developers","gwt"],"size": 12},{"id": "586D32'
'2B5E2764CF","created": "2007-11-13T19:41:21.000Z","updated": "2010-01'
'-04T17:41:16.000Z","author": "GoogleDevelopers","title": "Android","d'
'escription": "Demos and tutorials about the new Android platform.","t'
'ags": ["android","google","developers","mobile"],"size": 32}]}}')
VIDEO_EXAMPLE = (
'{"apiVersion": "2.0","data": {"updated": "2010-01-07T19:58:42.949Z","'
'totalItems": 800,"startIndex": 1,"itemsPerPage": 1, "items": [{"id": '
'"hYB0mn5zh2c","uploaded": "2007-06-05T22:07:03.000Z","updated": "2010'
'-01-07T13:26:50.000Z","uploader": "GoogleDeveloperDay","category": "N'
'ews","title": "Google Developers Day US - Maps API Introduction","des'
'cription": "Google Maps API Introduction ...","tags": ["GDD07","GDD07'
'US","Maps"],"thumbnail": {"default": "http://i.ytimg.com/vi/hYB0mn5zh'
'2c/default.jpg","hqDefault": "http://i.ytimg.com/vi/hYB0mn5zh2c/hqdef'
'ault.jpg"},"player": {"default": "http://www.youtube.com/watch?v'
'\u003dhYB0mn5zh2c"},"content": {"1": "rtsp://v5.cache3.c.youtube.com/'
'CiILENy.../0/0/0/video.3gp","5": "http://www.youtube.com/v/hYB0mn5zh2'
'c?f...","6": "rtsp://v1.cache1.c.youtube.com/CiILENy.../0/0/0/video.3'
'gp"},"duration": 2840,"rating": 4.63,"ratingCount": 68,"viewCount": 2'
'20101,"favoriteCount": 201,"commentCount": 22}]}}')
class JsoncConversionTest(unittest.TestCase):
# See http://code.google.com/apis/youtube/2.0/developers_guide_jsonc.html
def test_from_and_to_old_json(self):
json = ('{"media$group":{"media$credit":[{"$t":"GoogleDevelopers", '
'"role":"uploader", "scheme":"urn:youtube"}]}}')
jsonc_obj = gdata.core.parse_json(json)
self.assert_(isinstance(jsonc_obj, gdata.core.Jsonc))
raw = gdata.core._convert_to_object(jsonc_obj)
self.assertEqual(raw['media$group']['media$credit'][0]['$t'],
'GoogleDevelopers')
def test_to_and_from_jsonc(self):
x = {'a': 1}
jsonc_obj = gdata.core._convert_to_jsonc(x)
self.assertEqual(jsonc_obj.a, 1)
# Convert the json_obj back to a dict and compare.
self.assertEqual(x, gdata.core._convert_to_object(jsonc_obj))
def test_from_and_to_new_json(self):
x = gdata.core.parse_json(PLAYLIST_EXAMPLE)
self.assertEqual(x._dict['apiVersion'], '2.0')
self.assertEqual(x._dict['data']._dict['items'][0]._dict['id'],
'4DAEFAF23BB3CDD0')
self.assertEqual(x._dict['data']._dict['items'][1]._dict['id'],
'586D322B5E2764CF')
x = gdata.core.parse_json(VIDEO_EXAMPLE)
self.assertEqual(x._dict['apiVersion'], '2.0')
self.assertEqual(x.data._dict['totalItems'], 800)
self.assertEqual(x.data.items[0]._dict['viewCount'], 220101)
def test_pretty_print(self):
x = gdata.core.Jsonc(x=1, y=2, z=3)
pretty = gdata.core.prettify_jsonc(x)
self.assert_(isinstance(pretty, (str, unicode)))
pretty = gdata.core.prettify_jsonc(x, 4)
self.assert_(isinstance(pretty, (str, unicode)))
class MemberNameConversionTest(unittest.TestCase):
def test_member_to_jsonc(self):
self.assertEqual(gdata.core._to_jsonc_name(''), '')
self.assertEqual(gdata.core._to_jsonc_name('foo'), 'foo')
self.assertEqual(gdata.core._to_jsonc_name('Foo'), 'Foo')
self.assertEqual(gdata.core._to_jsonc_name('test_x'), 'testX')
self.assertEqual(gdata.core._to_jsonc_name('test_x_y_zabc'), 'testXYZabc')
def build_test_object():
return gdata.core.Jsonc(
api_version='2.0',
data=gdata.core.Jsonc(
total_items=800,
items=[
gdata.core.Jsonc(
view_count=220101,
comment_count=22,
favorite_count=201,
content={
'1': ('rtsp://v5.cache3.c.youtube.com'
'/CiILENy.../0/0/0/video.3gp')})]))
class JsoncObjectTest(unittest.TestCase):
def check_video_json(self, x):
"""Validates a JsoncObject similar to VIDEO_EXAMPLE."""
self.assert_(isinstance(x._dict, dict))
self.assert_(isinstance(x.data, gdata.core.Jsonc))
self.assert_(isinstance(x._dict['data'], gdata.core.Jsonc))
self.assert_(isinstance(x.data._dict, dict))
self.assert_(isinstance(x._dict['data']._dict, dict))
self.assert_(isinstance(x._dict['apiVersion'], (str, unicode)))
self.assert_(isinstance(x.api_version, (str, unicode)))
self.assert_(isinstance(x.data._dict['items'], list))
self.assert_(isinstance(x.data.items[0]._dict['commentCount'],
(int, long)))
self.assert_(isinstance(x.data.items[0].favorite_count, (int, long)))
self.assertEqual(x.data.total_items, 800)
self.assertEqual(x._dict['data']._dict['totalItems'], 800)
self.assertEqual(x.data.items[0].view_count, 220101)
self.assertEqual(x._dict['data']._dict['items'][0]._dict['viewCount'],
220101)
self.assertEqual(x.data.items[0].comment_count, 22)
self.assertEqual(x.data.items[0]._dict['commentCount'], 22)
self.assertEqual(x.data.items[0].favorite_count, 201)
self.assertEqual(x.data.items[0]._dict['favoriteCount'], 201)
self.assertEqual(
x.data.items[0].content._dict['1'],
'rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp')
self.assertEqual(x.api_version, '2.0')
self.assertEqual(x.api_version, x._dict['apiVersion'])
def test_convert_to_jsonc(self):
x = gdata.core._convert_to_jsonc(1)
self.assert_(isinstance(x, (int, long)))
self.assertEqual(x, 1)
x = gdata.core._convert_to_jsonc([1, 'a'])
self.assert_(isinstance(x, list))
self.assertEqual(len(x), 2)
self.assert_(isinstance(x[0], (int, long)))
self.assertEqual(x[0], 1)
self.assert_(isinstance(x[1], (str, unicode)))
self.assertEqual(x[1], 'a')
x = gdata.core._convert_to_jsonc([{'b': 1}, 'a'])
self.assert_(isinstance(x, list))
self.assertEqual(len(x), 2)
self.assert_(isinstance(x[0], gdata.core.Jsonc))
self.assertEqual(x[0].b, 1)
def test_non_json_members(self):
x = gdata.core.Jsonc(alpha=1, _beta=2, deep={'_bbb': 3, 'aaa': 2})
x.test = 'a'
x._bar = 'bacon'
# Should be able to access the _beta member.
self.assertEqual(x._beta, 2)
self.assertEqual(getattr(x, '_beta'), 2)
try:
self.assertEqual(getattr(x.deep, '_bbb'), 3)
except AttributeError:
pass
# There should not be a letter 'B' anywhere in the generated JSON.
self.assertEqual(gdata.core.jsonc_to_string(x).find('B'), -1)
# We should find a 'b' becuse we don't consider names of dict keys in
# the constructor as aliases to camelCase names.
self.assert_(not gdata.core.jsonc_to_string(x).find('b') == -1)
def test_constructor(self):
x = gdata.core.Jsonc(a=[{'x': 'y'}, 2])
self.assert_(isinstance(x, gdata.core.Jsonc))
self.assert_(isinstance(x.a, list))
self.assert_(isinstance(x.a[0], gdata.core.Jsonc))
self.assertEqual(x.a[0].x, 'y')
self.assertEqual(x.a[1], 2)
def test_read_json(self):
x = gdata.core.parse_json(PLAYLIST_EXAMPLE)
self.assert_(isinstance(x._dict, dict))
self.assertEqual(x._dict['apiVersion'], '2.0')
self.assertEqual(x.api_version, '2.0')
x = gdata.core.parse_json(VIDEO_EXAMPLE)
self.assert_(isinstance(x._dict, dict))
self.assertEqual(x._dict['apiVersion'], '2.0')
self.assertEqual(x.api_version, '2.0')
x = gdata.core.parse_json(VIDEO_EXAMPLE)
self.check_video_json(x)
def test_write_json(self):
x = gdata.core.Jsonc()
x._dict['apiVersion'] = '2.0'
x.data = {'totalItems': 800}
x.data.items = []
x.data.items.append(gdata.core.Jsonc(view_count=220101))
x.data.items[0]._dict['favoriteCount'] = 201
x.data.items[0].comment_count = 22
x.data.items[0].content = {
'1': 'rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp'}
self.check_video_json(x)
def test_build_using_contructor(self):
x = build_test_object()
self.check_video_json(x)
def test_to_dict(self):
x = build_test_object()
self.assertEqual(
gdata.core._convert_to_object(x),
{'data': {'totalItems': 800, 'items': [
{'content': {
'1': 'rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp'},
'viewCount': 220101, 'commentCount': 22, 'favoriteCount': 201}]},
'apiVersion': '2.0'})
def test_try_json_syntax(self):
x = build_test_object()
self.assertEqual(x.data.items[0].commentCount, 22)
x.data.items[0].commentCount = 33
self.assertEqual(x.data.items[0].commentCount, 33)
self.assertEqual(x.data.items[0].comment_count, 33)
self.assertEqual(x.data.items[0]._dict['commentCount'], 33)
def test_to_string(self):
self.check_video_json(
gdata.core.parse_json(
gdata.core.jsonc_to_string(
gdata.core._convert_to_object(
build_test_object()))))
def test_del_attr(self):
x = build_test_object()
self.assertEqual(x.data.items[0].commentCount, 22)
del x.data.items[0].comment_count
try:
x.data.items[0].commentCount
self.fail('Should not be able to access commentCount after deletion')
except AttributeError:
pass
self.assertEqual(x.data.items[0].favorite_count, 201)
del x.data.items[0].favorite_count
try:
x.data.items[0].favorite_count
self.fail('Should not be able to access favorite_count after deletion')
except AttributeError:
pass
try:
x.data.items[0]._dict['favoriteCount']
self.fail('Should not see [\'favoriteCount\'] after deletion')
except KeyError:
pass
self.assertEqual(x.data.items[0].view_count, 220101)
del x.data.items[0]._dict['viewCount']
try:
x.data.items[0].view_count
self.fail('Should not be able to access view_count after deletion')
except AttributeError:
pass
try:
del x.data.missing
self.fail('Should not delete a missing attribute')
except AttributeError:
pass
def test_del_protected_attribute(self):
x = gdata.core.Jsonc(public='x', _private='y')
self.assertEqual(x.public, 'x')
self.assertEqual(x._private, 'y')
self.assertEqual(x['public'], 'x')
try:
x['_private']
self.fail('Should not be able to getitem with _name')
except KeyError:
pass
del x._private
try:
x._private
self.fail('Should not be able to access deleted member')
except AttributeError:
pass
def test_get_set_del_item(self):
x = build_test_object()
# Check for expected members using different access patterns.
self.assert_(isinstance(x._dict, dict))
self.assert_(isinstance(x['data'], gdata.core.Jsonc))
self.assert_(isinstance(x._dict['data'], gdata.core.Jsonc))
self.assert_(isinstance(x['data']._dict, dict))
self.assert_(isinstance(x._dict['data']._dict, dict))
self.assert_(isinstance(x['apiVersion'], (str, unicode)))
try:
x['api_version']
self.fail('Should not find using Python style name')
except KeyError:
pass
self.assert_(isinstance(x.data['items'], list))
self.assert_(isinstance(x.data['items'][0]._dict['commentCount'],
(int, long)))
self.assert_(isinstance(x['data'].items[0]['favoriteCount'], (int, long)))
self.assertEqual(x['data'].total_items, 800)
self.assertEqual(x['data']['totalItems'], 800)
self.assertEqual(x.data['items'][0]['viewCount'], 220101)
self.assertEqual(x._dict['data'].items[0]._dict['viewCount'],
220101)
self.assertEqual(x['data'].items[0].comment_count, 22)
self.assertEqual(x.data.items[0]['commentCount'], 22)
self.assertEqual(x.data.items[0]['favoriteCount'], 201)
self.assertEqual(x.data.items[0]._dict['favoriteCount'], 201)
self.assertEqual(
x.data.items[0].content['1'],
'rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp')
self.assertEqual(
x.data.items[0]['content']['1'],
'rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp')
self.assertEqual(x.api_version, '2.0')
self.assertEqual(x['apiVersion'], x._dict['apiVersion'])
# Set properties using setitem
x['apiVersion'] = '3.2'
self.assertEqual(x.api_version, '3.2')
x.data['totalItems'] = 500
self.assertEqual(x['data'].total_items, 500)
self.assertEqual(x['data'].items[0].favoriteCount, 201)
try:
del x['data']['favoriteCount']
self.fail('Should not be able to delete missing item')
except KeyError:
pass
del x.data['items'][0]['favoriteCount']
try:
x['data'].items[0].favoriteCount
self.fail('Should not find favoriteCount removed using del item')
except AttributeError:
pass
def suite():
return conf.build_suite([JsoncConversionTest, MemberNameConversionTest,
JsoncObjectTest])
if __name__ == '__main__':
unittest.main()
|