-
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 #79 from sphereio/money_mongo_formats
MongoFormat for BaseMoney
- Loading branch information
Showing
5 changed files
with
224 additions
and
23 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
97 changes: 97 additions & 0 deletions
97
mongo/src/test/scala/io/sphere/mongo/format/BaseMoneyMongoFormatTest.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 java.util.Currency | ||
|
||
import io.sphere.util.{BaseMoney, HighPrecisionMoney, Money} | ||
import org.scalatest.{Matchers, WordSpec} | ||
import DefaultMongoFormats._ | ||
import io.sphere.mongo.MongoUtils._ | ||
import org.bson.BSONObject | ||
import scala.collection.JavaConverters._ | ||
|
||
class BaseMoneyMongoFormatTest extends WordSpec with Matchers { | ||
|
||
"MongoFormat[BaseMoney]" should { | ||
"be symmetric" in { | ||
val money = Money.EUR(34.56) | ||
val f = MongoFormat[Money] | ||
val dbo = f.toMongoValue(money) | ||
val readMoney = f.fromMongoValue(dbo) | ||
|
||
money should be (readMoney) | ||
} | ||
|
||
"decode with type info" in { | ||
val dbo = dbObj( | ||
"type" -> "centPrecision", | ||
"currencyCode" -> "USD", | ||
"centAmount" -> 3298 | ||
) | ||
|
||
MongoFormat[BaseMoney].fromMongoValue(dbo) should be (Money.USD(BigDecimal("32.98"))) | ||
} | ||
|
||
"decode without type info" in { | ||
val dbo = dbObj( | ||
"currencyCode" -> "USD", | ||
"centAmount" -> 3298 | ||
) | ||
|
||
MongoFormat[BaseMoney].fromMongoValue(dbo) should be (Money.USD(BigDecimal("32.98"))) | ||
} | ||
} | ||
|
||
"MongoFormat[HighPrecisionMoney]" should { | ||
"be symmetric" in { | ||
implicit val mode = BigDecimal.RoundingMode.HALF_EVEN | ||
|
||
val money = HighPrecisionMoney.fromDecimalAmount(34.123456, 6, Currency.getInstance("EUR")) | ||
val dbo = MongoFormat[HighPrecisionMoney].toMongoValue(money) | ||
|
||
val decodedMoney = MongoFormat[HighPrecisionMoney].fromMongoValue(dbo) | ||
val decodedBaseMoney = MongoFormat[BaseMoney].fromMongoValue(dbo) | ||
|
||
decodedMoney should equal (money) | ||
decodedBaseMoney should equal (money) | ||
} | ||
|
||
"decode with type info" in { | ||
val dbo = dbObj( | ||
"type" -> "highPrecision", | ||
"currencyCode" -> "USD", | ||
"preciseAmount" -> 42, | ||
"fractionDigits" -> 4 | ||
) | ||
|
||
MongoFormat[BaseMoney].fromMongoValue(dbo) should be ( | ||
HighPrecisionMoney.USD(BigDecimal("0.0042"), Some(4))) | ||
} | ||
|
||
"decode with centAmount" in { | ||
val dbo = dbObj( | ||
"type" -> "highPrecision", | ||
"currencyCode" -> "USD", | ||
"preciseAmount" -> 42, | ||
"centAmount" -> 1, | ||
"fractionDigits" -> 4 | ||
) | ||
|
||
val parsed = MongoFormat[BaseMoney].fromMongoValue(dbo) | ||
MongoFormat[BaseMoney].toMongoValue(parsed).asInstanceOf[BSONObject].toMap.asScala should be ( | ||
dbo.toMap.asScala) | ||
} | ||
|
||
"validate data when decoded from JSON" in { | ||
val dbo = dbObj( | ||
"type" -> "highPrecision", | ||
"currencyCode" -> "USD", | ||
"preciseAmount" -> 42, | ||
"fractionDigits" -> 1 | ||
) | ||
|
||
an[Exception] shouldBe thrownBy (MongoFormat[BaseMoney].fromMongoValue(dbo)) | ||
} | ||
} | ||
|
||
} |
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