Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 6.22 KB | None | 0 0
  1. package scheduling_backend.read_model.reports.query
  2.  
  3. import org.joda.time.DateTime
  4. import org.scalatest.{WordSpecLike, _}
  5. import org.scalatest.concurrent._
  6. import org.specs2.mock.Mockito
  7. import redis.RedisClient
  8. import scheduling_backend.read_model.reports._
  9.  
  10. import scala.concurrent.Future
  11.  
  12. /**
  13.  * @author mciolek
  14.  */
  15. class RedisQueryEndpointSpec extends FlatSpec with ScalaFutures with Mockito {
  16.  
  17.   import scheduling_backend.read_model.reports.query.RedisQueryEndpoint.IntRedisDeserializer
  18.  
  19.   val redisClient = mock[RedisClient]
  20.   var endpoint = new RedisQueryEndpoint(redisClient)
  21.  
  22.  
  23.   "A RedisQueryEndpoint" should "return 3 results for byDay query in bound of 3 days" in {
  24.     //given
  25.     val startDate = new DateTime(2015, 1, 1, 20, 0)
  26.     val endDate = startDate.plusDays(2)
  27.     val startDateFloor = startDate
  28.       .withMillisOfSecond(0)
  29.       .withSecondOfMinute(0)
  30.       .withMinuteOfHour(0)
  31.       .withHourOfDay(0)
  32.  
  33.     redisClient.get[Int](ByDayProbe(Flights(), startDateFloor).key) returns Future.successful(Some(1))
  34.     redisClient.get[Int](ByDayProbe(Flights(), startDateFloor.plusDays(1)).key) returns Future.successful(Some(2))
  35.     redisClient.get[Int](ByDayProbe(Flights(), startDateFloor.plusDays(2)).key) returns Future.successful(Some(3))
  36.     redisClient.get[Int](ByDayProbe(Flights(), startDateFloor.plusDays(3)).key) returns Future.successful(Some(3))
  37.  
  38.     //when
  39.     val queryResult = endpoint.findGroupedBy[Flights, ByDay](Flights(), startDate, endDate)
  40.  
  41.     //then
  42.     whenReady(queryResult) { result =>
  43.       assert(result(0) == QueryResult(startDateFloor, 1))
  44.       assert(result(1) == QueryResult(startDateFloor.plusDays(1), 2))
  45.       assert(result(2) == QueryResult(startDateFloor.plusDays(2), 3))
  46.     }
  47.  
  48.  
  49.   }
  50.  
  51.   "A RedisQueryEndpoint" should "return by ready to handle empty buckets with 0 value" in {
  52.     //given
  53.     val startDate = new DateTime(2015, 1, 1, 20, 0)
  54.     val endDate = startDate.plusDays(2)
  55.     val startDateFloor = startDate
  56.       .withMillisOfSecond(0)
  57.       .withSecondOfMinute(0)
  58.       .withMinuteOfHour(0)
  59.       .withHourOfDay(0)
  60.  
  61.     redisClient.get[Int](ByDayProbe(Flights(), startDateFloor).key) returns Future.successful(Some(1))
  62.     redisClient.get[Int](ByDayProbe(Flights(), startDateFloor.plusDays(1)).key) returns Future.successful(None)
  63.  
  64.     //when
  65.     val queryResult = endpoint.findGroupedBy[Flights, ByDay](Flights(), startDate, endDate)
  66.  
  67.     //then
  68.     whenReady(queryResult) { result =>
  69.       assert(result(0) == QueryResult(startDateFloor, 1))
  70.       assert(result(1) == QueryResult(startDateFloor.plusDays(1), 0))
  71.     }
  72.   }
  73.  
  74.   "A RedisQueryEndpoint" should "return 4 results for byWeek query in bound of 16 days" in {
  75.     //given
  76.     val startDate = new DateTime(2015, 1, 1, 20, 0)
  77.     val endDate = startDate.plusDays(21)
  78.     val startDateFloor = startDate
  79.       .withMillisOfSecond(0)
  80.       .withSecondOfMinute(0)
  81.       .withMinuteOfHour(0)
  82.       .withHourOfDay(0)
  83.  
  84.     redisClient.get[Int](ByWeekProbe(Flights(), startDateFloor).key) returns Future.successful(Some(5))
  85.     redisClient.get[Int](ByWeekProbe(Flights(), startDateFloor.plusDays(7)).key) returns Future.successful(Some(3))
  86.     redisClient.get[Int](ByWeekProbe(Flights(), startDateFloor.plusDays(14)).key) returns Future.successful(Some(9))
  87.     redisClient.get[Int](ByWeekProbe(Flights(), startDateFloor.plusDays(21)).key) returns Future.successful(Some(20))
  88.  
  89.     //when
  90.     val queryResult = endpoint.findGroupedBy[Flights, ByWeek](Flights(), startDate, endDate)
  91.  
  92.     //then
  93.     whenReady(queryResult) { result =>
  94.       assert(result(0) == QueryResult(startDateFloor, 5))
  95.       assert(result(1) == QueryResult(startDateFloor.plusDays(7), 3))
  96.       assert(result(2) == QueryResult(startDateFloor.plusDays(14), 9))
  97.       assert(result(3) == QueryResult(startDateFloor.plusDays(21), 20))
  98.     }
  99.  
  100.   }
  101.  
  102.   "A RedisQueryEndpoint" should "return 4 results for byMonth query in bound of 4 months" in {
  103.     //given
  104.     val startDate = new DateTime(2015, 1, 1, 20, 0)
  105.     val endDate = startDate.plusMonths(4)
  106.     val startDateFloor = startDate
  107.       .withMillisOfSecond(0)
  108.       .withSecondOfMinute(0)
  109.       .withMinuteOfHour(0)
  110.       .withHourOfDay(0)
  111.       .withDayOfMonth(1)
  112.  
  113.     redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor).key) returns Future.successful(Some(1))
  114.     redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor.plusMonths(1)).key) returns Future.successful(Some(2))
  115.     redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor.plusMonths(2)).key) returns Future.successful(Some(3))
  116.     redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor.plusMonths(3)).key) returns Future.successful(Some(4))
  117.     redisClient.get[Int](ByMonthProbe(Flights(), startDateFloor.plusMonths(4)).key) returns Future.successful(Some(5))
  118.  
  119.     //when
  120.     val querResult = endpoint.findGroupedBy[Flights, ByMonth](Flights(), startDate, endDate)
  121.  
  122.     //then
  123.     whenReady(querResult) { result =>
  124.       assert(result(0) == QueryResult(startDateFloor, 1))
  125.       assert(result(1) == QueryResult(startDateFloor.plusMonths(1), 2))
  126.       assert(result(2) == QueryResult(startDateFloor.plusMonths(2), 3))
  127.       assert(result(3) == QueryResult(startDateFloor.plusMonths(3), 4))
  128.       assert(result(4) == QueryResult(startDateFloor.plusMonths(4), 5))
  129.     }
  130.  
  131.   }
  132.  
  133.   "A RedisQueryEndpoint" should "return 2 results for byYear query in bound of 2 years" in {
  134.     //given
  135.     val startDate = new DateTime(2015, 1, 1, 20, 0)
  136.     val endDate = startDate.plusYears(1)
  137.     val startDateFloor = startDate
  138.       .withMillisOfSecond(0)
  139.       .withSecondOfMinute(0)
  140.       .withMinuteOfHour(0)
  141.       .withHourOfDay(0)
  142.       .withDayOfMonth(1)
  143.       .withMonthOfYear(1)
  144.  
  145.     redisClient.get[Int](ByYearProbe(Flights(), startDateFloor).key) returns Future.successful(Some(1))
  146.     redisClient.get[Int](ByYearProbe(Flights(), startDateFloor.plusYears(1)).key) returns Future.successful(Some(2))
  147.  
  148.     //when
  149.     val queryResult = endpoint.findGroupedBy[Flights, ByYear](Flights(), startDate, endDate)
  150.  
  151.     //then
  152.     whenReady(queryResult) { result =>
  153.       assert(result(0) == QueryResult(startDateFloor, 1))
  154.       assert(result(1) == QueryResult(startDateFloor.plusYears(1), 2))
  155.     }
  156.  
  157.   }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement