def test_cql_result_metadata(self): """ execute should use the metadata included with the CqlResult for deserializing values. """ expected = [{"foo": UUID('114b8328-d1f1-11e2-8683-000c29bc9473')}] mockrow = [ ttypes.CqlRow( key='blah', columns=[ ttypes.Column( name='foo', value= '\x11K\x83(\xd1\xf1\x11\xe2\x86\x83\x00\x0c)\xbc\x94s') ]) ] self.mock_results = ttypes.CqlResult( type=ttypes.CqlResultType.ROWS, rows=mockrow, schema=ttypes.CqlMetadata( value_types={ 'foo': 'org.apache.cassandra.db.marshal.TimeUUIDType' })) client = CQLClient(self.endpoint, 'blah') d = client.execute("SELECT * FROM blah;", {}, ConsistencyLevel.ONE) self.assertEqual(self.assertFired(d), expected)
def test_unsupported_types_are_returned_as_bytes(self): """ When a table includes a column of a type that is not explicitly supported we should return the raw bytes instead of attempting to unmarshal the data. """ mock_rows = [ ttypes.CqlRow(key='', columns=[ ttypes.Column(name='an_unknown_type', value="\x00\x01") ]) ] self.mock_results = ttypes.CqlResult( type=ttypes.CqlResultType.ROWS, rows=mock_rows, schema=ttypes.CqlMetadata( value_types={'an_unknown_type': 'an.unknown.type'})) client = CQLClient(self.endpoint, 'blah') d = client.execute("SELECT * FROM blah", {}, ConsistencyLevel.ONE) results = self.assertFired(d) self.assertEqual(results, [{'an_unknown_type': '\x00\x01'}])
def test_cql_sequence(self): """ Test a sequence of operations results in only one handshake but two requests. """ expected = [{"foo": "{P}"}] mockrow = [ ttypes.CqlRow(key='blah', columns=[ttypes.Column(name='foo', value='{P}')]) ] self.mock_results = ttypes.CqlResult( type=ttypes.CqlResultType.ROWS, rows=mockrow, schema=ttypes.CqlMetadata( value_types={ 'foo': 'org.apache.cassandra.db.marshal.UTF8Type' })) client = CQLClient(self.endpoint, 'blah') def _cqlProc(r): return client.execute("SELECT :sel FROM test_blah", {"sel": "blah"}, ConsistencyLevel.ONE) d = client.execute("SELECT :sel FROM test_blah", {"sel": "ffh"}, ConsistencyLevel.ONE) d.addCallback(_cqlProc) self.assertEqual(self.assertFired(d), expected) self.client_proto.execute_cql3_query.assert_any_call( "SELECT 'blah' FROM test_blah", 2, ConsistencyLevel.ONE) self.client_proto.execute_cql3_query.assert_any_call( "SELECT 'ffh' FROM test_blah", 2, ConsistencyLevel.ONE) self.client_proto.set_keyspace.assert_called_once_with('blah')
def test_cql_list_deserial(self): expected = [{'fff': ['ggg', 'hhh']}] mockrow = [ ttypes.CqlRow(key='blah', columns=[ ttypes.Column( name='fff', value='\x00\x02\x00\x03ggg\x00\x03hhh') ]) ] list_type = 'org.apache.cassandra.db.marshal.ListType' text_type = 'org.apache.cassandra.db.marshal.UTF8Type' text_list_type = list_type + '(' + text_type + ')' self.mock_results = ttypes.CqlResult( type=ttypes.CqlResultType.ROWS, rows=mockrow, schema=ttypes.CqlMetadata(value_types={'fff': text_list_type})) client = CQLClient(self.endpoint, 'blah') d = client.execute("SELECT * FROM :tablename;", {"tablename": "blah"}, ConsistencyLevel.ONE) self.assertEqual(self.assertFired(d), expected) self.client_proto.execute_cql3_query.assert_called_once_with( "SELECT * FROM 'blah';", 2, ConsistencyLevel.ONE) self.client_proto.set_keyspace.assert_called_once_with('blah')
def test_cql_array(self): """Test that a full CQL response (e.g. SELECT) works.""" expected = [{"foo": "{P}"}] mockrow = [ttypes.CqlRow(key='blah', columns=[ttypes.Column(name='foo', value='{P}')])] self.mock_results = ttypes.CqlResult( type=ttypes.CqlResultType.ROWS, rows=mockrow, schema=ttypes.CqlMetadata(value_types={'foo': 'org.apache.cassandra.db.marshal.UTF8Type'})) client = CQLClient(self.endpoint, 'blah') d = client.execute("SELECT :sel FROM test_blah", {"sel": "blah"}, ConsistencyLevel.ONE) self.assertEqual(self.assertFired(d), expected) self.client_proto.execute_cql3_query.assert_called_once_with("SELECT 'blah' FROM test_blah", 2, ConsistencyLevel.ONE) self.client_proto.set_keyspace.assert_called_once_with('blah')
def test_cql_array_deserial(self): """Make sure that values that need to be deserialized correctly are.""" expected = [{"fff": 1222}] mockrow = [ttypes.CqlRow(key='blah', columns=[ttypes.Column(name='fff', value='\x04\xc6')])] self.mock_results = ttypes.CqlResult(type=ttypes.CqlResultType.ROWS, rows=mockrow, schema=ttypes.CqlMetadata(value_types={ 'fff': 'org.apache.cassandra.db.marshal.IntegerType' })) client = CQLClient(self.endpoint, 'blah') d = client.execute("SELECT * FROM :tablename;", {"tablename": "blah"}, ConsistencyLevel.ONE) self.assertEqual(self.assertFired(d), expected) self.client_proto.execute_cql3_query.assert_called_once_with("SELECT * FROM 'blah';", 2, ConsistencyLevel.ONE) self.client_proto.set_keyspace.assert_called_once_with('blah')
def test_cql_None_not_deserialized(self): """ If the value is None, it is not deserialized at all. """ raw_rows = [ttypes.CqlRow( key='blah', columns=[ttypes.Column(name='fff', value=None)])] schema = ttypes.CqlMetadata(value_types={ 'fff': 'org.apache.cassandra.db.marshal.AlwaysFailType'}) client = CQLClient(self.endpoint, 'blah') always_blow_up = mock.Mock(spec=[], side_effect=Exception) rows = client._unmarshal_result(schema, raw_rows, { 'org.apache.cassandra.db.marshal.AlwaysFailType': always_blow_up }) self.assertEqual(rows, [{'fff': None}]) self.assertEqual(always_blow_up.call_count, 0)