Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cassandra/cqlengine/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ def get_column_def(self):
Returns a column definition for CQL table definition
"""
static = "static" if self.static else ""
return '{0} {1} {2}'.format(self.cql, self.db_type, static)
db_type = self.db_type
# Indexed collection columns must be frozen
if self.index and isinstance(self, BaseContainerColumn):
if not db_type.startswith('frozen'):
db_type = "frozen<%s>" % (db_type,)
return '{0} {1} {2}'.format(self.cql, db_type, static)

# TODO: make columns use cqltypes under the hood
# until then, this bridges the gap in using types along with cassandra.metadata for CQL generation
Expand Down
45 changes: 44 additions & 1 deletion tests/unit/cqlengine/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import unittest

from cassandra.cqlengine.columns import Column
from cassandra.cqlengine.columns import Column, List, Set, Map, Integer, Text


class ColumnTest(unittest.TestCase):
Expand Down Expand Up @@ -66,3 +66,46 @@ def test_hash(self):
c0 = Column()
assert id(c0) == c0.__hash__()


class IndexedCollectionColumnsTest(unittest.TestCase):
"""Test that indexed collection columns are properly frozen in column definitions."""

def test_list_with_index_is_frozen(self):
col = List(Integer, index=True)
col.set_column_name('test_list')
column_def = col.get_column_def()
assert 'frozen<list<int>>' in column_def

def test_list_without_index_is_not_frozen(self):
col = List(Integer, index=False)
col.set_column_name('test_list')
column_def = col.get_column_def()
assert 'frozen' not in column_def
assert 'list<int>' in column_def

def test_set_with_index_is_frozen(self):
col = Set(Integer, index=True)
col.set_column_name('test_set')
column_def = col.get_column_def()
assert 'frozen<set<int>>' in column_def

def test_set_without_index_is_not_frozen(self):
col = Set(Integer, index=False)
col.set_column_name('test_set')
column_def = col.get_column_def()
assert 'frozen' not in column_def
assert 'set<int>' in column_def

def test_map_with_index_is_frozen(self):
col = Map(Text, Integer, index=True)
col.set_column_name('test_map')
column_def = col.get_column_def()
assert 'frozen<map<text, int>>' in column_def

def test_map_without_index_is_not_frozen(self):
col = Map(Text, Integer, index=False)
col.set_column_name('test_map')
column_def = col.get_column_def()
assert 'frozen' not in column_def
assert 'map<text, int>' in column_def

Loading