From c01778363627f4350ebbcc1be8c87d2fbca0fd0f Mon Sep 17 00:00:00 2001 From: Romanas Sonkinas Date: Thu, 27 Jul 2023 15:28:04 +0100 Subject: [PATCH] Added automatic conversion of UUIDs to strings. --- .gitignore | 1 + CHANGES.txt | 1 + src/crate/client/http.py | 4 +++- src/crate/client/test_http.py | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3b32ddeb..be2a312f 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ htmlcov/ out/ parts/ tmp/ +env/ diff --git a/CHANGES.txt b/CHANGES.txt index d04a31a2..19b3db02 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,6 +5,7 @@ Changes for crate Unreleased ========== +- Properly handle Python-native UUID types in SQL parameters 2023/07/17 0.33.0 ================= diff --git a/src/crate/client/http.py b/src/crate/client/http.py index d4522612..76df121c 100644 --- a/src/crate/client/http.py +++ b/src/crate/client/http.py @@ -35,6 +35,8 @@ from time import time from datetime import datetime, date, timezone from decimal import Decimal +from uuid import UUID + from urllib3 import connection_from_url from urllib3.connection import HTTPConnection from urllib3.exceptions import ( @@ -86,7 +88,7 @@ class CrateJsonEncoder(json.JSONEncoder): epoch_naive = datetime(1970, 1, 1) def default(self, o): - if isinstance(o, Decimal): + if isinstance(o, (Decimal, UUID)): return str(o) if isinstance(o, datetime): if o.tzinfo is not None: diff --git a/src/crate/client/test_http.py b/src/crate/client/test_http.py index 0f7afa35..de287834 100644 --- a/src/crate/client/test_http.py +++ b/src/crate/client/test_http.py @@ -35,9 +35,12 @@ from threading import Thread, Event from decimal import Decimal import datetime as dt + import urllib3.exceptions from base64 import b64decode from urllib.parse import urlparse, parse_qs + +import uuid from setuptools.ssl_support import find_ca_bundle from .http import Client, CrateJsonEncoder, _get_socket_opts, _remove_certs_for_non_https @@ -287,6 +290,18 @@ def test_socket_options_contain_keepalive(self): ) client.close() + @patch(REQUEST, autospec=True) + def test_uuid_serialization(self, request): + client = Client(servers="localhost:4200") + request.return_value = fake_response(200) + + uid = uuid.uuid4() + client.sql('insert into my_table (str_col) values (?)', (uid,)) + + data = json.loads(request.call_args[1]['data']) + self.assertEqual(data['args'], [str(uid)]) + client.close() + @patch(REQUEST, fail_sometimes) class ThreadSafeHttpClientTest(TestCase):