Skip to content

Commit

Permalink
Creating cats-effect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sagoez committed Jun 1, 2023
1 parent 0ae054b commit c3d4dbb
Show file tree
Hide file tree
Showing 15 changed files with 1,052 additions and 24 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ lazy val catsEffect =
Seq(
"org.typelevel" %% "cats-effect" % "3.5.0",
"co.fs2" %% "fs2-core" % "3.7.0",
"co.fs2" %% "fs2-io" % "3.7.0",
disney %% "weaver-cats" % weaverV % Test,
disney %% "weaver-scalacheck" % weaverV % Test
)
Expand Down
159 changes: 159 additions & 0 deletions cats-effect/src/test/resources/migrations.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
-- Please note: tests are designed to be run in parallel so create tables per test to avoid collisions
CREATE TABLE IF NOT EXISTS ziocassandrasessionspec_executeAction
(
id INT PRIMARY KEY,
info TEXT
);

CREATE TABLE IF NOT EXISTS ziocassandrasessionspec_executeBatchAction
(
id INT PRIMARY KEY,
info TEXT
);

CREATE TABLE IF NOT EXISTS ziocassandrasessionspec_selectPage
(
id INT,
bucket INT,
info TEXT,
PRIMARY KEY ((id), bucket)
);

CREATE TABLE IF NOT EXISTS ziocassandrasessionspec_timeoutcheck
(
id INT PRIMARY KEY,
info TEXT,
another_info TEXT
);

CREATE TABLE IF NOT EXISTS ziocassandrasessionspec_pageSizeCheck
(
id INT PRIMARY KEY,
info TEXT
);

CREATE TYPE udt_address (
number INT,
street TEXT,
city TEXT,
);

CREATE TYPE udt_email (
username TEXT,
domain TEXT,
domain_name TEXT
);

CREATE TYPE udt_data (
addresses frozen<list<udt_address>>,
email frozen<udt_email>
);

CREATE TABLE IF NOT EXISTS userdefinedtypesspec_person
(
id INT PRIMARY KEY,
name TEXT,
age INT,
data frozen<udt_data>
);

CREATE TYPE example_type (
x bigint,
y bigint,
date date,
time time
);

CREATE TYPE example_nested_type(
a int,
b text,
c frozen<example_type>
);

CREATE TYPE example_collection_nested_udt_type(
a int,
b frozen<map<int,frozen<set<frozen<set<frozen<set<frozen<set<example_nested_type>>>>>>>>>>,
c frozen<example_nested_type>
);

CREATE TABLE userdefinedtypesspec_heavilynestedudttable
(
id int,
data example_collection_nested_udt_type,
PRIMARY KEY (id)
);

CREATE TABLE collectionspec_simplecollectiontable
(
id INT PRIMARY KEY,
map_test FROZEN<MAP<INT,TEXT>>,
set_test FROZEN<SET<BIGINT>>,
list_test FROZEN<LIST<TEXT>>
);

CREATE TABLE collectionspec_optioncollectiontable
(
id INT PRIMARY KEY,
opt_map_test FROZEN<MAP<INT,TEXT>>,
opt_set_test FROZEN<SET<BIGINT>>,
opt_list_test FROZEN<LIST<TEXT>>
);

CREATE TABLE collectionspec_nestedcollectiontable
(
a int PRIMARY KEY,
b frozen<map<int, frozen<set<frozen<set<frozen<set<frozen<set<int>>>>>>>>>>
);

CREATE TYPE cursorspec_note(
data TEXT,
ip INET
);

CREATE TYPE cursorspec_address(
street TEXT,
city TEXT,
state TEXT,
zip TEXT,
note frozen<cursorspec_note>
);

CREATE TABLE cursorspec_cursorexampletable
(
id BIGINT PRIMARY KEY,
name TEXT,
may_be_empty TEXT,
age SMALLINT,
addresses frozen<list<cursorspec_address>>,
);

CREATE TABLE updatebuilderspec_person
(
id INT PRIMARY KEY,
name TEXT,
age INT
);

CREATE TABLE updatebuilderspec_counter
(
id INT PRIMARY KEY,
likes COUNTER
);

CREATE TABLE relationspec_person(
id INT PRIMARY KEY,
name TEXT,
age INT
);

CREATE TABLE deletebuilderspec_person(
id INT PRIMARY KEY,
name TEXT,
age INT
);

