aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVlad Uspensky <v.uspenskiy@icloud.com>2017-02-10 16:36:51 -0800
committerGitHub <noreply@github.com>2017-02-10 16:36:51 -0800
commit6554b26e100ff7cf1b81eff5c1e895a11b878d15 (patch)
treeca84f159d76ea6e751caa5f06afa64acff4ca837
parent4adc14f77762f48436f3c56f9328a6e4bedfe824 (diff)
parentc67fb0d8e6779191620d5485f21003a2a0d9573d (diff)
downloaddriver-core-6554b26e100ff7cf1b81eff5c1e895a11b878d15.tar.gz
driver-core-6554b26e100ff7cf1b81eff5c1e895a11b878d15.tar.bz2
driver-core-6554b26e100ff7cf1b81eff5c1e895a11b878d15.zip
Merge pull request #20 from drivergroup/hhuang/date-orderingv0.10.7
Add the companion ordering object for Date
-rw-r--r--src/main/scala/xyz/driver/core/date.scala10
-rw-r--r--src/test/scala/xyz/driver/core/CoreTest.scala28
-rw-r--r--src/test/scala/xyz/driver/core/DateTest.scala51
-rw-r--r--src/test/scala/xyz/driver/core/TimeTest.scala16
4 files changed, 77 insertions, 28 deletions
diff --git a/src/main/scala/xyz/driver/core/date.scala b/src/main/scala/xyz/driver/core/date.scala
index bb88501..f0b9bf6 100644
--- a/src/main/scala/xyz/driver/core/date.scala
+++ b/src/main/scala/xyz/driver/core/date.scala
@@ -26,6 +26,16 @@ object date {
}
object Date {
+ implicit def dateOrdering: Ordering[Date] = Ordering.fromLessThan { (date1, date2) =>
+ if (date1.year != date2.year) {
+ date1.year < date2.year
+ } else if (date1.month != date2.month) {
+ date1.month < date2.month
+ } else {
+ date1.day < date2.day
+ }
+ }
+
def fromString(dateString: String): Option[Date] = {
util.Try(dateString.split("-").map(_.toInt)).toOption collect {
case Array(year, month, day) if (1 to 12 contains month) && (1 to 31 contains day) =>
diff --git a/src/test/scala/xyz/driver/core/CoreTest.scala b/src/test/scala/xyz/driver/core/CoreTest.scala
index 08b1df2..bb4742a 100644
--- a/src/test/scala/xyz/driver/core/CoreTest.scala
+++ b/src/test/scala/xyz/driver/core/CoreTest.scala
@@ -63,34 +63,6 @@ class CoreTest extends FlatSpec with Matchers with MockitoSugar {
(xid: Id[X]) should be(zid: Id[Z])
}
- "Time" should "use TimeZone correctly when converting to Date" in {
-
- import time._
-
- val EST = java.util.TimeZone.getTimeZone("EST")
- val PST = java.util.TimeZone.getTimeZone("PST")
-
- val timestamp = {
- import java.util.Calendar
- val cal = Calendar.getInstance(EST)
- cal.set(Calendar.HOUR_OF_DAY, 1)
- Time(cal.getTime().getTime())
- }
-
- textualDate(EST)(timestamp) should not be textualDate(PST)(timestamp)
- timestamp.toDate(EST) should not be timestamp.toDate(PST)
- }
-
- "Date" should "correctly convert to and from String" in {
-
- import xyz.driver.core.generators.nextDate
- import date._
-
- for (date <- 1 to 100 map (_ => nextDate())) {
- Some(date) should be(Date.fromString(date.toString))
- }
- }
-
"Name" should "have equality and ordering working correctly" in {
(Name[String]("foo") === Name[String]("foo")) should be(true)
diff --git a/src/test/scala/xyz/driver/core/DateTest.scala b/src/test/scala/xyz/driver/core/DateTest.scala
new file mode 100644
index 0000000..dc9bca3
--- /dev/null
+++ b/src/test/scala/xyz/driver/core/DateTest.scala
@@ -0,0 +1,51 @@
+package xyz.driver.core
+
+import org.scalacheck.{Arbitrary, Gen}
+import org.scalatest.prop.Checkers
+import org.scalatest.{FlatSpec, Matchers}
+import xyz.driver.core.date.Date
+
+class DateTest extends FlatSpec with Matchers with Checkers {
+ val dateGenerator = for {
+ year <- Gen.choose(0, 3000)
+ month <- Gen.choose(0, 11)
+ day <- Gen.choose(1, 31)
+ } yield Date(year, date.tagMonth(month), day)
+ implicit val arbitraryDate = Arbitrary[Date](dateGenerator)
+
+ "Date" should "correctly convert to and from String" in {
+
+ import xyz.driver.core.generators.nextDate
+ import date._
+
+ for (date <- 1 to 100 map (_ => nextDate())) {
+ Some(date) should be(Date.fromString(date.toString))
+ }
+ }
+
+ it should "have ordering defined correctly" in {
+ Seq(Date.fromString("2013-05-10"),
+ Date.fromString("2020-02-15"),
+ Date.fromString("2017-03-05"),
+ Date.fromString("2013-05-12")).sorted should
+ contain theSameElementsInOrderAs Seq(Date.fromString("2013-05-10"),
+ Date.fromString("2013-05-12"),
+ Date.fromString("2017-03-05"),
+ Date.fromString("2020-02-15"))
+
+ check { dates: List[Date] =>
+ dates.sorted.sliding(2).filter(_.size == 2).forall {
+ case Seq(a, b) =>
+ if (a.year == b.year) {
+ if (a.month == b.month) {
+ a.day < b.day
+ } else {
+ a.month < b.month
+ }
+ } else {
+ a.year < b.year
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/scala/xyz/driver/core/TimeTest.scala b/src/test/scala/xyz/driver/core/TimeTest.scala
index 76ef42c..b83137c 100644
--- a/src/test/scala/xyz/driver/core/TimeTest.scala
+++ b/src/test/scala/xyz/driver/core/TimeTest.scala
@@ -84,4 +84,20 @@ class TimeTest extends FlatSpec with Matchers with Checkers {
TimeRange(Time(432L), Time(321L)).duration should be((-111).milliseconds)
TimeRange(Time(333L), Time(333L)).duration should be(0.milliseconds)
}
+
+ "Time" should "use TimeZone correctly when converting to Date" in {
+
+ val EST = java.util.TimeZone.getTimeZone("EST")
+ val PST = java.util.TimeZone.getTimeZone("PST")
+
+ val timestamp = {
+ import java.util.Calendar
+ val cal = Calendar.getInstance(EST)
+ cal.set(Calendar.HOUR_OF_DAY, 1)
+ Time(cal.getTime().getTime())
+ }
+
+ textualDate(EST)(timestamp) should not be textualDate(PST)(timestamp)
+ timestamp.toDate(EST) should not be timestamp.toDate(PST)
+ }
}