|
16 | 16 | # limitations under the License. |
17 | 17 |
|
18 | 18 |
|
| 19 | +from datetime import timedelta |
| 20 | + |
| 21 | +import pytz |
| 22 | + |
19 | 23 | from neo4j import Query |
| 24 | +from neo4j.spatial import ( |
| 25 | + CartesianPoint, |
| 26 | + WGS84Point, |
| 27 | +) |
| 28 | +from neo4j.time import ( |
| 29 | + Date, |
| 30 | + DateTime, |
| 31 | + Duration, |
| 32 | + Time, |
| 33 | +) |
20 | 34 |
|
21 | 35 |
|
22 | 36 | def to_cypher_and_params(data): |
@@ -54,24 +68,81 @@ def to_query_and_params(data): |
54 | 68 | def to_param(m): |
55 | 69 | """ Converts testkit parameter format to driver (python) parameter |
56 | 70 | """ |
57 | | - value = m["data"]["value"] |
| 71 | + data = m["data"] |
58 | 72 | name = m["name"] |
59 | 73 | if name == "CypherNull": |
| 74 | + if data["value"] is not None: |
| 75 | + raise ValueError("CypherNull should be None") |
60 | 76 | return None |
61 | 77 | if name == "CypherString": |
62 | | - return str(value) |
| 78 | + return str(data["value"]) |
63 | 79 | if name == "CypherBool": |
64 | | - return bool(value) |
| 80 | + return bool(data["value"]) |
65 | 81 | if name == "CypherInt": |
66 | | - return int(value) |
| 82 | + return int(data["value"]) |
67 | 83 | if name == "CypherFloat": |
68 | | - return float(value) |
| 84 | + return float(data["value"]) |
69 | 85 | if name == "CypherString": |
70 | | - return str(value) |
| 86 | + return str(data["value"]) |
71 | 87 | if name == "CypherBytes": |
72 | | - return bytearray([int(byte, 16) for byte in value.split()]) |
| 88 | + return bytearray([int(byte, 16) for byte in data["value"].split()]) |
73 | 89 | if name == "CypherList": |
74 | | - return [to_param(v) for v in value] |
| 90 | + return [to_param(v) for v in data["value"]] |
75 | 91 | if name == "CypherMap": |
76 | | - return {k: to_param(value[k]) for k in value} |
77 | | - raise Exception("Unknown param type " + name) |
| 92 | + return {k: to_param(data["value"][k]) for k in data["value"]} |
| 93 | + if name == "CypherPoint": |
| 94 | + coords = [data["x"], data["y"]] |
| 95 | + if data.get("z") is not None: |
| 96 | + coords.append(data["z"]) |
| 97 | + if data["system"] == "cartesian": |
| 98 | + return CartesianPoint(coords) |
| 99 | + if data["system"] == "wgs84": |
| 100 | + return WGS84Point(coords) |
| 101 | + raise ValueError("Unknown point system: {}".format(data["system"])) |
| 102 | + if name == "CypherDate": |
| 103 | + return Date(data["year"], data["month"], data["day"]) |
| 104 | + if name == "CypherTime": |
| 105 | + tz = None |
| 106 | + utc_offset_s = data.get("utc_offset_s") |
| 107 | + if utc_offset_s is not None: |
| 108 | + utc_offset_m = utc_offset_s // 60 |
| 109 | + if utc_offset_m * 60 != utc_offset_s: |
| 110 | + raise ValueError("the used timezone library only supports " |
| 111 | + "UTC offsets by minutes") |
| 112 | + tz = pytz.FixedOffset(utc_offset_m) |
| 113 | + return Time(data["hour"], data["minute"], data["second"], |
| 114 | + data["nanosecond"], tzinfo=tz) |
| 115 | + if name == "CypherDateTime": |
| 116 | + datetime = DateTime( |
| 117 | + data["year"], data["month"], data["day"], |
| 118 | + data["hour"], data["minute"], data["second"], data["nanosecond"] |
| 119 | + ) |
| 120 | + utc_offset_s = data["utc_offset_s"] |
| 121 | + timezone_id = data["timezone_id"] |
| 122 | + if timezone_id is not None: |
| 123 | + utc_offset = timedelta(seconds=utc_offset_s) |
| 124 | + tz = pytz.timezone(timezone_id) |
| 125 | + localized_datetime = tz.localize(datetime, is_dst=False) |
| 126 | + if localized_datetime.utcoffset() == utc_offset: |
| 127 | + return localized_datetime |
| 128 | + localized_datetime = tz.localize(datetime, is_dst=True) |
| 129 | + if localized_datetime.utcoffset() == utc_offset: |
| 130 | + return localized_datetime |
| 131 | + raise ValueError( |
| 132 | + "cannot localize datetime %s to timezone %s with UTC " |
| 133 | + "offset %s" % (datetime, timezone_id, utc_offset) |
| 134 | + ) |
| 135 | + elif utc_offset_s is not None: |
| 136 | + utc_offset_m = utc_offset_s // 60 |
| 137 | + if utc_offset_m * 60 != utc_offset_s: |
| 138 | + raise ValueError("the used timezone library only supports " |
| 139 | + "UTC offsets by minutes") |
| 140 | + tz = pytz.FixedOffset(utc_offset_m) |
| 141 | + return tz.localize(datetime) |
| 142 | + return datetime |
| 143 | + if name == "CypherDuration": |
| 144 | + return Duration( |
| 145 | + months=data["months"], days=data["days"], |
| 146 | + seconds=data["seconds"], nanoseconds=data["nanoseconds"] |
| 147 | + ) |
| 148 | + raise ValueError("Unknown param type " + name) |
0 commit comments