Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scheduling_backend.read_model.reports.query
- import org.joda.time.DateTime
- import org.scalatest.{WordSpecLike, _}
- import org.scalatest.concurrent._
- import org.specs2.mock.Mockito
- import redis.RedisClient
- import scheduling_backend.read_model.reports._
- import scala.concurrent.Future
- /**
- * @author mciolek
- */
- class RedisQueryEndpointSpec extends FlatSpec with ScalaFutures with Mockito {
- import scheduling_backend.read_model.reports.query.RedisQueryEndpoint.IntRedisDeserializer
- val redisClient = mock[RedisClient]
- var endpoint = new RedisQueryEndpoint(redisClient)
- "A RedisQueryEndpoint" should "return 3 results for byDay query in bound of 3 days" in {
- //given
- val startDate = new DateTime(2015, 1, 1, 20, 0)
- val endDate = startDate.plusDays(2)
- val startDateFloor = startDate
- .withMillisOfSecond(0)
- .withSecondOfMinute(0)
- .withMinuteOfHour(0)
- .withHourOfDay(0)
- redisClient.get[Int](ByDayProbe(Flights(), startDateFloor).key) returns Future.successful(Some(1))
- redisClient.get[Int](ByDayProbe(Flights(), startDateFloor.plusDays(1)).key) returns Future.successful(Some(2))
- redisClient.get[Int](ByDayProbe(Flights(), startDateFloor.plusDays(2)).key) returns Future.successful(Some(3))
- redisClient.get[Int](ByDayProbe(Flights(), startDateFloor.plusDays(3)).key) returns Future.successful(Some(3))
- //when
- val queryResult = endpoint.findGroupedBy[Flights, ByDay](Flights(), startDate, endDate)
- //then
- whenReady(queryResult) { result =>
- assert(result(0) == QueryResult(startDateFloor, 1))
- assert(result(1) == QueryResult(startDateFloor.plusDays(1), 2))
- assert(result(2) == QueryResult(startDateFloor.plusDays(2), 3))
- }
- }
- "A RedisQueryEndpoint" should "return by ready to handle empty buckets with 0 value" in {
- //given
- val startDate = new DateTime(2015, 1, 1, 20, 0)
- val endDate = startDate.plusDays(2)
- val startDateFloor = startDate
- .withMillisOfSecond(0)
- .withSecondOfMinute(0)
- .withMinuteOfHour(0)
- .withHourOfDay(0)
- redisClient.get[Int](ByDayProbe(Flights(), startDateFloor).key) returns Future.successful(Some(1))
- redisClient.get[Int](ByDayProbe(Flights(), startDateFloor.plusDays(1)).key) returns Future.successful(None)
- //when
- val queryResult = endpoint.findGroupedBy[Flights, ByDay](Flights(), startDate, endDate)
- //then
- whenReady(queryResult) { result =>
- assert(result(0) == QueryResult(startDateFloor, 1))
- assert(result(1) == QueryResult(startDateFloor.plusDays(1), 0))
- }
- }
- "A RedisQueryEndpoint" should "return 4 results for byWeek query in bound of 16 days" in {
- //given
- val startDate = new DateTime(2015, 1, 1, 20, 0)
- val endDate = startDate.plusDays(21)
- val startDateFloor = startDate
- .withMillisOfSecond(0)
- .withSecondOfMinute(0)
- .withMinuteOfHour(0)
- .withHourOfDay(0)
- redisClient.get[Int](ByWeekProbe(Flights(), startDateFloor).key) returns Future.successful(Some(5))
- redisClient.get[Int](ByWeekProbe(Flights(), startDateFloor.plusDays(7)).key) returns Future.successful(Some(3))
- redisClient.get[Int](ByWeekProbe(Flights(), startDateFloor.plusDays(14)).key) returns Future.successful(Some(9))
- redisClient.get[Int](ByWeekProbe(Flights(), startDateFloor.plusDays(21)).key) returns Future.successful(Some(20))
- //when
- val queryResult = endpoint.findGroupedBy[Flights, ByWeek](Flights(), startDate, endDate)
- //then
- whenReady(queryResult) { result =>
- assert(result(0) == QueryResult(startDateFloor, 5))
- assert(result(1) == QueryResult(startDateFloor.plusDays(7), 3))
- assert(result(2) == QueryResult(startDateFloor.plusDays(14), 9))
- assert(result(3) == QueryResult(startDateFloor.plusDays(21), 20))
- }
- }
- "A RedisQueryEndpoint" should "return 4 results for byMonth query in bound of 4 months" in {
- //given
- val startDate = new DateTime(2015, 1, 1, 20, 0)
- val endDate = startDate.plusMonths(4)
- val startDateFloor = startDate
- .withMillisOfSecond(0)
- .withSecondOfMinute(0)
- .withMinuteOfHour(0)
- .withHourOfDay(0)
- .withDayOfMonth(1)
- redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor).key) returns Future.successful(Some(1))
- redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor.plusMonths(1)).key) returns Future.successful(Some(2))
- redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor.plusMonths(2)).key) returns Future.successful(Some(3))
- redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor.plusMonths(3)).key) returns Future.successful(Some(4))
- redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor.plusMonths(4)).key) returns Future.successful(Some(5))
- //when
- val querResult = endpoint.findGroupedBy[Flights, ByMonth](Flights(), startDate, endDate)
- //then
- whenReady(querResult) { result =>
- assert(result(0) == QueryResult(startDateFloor, 1))
- assert(result(1) == QueryResult(startDateFloor.plusMonths(1), 2))
- assert(result(2) == QueryResult(startDateFloor.plusMonths(2), 3))
- assert(result(3) == QueryResult(startDateFloor.plusMonths(3), 4))
- assert(result(4) == QueryResult(startDateFloor.plusMonths(4), 5))
- }
- }
- "A RedisQueryEndpoint" should "return 2 results for byYear query in bound of 2 years" in {
- //given
- val startDate = new DateTime(2015, 1, 1, 20, 0)
- val endDate = startDate.plusYears(1)
- val startDateFloor = startDate
- .withMillisOfSecond(0)
- .withSecondOfMinute(0)
- .withMinuteOfHour(0)
- .withHourOfDay(0)
- .withDayOfMonth(1)
- .withMonthOfYear(1)
- redisClient.get[Int](ByYearProbe(Flights(), startDateFloor).key) returns Future.successful(Some(1))
- redisClient.get[Int](ByYearProbe(Flights(), startDateFloor.plusYears(1)).key) returns Future.successful(Some(2))
- //when
- val queryResult = endpoint.findGroupedBy[Flights, ByYear](Flights(), startDate, endDate)
- //then
- whenReady(queryResult) { result =>
- assert(result(0) == QueryResult(startDateFloor, 1))
- assert(result(1) == QueryResult(startDateFloor.plusYears(1), 2))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement