async def session(event_loop, cluster, keyspace): # Create the acsylla keyspace if it does not exist yet session_without_keyspace = await cluster.create_session() create_keyspace_statement = create_statement( "CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = ".format(keyspace) + "{ 'class': 'SimpleStrategy', 'replication_factor': 1}") await session_without_keyspace.execute(create_keyspace_statement) await session_without_keyspace.close() session = await cluster.create_session(keyspace=keyspace) # Drop table if exits, will truncate any data used before # and will enforce in the next step that if the schema of # table changed is used during the tests. create_table_statement = create_statement("DROP TABLE IF EXISTS test") await session.execute(create_table_statement) # Create the table test in the acsylla keyspace create_table_statement = create_statement( "CREATE TABLE test(id int PRIMARY KEY," + "value int," + "value_int int," + "value_float float," + "value_bool boolean," + "value_text text," + "value_blob blob)") await session.execute(create_table_statement) try: yield session finally: await session.close()
async def test_execute_batch(self, session, id_generation): batch = create_batch_unlogged() key_and_value = str(next(id_generation)) batch.add_statement( create_statement("INSERT INTO test (id, value) values(" + key_and_value + "," + key_and_value + ")") ) key_and_value = str(next(id_generation)) batch.add_statement( create_statement("INSERT INTO test (id, value) values(" + key_and_value + "," + key_and_value + ")") ) await session.execute_batch(batch)
async def test_result_invalid_column_name(self, session, id_generation): id_ = next(id_generation) value = 100 # insert a new value into the table statement = create_statement("INSERT INTO test (id, value) values(" + str(id_) + ", " + str(value) + ")") await session.execute(statement) # read the new inserted value statement = create_statement("SELECT id, value FROM test WHERE id =" + str(id_)) result = await session.execute(statement) row = result.first() with pytest.raises(ColumnNotFound): row.column_by_name("invalid_column_name")
async def test_result_paging(self, session, id_generation, type_, page_size, pages_fetched_expected): total_rows = 100 value = 33 insert_statement = "INSERT INTO test (id, value) values(?, ?)" select_statement = "SELECT id, value FROM test WHERE id >= :min and id <= :max ALLOW FILTERING" statement = create_statement(insert_statement, parameters=2) statement.bind_int(1, value) ids = [next(id_generation) for i in range(total_rows)] # write results for id_ in ids: statement.bind_int(0, id_) await session.execute(statement) # read all results using pagination page_state = None pages_fetched = 0 while True: statement = await self._build_statement( session, type_, select_statement, 2, page_size=page_size, page_state=page_state ) statement.bind_int(0, ids[0]) statement.bind_int(1, ids[-1]) result = await session.execute(statement) pages_fetched += 1 if not result.has_more_pages(): break page_state = result.page_state() assert pages_fetched == pages_fetched_expected
async def test_bytes(self, session, id_generation): id_ = next(id_generation) value = b"acsylla" insert_statement = create_statement( "INSERT INTO test (id, value_blob) values (?, ?)", parameters=2) insert_statement.bind_int(0, id_) insert_statement.bind_bytes(1, value) await session.execute(insert_statement) select_statement = create_statement( "SELECT value_blob FROM test WHERE ( id = ? )", parameters=1) select_statement.bind_int(0, id_) result = await session.execute(select_statement) row = result.first() assert row.column_by_name("value_blob").bytes() == value
async def read(session, key, value): start = time.monotonic() statement = create_statement("SELECT id, value FROM test WHERE id =" + key) result = await session.execute(statement) if result.count() > 0: row = result.first() _ = row.column_by_name("value").int() return time.monotonic() - start
async def statement(self, request, session): statement_str = "INSERT INTO test (id, value) values " + "(?, ?)" if request.param == "none_prepared": statement_ = create_statement(statement_str, parameters=2) elif request.param == "prepared": prepared = await session.create_prepared(statement_str) statement_ = prepared.bind() else: raise ValueError() return statement_
async def _build_statement(self, session, type_, statement_str, parameters, page_size=None, page_state=None): if type_ == "none_prepared": statement_ = create_statement( statement_str, parameters=parameters, page_size=page_size, page_state=page_state ) elif type_ == "prepared": prepared = await session.create_prepared(statement_str) statement_ = prepared.bind(page_size=page_size, page_state=page_state) else: raise ValueError(type_) return statement_
async def statement(self, request, session): statement_str = ( "INSERT INTO test (id, value, value_int, value_float, value_bool, value_text, value_blob) values " + "(?, ?, ?, ?, ?, ?, ?)") if request.param == "none_prepared": statement_ = create_statement(statement_str, parameters=7) elif request.param == "prepared": prepared = await session.create_prepared(statement_str) statement_ = prepared.bind() else: raise ValueError() return statement_
async def test_types(host, keyspace, id_generation): id_ = next(id_generation) value = id_ cluster: Cluster = create_cluster([host]) session: Session = await cluster.create_session(keyspace=keyspace) statement: Statement = create_statement( "INSERT INTO test (id, value) values(" + str(id_) + ", " + str(value) + ")") await session.execute(statement) # read the new inserted value prepared: PreparedStatement = await session.create_prepared( "SELECT id, value FROM test WHERE id = ?") statement: Statement = prepared.bind() statement.bind_int_by_name("id", id_) result: Result = await session.execute(statement) _: int = result.count() _: int = result.column_count() row: Row = result.first() value: Value = row.column_by_name("id") _: int = value.int()
async def test_execute_invalid_query(self, session): with pytest.raises(CassErrorServerInvalidQuery): await session.execute(create_statement("CREATE TABLE foo(id invalid_type PRIMARY KEY)"))
async def test_execute_syntax_error(self, session): with pytest.raises(CassErrorServerSyntaxError): await session.execute(create_statement("foobar"))
async def test_execute_using_a_closed_session(self, session): await session.close() with pytest.raises(RuntimeError): await session.execute(create_statement("foobar"))
async def test_execute(self, session, id_generation): key_and_value = str(next(id_generation)) statement = create_statement("INSERT INTO test (id, value) values(" + key_and_value + "," + key_and_value + ")") await session.execute(statement)
async def write(session, key, value): start = time.monotonic() statement = create_statement("INSERT INTO test (id, value) values(" + key + "," + value + ")") await session.execute(statement) return time.monotonic() - start