Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added automatic conversion of UUIDs to strings. #567

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ htmlcov/
out/
parts/
tmp/
env/
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changes for crate
Unreleased
==========

- Properly handle Python-native UUID types in SQL parameters

2023/07/17 0.33.0
=================
Expand Down
4 changes: 3 additions & 1 deletion src/crate/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions src/crate/client/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,))
matriv marked this conversation as resolved.
Show resolved Hide resolved

data = json.loads(request.call_args[1]['data'])
self.assertEqual(data['args'], [str(uid)])
client.close()


@patch(REQUEST, fail_sometimes)
class ThreadSafeHttpClientTest(TestCase):
Expand Down