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
QuerySpecparent class will generate a set of entities and persist them all to the test database using the
- You construct queries to test in the subclass you created.
- You call
QuerySpec.exerciseQuery(query: Query[P])in your subclass.
exerciseQuerydetermines 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
Take a look at the source
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.
- You can override
protected val numEntities: Intif you want to change the size of the set of test data.
- There are some other
varsthere 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
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.