fixed TTL queries

This commit is contained in:
Jon Haddad
2014-06-18 16:28:07 -07:00
parent 0761453e40
commit 91ef3c32f4
5 changed files with 30 additions and 13 deletions

View File

@@ -24,6 +24,7 @@ from contextlib import contextmanager
from thrift.transport.TTransport import TTransportException from thrift.transport.TTransport import TTransportException
from cqlengine.statements import BaseCQLStatement from cqlengine.statements import BaseCQLStatement
from cassandra.query import dict_factory
LOG = logging.getLogger('cqlengine.cql') LOG = logging.getLogger('cqlengine.cql')
@@ -83,6 +84,7 @@ def setup(
cluster = Cluster(hosts) cluster = Cluster(hosts)
session = cluster.connect() session = cluster.connect()
session.row_factory = dict_factory
_max_connections = max_connections _max_connections = max_connections
@@ -273,8 +275,14 @@ def execute_native(query, params=None, consistency_level=None):
query = str(query) query = str(query)
params = params or {} params = params or {}
result = session.execute(query, params) result = session.execute(query, params)
import ipdb; ipdb.set_trace()
return ([], result) if result:
keys = result[0].keys()
else:
keys = []
return QueryResult(keys, result)
def get_session(): def get_session():
return session return session

View File

@@ -229,13 +229,13 @@ def get_fields(model):
# Tables containing only primary keys do not appear to create # Tables containing only primary keys do not appear to create
# any entries in system.schema_columns, as only non-primary-key attributes # any entries in system.schema_columns, as only non-primary-key attributes
# appear to be inserted into the schema_columns table # appear to be inserted into the schema_columns table
if not tmp: if not tmp[1]:
return [] return []
try: try:
return [Field(x.column_name, x.validator) for x in tmp if x.type == 'regular'] return [Field(x['column_name'], x['validator']) for x in tmp[1] if x['type'] == 'regular']
except ValueError: except ValueError:
return [Field(x.column_name, x.validator) for x in tmp] return [Field(x['column_name'], x['validator']) for x in tmp[1]]
# convert to Field named tuples # convert to Field named tuples

View File

@@ -232,7 +232,6 @@ class AbstractQuerySet(object):
return self._batch.add_query(q) return self._batch.add_query(q)
else: else:
result = execute_native(q, consistency_level=self._consistency) result = execute_native(q, consistency_level=self._consistency)
import ipdb; ipdb.set_trace()
return result return result
def __unicode__(self): def __unicode__(self):

View File

@@ -17,7 +17,7 @@ class InsertStatementTests(TestCase):
self.assertEqual( self.assertEqual(
unicode(ist), unicode(ist),
'INSERT INTO table ("a", "c") VALUES (?, ?)' 'INSERT INTO table ("a", "c") VALUES (%(0)s, %(1)s)'
) )
def test_context_update(self): def test_context_update(self):
@@ -28,7 +28,7 @@ class InsertStatementTests(TestCase):
ist.update_context_id(4) ist.update_context_id(4)
self.assertEqual( self.assertEqual(
unicode(ist), unicode(ist),
'INSERT INTO table ("a", "c") VALUES (?, ?)' 'INSERT INTO table ("a", "c") VALUES (%(4)s, %(5)s)'
) )
ctx = ist.get_context() ctx = ist.get_context()
self.assertEqual(ctx, {'4': 'b', '5': 'd'}) self.assertEqual(ctx, {'4': 'b', '5': 'd'})

View File

@@ -4,7 +4,8 @@ from cqlengine.models import Model
from uuid import uuid4 from uuid import uuid4
from cqlengine import columns from cqlengine import columns
import mock import mock
from cqlengine.connection import ConnectionPool from cqlengine.connection import ConnectionPool, get_session
class TestTTLModel(Model): class TestTTLModel(Model):
id = columns.UUID(primary_key=True, default=lambda:uuid4()) id = columns.UUID(primary_key=True, default=lambda:uuid4())
@@ -39,7 +40,9 @@ class TTLModelTests(BaseTTLTest):
def test_ttl_included_on_create(self): def test_ttl_included_on_create(self):
""" tests that ttls on models work as expected """ """ tests that ttls on models work as expected """
with mock.patch.object(ConnectionPool, 'execute') as m: session = get_session()
with mock.patch.object(session, 'execute') as m:
TestTTLModel.ttl(60).create(text="hello blake") TestTTLModel.ttl(60).create(text="hello blake")
query = m.call_args[0][0] query = m.call_args[0][0]
@@ -56,8 +59,10 @@ class TTLModelTests(BaseTTLTest):
class TTLInstanceUpdateTest(BaseTTLTest): class TTLInstanceUpdateTest(BaseTTLTest):
def test_update_includes_ttl(self): def test_update_includes_ttl(self):
session = get_session()
model = TestTTLModel.create(text="goodbye blake") model = TestTTLModel.create(text="goodbye blake")
with mock.patch.object(ConnectionPool, 'execute') as m: with mock.patch.object(session, 'execute') as m:
model.ttl(60).update(text="goodbye forever") model.ttl(60).update(text="goodbye forever")
query = m.call_args[0][0] query = m.call_args[0][0]
@@ -84,22 +89,27 @@ class TTLInstanceTest(BaseTTLTest):
self.assertEqual(60, o._ttl) self.assertEqual(60, o._ttl)
def test_ttl_is_include_with_query_on_update(self): def test_ttl_is_include_with_query_on_update(self):
session = get_session()
o = TestTTLModel.create(text="whatever") o = TestTTLModel.create(text="whatever")
o.text = "new stuff" o.text = "new stuff"
o = o.ttl(60) o = o.ttl(60)
with mock.patch.object(ConnectionPool, 'execute') as m: with mock.patch.object(session, 'execute') as m:
o.save() o.save()
query = m.call_args[0][0] query = m.call_args[0][0]
self.assertIn("USING TTL", query) self.assertIn("USING TTL", query)
class TTLBlindUpdateTest(BaseTTLTest): class TTLBlindUpdateTest(BaseTTLTest):
def test_ttl_included_with_blind_update(self): def test_ttl_included_with_blind_update(self):
session = get_session()
o = TestTTLModel.create(text="whatever") o = TestTTLModel.create(text="whatever")
tid = o.id tid = o.id
with mock.patch.object(ConnectionPool, 'execute') as m: with mock.patch.object(session, 'execute') as m:
TestTTLModel.objects(id=tid).ttl(60).update(text="bacon") TestTTLModel.objects(id=tid).ttl(60).update(text="bacon")
query = m.call_args[0][0] query = m.call_args[0][0]