Testing
=======

All tests can be run using `tox <https://tox.wiki/en/latest/>`_ simply by running the `tox` command.  By default, tests
are run against a local sqlite2 instance.  `pyenv <https://github.com/pyenv/pyenv>`_ can be used to manage multiple
python installations.

MySql / Postgres tests
######################

By using Docker, you can also run tests against either a MySQL db and/or Postgres.

The ``IMPORT_EXPORT_TEST_TYPE`` must be set according to the type of tests you wish to run.  Set to 'postgres' for
postgres tests, and 'mysql-innodb' for mysql tests.  If this environment variable is blank (or is any other value) then
the default sqlite2 db will be used.

This process is scripted in ``runtests.sh``.  Assuming that you have docker installed on your system, running
``runtests.sh`` will run tox against sqlite2, mysql and postgres.  You can edit this script to customise testing as you
wish.

Note that this is the process which is undertaken by CI builds.

Coverage
########

Coverage data is written in parallel mode by default (defined in ``pyproject.toml``).

A simple coverage report can be obtained with

.. code-block:: bash

  make coverage

However this may omit lines which are db specific.  A full coverage report can be obtained by running tox.

After a tox run, you can view coverage data as follows:

.. code-block:: bash

  # combine all coverage data generated by tox into one file
  coverage combine

  # produce an HTML coverage report
  coverage html

Check the output of the above commands to locate the coverage HTML file.

Bulk testing
############

There is a helper script available to generate and profile bulk loads.  See ``scripts/bulk_import.py``.

You can use this script by configuring environment variables as defined above, and then installing and running the test
application.  In order to run the helper script, you will need to install ``make install-test-requirements``, and then add
`django-extensions` to `settings.py` (`INSTALLED_APPS`).

You can then run the script as follows:

.. code-block:: bash

  # run creates, updates, and deletes
  ./manage.py runscript bulk_import

  # pass 'create', 'update' or 'delete' to run the single test
  ./manage.py runscript bulk_import --script-args create

Enable logging
^^^^^^^^^^^^^^

You can see console SQL debug logging by updating the ``LOGGING`` block in `settings.py`::

    LOGGING = {
        "version": 1,
        "handlers": {"console": {"class": "logging.StreamHandler"}},
        "root": {
            "handlers": ["console"],
        },
        "loggers": {
            "django.db.backends": {"level": "DEBUG", "handlers": ["console"]},
        }
    }


