Skip to content

Commit

Permalink
Feature/indexed primitives test (#694)
Browse files Browse the repository at this point in the history
* Adding tests for indexed primitives.

* Adding roundtrip tests for indexed primitives.

* Removing dead code.
  • Loading branch information
alexflav23 authored May 26, 2017
1 parent 67ad47d commit c6d2148
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 77 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ val sharedSettings: Seq[Def.Setting[_]] = Defaults.coreDefaultSettings ++ Seq(
),
envVars := Map("SCALACTIC_FILL_FILE_PATHNAMES" -> "yes"),
gitTagName in ThisBuild := s"version=${scalaVersion.value}",
testFrameworks in Test ++= Seq(new TestFramework("org.scalameter.ScalaMeterFramework")),
parallelExecution in ThisBuild := false
) ++ VersionManagement.newSettings ++
GitProject.gitSettings ++
Expand Down Expand Up @@ -212,6 +211,7 @@ lazy val phantomFinagle = (project in file("phantom-finagle"))
name := "phantom-finagle",
moduleName := "phantom-finagle",
crossScalaVersions := Seq("2.10.6", "2.11.8", "2.12.1"),
testFrameworks in Test ++= Seq(new TestFramework("org.scalameter.ScalaMeterFramework")),
libraryDependencies ++= Seq(
"com.twitter" %% "util-core" % Versions.twitterUtil(scalaVersion.value),
"com.outworkers" %% "util-testing" % Versions.util % Test,
Expand Down Expand Up @@ -266,6 +266,7 @@ lazy val phantomStreams = (project in file("phantom-streams"))
.settings(
name := "phantom-streams",
moduleName := "phantom-streams",
testFrameworks in Test ++= Seq(new TestFramework("org.scalameter.ScalaMeterFramework")),
crossScalaVersions := Seq("2.10.6", "2.11.8", "2.12.1"),
libraryDependencies ++= Seq(
"com.typesafe" % "config" % Versions.typesafeConfig force(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,6 @@ private[phantom] trait CassandraOperations extends SessionAugmenterImplicits {
scalaQueryStringToPromise(st).future
}

protected[this] def preparedStatementToPromise(st: String)(
implicit session: Session,
executor: ExecutionContextExecutor
): ScalaPromise[PreparedStatement] = {
Manager.logger.debug(s"Executing prepared statement: ${st.toString}")

val promise = ScalaPromise[PreparedStatement]()

val future = session.prepareAsync(st)

val callback = new FutureCallback[PreparedStatement] {
def onSuccess(result: PreparedStatement): Unit = {
promise success result
}

def onFailure(err: Throwable): Unit = {
Manager.logger.error(err.getMessage)
promise failure err
}
}

Futures.addCallback(future, callback, executor)
promise
}


protected[this] def scalaQueryStringToPromise(st: Statement)(
implicit session: Session,
executor: ExecutionContextExecutor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MapColumn[
implicit ev: Primitive[Map[K, V]],
keyPrimitive: Primitive[K],
val valuePrimitive: Primitive[V]
) extends AbstractMapColumn[Owner, Record, K, V](table) with PrimitiveCollectionValue[V] {
) extends AbstractMapColumn[Owner, Record, K, V](table) {

override def keyAsCql(v: K): String = keyPrimitive.asCql(v)

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,16 @@
*/
package com.outworkers.phantom.jdk8

import java.time.{Instant, OffsetDateTime, ZoneId, ZonedDateTime}
import org.scalacheck.Gen
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import org.scalatest.{FlatSpec, Matchers}
import scala.collection.JavaConverters._
import com.outworkers.phantom.jdk8.tables._

class Jdk8TimeuuidConvertTests extends FlatSpec with Matchers with GeneratorDrivenPropertyChecks {

private[this] val genLower: Int = -100000
private[this] val genHigher: Int = -genLower

implicit override val generatorDrivenConfig: PropertyCheckConfiguration = {
PropertyCheckConfiguration(minSuccessful = 300)
}

val zoneIdGen: Gen[ZoneId] = Gen.oneOf(ZoneId.getAvailableZoneIds.asScala.toSeq) map ZoneId.of

val zonedDateTimeGen: Gen[ZonedDateTime] = for {
offset <- Gen.choose(genLower, genHigher)
time = Instant.now().toEpochMilli
dt = time + offset
zone <- zoneIdGen
} yield ZonedDateTime.ofInstant(Instant.ofEpochMilli(dt), zone)

val offsetDateTimeGen: Gen[OffsetDateTime] = for {
offset <- Gen.choose(genLower, genHigher)
time = Instant.now().toEpochMilli
dt = time + offset
zone <- zoneIdGen
} yield OffsetDateTime.ofInstant(Instant.ofEpochMilli(dt), zone)

it should "convert a ZonedDateTime to a Timeuuid and back using the ZoneID argument method" in {
forAll(zonedDateTimeGen) { dt =>
dt.timeuuid.zonedDateTime(dt.getZone) shouldEqual dt
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2013 - 2017 Outworkers Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.outworkers.phantom.jdk8.indexed

import java.time.ZoneOffset

import com.outworkers.phantom.builder.primitives.Primitive
import com.outworkers.phantom.jdk8.tables._
import org.scalacheck.Gen
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import org.scalatest.{Assertion, FlatSpec, Matchers}

class IndexedRoundtripTests extends FlatSpec with Matchers with GeneratorDrivenPropertyChecks {

private[this] val genLower: Int = -100000
private[this] val genHigher: Int = -genLower

implicit override val generatorDrivenConfig: PropertyCheckConfiguration = {
PropertyCheckConfiguration(minSuccessful = 300)
}


def roundtrip[T](gen: Gen[T])(fn: T => T)(implicit ev: Primitive[T]): Assertion = {
forAll(gen, protocolGen) { (sample, version) =>
ev.deserialize(ev.serialize(sample, version), version) shouldEqual fn(sample)
}
}

it should "serialize and deserialize ZonedDateTime instances using indexed primitives" in {
roundtrip(zonedDateTimeGen)(_.withZoneSameInstant(ZoneOffset.UTC))
}

it should "serialize and deserialize OffsetDateTime instances using indexed primitives" in {
roundtrip(offsetDateTimeGen)(_.withOffsetSameInstant(ZoneOffset.UTC))
}

it should "serialize and deserialize LocalDate instances using indexed primitives" in {
roundtrip(localDateGen)(identity)
}

it should "serialize and deserialize LocalDateTime instances using indexed primitives" in {
roundtrip(localDateTimeGen)(identity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,43 @@ package com.outworkers.phantom.jdk8

import java.time._

import com.datastax.driver.core.ProtocolVersion
import com.outworkers.util.samplers._
import org.scalacheck.Arbitrary
import org.scalacheck.{Arbitrary, Gen}

import scala.collection.JavaConverters._
import scala.util.Random

package object tables {

private[this] val genLower: Int = -100000
private[this] val genHigher: Int = -genLower

val protocolGen: Gen[ProtocolVersion] = Gen.oneOf(ProtocolVersion.values())

val zoneIdGen: Gen[ZoneId] = Gen.oneOf(ZoneId.getAvailableZoneIds.asScala.toSeq) map ZoneId.of

val zonedDateTimeGen: Gen[ZonedDateTime] = for {
offset <- Gen.choose(genLower, genHigher)
time = Instant.now().toEpochMilli
dt = time + offset
zone <- zoneIdGen
} yield ZonedDateTime.ofInstant(Instant.ofEpochMilli(dt), zone)


val localDateGen: Gen[LocalDate] = zonedDateTimeGen.map(_.toLocalDate)

val localDateTimeGen: Gen[LocalDateTime] = zonedDateTimeGen.map(_.toLocalDateTime)


val offsetDateTimeGen: Gen[OffsetDateTime] = for {
offset <- Gen.choose(genLower, genHigher)
time = Instant.now().toEpochMilli
dt = time + offset
zone <- zoneIdGen
} yield OffsetDateTime.ofInstant(Instant.ofEpochMilli(dt), zone)


implicit object ZoneIdSampler extends Sample[ZoneId] {
override def sample: ZoneId = ZoneId.of(Generators.oneOf(ZoneId.getAvailableZoneIds.asScala.toSeq))
}
Expand Down

0 comments on commit c6d2148

Please # to comment.