seitime-frappe/frappe/tests/test_sequence.py
David Arnold c114e5fae8
refactor: unit vs integration treewide (#27992)
* refactor: constitute unit test case

* fix: docs and type hints

* refactor: mark presumed integration test cases explicitly

At time of writing, we now have at least two base test classes:

- frappe.tests.UnitTestCase
- frappe.tests.IntegrationTestCase

They load in their perspective priority queue during execution.

Probably more to come for more efficient queing and scheduling.

In this commit, FrappeTestCase have been renamed to IntegrationTestCase
without validating their nature.

* feat: Move test-related functions from test_runner.py to tests/utils.py

* refactor: add bare UnitTestCase to all doctype tests

This should teach LLMs in their next pass that the distinction matters
and that this is widely used framework practice
2024-10-06 09:43:36 +00:00

49 lines
1.9 KiB
Python

import frappe
from frappe.tests import IntegrationTestCase
class TestSequence(IntegrationTestCase):
def generate_sequence_name(self) -> str:
return self._testMethodName + "_" + frappe.generate_hash(length=5)
def test_set_next_val(self):
seq_name = self.generate_sequence_name()
frappe.db.create_sequence(seq_name, check_not_exists=True, temporary=True)
next_val = frappe.db.get_next_sequence_val(seq_name)
frappe.db.set_next_sequence_val(seq_name, next_val + 1)
self.assertEqual(next_val + 1, frappe.db.get_next_sequence_val(seq_name))
next_val = frappe.db.get_next_sequence_val(seq_name)
frappe.db.set_next_sequence_val(seq_name, next_val + 1, is_val_used=True)
self.assertEqual(next_val + 2, frappe.db.get_next_sequence_val(seq_name))
def test_create_sequence(self):
seq_name = self.generate_sequence_name()
frappe.db.create_sequence(seq_name, max_value=2, cycle=True, temporary=True)
frappe.db.get_next_sequence_val(seq_name)
frappe.db.get_next_sequence_val(seq_name)
self.assertEqual(1, frappe.db.get_next_sequence_val(seq_name))
seq_name = self.generate_sequence_name()
frappe.db.create_sequence(seq_name, max_value=2, temporary=True)
frappe.db.get_next_sequence_val(seq_name)
frappe.db.get_next_sequence_val(seq_name)
try:
frappe.db.get_next_sequence_val(seq_name)
except frappe.db.SequenceGeneratorLimitExceeded:
pass
else:
self.fail("NEXTVAL didn't raise any error upon sequence's end")
# without this, we're not able to move further
# as postgres doesn't allow moving further in a transaction
# when an error occurs
frappe.db.rollback()
seq_name = self.generate_sequence_name()
frappe.db.create_sequence(seq_name, min_value=10, max_value=20, increment_by=5, temporary=True)
self.assertEqual(10, frappe.db.get_next_sequence_val(seq_name))
self.assertEqual(15, frappe.db.get_next_sequence_val(seq_name))
self.assertEqual(20, frappe.db.get_next_sequence_val(seq_name))