|
| 1 | +# -*- coding: utf-8; -*- |
| 2 | +# |
| 3 | +# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor |
| 4 | +# license agreements. See the NOTICE file distributed with this work for |
| 5 | +# additional information regarding copyright ownership. Crate licenses |
| 6 | +# this file to you under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. You may |
| 8 | +# obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | +# License for the specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +# However, if you have executed another commercial license agreement |
| 19 | +# with Crate these terms will supersede the license and you may use the |
| 20 | +# software solely pursuant to the terms of the relevant commercial agreement. |
| 21 | + |
| 22 | +""" |
| 23 | +Tests for serializing data, typically python objects into CrateDB-sql compatible structures. |
| 24 | +""" |
| 25 | +import datetime |
| 26 | + |
| 27 | +import uuid |
| 28 | +from decimal import Decimal |
| 29 | +from unittest.mock import patch, MagicMock |
| 30 | +import datetime as dt |
| 31 | + |
| 32 | +from crate.client.http import json_dumps, Client |
| 33 | +from tests.conftest import REQUEST_PATH, fake_response |
| 34 | + |
| 35 | + |
| 36 | +def test_data_is_serialized(): |
| 37 | + """ |
| 38 | + Verify that when a request is issued, `json_dumps` is called with the right parameters |
| 39 | + and that a requests gets the output from json_dumps, this verifies the entire |
| 40 | + serialization call chain, so in the following tests we can just test `json_dumps` and ignore |
| 41 | + `Client` altogether. |
| 42 | + """ |
| 43 | + mock = MagicMock(spec=bytes) |
| 44 | + |
| 45 | + with patch('crate.client.http.json_dumps', return_value=mock) as json_dumps: |
| 46 | + with patch(REQUEST_PATH, return_value=fake_response(200)) as request: |
| 47 | + client = Client(servers="localhost:4200") |
| 48 | + client.sql( |
| 49 | + "insert into t (a, b) values (?, ?)", |
| 50 | + (datetime.datetime(2025, 10, 23, 11, ), |
| 51 | + "ss" |
| 52 | + ) |
| 53 | + ) |
| 54 | + |
| 55 | + # Verify json_dumps is called with the right parameters. |
| 56 | + json_dumps.assert_called_once_with( |
| 57 | + { |
| 58 | + 'stmt': 'insert into t (a, b) values (?, ?)', |
| 59 | + 'args': (datetime.datetime(2025, 10, 23, 11, 0), 'ss') |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + # Verify that the output of json_dumps is used as call argument for a request. |
| 64 | + assert request.call_args[1]['data'] is mock |
| 65 | + |
| 66 | + |
| 67 | +def test_naive_datetime_serialization(): |
| 68 | + """ |
| 69 | + Verify that a `datetime.datetime` can be serialized. |
| 70 | + """ |
| 71 | + data = dt.datetime(2015, 2, 28, 7, 31, 40) |
| 72 | + result = json_dumps(data) |
| 73 | + assert isinstance(result, bytes) |
| 74 | + assert result == b'1425108700000' |
| 75 | + |
| 76 | + |
| 77 | +def test_aware_datetime_serialization(): |
| 78 | + """ |
| 79 | + Verify that a `datetime` that is tz aware type can be serialized. |
| 80 | + """ |
| 81 | + data = dt.datetime.fromisoformat("2023-06-26T09:24:00.123+02:00") |
| 82 | + result = json_dumps(data) |
| 83 | + assert isinstance(result, bytes) |
| 84 | + assert result == b"1687764240123" |
| 85 | + |
| 86 | + |
| 87 | +def test_decimal_serialization(): |
| 88 | + """ |
| 89 | + Verify that a `Decimal` type can be serialized. |
| 90 | + """ |
| 91 | + |
| 92 | + data = Decimal(0.12) |
| 93 | + result = json_dumps(data) |
| 94 | + assert isinstance(result, bytes) |
| 95 | + |
| 96 | + # Question: Is this deterministic in every Python release? |
| 97 | + assert result == b'"0.11999999999999999555910790149937383830547332763671875"' |
| 98 | + |
| 99 | + |
| 100 | +def test_date_serialization(): |
| 101 | + """ |
| 102 | + Verify that a `datetime.date` can be serialized. |
| 103 | + """ |
| 104 | + data = dt.date(2016, 4, 21) |
| 105 | + result = json_dumps(data) |
| 106 | + assert result == b'1461196800000' |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +def test_uuid_serialization(): |
| 111 | + """ |
| 112 | + Verify that a `uuid.UUID` can be serialized. We do not care about specific uuid versions, |
| 113 | + just the object that is re-used across all versions by the uuid module. |
| 114 | + """ |
| 115 | + data = uuid.UUID(bytes=(50583033507982468033520929066863110751).to_bytes(16), version=4) |
| 116 | + result = json_dumps(data) |
| 117 | + assert result == b'"260df019-a183-431f-ad46-115ccdf12a5f"' |
| 118 | + |
0 commit comments