Skip to content

Commit

Permalink
Added automatic conversion of UUIDs to strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
SStorm committed Jul 27, 2023
1 parent 05159b8 commit 46b1eb5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
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
==========

- Added automatic conversion of UUIDs to strings.

2023/07/17 0.33.0
=================
Expand Down
4 changes: 4 additions & 0 deletions 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 @@ -88,6 +90,8 @@ class CrateJsonEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, Decimal):
return str(o)
if isinstance(o, UUID):
return str(o)
if isinstance(o, datetime):
if o.tzinfo is not None:
delta = o - self.epoch_aware
Expand Down
16 changes: 16 additions & 0 deletions src/crate/client/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@
from threading import Thread, Event
from decimal import Decimal
import datetime as dt
from uuid import UUID

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 +291,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):
Expand Down

0 comments on commit 46b1eb5

Please sign in to comment.