query spec
The query spec is a useful tool for testing any queries that you depend on in your application. The way it works is this:
- You subclass
longevity.test.QuerySpec
, providing the persistent type that you are querying on, and the longevity context you’d like to use. - When the test runs, the
QuerySpec
parent class will generate a set of entities and persist them all to the test database using thetestRepoPool
. - You construct queries to test in the subclass you created.
- You call
QuerySpec.exerciseQuery(query: Query[P])
in your subclass. exerciseQuery
determines the expected results in memory, and compares it to the actual results returned by the query. It fails the test if there is a discrepancy.
For examples on how to use QuerySpec
, please see the longevity test
suite for
queries.
Take a look at the source
code for QuerySpec
as well. Unfortunately, the ScalaDoc for this class is quite useless,
as everything from ScalaTest is included there, and none of the
protected values from QuerySpec.
Some notes:
- You can override
protected val numEntities: Int
if you want to change the size of the set of test data. - There are some other
vals
andvars
there that you can use to help write your tests. - There are also a handful of methods you can use to select persistent objects to help you generate
reasonable queries:
randomP
,medianPropVal
, andorderStatPropVal
Please note that the default test data generator will not work out of the box in the face of constraint violations causing exceptions to be thrown from your constructor. In this case, you need to provide custom generators for your types. This is described in the section on enforcing constraints.
ScalaTest is an optional dependency in longevity, so you’ll need to declare a dependency on ScalaTest in your own project to use the query specs. Please use ScalaTest version 3.0.1 or later.