Uploaded image for project: 'SimplyE 2.0'
  1. SimplyE 2.0
  2. SIMPLY-3952

Run each test in a transaction that's rolled back at the end of the test

XMLWordPrintable

    • SIMPLY S23 Nov 10 - 24, SIMPLY S24 Nov 24 - December 7

      In the circulation manager's test suite we have setup and teardown code that ensures each test is run in a database subtransaction:

      https://github.com/NYPL-Simplified/server_core/blob/develop/testing.py#L186

      https://github.com/NYPL-Simplified/server_core/blob/develop/testing.py#L199

      That's good because it means each test can do whatever it wants to the database without affecting other tests. We need to bring this idea to the library registry tests. This will let us get rid of a lot of code that explicitly deletes database objects.

      But this setup/teardown technique is a little old-fashioned. Instead, modify the db_session fixture to start a transaction before yielding the session, then roll back the transaction after control returns to db_session:

      https://github.com/NYPL-Simplified/library_registry/blob/develop/tests/conftest.py#L81

      Then you should be able to get rid of all the "destroy_" type fixtures.

      I think starting a regular database transaction for each test (rather than a subtransaction, as server_core does) will be easier to start with. But eventually we want a transaction for the whole test suite, and then a subtransaction for each individual test.

            consultkbauer Keith Bauer
            leonardrichardson Leonard Richardson [X] (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: