Skip to content

Commit

Permalink
Merge pull request #379 from ucam-department-of-psychiatry/test-refac…
Browse files Browse the repository at this point in the history
…toring

Refactor python tests to use test factories
  • Loading branch information
martinburchell authored Oct 15, 2024
2 parents 45a665e + f92e399 commit 5f5b186
Show file tree
Hide file tree
Showing 27 changed files with 4,286 additions and 3,090 deletions.
2 changes: 2 additions & 0 deletions docs/source/autodoc/server/camcops_server/_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ server/camcops_server
cc_modules/cc_taskschedule.py.rst
cc_modules/cc_taskschedulereports.py.rst
cc_modules/cc_testfactories.py.rst
cc_modules/cc_testproviders.py.rst
cc_modules/cc_text.py.rst
cc_modules/cc_tracker.py.rst
cc_modules/cc_trackerhelpers.py.rst
Expand Down Expand Up @@ -649,5 +650,6 @@ server/camcops_server
templates/test/test_template_filters.mako.rst
templates/test/testpage.mako.rst
tools/fetch_snomed_codes.py.rst
tools/generate_task_factories.py.rst
tools/print_latest_github_version.py.rst
tools/run_server_self_tests.py.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.. docs/source/autodoc/server/camcops_server/cc_modules/cc_testproviders.py.rst
.. THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
.. Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
Created by Rudolf Cardinal ([email protected]).
.
This file is part of CamCOPS.
.
CamCOPS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
.
CamCOPS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
.
You should have received a copy of the GNU General Public License
along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
camcops_server.cc_modules.cc_testproviders
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: camcops_server.cc_modules.cc_testproviders
:members:
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.. docs/source/autodoc/server/camcops_server/tools/generate_task_factories.py.rst
.. THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
.. Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
Created by Rudolf Cardinal ([email protected]).
.
This file is part of CamCOPS.
.
CamCOPS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
.
CamCOPS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
.
You should have received a copy of the GNU General Public License
along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
camcops_server.tools.generate_task_factories
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: camcops_server.tools.generate_task_factories
:members:
28 changes: 21 additions & 7 deletions docs/source/developer/server_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
.. _pytest: https://docs.pytest.org/en/stable/

.. _Factory Boy: https://factoryboy.readthedocs.io/en/stable/

Testing the server code
=======================
Expand All @@ -32,12 +32,26 @@ with the filename of the module appended with ``_tests.py``. So the module
``camcops_server/cc_modules/cc_patient.py`` is tested in
``camcops_server/cc_modules/tests/cc_patient_tests.py``.

Test classes should end in ``Tests`` e.g. ``PatientTests``. Tests that require
an empty database should inherit from ``DemoRequestTestCase``. Tests that
require the demonstration database should inherit from
``DemoDatabaseTestCase``. See ``camcops_server/cc_modules/cc_unittest``. Tests
that do not require a database can just inherit from the standard python
``unittest.TestCase``
Test classes should end in ``Tests`` e.g. ``PatientTests``. A number of
``unittest.TestCase`` subclasses are defined in
``camcops_server/cc_modules/cc_unittest``.

- Tests that require an empty database and a request object should inherit from
``DemoRequestTestCase``.

- Tests that require a minimal database setup (system user, superuser set on the
request object, group administrator and a server device) should inherit from
``BasicDatabaseTestCase``.

- Tests that require the demonstration database, which has a patient and two
instances of each type of task should inherit from ``DemoDatabaseTestCase``.

- Tests that do not require a database
can just inherit from the standard python ``unittest.TestCase``.

Use `Factory Boy`_ test factories to create test instances of SQLAlchemy
database models. See ``camcops_server/cc_modules/cc_testfactories.py`` and
``camcops_server/tasks/tests/factories.py``.

.. _run_all_server_tests:

Expand Down
4 changes: 0 additions & 4 deletions server/camcops_server/cc_modules/cc_membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,6 @@ class UserGroupMembership(Base):
group = relationship("Group", back_populates="user_group_memberships")
user = relationship("User", back_populates="user_group_memberships")

def __init__(self, user_id: int, group_id: int):
self.user_id = user_id
self.group_id = group_id

@classmethod
def get_ugm_by_id(
cls, dbsession: SqlASession, ugm_id: Optional[int]
Expand Down
3 changes: 0 additions & 3 deletions server/camcops_server/cc_modules/cc_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2540,8 +2540,5 @@ def get_unittest_request(
req.set_get_params(params)

req._debugging_db_session = dbsession
user = User()
user.superuser = True
req._debugging_user = user

return req
Loading

0 comments on commit 5f5b186

Please sign in to comment.