Skip to content

assertExactQueries unit test helper

A unit test helper that compares all queries performed in the context manager to a .sql file in the test's folder:

with self.assertExactQueries('something.sql'):
    client.post(reverse('api:element-create'), {"corpus": ""})
-- something.sql
SELECT id FROM images_zone ;
INSERT INTO documents_element VALUES ();
======================================================================
FAIL: test_create_element (arkindex.documents.tests.test_cool_example)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/lucidiot/dev/ark/backend/arkindex/documents/tests/test_cool_example.py", line 1337, in test_create_element
    client.post(reverse('api:element-create'), {"corpus": "…"})
AssertionError: SQL queries did not match those in something.sql
  SELECT id FROM images_zone …;
+ SELECT id FROM documents_element …;
  INSERT INTO documents_element VALUES (…);

Could then be used for element deletion, ListProcessElements, or other sensitive/database heavy tests.

Edited by Erwan Rouchet