|
| 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 | +from __future__ import absolute_import |
| 23 | + |
| 24 | +import re |
| 25 | +import sys |
| 26 | +from unittest import TestCase |
| 27 | +from unittest.mock import MagicMock, patch |
| 28 | + |
| 29 | +import pytest |
| 30 | +import sqlalchemy as sa |
| 31 | +from sqlalchemy.orm import Session |
| 32 | +from sqlalchemy.sql import select |
| 33 | + |
| 34 | +from sqlalchemy_cratedb import SA_VERSION, SA_1_4 |
| 35 | +from sqlalchemy_cratedb.type import FloatVector |
| 36 | + |
| 37 | +from crate.client.cursor import Cursor |
| 38 | + |
| 39 | +from sqlalchemy_cratedb.type.vector import from_db, to_db |
| 40 | + |
| 41 | +fake_cursor = MagicMock(name="fake_cursor") |
| 42 | +FakeCursor = MagicMock(name="FakeCursor", spec=Cursor) |
| 43 | +FakeCursor.return_value = fake_cursor |
| 44 | + |
| 45 | + |
| 46 | +if SA_VERSION < SA_1_4: |
| 47 | + pytest.skip(reason="The FloatVector type is not supported on SQLAlchemy 1.3 and earlier", allow_module_level=True) |
| 48 | + |
| 49 | + |
| 50 | +@patch("crate.client.connection.Cursor", FakeCursor) |
| 51 | +class SqlAlchemyVectorTypeTest(TestCase): |
| 52 | + """ |
| 53 | + Verify compilation of SQL statements where the schema includes the `FloatVector` type. |
| 54 | + """ |
| 55 | + def setUp(self): |
| 56 | + self.engine = sa.create_engine("crate://") |
| 57 | + metadata = sa.MetaData() |
| 58 | + self.table = sa.Table( |
| 59 | + "testdrive", |
| 60 | + metadata, |
| 61 | + sa.Column("name", sa.String), |
| 62 | + sa.Column("data", FloatVector(3)), |
| 63 | + ) |
| 64 | + self.session = Session(bind=self.engine) |
| 65 | + |
| 66 | + def assertSQL(self, expected_str, actual_expr): |
| 67 | + self.assertEqual(expected_str, str(actual_expr).replace('\n', '')) |
| 68 | + |
| 69 | + def test_create_invoke(self): |
| 70 | + self.table.create(self.engine) |
| 71 | + fake_cursor.execute.assert_called_with( |
| 72 | + ( |
| 73 | + "\nCREATE TABLE testdrive (\n\t" |
| 74 | + "name STRING, \n\t" |
| 75 | + "data FLOAT_VECTOR(3)\n)\n\n" |
| 76 | + ), |
| 77 | + (), |
| 78 | + ) |
| 79 | + |
| 80 | + def test_insert_invoke(self): |
| 81 | + stmt = self.table.insert().values( |
| 82 | + name="foo", data=[42.42, 43.43, 44.44] |
| 83 | + ) |
| 84 | + with self.engine.connect() as conn: |
| 85 | + conn.execute(stmt) |
| 86 | + fake_cursor.execute.assert_called_with( |
| 87 | + ("INSERT INTO testdrive (name, data) VALUES (?, ?)"), |
| 88 | + ("foo", [42.42, 43.43, 44.44]), |
| 89 | + ) |
| 90 | + |
| 91 | + def test_select_invoke(self): |
| 92 | + stmt = select(self.table.c.data) |
| 93 | + with self.engine.connect() as conn: |
| 94 | + conn.execute(stmt) |
| 95 | + fake_cursor.execute.assert_called_with( |
| 96 | + ("SELECT testdrive.data \nFROM testdrive"), |
| 97 | + (), |
| 98 | + ) |
| 99 | + |
| 100 | + def test_sql_select(self): |
| 101 | + self.assertSQL( |
| 102 | + "SELECT testdrive.data FROM testdrive", select(self.table.c.data) |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def test_from_db_success(): |
| 107 | + """ |
| 108 | + Verify succeeding uses of `sqlalchemy_cratedb.type.vector.from_db`. |
| 109 | + """ |
| 110 | + np = pytest.importorskip("numpy") |
| 111 | + assert from_db(None) is None |
| 112 | + assert np.array_equal(from_db(False), np.array(0., dtype=np.float32)) |
| 113 | + assert np.array_equal(from_db(True), np.array(1., dtype=np.float32)) |
| 114 | + assert np.array_equal(from_db(42), np.array(42, dtype=np.float32)) |
| 115 | + assert np.array_equal(from_db(42.42), np.array(42.42, dtype=np.float32)) |
| 116 | + assert np.array_equal(from_db([42.42, 43.43]), np.array([42.42, 43.43], dtype=np.float32)) |
| 117 | + assert np.array_equal(from_db("42.42"), np.array(42.42, dtype=np.float32)) |
| 118 | + assert np.array_equal(from_db(["42.42", "43.43"]), np.array([42.42, 43.43], dtype=np.float32)) |
| 119 | + |
| 120 | + |
| 121 | +def test_from_db_failure(): |
| 122 | + """ |
| 123 | + Verify failing uses of `sqlalchemy_cratedb.type.vector.from_db`. |
| 124 | + """ |
| 125 | + pytest.importorskip("numpy") |
| 126 | + |
| 127 | + with pytest.raises(ValueError) as ex: |
| 128 | + from_db("foo") |
| 129 | + assert ex.match("could not convert string to float: 'foo'") |
| 130 | + |
| 131 | + with pytest.raises(ValueError) as ex: |
| 132 | + from_db(["foo"]) |
| 133 | + assert ex.match("could not convert string to float: 'foo'") |
| 134 | + |
| 135 | + with pytest.raises(TypeError) as ex: |
| 136 | + from_db({"foo": "bar"}) |
| 137 | + if sys.version_info < (3, 10): |
| 138 | + assert ex.match(re.escape("float() argument must be a string or a number, not 'dict'")) |
| 139 | + else: |
| 140 | + assert ex.match(re.escape("float() argument must be a string or a real number, not 'dict'")) |
| 141 | + |
| 142 | + |
| 143 | +def test_to_db_success(): |
| 144 | + """ |
| 145 | + Verify succeeding uses of `sqlalchemy_cratedb.type.vector.to_db`. |
| 146 | + """ |
| 147 | + np = pytest.importorskip("numpy") |
| 148 | + assert to_db(None) is None |
| 149 | + assert to_db(False) is False |
| 150 | + assert to_db(True) is True |
| 151 | + assert to_db(42) == 42 |
| 152 | + assert to_db(42.42) == 42.42 |
| 153 | + assert to_db([42.42, 43.43]) == [42.42, 43.43] |
| 154 | + assert to_db(np.array([42.42, 43.43])) == [42.42, 43.43] |
| 155 | + assert to_db("42.42") == "42.42" |
| 156 | + assert to_db("foo") == "foo" |
| 157 | + assert to_db(["foo"]) == ["foo"] |
| 158 | + assert to_db({"foo": "bar"}) == {"foo": "bar"} |
| 159 | + assert isinstance(to_db(object()), object) |
| 160 | + |
| 161 | + |
| 162 | +def test_to_db_failure(): |
| 163 | + """ |
| 164 | + Verify failing uses of `sqlalchemy_cratedb.type.vector.to_db`. |
| 165 | + """ |
| 166 | + np = pytest.importorskip("numpy") |
| 167 | + |
| 168 | + with pytest.raises(ValueError) as ex: |
| 169 | + to_db(np.array(["42.42", "43.43"])) |
| 170 | + assert ex.match("dtype must be numeric") |
| 171 | + |
| 172 | + with pytest.raises(ValueError) as ex: |
| 173 | + to_db(np.array([42.42, 43.43]), dim=33) |
| 174 | + assert ex.match("expected 33 dimensions, not 2") |
| 175 | + |
| 176 | + with pytest.raises(ValueError) as ex: |
| 177 | + to_db(np.array([[42.42, 43.43]])) |
| 178 | + assert ex.match("expected ndim to be 1") |
| 179 | + |
| 180 | + |
| 181 | +def test_float_vector_no_dimension_size(): |
| 182 | + """ |
| 183 | + Verify a FloatVector can not be initialized without a dimension size. |
| 184 | + """ |
| 185 | + engine = sa.create_engine("crate://") |
| 186 | + metadata = sa.MetaData() |
| 187 | + table = sa.Table( |
| 188 | + "foo", |
| 189 | + metadata, |
| 190 | + sa.Column("data", FloatVector), |
| 191 | + ) |
| 192 | + with pytest.raises(ValueError) as ex: |
| 193 | + table.create(engine) |
| 194 | + ex.match("FloatVector must be initialized with dimension size") |
| 195 | + |
| 196 | + |
| 197 | +def test_float_vector_as_generic(): |
| 198 | + """ |
| 199 | + Verify the `as_generic` and `python_type` method/property on the FloatVector type object. |
| 200 | + """ |
| 201 | + fv = FloatVector(3) |
| 202 | + assert isinstance(fv.as_generic(), sa.ARRAY) |
| 203 | + assert fv.python_type is list |
0 commit comments