Tests Cookbook - papito/altitude GitHub Wiki

Check that an exception is thrown

intercept[NotFoundException] {
  // your code here
}

Check that a collection is sorted

This checks that the items are sorted by date, descending:

myCollection.sliding(2).forall(
  items => items.head.createdAt.get >= items.last.createdAt.get)

Making arbitrary DB queries

The Core test class has a utility function getManyBySql(), which will return raw JDBC rows:

testApp.txManager.asReadOnly {
  val row = this.query("SELECT name FROM person WHERE id = ?, person.persistedId).head
  row("name") should be("Gemma")
  }

Updating DB records with arbitrary SQL

The Core test class has a utility function updateBySQL()

testApp.txManager.withTransaction {
   this.update("UPDATE person SET name = ? WHERE id = ?", "Gemma", 2181833)
}

Creating assets

    val assets = 1 to 5 map { _ =>
      testContext.persistAsset()
    }

val assets = List.fill(5)(testContext.persistAsset())

Creating assets with people and faces

val people = List.fill(2)(testApp.service.person.addPerson(Person()))
testContext.addTestFacesAndAssets(people, assetCount = totalAssets)

[!WARNING]
The "faces" are not valid faces, but contain valid binary data.

Creating assets with custom data

makeAsset and persistAsset has override options to make assets in folders, triaged, recycled, etc...