forked from DanielaSfregola/twitter4s
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON serialization test for User model (DanielaSfregola#109)
- Loading branch information
1 parent
0a81ed1
commit f0cebfa
Showing
2 changed files
with
57 additions
and
1 deletion.
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
54 changes: 54 additions & 0 deletions
54
src/test/scala/com/danielasfregola/twitter4s/entities/SerializationSpec.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,54 @@ | ||
package com.danielasfregola.twitter4s.entities | ||
|
||
import java.util.Date | ||
|
||
import com.danielasfregola.randomdatagenerator.RandomDataGenerator | ||
import com.danielasfregola.twitter4s.http.serializers.JsonSupport | ||
import org.json4s.native.Serialization | ||
import org.scalacheck.Gen.alphaChar | ||
import org.scalacheck.{Arbitrary, Gen} | ||
import org.specs2.mutable.Specification | ||
import org.specs2.specification.core.Fragment | ||
|
||
import scala.reflect._ | ||
|
||
class SerializationSpec extends Specification with RandomDataGenerator with JsonSupport { | ||
|
||
// We serialize dates to second precision | ||
implicit val arbitraryDate: Arbitrary[Date] = Arbitrary { | ||
for { | ||
timeInMicroseconds: Long <- Gen.chooseNum(1142899200L, 1512442349L) | ||
} yield { | ||
new Date(timeInMicroseconds * 1000) | ||
} | ||
} | ||
|
||
implicit val arbitraryProfileImage: Arbitrary[ProfileImage] = Arbitrary { | ||
for { | ||
prefix: String <- Gen.nonEmptyListOf(alphaChar).map(_.mkString) | ||
suffix: String <- Gen.oneOf("_mini", "_normal", "_bigger", "") | ||
} yield { | ||
ProfileImage(s"${prefix}_$suffix.jpg") | ||
} | ||
} | ||
|
||
"JSON serialization" should { | ||
|
||
def roundtripTest[T <: AnyRef : Manifest : Arbitrary]: Fragment = { | ||
|
||
val className = classTag[T].runtimeClass.getSimpleName | ||
|
||
s"round-trip successfully for $className" in { | ||
val randomEntity = random[T] | ||
|
||
val serializedJson = Serialization.write[T](randomEntity) | ||
|
||
val deserializedEntity = Serialization.read[T](serializedJson) | ||
|
||
deserializedEntity === randomEntity | ||
} | ||
} | ||
|
||
roundtripTest[User] | ||
} | ||
} |