Skip to content

Commit

Permalink
Added test for prepared statement with constant TTL to TTLTest. Added…
Browse files Browse the repository at this point in the history
… test for prepared statement with constant limit to TimeSeriesTest, and refactored test file to avoid duplication. Added a RelationalOperatorsTest file with queries using relational operators with Scala and Twitter Futures. Merged in tests from PreparedRelationalOperatorQueryTest into RelationalOperatorsTest, using the timeSeriesTable. Removed the clicks table since it was no longer needed.
  • Loading branch information
levinson committed Mar 15, 2016
1 parent 404a605 commit 2433c0c
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 430 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
/*
* Copyright 2013-2016 Websudos, Limited.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Explicit consent must be obtained from the copyright owner, Websudos Limited before any redistribution is made.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.websudos.phantom.builder.query.db.crud

import com.twitter.util.{Future => TwitterFuture}
import com.websudos.phantom.PhantomSuite
import com.websudos.phantom.builder.query.db.ordering.TimeSeriesTest
import com.websudos.phantom.builder.query.prepared._
import com.websudos.phantom.dsl._
import com.websudos.phantom.tables._
import com.websudos.util.testing._
import org.slf4j.LoggerFactory

import scala.concurrent.{Future => ScalaFuture}

class RelationalOperatorsTest extends PhantomSuite {
val logger = LoggerFactory.getLogger(this.getClass)

val numRecords = 100
val records: Seq[TimeSeriesRecord] = TimeSeriesTest.genSequentialRecords(numRecords)

override def beforeAll(): Unit = {
super.beforeAll()

TestDatabase.timeSeriesTable.insertSchema()

val chain = for {
truncate <- TestDatabase.timeSeriesTable.truncate.future()
inserts <- TimeSeriesTest.addRecordsToBatch(records).future()
} yield inserts

chain.successful { inserts =>
logger.debug(s"Initialized table with $numRecords records")
}
}

it should "fetch records using less than operator" in {
val maxIndex = 50
val maxTimestamp = records(maxIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp < maxTimestamp)
.allowFiltering()
.fetch()

val expected = records.filter(_.timestamp.isBefore(maxTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using less than operator with Twitter Futures" in {
val maxIndex = 50
val maxTimestamp = records(maxIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp < maxTimestamp)
.allowFiltering()
.collect()

val expected = records.filter(_.timestamp.isBefore(maxTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using less than operator with prepared statement" in {
val maxIndex = 50
val maxTimestamp = records(maxIndex).timestamp

val query = TestDatabase.timeSeriesTable.select
.p_where(_.timestamp < ?)
.allowFiltering()
.prepare()

val futureResults = query.bind(maxTimestamp).fetch()
val expected = records.filter(_.timestamp.isBefore(maxTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using less than or equal operator" in {
val maxIndex = 40
val maxTimestamp = records(maxIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp <= maxTimestamp)
.allowFiltering()
.fetch()

val expected = records.filter(!_.timestamp.isAfter(maxTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using less than or equal operator with Twitter Futures" in {
val maxIndex = 40
val maxTimestamp = records(maxIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp <= maxTimestamp)
.allowFiltering()
.collect()

val expected = records.filter(!_.timestamp.isAfter(maxTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using less than or equal operator with prepared statement" in {
val maxIndex = 40
val maxTimestamp = records(maxIndex).timestamp

val query = TestDatabase.timeSeriesTable.select
.p_where(_.timestamp <= ?)
.allowFiltering()
.prepare()

val futureResults = query.bind(maxTimestamp).fetch()
val expected = records.filter(!_.timestamp.isAfter(maxTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using greater than operator" in {
val minIndex = 60
val minTimestamp = records(minIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp > minTimestamp)
.allowFiltering()
.fetch()

val expected = records.filter(_.timestamp.isAfter(minTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using greater than operator with Twitter Futures" in {
val minIndex = 60
val minTimestamp = records(minIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp > minTimestamp)
.allowFiltering()
.collect()

val expected = records.filter(_.timestamp.isAfter(minTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using greater than operator with prepared statement" in {
val minIndex = 60
val minTimestamp = records(minIndex).timestamp

val query = TestDatabase.timeSeriesTable.select
.p_where(_.timestamp > ?)
.allowFiltering()
.prepare()

val futureResults = query.bind(minTimestamp).fetch()
val expected = records.filter(_.timestamp.isAfter(minTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using greater than or equal operator" in {
val minIndex = 75
val minTimestamp = records(minIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp >= minTimestamp)
.allowFiltering()
.fetch()

val expected = records.filter(!_.timestamp.isBefore(minTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using greater than or equal operator with Twitter Futures" in {
val minIndex = 75
val minTimestamp = records(minIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp >= minTimestamp)
.allowFiltering()
.collect()

val expected = records.filter(!_.timestamp.isBefore(minTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using greater than or equal operator with prepared statement" in {
val minIndex = 75
val minTimestamp = records(minIndex).timestamp

val query = TestDatabase.timeSeriesTable.select
.p_where(_.timestamp >= ?)
.allowFiltering()
.prepare()

val futureResults = query.bind(minTimestamp).fetch()
val expected = records.filter(!_.timestamp.isBefore(minTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using less than and greater than operators" in {
val minIndex = 10
val maxIndex = 40
val minTimestamp = records(minIndex).timestamp
val maxTimestamp = records(maxIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp > minTimestamp)
.and(_.timestamp < maxTimestamp)
.allowFiltering()
.fetch()

val expected = records.filter(r => r.timestamp.isAfter(minTimestamp) && r.timestamp.isBefore(maxTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using less than and greater than operators with Twitter Futures" in {
val minIndex = 10
val maxIndex = 40
val minTimestamp = records(minIndex).timestamp
val maxTimestamp = records(maxIndex).timestamp

val futureResults = TestDatabase.timeSeriesTable.select
.where(_.timestamp > minTimestamp)
.and(_.timestamp < maxTimestamp)
.allowFiltering()
.collect()

val expected = records.filter(r => r.timestamp.isAfter(minTimestamp) && r.timestamp.isBefore(maxTimestamp))
verifyResults(futureResults, expected)
}

it should "fetch records using less than and greater than operators with prepared statement" in {
val minIndex = 10
val maxIndex = 40
val minTimestamp = records(minIndex).timestamp
val maxTimestamp = records(maxIndex).timestamp

val query = TestDatabase.timeSeriesTable.select
.p_where(_.timestamp > ?)
.p_and(_.timestamp < ?)
.allowFiltering()
.prepare()

val futureResults = query.bind(minTimestamp, maxTimestamp).fetch()
val expected = records.filter(r => r.timestamp.isAfter(minTimestamp) && r.timestamp.isBefore(maxTimestamp))
verifyResults(futureResults, expected)
}

def verifyResults(futureResults: ScalaFuture[Seq[TimeSeriesRecord]], expected: Seq[TimeSeriesRecord]): Unit = {
futureResults.successful { results =>
results.toSet shouldEqual expected.toSet
}
}

def verifyResults(futureResults: TwitterFuture[Seq[TimeSeriesRecord]], expected: Seq[TimeSeriesRecord]): Unit = {
futureResults.successful { results =>
results.toSet shouldEqual expected.toSet
}
}
}
Loading

0 comments on commit 2433c0c

Please sign in to comment.