Permalink
Browse files

Use packed colname in _pack_value to determine packer

Fixes #107
  • Loading branch information...
1 parent 84a656d commit 3deb3d9000b712ba32e24e2ae421776dfc7a6baf @thobbs thobbs committed Jan 5, 2012
Showing with 34 additions and 4 deletions.
  1. +6 −4 pycassa/columnfamily.py
  2. +28 −0 tests/test_autopacking.py
View
@@ -423,7 +423,8 @@ def _pack_value(self, value, col_name):
% (str(col_name), value.__class__.__name__, str(value)))
return value
- packer = self._column_validators.packers.get(col_name, self._default_value_packer)
+ packed_col_name = self._pack_name(col_name, False)
+ packer = self._column_validators.packers.get(packed_col_name, self._default_value_packer)
try:
return packer(value)
except struct.error:
@@ -465,7 +466,6 @@ def _unpack_key(self, b):
def _make_mutation_list(self, columns, timestamp, ttl):
_pack_name = self._pack_name
_pack_value = self._pack_value
- _validate = lambda _: not isinstance(_[1], types.CassandraType)
if not self.super:
return map(lambda (c, v): Mutation(self._make_cosc(_pack_name(c), _pack_value(v, c), timestamp, ttl)),
columns.iteritems())
@@ -572,8 +572,8 @@ def get_indexed_slices(self, index_clause, columns=None, column_start="", column
new_exprs = []
# Pack the values in the index clause expressions
for expr in index_clause.expressions:
+ value = self._pack_value(expr.value, expr.column_name)
name = self._pack_name(expr.column_name)
- value = self._pack_value(expr.value, name)
new_exprs.append(IndexExpression(name, expr.op, value))
packed_start_key = self._pack_key(index_clause.start_key)
@@ -856,9 +856,11 @@ def insert(self, key, columns, timestamp=None, ttl=None,
else:
cp = self._column_path()
- colname = self._pack_name(columns.keys()[0], False)
+ colname = columns.keys()[0]
colval = self._pack_value(columns.values()[0], colname)
+ colname = self._pack_name(colname, False)
column = Column(colname, colval, timestamp, ttl)
+
return self.pool.execute('insert', packed_key, cp, column,
write_consistency_level or self.write_consistency_level)
else:
View
@@ -526,6 +526,29 @@ def test_super_column_family_subs(self):
class TestValidators(unittest.TestCase):
+ def test_validation_with_packed_names(self):
+ """
+ Make sure that validated columns are packed correctly when the
+ column names themselves must be packed
+ """
+ sys = SystemManager()
+ sys.create_column_family(TEST_KS, 'Validators2',
+ comparator_type=LongType(), default_validation_class=LongType())
+ sys.alter_column(TEST_KS, 'Validators2', 1, TimeUUIDType())
+ sys.close()
+
+ my_uuid = uuid.uuid1()
+ cf = ColumnFamily(pool, 'Validators2')
+
+ cf.insert('key', {0: 0})
+ assert_equal(cf.get('key'), {0: 0})
+
+ cf.insert('key', {1: my_uuid})
+ assert_equal(cf.get('key'), {0: 0, 1: my_uuid})
+
+ cf.insert('key', {0: 0, 1: my_uuid})
+ assert_equal(cf.get('key'), {0: 0, 1: my_uuid})
+
def test_validated_columns(self):
sys = SystemManager()
sys.create_column_family(TEST_KS, 'Validators',)
@@ -594,6 +617,11 @@ def test_default_validated_columns(self):
cf.insert(key, col_cm)
assert_equal(cf.get(key), {'aaaaaa': 1L, 'subcol': TIME1})
+ # Insert multiple columns at once
+ col_cf.update(col_cm)
+ cf.insert(key, col_cf)
+ assert_equal(cf.get(key), {'aaaaaa': 1L, 'subcol': TIME1})
+
assert_raises(TypeError, cf.insert, key, col_ncf)
cf.remove(key)

0 comments on commit 3deb3d9

Please sign in to comment.