Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
add one more test case
Browse files Browse the repository at this point in the history
  • Loading branch information
mavilein committed Feb 15, 2018
1 parent 1d9c60c commit 9a0d336
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,11 @@ object MigrationStepsJsonFormatter extends DefaultReads {
implicit val deleteEnumFormat = Json.format[DeleteEnum]
implicit val updateEnumFormat = Json.format[UpdateEnum]

// implicit val createRelationFormat = Json.format[CreateRelation]
// implicit val deleteRelationFormat = Json.format[DeleteRelation]
implicit val updateRelationFormat = Json.format[UpdateRelation]

implicit val createRelationFormat: OFormat[CreateRelation] = {
val reads = (
(JsPath \ "name").read[String] and
(JsPath \ "leftModelName").read[String] and
(JsPath \ "rightModelName").read[String] and
readOneOf[String]("leftModelName", "modelAName") and
readOneOf[String]("rightModelName", "modelBName") and
(JsPath \ "modelAOnDelete").readWithDefault(OnDelete.SetNull) and
(JsPath \ "modelBOnDelete").readWithDefault(OnDelete.SetNull)
)(CreateRelation.apply _)
Expand All @@ -126,18 +122,18 @@ object MigrationStepsJsonFormatter extends DefaultReads {
OFormat(reads, writes)
}

// implicit val updateRelationFormat: OFormat[UpdateRelation] = {
// val format: OFormat[UpdateRelation] = (
// (JsPath \ "name").format[String] and
// (JsPath \ "newName").formatNullable[String] and
// (JsPath \ "modelAId").formatNullable[String] and
// (JsPath \ "modelBId").formatNullable[String] and
// (JsPath \ "modelAOnDelete").formatNullable[OnDelete.Value] and
// (JsPath \ "modelBOnDelete").formatNullable[OnDelete.Value]
// )(UpdateRelation.apply, unlift(UpdateRelation.unapply))
//
// format
// }
implicit val updateRelationFormat: OFormat[UpdateRelation] = {
val format: OFormat[UpdateRelation] = (
(JsPath \ "name").format[String] and
(JsPath \ "newName").formatNullable[String] and
(JsPath \ "modelAId").formatNullable[String] and
(JsPath \ "modelBId").formatNullable[String] and
(JsPath \ "modelAOnDelete").formatNullable[OnDelete.Value] and
(JsPath \ "modelBOnDelete").formatNullable[OnDelete.Value]
)(UpdateRelation.apply, unlift(UpdateRelation.unapply))

format
}

implicit val migrationStepFormat: Format[MigrationStep] = new Format[MigrationStep] {
val discriminatorField = "discriminator"
Expand Down Expand Up @@ -192,4 +188,6 @@ object MigrationStepsJsonFormatter extends DefaultReads {
case JsDefined(json) => rds.reads(json).map(v => Some(Some(v)))
}
}

def readOneOf[T](field1: String, field2: String)(implicit reads: Reads[T]) = (JsPath \ field1).read[T].orElse((JsPath \ field2).read[T])
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MigrationStepsJsonFormatterSpec extends FlatSpec with Matchers {

import MigrationStepsJsonFormatter._

"CreateRelation" should "be readable in the old format" in {
"CreateRelation" should "be readable in the format of January 2018" in {
val json = Json.parse("""
|{
| "name": "ListToTodo",
Expand All @@ -25,7 +25,7 @@ class MigrationStepsJsonFormatterSpec extends FlatSpec with Matchers {
create.modelBOnDelete should equal(OnDelete.SetNull)
}

"CreateRelation" should "be readable in the new format" in {
"CreateRelation" should "be readable in the format of February 2018" in {
val json = Json.parse("""
|{
| "name": "ListToTodo",
Expand All @@ -45,7 +45,27 @@ class MigrationStepsJsonFormatterSpec extends FlatSpec with Matchers {
create.modelBOnDelete should equal(OnDelete.SetNull)
}

"UpdateRelation" should "be readable in the old format" in {
"CreateRelation" should "be readable in the BROKEN format of February 2018" in {
val json = Json.parse("""
|{
| "name": "ListToTodo",
| "modelAName": "List",
| "modelBName": "Todo",
| "modelAOnDelete": "CASCADE",
| "modelBOnDelete": "SET_NULL",
| "discriminator": "CreateRelation"
| }
""".stripMargin)

val create = json.as[CreateRelation]
create.name should equal("ListToTodo")
create.modelAName should equal("List")
create.modelBName should equal("Todo")
create.modelAOnDelete should equal(OnDelete.Cascade)
create.modelBOnDelete should equal(OnDelete.SetNull)
}

"UpdateRelation" should "be readable in the format of January 2018" in {
val json = Json.parse("""
|{
| "name": "ListToTodo",
Expand All @@ -65,7 +85,7 @@ class MigrationStepsJsonFormatterSpec extends FlatSpec with Matchers {
create.modelBOnDelete should equal(None)
}

"UpdateRelation" should "be readable in the new format" in {
"UpdateRelation" should "be readable in the format of February 2018" in {
val json = Json.parse("""
|{
| "name": "ListToTodo",
Expand Down

0 comments on commit 9a0d336

Please sign in to comment.