-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from sphereio/optional_embedded_field
handle optional embedded field
- Loading branch information
Showing
7 changed files
with
275 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
mongo/src/test/scala/io/sphere/mongo/format/OptionMongoFormatSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.sphere.mongo.format | ||
|
||
import io.sphere.mongo.generic._ | ||
import org.scalatest.{MustMatchers, OptionValues, WordSpec} | ||
import io.sphere.mongo.MongoUtils._ | ||
import DefaultMongoFormats._ | ||
|
||
object OptionMongoFormatSpec { | ||
|
||
case class SimpleClass(value1: String, value2: Int) | ||
|
||
object SimpleClass { | ||
implicit val mongo: MongoFormat[SimpleClass] = mongoProduct(apply _) | ||
} | ||
|
||
case class ComplexClass( | ||
name: String, | ||
simpleClass: Option[SimpleClass]) | ||
|
||
object ComplexClass { | ||
implicit val mongo: MongoFormat[ComplexClass] = mongoProduct(apply _) | ||
} | ||
|
||
} | ||
|
||
|
||
class OptionMongoFormatSpec extends WordSpec with MustMatchers with OptionValues { | ||
import OptionMongoFormatSpec._ | ||
|
||
"MongoFormat[Option[_]]" should { | ||
"handle presence of all fields" in { | ||
val dbo = dbObj( | ||
"value1" -> "a", | ||
"value2" -> 45 | ||
) | ||
val result = MongoFormat[Option[SimpleClass]].fromMongoValue(dbo) | ||
result.value.value1 mustEqual "a" | ||
result.value.value2 mustEqual 45 | ||
} | ||
|
||
"handle presence of all fields mixed with ignored fields" in { | ||
val dbo = dbObj( | ||
"value1" -> "a", | ||
"value2" -> 45, | ||
"value3" -> "b" | ||
) | ||
val result = MongoFormat[Option[SimpleClass]].fromMongoValue(dbo) | ||
result.value.value1 mustEqual "a" | ||
result.value.value2 mustEqual 45 | ||
} | ||
|
||
"handle presence of not all the fields" in { | ||
val dbo = dbObj("value1" -> "a") | ||
an[Exception] mustBe thrownBy (MongoFormat[Option[SimpleClass]].fromMongoValue(dbo)) | ||
} | ||
|
||
"handle absence of all fields" in { | ||
val dbo = dbObj() | ||
val result = MongoFormat[Option[SimpleClass]].fromMongoValue(dbo) | ||
result mustEqual None | ||
} | ||
|
||
"handle absence of all fields mixed with ignored fields" in { | ||
val dbo = dbObj("value3" -> "a") | ||
val result = MongoFormat[Option[SimpleClass]].fromMongoValue(dbo) | ||
result mustEqual None | ||
} | ||
|
||
"consider all fields if the data type does not impose any restriction" in { | ||
val dbo = dbObj( | ||
"key1" -> "value1", | ||
"key2" -> "value2" | ||
) | ||
val expected = Map("key1" → "value1", "key2" → "value2") | ||
val result = MongoFormat[Map[String, String]].fromMongoValue(dbo) | ||
result mustEqual expected | ||
|
||
val maybeResult = MongoFormat[Option[Map[String, String]]].fromMongoValue(dbo) | ||
maybeResult.value mustEqual expected | ||
} | ||
|
||
"parse optional element" in { | ||
val dbo = dbObj( | ||
"name" -> "ze name", | ||
"simpleClass" -> dbObj( | ||
"value1" -> "value1", | ||
"value2" -> 42 | ||
) | ||
) | ||
val result = MongoFormat[ComplexClass].fromMongoValue(dbo) | ||
result.simpleClass.value.value1 mustEqual "value1" | ||
result.simpleClass.value.value2 mustEqual 42 | ||
|
||
MongoFormat[ComplexClass].toMongoValue(result) mustEqual dbo | ||
} | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
mongo/src/test/scala/io/sphere/mongo/generic/MongoEmbeddedSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package io.sphere.mongo.generic | ||
|
||
import io.sphere.mongo.format.MongoFormat | ||
import org.scalatest.{MustMatchers, OptionValues, WordSpec} | ||
import io.sphere.mongo.format.DefaultMongoFormats._ | ||
import io.sphere.mongo.MongoUtils._ | ||
|
||
import scala.util.Try | ||
|
||
object MongoEmbeddedSpec { | ||
case class Embedded( | ||
value1: String, | ||
@MongoKey("_value2") value2: Int) | ||
|
||
object Embedded { | ||
implicit val mongo: MongoFormat[Embedded] = mongoProduct(apply _) | ||
} | ||
|
||
case class Test1( | ||
name: String, | ||
@MongoEmbedded embedded: Embedded) | ||
|
||
object Test1 { | ||
implicit val mongo: MongoFormat[Test1] = mongoProduct(apply _) | ||
} | ||
|
||
case class Test2( | ||
name: String, | ||
@MongoEmbedded embedded: Option[Embedded] = None) | ||
|
||
object Test2 { | ||
implicit val mongo: MongoFormat[Test2] = mongoProduct(apply _) | ||
} | ||
|
||
case class Test3( | ||
@MongoIgnore name: String = "default", | ||
@MongoEmbedded embedded: Option[Embedded] = None) | ||
|
||
object Test3 { | ||
implicit val mongo: MongoFormat[Test3] = mongoProduct(apply _) | ||
} | ||
|
||
case class SubTest4(@MongoEmbedded embedded: Embedded) | ||
object SubTest4 { | ||
implicit val mongo: MongoFormat[SubTest4] = mongoProduct(apply _) | ||
} | ||
|
||
case class Test4(subField: Option[SubTest4] = None) | ||
object Test4 { | ||
implicit val mongo: MongoFormat[Test4] = mongoProduct(apply _) | ||
} | ||
} | ||
|
||
class MongoEmbeddedSpec extends WordSpec with MustMatchers with OptionValues { | ||
import MongoEmbeddedSpec._ | ||
|
||
"MongoEmbedded" should { | ||
"flatten the db object in one object" in { | ||
val dbo = dbObj( | ||
"name" -> "ze name", | ||
"value1" -> "ze value1", | ||
"_value2" -> 45 | ||
) | ||
val test1 = MongoFormat[Test1].fromMongoValue(dbo) | ||
test1.name mustEqual "ze name" | ||
test1.embedded.value1 mustEqual "ze value1" | ||
test1.embedded.value2 mustEqual 45 | ||
|
||
val result = MongoFormat[Test1].toMongoValue(test1) | ||
result mustEqual dbo | ||
} | ||
|
||
"validate that the db object contains all needed fields" in { | ||
val dbo = dbObj( | ||
"name" -> "ze name", | ||
"value1" -> "ze value1" | ||
) | ||
Try(MongoFormat[Test1].fromMongoValue(dbo)).isFailure must be (true) | ||
} | ||
|
||
"support optional embedded attribute" in { | ||
val dbo = dbObj( | ||
"name" -> "ze name", | ||
"value1" -> "ze value1", | ||
"_value2" -> 45 | ||
) | ||
val test2 = MongoFormat[Test2].fromMongoValue(dbo) | ||
test2.name mustEqual "ze name" | ||
test2.embedded.value.value1 mustEqual "ze value1" | ||
test2.embedded.value.value2 mustEqual 45 | ||
|
||
val result = MongoFormat[Test2].toMongoValue(test2) | ||
result mustEqual dbo | ||
} | ||
|
||
"ignore unknown fields" in { | ||
val dbo = dbObj( | ||
"name" -> "ze name", | ||
"value1" -> "ze value1", | ||
"_value2" -> 45, | ||
"value4" -> true | ||
) | ||
val test2 = MongoFormat[Test2].fromMongoValue(dbo) | ||
test2.name mustEqual "ze name" | ||
test2.embedded.value.value1 mustEqual "ze value1" | ||
test2.embedded.value.value2 mustEqual 45 | ||
} | ||
|
||
"ignore ignored fields" in { | ||
val dbo = dbObj( | ||
"value1" -> "ze value1", | ||
"_value2" -> 45 | ||
) | ||
val test3 = MongoFormat[Test3].fromMongoValue(dbo) | ||
test3.name mustEqual "default" | ||
test3.embedded.value.value1 mustEqual "ze value1" | ||
test3.embedded.value.value2 mustEqual 45 | ||
} | ||
|
||
"check for sub-fields" in { | ||
val dbo = dbObj( | ||
"subField" -> dbObj( | ||
"value1" -> "ze value1", | ||
"_value2" -> 45 | ||
) | ||
) | ||
val test4 = MongoFormat[Test4].fromMongoValue(dbo) | ||
test4.subField.value.embedded.value1 mustEqual "ze value1" | ||
test4.subField.value.embedded.value2 mustEqual 45 | ||
} | ||
|
||
"support the absence of optional embedded attribute" in { | ||
val dbo = dbObj( | ||
"name" -> "ze name" | ||
) | ||
val test2 = MongoFormat[Test2].fromMongoValue(dbo) | ||
test2.name mustEqual "ze name" | ||
test2.embedded mustEqual None | ||
} | ||
|
||
"validate the absence of some embedded attributes" in { | ||
val dbo = dbObj( | ||
"name" -> "ze name", | ||
"value1" -> "ze value1" | ||
) | ||
Try(MongoFormat[Test2].fromMongoValue(dbo)).isFailure must be (true) | ||
} | ||
} | ||
} |