-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# -*- coding: utf-8; -*- | ||
# | ||
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor | ||
# license agreements. See the NOTICE file distributed with this work for | ||
# additional information regarding copyright ownership. Crate licenses | ||
# this file to you under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. You may | ||
# obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
# However, if you have executed another commercial license agreement | ||
# with Crate these terms will supersede the license and you may use the | ||
# software solely pursuant to the terms of the relevant commercial agreement. | ||
|
||
from __future__ import absolute_import | ||
|
||
from unittest import TestCase, skipIf | ||
from unittest.mock import MagicMock, patch | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.sql import select | ||
|
||
from sqlalchemy_cratedb import SA_VERSION, SA_1_4 | ||
from sqlalchemy_cratedb.type import FloatVector | ||
|
||
from crate.client.cursor import Cursor | ||
|
||
|
||
fake_cursor = MagicMock(name="fake_cursor") | ||
FakeCursor = MagicMock(name="FakeCursor", spec=Cursor) | ||
FakeCursor.return_value = fake_cursor | ||
|
||
|
||
@skipIf(SA_VERSION < SA_1_4, "SQLAlchemy 1.3 and earlier do not support the FloatVector type") | ||
@patch("crate.client.connection.Cursor", FakeCursor) | ||
class SqlAlchemyVectorTypeTest(TestCase): | ||
def setUp(self): | ||
self.engine = sa.create_engine("crate://") | ||
metadata = sa.MetaData() | ||
self.table = sa.Table( | ||
"testdrive", | ||
metadata, | ||
sa.Column("name", sa.String), | ||
sa.Column("data", FloatVector(3)), | ||
) | ||
|
||
def assertSQL(self, expected_str, selectable): | ||
actual_expr = selectable.compile(bind=self.engine) | ||
self.assertEqual(expected_str, str(actual_expr).replace("\n", "")) | ||
|
||
def test_create(self): | ||
self.table.create(self.engine) | ||
fake_cursor.execute.assert_called_with( | ||
( | ||
"\nCREATE TABLE testdrive (\n\t" | ||
"name STRING, \n\t" | ||
"data FLOAT_VECTOR(3)\n)\n\n" | ||
), | ||
(), | ||
) | ||
|
||
def test_insert(self): | ||
stmt = self.table.insert().values( | ||
name="foo", data=[42.42, 43.43, 44.44] | ||
) | ||
with self.engine.connect() as conn: | ||
conn.execute(stmt) | ||
fake_cursor.execute.assert_called_with( | ||
("INSERT INTO testdrive (name, data) VALUES (?, ?)"), | ||
("foo", [42.42, 43.43, 44.44]), | ||
) | ||
|
||
def test_select(self): | ||
self.assertSQL( | ||
"SELECT testdrive.data FROM testdrive", select(self.table.c.data) | ||
) |