From fc2f37b4a3c22747d0e913a2b7a379dbe7e9e7d1 Mon Sep 17 00:00:00 2001 From: vlad Date: Fri, 20 Oct 2017 14:17:05 -0700 Subject: Slick support for any Refined types, JSON format and generator for NonEmptyName, Unit-tests --- .../scala/xyz/driver/core/GeneratorsTest.scala | 10 +++++++ src/test/scala/xyz/driver/core/JsonTest.scala | 32 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) (limited to 'src/test') diff --git a/src/test/scala/xyz/driver/core/GeneratorsTest.scala b/src/test/scala/xyz/driver/core/GeneratorsTest.scala index 737cbcb..62ba7ae 100644 --- a/src/test/scala/xyz/driver/core/GeneratorsTest.scala +++ b/src/test/scala/xyz/driver/core/GeneratorsTest.scala @@ -44,6 +44,11 @@ class GeneratorsTest extends FlatSpec with Matchers with Assertions { assert(!fixedLengthName.value.exists(_.isControl)) } + it should "be able to generate com.drivergrp.core.NonEmptyName with non empty strings" in { + + assert(nextNonEmptyName[String]().value.value.nonEmpty) + } + it should "be able to generate proper UUIDs" in { nextUuid() should not be nextUuid() @@ -66,6 +71,11 @@ class GeneratorsTest extends FlatSpec with Matchers with Assertions { assert(!fixedLengthString.exists(_.isControl)) } + it should "be able to generate strings non-empty strings whic are non empty" in { + + assert(nextNonEmptyString().value.nonEmpty) + } + it should "be able to generate options which are sometimes have values and sometimes not" in { val generatedOption = nextOption("2") diff --git a/src/test/scala/xyz/driver/core/JsonTest.scala b/src/test/scala/xyz/driver/core/JsonTest.scala index bcf0ecf..26e3796 100644 --- a/src/test/scala/xyz/driver/core/JsonTest.scala +++ b/src/test/scala/xyz/driver/core/JsonTest.scala @@ -1,13 +1,17 @@ package xyz.driver.core +import eu.timepit.refined.collection.NonEmpty +import eu.timepit.refined.numeric.Positive +import eu.timepit.refined.refineMV import org.scalatest.{FlatSpec, Matchers} -import xyz.driver.core.json.{EnumJsonFormat, GadtJsonFormat, ValueClassFormat} +import xyz.driver.core.json._ import xyz.driver.core.time.provider.SystemTimeProvider import spray.json._ import xyz.driver.core.TestTypes.CustomGADT import xyz.driver.core.domain.{Email, PhoneNumber} class JsonTest extends FlatSpec with Matchers { + import DefaultJsonProtocol._ "Json format for Id" should "read and write correct JSON" in { @@ -31,6 +35,19 @@ class JsonTest extends FlatSpec with Matchers { parsedName should be(referenceName) } + "Json format for NonEmptyName" should "read and write correct JSON" in { + + val jsonFormat = json.nonEmptyNameFormat[String] + + val referenceNonEmptyName = NonEmptyName[String](refineMV[NonEmpty]("Homer")) + + val writtenJson = jsonFormat.write(referenceNonEmptyName) + writtenJson.prettyPrint should be("\"Homer\"") + + val parsedNonEmptyName = jsonFormat.read(writtenJson) + parsedNonEmptyName should be(referenceNonEmptyName) + } + "Json format for Time" should "read and write correct JSON" in { val referenceTime = new SystemTimeProvider().currentTime() @@ -168,4 +185,17 @@ class JsonTest extends FlatSpec with Matchers { parsedValue1 should be(referenceValue1) parsedValue2 should be(referenceValue2) } + + "Json format for a Refined value" should "read and write correct JSON" in { + + val jsonFormat = json.refinedJsonFormat[Int, Positive] + + val referenceRefinedNumber = refineMV[Positive](42) + + val writtenJson = jsonFormat.write(referenceRefinedNumber) + writtenJson should be("42".parseJson) + + val parsedRefinedNumber = jsonFormat.read(writtenJson) + parsedRefinedNumber should be(referenceRefinedNumber) + } } -- cgit v1.2.3