CREATE TABLE insertbuilderspec_person(
id INT PRIMARY KEY,
name TEXT,
age INT
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// package io.kaizensolutions.virgil
//
// import com.datastax.oss.driver.api.core.CqlSession
// import io.kaizensolutions.virgil.configuration.ConsistencyLevel
// import io.kaizensolutions.virgil.configuration.ExecutionAttributes
// import io.kaizensolutions.virgil.cql._
// import io.kaizensolutions.virgil.models.CqlExecutorSpecDatatypes._
// import zio.stream.ZStream
// import zio.test.Assertion._
// import zio.test.TestAspect._
// import zio.test._
// import zio.test.scalacheck._
// import zio.{test => _, _}
//
// import java.net.InetSocketAddress
//
// object CQLExecutorSpec {
// def executorSpec: Spec[Live with TestConfig with Sized with CassandraContainer with CQLExecutor, Any] =
// suite("Cassandra Session Interpreter Specification") {
// (queries + actions + configuration) @@ timeout(2.minutes) @@ samples(4)
// }
//
// def queries: Spec[Sized & CQLExecutor, Throwable] =
// suite("Queries") {
// test("selectFirst") {
// cql"SELECT now() FROM system.local"
// .query[SystemLocalResponse]
// .withAttributes(ExecutionAttributes.default.withConsistencyLevel(ConsistencyLevel.LocalOne))
// .execute
// .runLast
// .map(result => assertTrue(result.flatMap(_.time.toOption).get > 0L))
// } +
// test("select") {
// cql"SELECT prepared_id, logged_keyspace, query_string FROM system.prepared_statements"
// .query[PreparedStatementsResponse]
// .withAttributes(ExecutionAttributes.default.withConsistencyLevel(ConsistencyLevel.LocalOne))
// .execute
// .runCollect
// .map(results =>
// assertTrue(results.forall { r =>
// import r._
//
// query.contains("SELECT") ||
// query.contains("UPDATE") ||
// query.contains("CREATE") ||
// query.contains("DELETE") ||
// query.contains("INSERT") ||
// query.contains("USE")
// })
// )
// } +
// test("selectPage") {
// import SelectPageRow._
// // id is the primary key
// check(Gen.chunkOfN(50)(gen.toGenZIO).map(_.distinctBy(_.id))) { actual =>
// for {
// _ <- truncate.execute.runDrain
// _ <- ZIO.foreachParDiscard(actual.map(insert))(_.execute.runDrain)
// attr = ExecutionAttributes.default.withPageSize(actual.length / 2)
// all = selectAll.withAttributes(attr).execute.runCollect
// paged = selectPageStream(selectAll.withAttributes(attr)).runCollect
// result <- all.zipPar(paged)
// (dataFromSelect, dataFromPage) = result
// } yield assert(dataFromPage)(hasSameElements(dataFromSelect)) &&
// assert(dataFromSelect)(hasSameElements(actual))
// }
// } +
// test("take(1)") {
// cql"SELECT * FROM system.local".query
// .take(1)
// .execute
// .runCount
// .map(rowCount => assertTrue(rowCount > 0L))
// } +
// test("take(n > 1)") {
// check(Gen.long(2, 1000)) { n =>
// cql"SELECT * FROM system.local".query
// .take(n)
// .execute
// .runCount
// .map(rowCount => assertTrue(rowCount > 0L))
// }
// }
// }
//
// def actions: Spec[CQLExecutor, Throwable] =
// suite("Actions") {
// test("executeAction") {
// import ExecuteTestTable._
// // primary key is id
// val testGen = Gen.listOfN(10)(gen.toGenZIO).map(_.distinctBy(_.id))
// check(testGen) { elements =>
// val truncateData = truncate(table).execute.runDrain
// val toInsert = elements.map(insert(table))
// val actual = selectAllIn(table)(elements.map(_.id)).execute.runCollect.map(_.toList.sortBy(_.id))
//
// for {
// _ <- truncateData
// _ <- ZIO.foreachParDiscard(toInsert)(_.execute.runDrain)
// actual <- actual
// expected = elements.sortBy(_.id)
// } yield assertTrue(actual == expected)
// }
// } +
// test("executeBatchAction") {
// import ExecuteTestTable._
// // primary key is id
// val testGen = Gen.chunkOfN(10)(gen.toGenZIO).map(_.distinctBy(_.id))
// check(testGen) { elements =>
// val truncateData = truncate(batchTable).execute
// val batchedInsert: ZStream[CQLExecutor, Throwable, MutationResult] =
// elements
// .map(ExecuteTestTable.insert(batchTable))
// .reduce(_ + _)
// .batchType(BatchType.Unlogged)
// .execute
//
// val actual: ZStream[CQLExecutor, Throwable, ExecuteTestTable] =
// selectAllIn(batchTable)(elements.map(_.id)).execute
//
// for {
// _ <- truncateData.runDrain
// _ <- batchedInsert.runDrain
// actual <- actual.runCollect.map(_.sortBy(_.id))
// expected = elements.sortBy(_.id)
// } yield assert(expected)(hasSameElements(actual))
// }
// } +
// test("executeMutation") {
// import ExecuteTestTable._
// check(gen.toGenZIO) { data =>
// val truncateData = truncate(table).executeMutation
// val toInsert = insert(table)(data).executeMutation
// val search = selectAllIn(table)(data.id :: Nil).execute.runCollect
// truncateData *> toInsert *> search.map(result => assert(result)(hasSameElements(List(data))))
// }
// }
// } @@ sequential
//
// def configuration: Spec[CassandraContainer with Sized with TestConfig with CQLExecutor, Any] =
// suite("Session Configuration")(
// test("Creating a layer from an existing session allows you to access Cassandra") {
// val sessionScoped: URIO[CassandraContainer with Scope, CqlSession] = {
// val createSession = for {
// c <- ZIO.service[CassandraContainer]
// contactPoint <- c.getHost.zipWith(c.getPort)(InetSocketAddress.createUnresolved)
// session <- ZIO.succeed(
// CqlSession.builder
// .addContactPoint(contactPoint)
// .withLocalDatacenter("dc1")
// .withKeyspace("virgil")
// .build
// )
// } yield session
// val releaseSession = (session: CqlSession) => ZIO.succeed(session.close())
// ZIO.acquireRelease(createSession)(releaseSession).orDie
// }
//
// val sessionLayer = ZLayer.scoped(sessionScoped)
// val cqlExecutorLayer = sessionLayer >>> CQLExecutor.sessionLive
//
// cql"SELECT * FROM system.local".query.execute.runCount
// .map(numberOfRows => assertTrue(numberOfRows > 0L))
// .provideLayer(cqlExecutorLayer)
// },
// test("Timeouts are respected") {
// check(Gen.chunkOfN(4)(TimeoutCheckRow.gen.toGenZIO).map(_.distinctBy(_.id))) { rows =>
// val insert =
// ZStream
// .fromIterable(rows)
// .map(TimeoutCheckRow.insert)
// .timeout(4.seconds)
// .flatMap(_.execute)
//
// val select =
// TimeoutCheckRow.selectAll
// .timeout(2.second)
// .execute
// .runCount
//
// (insert.runDrain *> select)
// .map(c => assertTrue(c == rows.length.toLong))
// }
// } @@ samples(1),
// test("PageSize are respected and matches with chunk size") {
// check(Gen.chunkOfN(4)(PageSizeCheckRow.gen.toGenZIO).map(_.distinctBy(_.id))) { rows =>
// val insert =
// ZStream
// .fromIterable(rows)
// .map(PageSizeCheckRow.insert)
// .map(_.timeout(4.seconds))
// .flatMap(_.execute)
//
// val select =
// PageSizeCheckRow.selectAll
// .pageSize(2)
// .timeout(2.second)
// .execute
// .mapChunks(s => Chunk.single(s.size))
// .runCollect
//
// (insert.runDrain *> select)
// .map(c => assertTrue(c.size == 2 && c.forall(_ == 2)))
// }
// } @@ samples(1) @@ shrinks(0)
// )
//
// // Used to provide a similar API as the `select` method
// private def selectPageStream[ScalaType](
// query: CQL[ScalaType]
// ): ZStream[CQLExecutor, Throwable, ScalaType] =
// ZStream
// .fromZIO(query.executePage())
// .flatMap {
// case Paged(chunk, Some(page)) =>
// ZStream.from(chunk) ++
// ZStream.paginateChunkZIO(page)(nextPage =>
// query
// .executePage(Some(nextPage))
// .map(r => (Chunk.fromIterable(r.data), r.pageState))
// )
//
// case Paged(chunk, None) =>
// ZStream.from(chunk)
// }
// }
Loading

0 comments on commit c3d4dbb

Please sign in to comment.