|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2002-2017 "Neo Technology," |
| 5 | +# Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 6 | +# |
| 7 | +# This file is part of Neo4j. |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# you may not use this file except in compliance with the License. |
| 11 | +# You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | + |
| 21 | + |
| 22 | +from unittest import TestCase |
| 23 | +from uuid import uuid4 |
| 24 | + |
| 25 | +from neo4j.v1.api import coerce_parameters |
| 26 | + |
| 27 | + |
| 28 | +class ParameterTypeTestCase(TestCase): |
| 29 | + def test_should_allow_none(self): |
| 30 | + self.assertIsNone(coerce_parameters(None)) |
| 31 | + |
| 32 | + def test_should_allow_boolean(self): |
| 33 | + self.assertTrue(coerce_parameters(True)) |
| 34 | + self.assertFalse(coerce_parameters(False)) |
| 35 | + |
| 36 | + def test_should_allow_integer(self): |
| 37 | + self.assertEqual(coerce_parameters(0), 0) |
| 38 | + self.assertEqual(coerce_parameters(0x7F), 0x7F) |
| 39 | + self.assertEqual(coerce_parameters(0x7FFF), 0x7FFF) |
| 40 | + self.assertEqual(coerce_parameters(0x7FFFFFFF), 0x7FFFFFFF) |
| 41 | + self.assertEqual(coerce_parameters(0x7FFFFFFFFFFFFFFF), 0x7FFFFFFFFFFFFFFF) |
| 42 | + |
| 43 | + def test_should_disallow_oversized_integer(self): |
| 44 | + with self.assertRaises(ValueError): |
| 45 | + coerce_parameters(0x10000000000000000) |
| 46 | + with self.assertRaises(ValueError): |
| 47 | + coerce_parameters(-0x10000000000000000) |
| 48 | + |
| 49 | + def test_should_allow_float(self): |
| 50 | + self.assertEqual(coerce_parameters(0.0), 0.0) |
| 51 | + self.assertEqual(coerce_parameters(3.1415926), 3.1415926) |
| 52 | + |
| 53 | + def test_should_allow_string(self): |
| 54 | + self.assertEqual(coerce_parameters(u""), u"") |
| 55 | + self.assertEqual(coerce_parameters(u"hello, world"), u"hello, world") |
| 56 | + |
| 57 | + def test_should_allow_bytes(self): |
| 58 | + self.assertEqual(coerce_parameters(bytearray()), bytearray()) |
| 59 | + self.assertEqual(coerce_parameters(bytearray([1, 2, 3])), bytearray([1, 2, 3])) |
| 60 | + |
| 61 | + def test_should_allow_list(self): |
| 62 | + self.assertEqual(coerce_parameters([]), []) |
| 63 | + self.assertEqual(coerce_parameters([1, 2, 3]), [1, 2, 3]) |
| 64 | + |
| 65 | + def test_should_allow_dict(self): |
| 66 | + self.assertEqual(coerce_parameters({}), {}) |
| 67 | + self.assertEqual(coerce_parameters({u"one": 1, u"two": 1, u"three": 1}), {u"one": 1, u"two": 1, u"three": 1}) |
| 68 | + self.assertEqual(coerce_parameters( |
| 69 | + {u"list": [1, 2, 3, [4, 5, 6]], u"dict": {u"a": 1, u"b": 2}}), |
| 70 | + {u"list": [1, 2, 3, [4, 5, 6]], u"dict": {u"a": 1, u"b": 2}}) |
| 71 | + |
| 72 | + def test_should_disallow_object(self): |
| 73 | + with self.assertRaises(TypeError): |
| 74 | + coerce_parameters(object()) |
| 75 | + with self.assertRaises(TypeError): |
| 76 | + coerce_parameters(uuid4()) |
0 commit comments