Skip to content

Commit

Permalink
test: rollback test transaction after executing cmd (frappe#19606)
Browse files Browse the repository at this point in the history
In command tests if connection is active then due to repeatable read
isolation you will continue to read old data which might be modified by
the command you're trying to test.

It makes sense to end transaction after each command execution here.

[skip ci]
  • Loading branch information
ankush authored Jan 16, 2023
1 parent c7edd7e commit 433115f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions frappe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def setUpClass(cls) -> None:

@classmethod
def execute(self, command, kwargs=None):
# tests might have written to DB which wont be visible to commands until we end current transaction
frappe.db.commit()

site = {"site": frappe.local.site}
cmd_input = None
if kwargs:
Expand All @@ -165,6 +168,9 @@ def execute(self, command, kwargs=None):
self.stderr = clean(self._proc.stderr)
self.returncode = clean(self._proc.returncode)

# Commands might have written to DB which wont be visible until we end current transaction
frappe.db.rollback()

@classmethod
def setup_test_site(cls):
cmd_config = {
Expand Down Expand Up @@ -407,24 +413,17 @@ def test_set_password(self):

self.execute("bench --site {site} set-password Administrator test1")
self.assertEqual(self.returncode, 0)
frappe.db.rollback()
self.assertEqual(check_password("Administrator", "test1"), "Administrator")
# to release the lock taken by check_password
frappe.db.rollback()

self.execute("bench --site {site} set-admin-password test2")
self.assertEqual(self.returncode, 0)
frappe.db.rollback()
self.assertEqual(check_password("Administrator", "test2"), "Administrator")
frappe.db.rollback()

# Reset it back to original password
original_password = frappe.conf.admin_password or "admin"
self.execute("bench --site {site} set-admin-password %s" % original_password)
self.assertEqual(self.returncode, 0)
frappe.db.rollback()
self.assertEqual(check_password("Administrator", original_password), "Administrator")
frappe.db.rollback()

@skipIf(
not (
Expand Down

0 comments on commit 433115f

Please sign in to comment.