|
| 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 | +from unittest import TestCase, skipIf |
| 24 | + |
| 25 | +import sqlalchemy as sa |
| 26 | +from sqlalchemy.orm import Session |
| 27 | +from sqlalchemy.sql.operators import eq |
| 28 | + |
| 29 | +from crate.client.sqlalchemy import SA_VERSION, SA_1_4 |
| 30 | +from crate.testing.settings import crate_host |
| 31 | + |
| 32 | +try: |
| 33 | + from sqlalchemy.orm import declarative_base |
| 34 | +except ImportError: |
| 35 | + from sqlalchemy.ext.declarative import declarative_base |
| 36 | + |
| 37 | +from crate.client.sqlalchemy.types import Object, ObjectArray |
| 38 | + |
| 39 | + |
| 40 | +class SqlAlchemyQueryCompilationCaching(TestCase): |
| 41 | + |
| 42 | + def setUp(self): |
| 43 | + self.engine = sa.create_engine(f"crate://{crate_host}") |
| 44 | + self.metadata = sa.MetaData(schema="testdrive") |
| 45 | + self.session = Session(bind=self.engine) |
| 46 | + self.Character = self.setup_entity() |
| 47 | + |
| 48 | + def setup_entity(self): |
| 49 | + """ |
| 50 | + Define ORM entity. |
| 51 | + """ |
| 52 | + Base = declarative_base(metadata=self.metadata) |
| 53 | + |
| 54 | + class Character(Base): |
| 55 | + __tablename__ = 'characters' |
| 56 | + name = sa.Column(sa.String, primary_key=True) |
| 57 | + age = sa.Column(sa.Integer) |
| 58 | + data = sa.Column(Object) |
| 59 | + data_list = sa.Column(ObjectArray) |
| 60 | + |
| 61 | + return Character |
| 62 | + |
| 63 | + def setup_data(self): |
| 64 | + """ |
| 65 | + Insert two records into the `characters` table. |
| 66 | + """ |
| 67 | + self.metadata.drop_all(self.engine) |
| 68 | + self.metadata.create_all(self.engine) |
| 69 | + |
| 70 | + Character = self.Character |
| 71 | + char1 = Character(name='Trillian', data={'x': 1}, data_list=[{'foo': 1, 'bar': 10}]) |
| 72 | + char2 = Character(name='Slartibartfast', data={'y': 2}, data_list=[{'bar': 2}]) |
| 73 | + self.session.add(char1) |
| 74 | + self.session.add(char2) |
| 75 | + self.session.commit() |
| 76 | + self.session.execute(sa.text("REFRESH TABLE testdrive.characters;")) |
| 77 | + |
| 78 | + @skipIf(SA_VERSION < SA_1_4, "On SA13, the 'ResultProxy' object has no attribute 'scalar_one'") |
| 79 | + def test_object_multiple_select(self): |
| 80 | + """ |
| 81 | + The SQLAlchemy implementation of CrateDB's `OBJECT` type offers indexed |
| 82 | + access to the instance's content in form of a dictionary. Thus, it must |
| 83 | + not use `cache_ok = True` on its implementation, i.e. this part of the |
| 84 | + compiled SQL clause must not be cached. |
| 85 | +
|
| 86 | + This test verifies that two subsequent `SELECT` statements are translated |
| 87 | + well, and don't trip on incorrect SQL compiled statement caching. |
| 88 | + """ |
| 89 | + self.setup_data() |
| 90 | + Character = self.Character |
| 91 | + |
| 92 | + selectable = sa.select(Character).where(Character.data['x'] == 1) |
| 93 | + result = self.session.execute(selectable).scalar_one().data |
| 94 | + self.assertEqual({"x": 1}, result) |
| 95 | + |
| 96 | + selectable = sa.select(Character).where(Character.data['y'] == 2) |
| 97 | + result = self.session.execute(selectable).scalar_one().data |
| 98 | + self.assertEqual({"y": 2}, result) |
| 99 | + |
| 100 | + @skipIf(SA_VERSION < SA_1_4, "On SA13, the 'ResultProxy' object has no attribute 'scalar_one'") |
| 101 | + def test_objectarray_multiple_select(self): |
| 102 | + """ |
| 103 | + The SQLAlchemy implementation of CrateDB's `ARRAY` type in form of the |
| 104 | + `ObjectArray`, does *not* offer indexed access to the instance's content. |
| 105 | + Thus, using `cache_ok = True` on that type should be sane, and not mess |
| 106 | + up SQLAlchemy's SQL compiled statement caching. |
| 107 | + """ |
| 108 | + self.setup_data() |
| 109 | + Character = self.Character |
| 110 | + |
| 111 | + selectable = sa.select(Character).where(Character.data_list['foo'].any(1, operator=eq)) |
| 112 | + result = self.session.execute(selectable).scalar_one().data |
| 113 | + self.assertEqual({"x": 1}, result) |
| 114 | + |
| 115 | + selectable = sa.select(Character).where(Character.data_list['bar'].any(2, operator=eq)) |
| 116 | + result = self.session.execute(selectable).scalar_one().data |
| 117 | + self.assertEqual({"y": 2}, result) |
0 commit comments