github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/internal/gcp/json_value_test.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 """Unit tests for the json_value module.""" 19 20 # pytype: skip-file 21 22 import unittest 23 24 from apache_beam.internal.gcp.json_value import from_json_value 25 from apache_beam.internal.gcp.json_value import to_json_value 26 from apache_beam.options.value_provider import RuntimeValueProvider 27 from apache_beam.options.value_provider import StaticValueProvider 28 29 # Protect against environments where apitools library is not available. 30 # pylint: disable=wrong-import-order, wrong-import-position 31 try: 32 from apitools.base.py.extra_types import JsonValue 33 except ImportError: 34 JsonValue = None 35 # pylint: enable=wrong-import-order, wrong-import-position 36 37 38 @unittest.skipIf(JsonValue is None, 'GCP dependencies are not installed') 39 class JsonValueTest(unittest.TestCase): 40 def test_string_to(self): 41 self.assertEqual(JsonValue(string_value='abc'), to_json_value('abc')) 42 43 def test_bytes_to(self): 44 self.assertEqual(JsonValue(string_value='abc'), to_json_value(b'abc')) 45 46 def test_true_to(self): 47 self.assertEqual(JsonValue(boolean_value=True), to_json_value(True)) 48 49 def test_false_to(self): 50 self.assertEqual(JsonValue(boolean_value=False), to_json_value(False)) 51 52 def test_int_to(self): 53 self.assertEqual(JsonValue(integer_value=14), to_json_value(14)) 54 55 def test_float_to(self): 56 self.assertEqual(JsonValue(double_value=2.75), to_json_value(2.75)) 57 58 def test_static_value_provider_to(self): 59 svp = StaticValueProvider(str, 'abc') 60 self.assertEqual(JsonValue(string_value=svp.value), to_json_value(svp)) 61 62 def test_runtime_value_provider_to(self): 63 RuntimeValueProvider.set_runtime_options(None) 64 rvp = RuntimeValueProvider('arg', 123, int) 65 self.assertEqual(JsonValue(is_null=True), to_json_value(rvp)) 66 # Reset runtime options to avoid side-effects in other tests. 67 RuntimeValueProvider.set_runtime_options(None) 68 69 def test_none_to(self): 70 self.assertEqual(JsonValue(is_null=True), to_json_value(None)) 71 72 def test_string_from(self): 73 self.assertEqual('WXYZ', from_json_value(to_json_value('WXYZ'))) 74 75 def test_true_from(self): 76 self.assertEqual(True, from_json_value(to_json_value(True))) 77 78 def test_false_from(self): 79 self.assertEqual(False, from_json_value(to_json_value(False))) 80 81 def test_int_from(self): 82 self.assertEqual(-27, from_json_value(to_json_value(-27))) 83 84 def test_float_from(self): 85 self.assertEqual(4.5, from_json_value(to_json_value(4.5))) 86 87 def test_with_type(self): 88 rt = from_json_value(to_json_value('abcd', with_type=True)) 89 self.assertEqual('http://schema.org/Text', rt['@type']) 90 self.assertEqual('abcd', rt['value']) 91 92 def test_none_from(self): 93 self.assertIsNone(from_json_value(to_json_value(None))) 94 95 def test_large_integer(self): 96 num = 1 << 35 97 self.assertEqual(num, from_json_value(to_json_value(num))) 98 99 def test_long_value(self): 100 num = 1 << 63 - 1 101 self.assertEqual(num, from_json_value(to_json_value(num))) 102 103 def test_too_long_value(self): 104 with self.assertRaises(TypeError): 105 to_json_value(1 << 64) 106 107 108 if __name__ == '__main__': 109 unittest.main()