|
| 1 | +/* |
| 2 | + * Copyright 2013-2016 Websudos, Limited. |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * - Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * - Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * - Explicit consent must be obtained from the copyright owner, Websudos Limited before any redistribution is made. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.websudos.phantom.builder.query.db.crud |
| 31 | + |
| 32 | +import com.twitter.util.{Future => TwitterFuture} |
| 33 | +import com.websudos.phantom.PhantomSuite |
| 34 | +import com.websudos.phantom.builder.query.db.ordering.TimeSeriesTest |
| 35 | +import com.websudos.phantom.builder.query.prepared._ |
| 36 | +import com.websudos.phantom.dsl._ |
| 37 | +import com.websudos.phantom.tables._ |
| 38 | +import com.websudos.util.testing._ |
| 39 | +import org.slf4j.LoggerFactory |
| 40 | + |
| 41 | +import scala.concurrent.{Future => ScalaFuture} |
| 42 | + |
| 43 | +class RelationalOperatorsTest extends PhantomSuite { |
| 44 | + val logger = LoggerFactory.getLogger(this.getClass) |
| 45 | + |
| 46 | + val numRecords = 100 |
| 47 | + val records: Seq[TimeSeriesRecord] = TimeSeriesTest.genSequentialRecords(numRecords) |
| 48 | + |
| 49 | + override def beforeAll(): Unit = { |
| 50 | + super.beforeAll() |
| 51 | + |
| 52 | + TestDatabase.timeSeriesTable.insertSchema() |
| 53 | + |
| 54 | + val chain = for { |
| 55 | + truncate <- TestDatabase.timeSeriesTable.truncate.future() |
| 56 | + inserts <- TimeSeriesTest.addRecordsToBatch(records).future() |
| 57 | + } yield inserts |
| 58 | + |
| 59 | + chain.successful { inserts => |
| 60 | + logger.debug(s"Initialized table with $numRecords records") |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + it should "fetch records using less than operator" in { |
| 65 | + val maxIndex = 50 |
| 66 | + val maxTimestamp = records(maxIndex).timestamp |
| 67 | + |
| 68 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 69 | + .where(_.timestamp < maxTimestamp) |
| 70 | + .allowFiltering() |
| 71 | + .fetch() |
| 72 | + |
| 73 | + val expected = records.filter(_.timestamp.isBefore(maxTimestamp)) |
| 74 | + verifyResults(futureResults, expected) |
| 75 | + } |
| 76 | + |
| 77 | + it should "fetch records using less than operator with Twitter Futures" in { |
| 78 | + val maxIndex = 50 |
| 79 | + val maxTimestamp = records(maxIndex).timestamp |
| 80 | + |
| 81 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 82 | + .where(_.timestamp < maxTimestamp) |
| 83 | + .allowFiltering() |
| 84 | + .collect() |
| 85 | + |
| 86 | + val expected = records.filter(_.timestamp.isBefore(maxTimestamp)) |
| 87 | + verifyResults(futureResults, expected) |
| 88 | + } |
| 89 | + |
| 90 | + it should "fetch records using less than operator with prepared statement" in { |
| 91 | + val maxIndex = 50 |
| 92 | + val maxTimestamp = records(maxIndex).timestamp |
| 93 | + |
| 94 | + val query = TestDatabase.timeSeriesTable.select |
| 95 | + .p_where(_.timestamp < ?) |
| 96 | + .allowFiltering() |
| 97 | + .prepare() |
| 98 | + |
| 99 | + val futureResults = query.bind(maxTimestamp).fetch() |
| 100 | + val expected = records.filter(_.timestamp.isBefore(maxTimestamp)) |
| 101 | + verifyResults(futureResults, expected) |
| 102 | + } |
| 103 | + |
| 104 | + it should "fetch records using less than or equal operator" in { |
| 105 | + val maxIndex = 40 |
| 106 | + val maxTimestamp = records(maxIndex).timestamp |
| 107 | + |
| 108 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 109 | + .where(_.timestamp <= maxTimestamp) |
| 110 | + .allowFiltering() |
| 111 | + .fetch() |
| 112 | + |
| 113 | + val expected = records.filter(!_.timestamp.isAfter(maxTimestamp)) |
| 114 | + verifyResults(futureResults, expected) |
| 115 | + } |
| 116 | + |
| 117 | + it should "fetch records using less than or equal operator with Twitter Futures" in { |
| 118 | + val maxIndex = 40 |
| 119 | + val maxTimestamp = records(maxIndex).timestamp |
| 120 | + |
| 121 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 122 | + .where(_.timestamp <= maxTimestamp) |
| 123 | + .allowFiltering() |
| 124 | + .collect() |
| 125 | + |
| 126 | + val expected = records.filter(!_.timestamp.isAfter(maxTimestamp)) |
| 127 | + verifyResults(futureResults, expected) |
| 128 | + } |
| 129 | + |
| 130 | + it should "fetch records using less than or equal operator with prepared statement" in { |
| 131 | + val maxIndex = 40 |
| 132 | + val maxTimestamp = records(maxIndex).timestamp |
| 133 | + |
| 134 | + val query = TestDatabase.timeSeriesTable.select |
| 135 | + .p_where(_.timestamp <= ?) |
| 136 | + .allowFiltering() |
| 137 | + .prepare() |
| 138 | + |
| 139 | + val futureResults = query.bind(maxTimestamp).fetch() |
| 140 | + val expected = records.filter(!_.timestamp.isAfter(maxTimestamp)) |
| 141 | + verifyResults(futureResults, expected) |
| 142 | + } |
| 143 | + |
| 144 | + it should "fetch records using greater than operator" in { |
| 145 | + val minIndex = 60 |
| 146 | + val minTimestamp = records(minIndex).timestamp |
| 147 | + |
| 148 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 149 | + .where(_.timestamp > minTimestamp) |
| 150 | + .allowFiltering() |
| 151 | + .fetch() |
| 152 | + |
| 153 | + val expected = records.filter(_.timestamp.isAfter(minTimestamp)) |
| 154 | + verifyResults(futureResults, expected) |
| 155 | + } |
| 156 | + |
| 157 | + it should "fetch records using greater than operator with Twitter Futures" in { |
| 158 | + val minIndex = 60 |
| 159 | + val minTimestamp = records(minIndex).timestamp |
| 160 | + |
| 161 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 162 | + .where(_.timestamp > minTimestamp) |
| 163 | + .allowFiltering() |
| 164 | + .collect() |
| 165 | + |
| 166 | + val expected = records.filter(_.timestamp.isAfter(minTimestamp)) |
| 167 | + verifyResults(futureResults, expected) |
| 168 | + } |
| 169 | + |
| 170 | + it should "fetch records using greater than operator with prepared statement" in { |
| 171 | + val minIndex = 60 |
| 172 | + val minTimestamp = records(minIndex).timestamp |
| 173 | + |
| 174 | + val query = TestDatabase.timeSeriesTable.select |
| 175 | + .p_where(_.timestamp > ?) |
| 176 | + .allowFiltering() |
| 177 | + .prepare() |
| 178 | + |
| 179 | + val futureResults = query.bind(minTimestamp).fetch() |
| 180 | + val expected = records.filter(_.timestamp.isAfter(minTimestamp)) |
| 181 | + verifyResults(futureResults, expected) |
| 182 | + } |
| 183 | + |
| 184 | + it should "fetch records using greater than or equal operator" in { |
| 185 | + val minIndex = 75 |
| 186 | + val minTimestamp = records(minIndex).timestamp |
| 187 | + |
| 188 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 189 | + .where(_.timestamp >= minTimestamp) |
| 190 | + .allowFiltering() |
| 191 | + .fetch() |
| 192 | + |
| 193 | + val expected = records.filter(!_.timestamp.isBefore(minTimestamp)) |
| 194 | + verifyResults(futureResults, expected) |
| 195 | + } |
| 196 | + |
| 197 | + it should "fetch records using greater than or equal operator with Twitter Futures" in { |
| 198 | + val minIndex = 75 |
| 199 | + val minTimestamp = records(minIndex).timestamp |
| 200 | + |
| 201 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 202 | + .where(_.timestamp >= minTimestamp) |
| 203 | + .allowFiltering() |
| 204 | + .collect() |
| 205 | + |
| 206 | + val expected = records.filter(!_.timestamp.isBefore(minTimestamp)) |
| 207 | + verifyResults(futureResults, expected) |
| 208 | + } |
| 209 | + |
| 210 | + it should "fetch records using greater than or equal operator with prepared statement" in { |
| 211 | + val minIndex = 75 |
| 212 | + val minTimestamp = records(minIndex).timestamp |
| 213 | + |
| 214 | + val query = TestDatabase.timeSeriesTable.select |
| 215 | + .p_where(_.timestamp >= ?) |
| 216 | + .allowFiltering() |
| 217 | + .prepare() |
| 218 | + |
| 219 | + val futureResults = query.bind(minTimestamp).fetch() |
| 220 | + val expected = records.filter(!_.timestamp.isBefore(minTimestamp)) |
| 221 | + verifyResults(futureResults, expected) |
| 222 | + } |
| 223 | + |
| 224 | + it should "fetch records using less than and greater than operators" in { |
| 225 | + val minIndex = 10 |
| 226 | + val maxIndex = 40 |
| 227 | + val minTimestamp = records(minIndex).timestamp |
| 228 | + val maxTimestamp = records(maxIndex).timestamp |
| 229 | + |
| 230 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 231 | + .where(_.timestamp > minTimestamp) |
| 232 | + .and(_.timestamp < maxTimestamp) |
| 233 | + .allowFiltering() |
| 234 | + .fetch() |
| 235 | + |
| 236 | + val expected = records.filter(r => r.timestamp.isAfter(minTimestamp) && r.timestamp.isBefore(maxTimestamp)) |
| 237 | + verifyResults(futureResults, expected) |
| 238 | + } |
| 239 | + |
| 240 | + it should "fetch records using less than and greater than operators with Twitter Futures" in { |
| 241 | + val minIndex = 10 |
| 242 | + val maxIndex = 40 |
| 243 | + val minTimestamp = records(minIndex).timestamp |
| 244 | + val maxTimestamp = records(maxIndex).timestamp |
| 245 | + |
| 246 | + val futureResults = TestDatabase.timeSeriesTable.select |
| 247 | + .where(_.timestamp > minTimestamp) |
| 248 | + .and(_.timestamp < maxTimestamp) |
| 249 | + .allowFiltering() |
| 250 | + .collect() |
| 251 | + |
| 252 | + val expected = records.filter(r => r.timestamp.isAfter(minTimestamp) && r.timestamp.isBefore(maxTimestamp)) |
| 253 | + verifyResults(futureResults, expected) |
| 254 | + } |
| 255 | + |
| 256 | + it should "fetch records using less than and greater than operators with prepared statement" in { |
| 257 | + val minIndex = 10 |
| 258 | + val maxIndex = 40 |
| 259 | + val minTimestamp = records(minIndex).timestamp |
| 260 | + val maxTimestamp = records(maxIndex).timestamp |
| 261 | + |
| 262 | + val query = TestDatabase.timeSeriesTable.select |
| 263 | + .p_where(_.timestamp > ?) |
| 264 | + .p_and(_.timestamp < ?) |
| 265 | + .allowFiltering() |
| 266 | + .prepare() |
| 267 | + |
| 268 | + val futureResults = query.bind(minTimestamp, maxTimestamp).fetch() |
| 269 | + val expected = records.filter(r => r.timestamp.isAfter(minTimestamp) && r.timestamp.isBefore(maxTimestamp)) |
| 270 | + verifyResults(futureResults, expected) |
| 271 | + } |
| 272 | + |
| 273 | + def verifyResults(futureResults: ScalaFuture[Seq[TimeSeriesRecord]], expected: Seq[TimeSeriesRecord]): Unit = { |
| 274 | + futureResults.successful { results => |
| 275 | + results.toSet shouldEqual expected.toSet |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + def verifyResults(futureResults: TwitterFuture[Seq[TimeSeriesRecord]], expected: Seq[TimeSeriesRecord]): Unit = { |
| 280 | + futureResults.successful { results => |
| 281 | + results.toSet shouldEqual expected.toSet |
| 282 | + } |
| 283 | + } |
| 284 | +} |
0 commit comments