Skip to content

Instantly share code, notes, and snippets.

@edwardsmatt
Last active February 28, 2016 23:14
Show Gist options
  • Save edwardsmatt/2537fd44c995af78ddda to your computer and use it in GitHub Desktop.
Save edwardsmatt/2537fd44c995af78ddda to your computer and use it in GitHub Desktop.
How to compare evaluate equals ignoring case class members?
scala> :paste
// Entering paste mode (ctrl-D to finish)
import org.joda.time.Instant
case class ADbRecord(
id: Option[Long] = None,
name: String,
isDeleted: Boolean = false,
createdAt: Instant = Instant.now,
updatedAt: Instant = Instant.now
)
// Exiting paste mode, now interpreting.
import org.joda.time.Instant
defined class ADbRecord
scala> val db1 = ADbRecord(Some(1L), "A name value")
db1: ADbRecord = ADbRecord(Some(1),A name value,false,2016-02-25T06:25:09.891Z,2016-02-25T06:25:09.891Z)
scala> val db2 = ADbRecord(Some(1L), "A name value")
db2: ADbRecord = ADbRecord(Some(1),A name value,false,2016-02-25T06:25:40.100Z,2016-02-25T06:25:40.100Z)
scala> db1 == db2
res0: Boolean = false
// During my tests (scalatest), how can I compare these two instances of ADbRecord and ignore the Instant timestamps (which are set by the DB in practice)?
// i.e. if they have the same id, name, and isDeleted values, I would like db1 must equal(db2) to evaluate to true
@edwardsmatt
Copy link
Author

Hey all,
I have a play 2.4/Slick3.x application which has many case classes, most of which contain some kind of createdAt + updatedAt structure representing database records with createdAt and updatedAt timestamps.

During some integration tests which write to an in memory H2 instance, the createdAt and updateAt values are set by the DB. I was wondering if it is possible to 'save' a db record, and then retrieve it and verify that it matches a fixture value, ignoring the dates. Does anyone know of a library to do this in my tests that doesn't involve a lot of boiler plate code?

I have tried scalactic and using Equality[Instant] with an arbitrary tolerance, however that doesn't appear to work when the dates are nested within case classes.

Otherwise, I suppose I can just suck it up and write the code for each class to compare two instances ignoring the dates, or using scalactic to allow tolerance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment