aboutsummaryrefslogtreecommitdiff
path: root/core-database/src/test/scala/xyz/driver/core/database/DatabaseTest.scala
blob: 1fee902243f1ba744fcb262bd2fbdbe524f6dc2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package xyz.driver.core.database

import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.prop.Checkers

class DatabaseTest extends FlatSpec with Matchers with Checkers {
  import xyz.driver.core.generators._
  "Date SQL converter" should "correctly convert back and forth to SQL dates" in {
    for (date <- 1 to 100 map (_ => nextDate())) {
      sqlDateToDate(dateToSqlDate(date)) should be(date)
    }
  }

  "Converter helper methods" should "work correctly" in {
    object TestConverter extends Converters

    val validLength                       = nextInt(10)
    val valid                             = nextToken(validLength)
    val validOp                           = Some(valid)
    val invalid                           = nextToken(validLength + nextInt(10, 1))
    val invalidOp                         = Some(invalid)
    def mapper(s: String): Option[String] = if (s.length == validLength) Some(s) else None

    TestConverter.fromStringOrThrow(valid, mapper, valid) should be(valid)

    TestConverter.expectValid(mapper, valid) should be(valid)

    TestConverter.expectExistsAndValid(mapper, validOp) should be(valid)

    TestConverter.expectValidOrEmpty(mapper, validOp) should be(Some(valid))
    TestConverter.expectValidOrEmpty(mapper, None) should be(None)

    an[DatabaseException] should be thrownBy TestConverter.fromStringOrThrow(invalid, mapper, invalid)

    an[DatabaseException] should be thrownBy TestConverter.expectValid(mapper, invalid)

    an[DatabaseException] should be thrownBy TestConverter.expectExistsAndValid(mapper, invalidOp)

    an[DatabaseException] should be thrownBy TestConverter.expectValidOrEmpty(mapper, invalidOp)
  }
}