/usr/lib/python2.7/dist-packages/protorpc/protojson_test.py is in python-protorpc-standalone 0.9.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | #!/usr/bin/env python
#
# Copyright 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.
#
"""Tests for protorpc.protojson."""
__author__ = 'rafek@google.com (Rafe Kaplan)'
import __builtin__
import base64
import datetime
import sys
import unittest
from protorpc import message_types
from protorpc import messages
from protorpc import protojson
from protorpc import test_util
import simplejson
class MyMessage(messages.Message):
"""Test message containing various types."""
class Color(messages.Enum):
RED = 1
GREEN = 2
BLUE = 3
class Nested(messages.Message):
nested_value = messages.StringField(1)
a_string = messages.StringField(2)
an_integer = messages.IntegerField(3)
a_float = messages.FloatField(4)
a_boolean = messages.BooleanField(5)
an_enum = messages.EnumField(Color, 6)
a_nested = messages.MessageField(Nested, 7)
a_repeated = messages.IntegerField(8, repeated=True)
a_repeated_float = messages.FloatField(9, repeated=True)
a_datetime = message_types.DateTimeField(10)
a_repeated_datetime = message_types.DateTimeField(11, repeated=True)
class ModuleInterfaceTest(test_util.ModuleInterfaceTest,
test_util.TestCase):
MODULE = protojson
# TODO(rafek): Convert this test to the compliance test in test_util.
class ProtojsonTest(test_util.TestCase,
test_util.ProtoConformanceTestBase):
"""Test JSON encoding and decoding."""
PROTOLIB = protojson
def CompareEncoded(self, expected_encoded, actual_encoded):
"""JSON encoding will be laundered to remove string differences."""
self.assertEquals(simplejson.loads(expected_encoded),
simplejson.loads(actual_encoded))
encoded_empty_message = '{}'
encoded_partial = """{
"double_value": 1.23,
"int64_value": -100000000000,
"int32_value": 1020,
"string_value": "a string",
"enum_value": "VAL2"
}
"""
encoded_full = """{
"double_value": 1.23,
"float_value": -2.5,
"int64_value": -100000000000,
"uint64_value": 102020202020,
"int32_value": 1020,
"bool_value": true,
"string_value": "a string\u044f",
"bytes_value": "YSBieXRlc//+",
"enum_value": "VAL2"
}
"""
encoded_repeated = """{
"double_value": [1.23, 2.3],
"float_value": [-2.5, 0.5],
"int64_value": [-100000000000, 20],
"uint64_value": [102020202020, 10],
"int32_value": [1020, 718],
"bool_value": [true, false],
"string_value": ["a string\u044f", "another string"],
"bytes_value": ["YSBieXRlc//+", "YW5vdGhlciBieXRlcw=="],
"enum_value": ["VAL2", "VAL1"]
}
"""
encoded_nested = """{
"nested": {
"a_value": "a string"
}
}
"""
encoded_repeated_nested = """{
"repeated_nested": [{"a_value": "a string"},
{"a_value": "another string"}]
}
"""
unexpected_tag_message = '{"unknown": "value"}'
encoded_default_assigned = '{"a_value": "a default"}'
encoded_nested_empty = '{"nested": {}}'
encoded_repeated_nested_empty = '{"repeated_nested": [{}, {}]}'
encoded_extend_message = '{"int64_value": [400, 50, 6000]}'
encoded_string_types = '{"string_value": "Latin"}'
encoded_invalid_enum = '{"enum_value": "undefined"}'
def testConvertIntegerToFloat(self):
"""Test that integers passed in to float fields are converted.
This is necessary because JSON outputs integers for numbers with 0 decimals.
"""
message = protojson.decode_message(MyMessage, '{"a_float": 10}')
self.assertTrue(isinstance(message.a_float, float))
self.assertEquals(10.0, message.a_float)
def testConvertStringToNumbers(self):
"""Test that strings passed to integer fields are converted."""
message = protojson.decode_message(MyMessage,
"""{"an_integer": "10",
"a_float": "3.5",
"a_repeated": ["1", "2"],
"a_repeated_float": ["1.5", "2", 10]
}""")
self.assertEquals(MyMessage(an_integer=10,
a_float=3.5,
a_repeated=[1, 2],
a_repeated_float=[1.5, 2.0, 10.0]),
message)
def testWrongTypeAssignment(self):
"""Test when wrong type is assigned to a field."""
self.assertRaises(messages.ValidationError,
protojson.decode_message,
MyMessage, '{"a_string": 10}')
self.assertRaises(messages.ValidationError,
protojson.decode_message,
MyMessage, '{"an_integer": 10.2}')
self.assertRaises(messages.ValidationError,
protojson.decode_message,
MyMessage, '{"an_integer": "10.2"}')
def testNumericEnumeration(self):
"""Test that numbers work for enum values."""
message = protojson.decode_message(MyMessage, '{"an_enum": 2}')
expected_message = MyMessage()
expected_message.an_enum = MyMessage.Color.GREEN
self.assertEquals(expected_message, message)
def testNullValues(self):
"""Test that null values overwrite existing values."""
self.assertEquals(MyMessage(),
protojson.decode_message(MyMessage,
('{"an_integer": null,'
' "a_nested": null'
'}')))
def testEmptyList(self):
"""Test that empty lists are ignored."""
self.assertEquals(MyMessage(),
protojson.decode_message(MyMessage,
'{"a_repeated": []}'))
def testNotJSON(self):
"""Test error when string is not valid JSON."""
self.assertRaises(ValueError,
protojson.decode_message, MyMessage, '{this is not json}')
def testDoNotEncodeStrangeObjects(self):
"""Test trying to encode a strange object.
The main purpose of this test is to complete coverage. It ensures that
the default behavior of the JSON encoder is preserved when someone tries to
serialized an unexpected type.
"""
class BogusObject(object):
def check_initialized(self):
pass
self.assertRaises(TypeError,
protojson.encode_message,
BogusObject())
def testMergeEmptyString(self):
"""Test merging the empty or space only string."""
message = protojson.decode_message(test_util.OptionalMessage, '')
self.assertEquals(test_util.OptionalMessage(), message)
message = protojson.decode_message(test_util.OptionalMessage, ' ')
self.assertEquals(test_util.OptionalMessage(), message)
def testProtojsonUnrecognizedFieldName(self):
"""Test that unrecognized fields are saved and can be accessed."""
decoded = protojson.decode_message(MyMessage,
('{"an_integer": 1, "unknown_val": 2}'))
self.assertEquals(decoded.an_integer, 1)
self.assertEquals(1, len(decoded.all_unrecognized_fields()))
self.assertEquals('unknown_val', decoded.all_unrecognized_fields()[0])
self.assertEquals((2, messages.Variant.INT64),
decoded.get_unrecognized_field_info('unknown_val'))
def testProtojsonUnrecognizedFieldNumber(self):
"""Test that unrecognized fields are saved and can be accessed."""
decoded = protojson.decode_message(
MyMessage,
'{"an_integer": 1, "1001": "unknown", "-123": "negative", '
'"456_mixed": 2}')
self.assertEquals(decoded.an_integer, 1)
self.assertEquals(3, len(decoded.all_unrecognized_fields()))
self.assertIn(1001, decoded.all_unrecognized_fields())
self.assertEquals(('unknown', messages.Variant.STRING),
decoded.get_unrecognized_field_info(1001))
self.assertIn('-123', decoded.all_unrecognized_fields())
self.assertEquals(('negative', messages.Variant.STRING),
decoded.get_unrecognized_field_info('-123'))
self.assertIn('456_mixed', decoded.all_unrecognized_fields())
self.assertEquals((2, messages.Variant.INT64),
decoded.get_unrecognized_field_info('456_mixed'))
def testProtojsonUnrecognizedNull(self):
"""Test that unrecognized fields that are None are skipped."""
decoded = protojson.decode_message(
MyMessage,
'{"an_integer": 1, "unrecognized_null": null}')
self.assertEquals(decoded.an_integer, 1)
self.assertEquals(decoded.all_unrecognized_fields(), [])
def testUnrecognizedFieldVariants(self):
"""Test that unrecognized fields are mapped to the right variants."""
for encoded, expected_variant in (
('{"an_integer": 1, "unknown_val": 2}', messages.Variant.INT64),
('{"an_integer": 1, "unknown_val": 2.0}', messages.Variant.DOUBLE),
('{"an_integer": 1, "unknown_val": "string value"}',
messages.Variant.STRING),
('{"an_integer": 1, "unknown_val": [1, 2, 3]}', messages.Variant.INT64),
('{"an_integer": 1, "unknown_val": [1, 2.0, 3]}',
messages.Variant.DOUBLE),
('{"an_integer": 1, "unknown_val": [1, "foo", 3]}',
messages.Variant.STRING),
('{"an_integer": 1, "unknown_val": true}', messages.Variant.BOOL)):
decoded = protojson.decode_message(MyMessage, encoded)
self.assertEquals(decoded.an_integer, 1)
self.assertEquals(1, len(decoded.all_unrecognized_fields()))
self.assertEquals('unknown_val', decoded.all_unrecognized_fields()[0])
_, decoded_variant = decoded.get_unrecognized_field_info('unknown_val')
self.assertEquals(expected_variant, decoded_variant)
def testDecodeDateTime(self):
for datetime_string, datetime_vals in (
('2012-09-30T15:31:50.262', (2012, 9, 30, 15, 31, 50, 262000)),
('2012-09-30T15:31:50', (2012, 9, 30, 15, 31, 50, 0))):
message = protojson.decode_message(
MyMessage, '{"a_datetime": "%s"}' % datetime_string)
expected_message = MyMessage(
a_datetime=datetime.datetime(*datetime_vals))
self.assertEquals(expected_message, message)
def testDecodeInvalidDateTime(self):
self.assertRaises(messages.DecodeError, protojson.decode_message,
MyMessage, '{"a_datetime": "invalid"}')
def testEncodeDateTime(self):
for datetime_string, datetime_vals in (
('2012-09-30T15:31:50.262000', (2012, 9, 30, 15, 31, 50, 262000)),
('2012-09-30T15:31:50.262123', (2012, 9, 30, 15, 31, 50, 262123)),
('2012-09-30T15:31:50', (2012, 9, 30, 15, 31, 50, 0))):
decoded_message = protojson.encode_message(
MyMessage(a_datetime=datetime.datetime(*datetime_vals)))
expected_decoding = '{"a_datetime": "%s"}' % datetime_string
self.CompareEncoded(expected_decoding, decoded_message)
def testDecodeRepeatedDateTime(self):
message = protojson.decode_message(
MyMessage,
'{"a_repeated_datetime": ["2012-09-30T15:31:50.262", '
'"2010-01-21T09:52:00", "2000-01-01T01:00:59.999999"]}')
expected_message = MyMessage(
a_repeated_datetime=[
datetime.datetime(2012, 9, 30, 15, 31, 50, 262000),
datetime.datetime(2010, 1, 21, 9, 52),
datetime.datetime(2000, 1, 1, 1, 0, 59, 999999)])
self.assertEquals(expected_message, message)
def testDecodeBadBase64BytesField(self):
"""Test decoding improperly encoded base64 bytes value."""
self.assertRaisesWithRegexpMatch(
messages.DecodeError,
'Base64 decoding error: Incorrect padding',
protojson.decode_message,
test_util.OptionalMessage,
'{"bytes_value": "abcdefghijklmnopq"}')
class CustomProtoJson(protojson.ProtoJson):
def encode_field(self, field, value):
return '{encoded}' + value
def decode_field(self, field, value):
return '{decoded}' + value
class CustomProtoJsonTest(test_util.TestCase):
"""Tests for serialization overriding functionality."""
def setUp(self):
self.protojson = CustomProtoJson()
def testEncode(self):
self.assertEqual('{"a_string": "{encoded}xyz"}',
self.protojson.encode_message(MyMessage(a_string='xyz')))
def testDecode(self):
self.assertEqual(
MyMessage(a_string='{decoded}xyz'),
self.protojson.decode_message(MyMessage, '{"a_string": "xyz"}'))
def testDefault(self):
self.assertTrue(protojson.ProtoJson.get_default(),
protojson.ProtoJson.get_default())
instance = CustomProtoJson()
protojson.ProtoJson.set_default(instance)
self.assertTrue(instance is protojson.ProtoJson.get_default())
class InvalidJsonModule(object):
pass
class ValidJsonModule(object):
class JSONEncoder(object):
pass
class TestJsonDependencyLoading(test_util.TestCase):
"""Test loading various implementations of json."""
def get_import(self):
"""Get __import__ method.
Returns:
The current __import__ method.
"""
if isinstance(__builtins__, dict):
return __builtins__['__import__']
else:
return __builtins__.__import__
def set_import(self, new_import):
"""Set __import__ method.
Args:
new_import: Function to replace __import__.
"""
if isinstance(__builtins__, dict):
__builtins__['__import__'] = new_import
else:
__builtins__.__import__ = new_import
def setUp(self):
"""Save original import function."""
self.simplejson = sys.modules.pop('simplejson', None)
self.json = sys.modules.pop('json', None)
self.original_import = self.get_import()
def block_all_jsons(name, *args, **kwargs):
if 'json' in name:
if name in sys.modules:
module = sys.modules[name]
module.name = name
return module
raise ImportError('Unable to find %s' % name)
else:
return self.original_import(name, *args, **kwargs)
self.set_import(block_all_jsons)
def tearDown(self):
"""Restore original import functions and any loaded modules."""
def reset_module(name, module):
if module:
sys.modules[name] = module
else:
sys.modules.pop(name, None)
reset_module('simplejson', self.simplejson)
reset_module('json', self.json)
reload(protojson)
def testLoadProtojsonWithValidJsonModule(self):
"""Test loading protojson module with a valid json dependency."""
sys.modules['json'] = ValidJsonModule
# This will cause protojson to reload with the default json module
# instead of simplejson.
reload(protojson)
self.assertEquals('json', protojson.json.name)
def testLoadProtojsonWithSimplejsonModule(self):
"""Test loading protojson module with simplejson dependency."""
sys.modules['simplejson'] = ValidJsonModule
# This will cause protojson to reload with the default json module
# instead of simplejson.
reload(protojson)
self.assertEquals('simplejson', protojson.json.name)
def testLoadProtojsonWithInvalidJsonModule(self):
"""Loading protojson module with an invalid json defaults to simplejson."""
sys.modules['json'] = InvalidJsonModule
sys.modules['simplejson'] = ValidJsonModule
# Ignore bad module and default back to simplejson.
reload(protojson)
self.assertEquals('simplejson', protojson.json.name)
def testLoadProtojsonWithInvalidJsonModuleAndNoSimplejson(self):
"""Loading protojson module with invalid json and no simplejson."""
sys.modules['json'] = InvalidJsonModule
# Bad module without simplejson back raises errors.
self.assertRaisesWithRegexpMatch(
ImportError,
'json library "json" is not compatible with ProtoRPC',
reload,
protojson)
def testLoadProtojsonWithNoJsonModules(self):
"""Loading protojson module with invalid json and no simplejson."""
# No json modules raise the first exception.
self.assertRaisesWithRegexpMatch(
ImportError,
'Unable to find json',
reload,
protojson)
if __name__ == '__main__':
unittest.main()
|