title: SQL Alchemy Rollback Deletes Committed Entries In Unit Test
body: <p>I am writing a unit test for a new method, and it is executing the expected lines in the expected order. However, when it hits a SQL Alchemy <code>rollback</code> command, it is undoing not only the changes in the sub-method that is being tested but also deletes all of the testing entries that I had created. Does anyone know why this might have happened?</p>
  <p><strong>EDIT:</strong> I have determined that the error is because in my testing suite, <code>db_session</code> is a <code>scoped_session</code>, not the primary session. To get exactly the performance I want, I would need to mock the <code>db_session.rollback</code> method to call the rollback of a nested session, something like the lines shown below. However, when I attempt to use the lines shown, I get an error. Anyone have an idea of how I could do it instead?</p>
  <pre class="lang-py prettyprint-override"><code>nested_sess = self.db_session.begin_nested()
  with patch.object(self.db_session, &quot;rollback&quot;, nested_sess.rollback):
  </code></pre>
  <p>The code of the unit test is shown below (Although it is mostly comprised of other custom methods):</p>
  <pre class="lang-py prettyprint-override"><code># Create the mock admin user
  admin_id = random.randint(1, 90000)
  test_utils.create_user_v2(
      db_session=self.db_session, user_args={&quot;id&quot;: admin_id}
  )
  
  # Create a test site, user and active sub
  test_items: dict = self.create_needed_entries(
      site_commissioned=True,
      active_sub_count=1,
      future_sub_count=0,
  )
  
  # Pull the IDs of the test items
  test_item_ids: dict = self.pull_test_entry_ids(test_items)
  
  # Get the needed IDs
  site_id: int = test_items[self.SITE_KEY].id
  
  # Mock the admin list constant
  with patch(self.ADMIN_USER_LIST_PATCH, [admin_id]):
      # Test the handling method with the admin user
      response: MigrationResponse = handle_subscription_migration(
          db_session=self.db_session,
          site_id=site_id,
          user_id=admin_id,
          required_access=&quot;admin&quot;,
          cancel_subscription=True,
      )
  
      # Assert that the cancellation failed; Lifetimes not cancelled
      self.assert_unchanged_on_fail(response, test_item_ids)
  </code></pre>
  <p>There is a <code>self.db_session.commit()</code> statement in the method <code>self.create_needed_entries</code>, and this works as expected (Tested using VSCode debugging tools) up until the last non-return line of <code>handle_subscription_migration</code>, which is a <code>db_session.rollback</code> line; After the rollback executes, SQL alchemy methods like <code>Query.first</code> and <code>Query.all</code> start returning None rather than the entries created.</p>
  <p>I have other Unit Tests that are mostly the same logic, but work as intended. The only salient difference that I can see between them and the code shown above is that the others don't have the patch line.</p>