seitime-frappe/frappe/tests/test_sequence.py
Ritwik Puri b41379c78b
fix: misc fixes (integer primary keys) (#16307)
* fix: misc fixes

local.x gets resetted on every request so switched to a simple dict

simplified is_val_used in set_next_val function for sequences

* chore: use multisql for sequence methods

* fix: fields not updating on form

* minor(base_input): removed unnecessary branching in update_input

* chore: remove prints and rename autoincremented_status_map

* chore: added proper type hint + comment + formatting

* fix: added searching in cast_name rather than handling it manually

* fix: share condition query + test_build_match_conditions

* fix: add cast_name to more places

* test: test for sequence

* fix: sequence functions

* fix: inherit frappetestcase

* minor: attach sequence methods to db context local

* chore: update sequence function names in Database

use frappe.db for sequences in naming.py

* fix: convert filename to str (for autoincremented doctypes)

* chore: better regex for modifying values for postgres

* minor: allow changing name column type (if no data is present in the doctype)

* refactor: validate_autoname

converted it to a simple function
enabled changing autoincrement autoname from customize form

* fix: use sql_ddl for change_column_type in postgres

* fix: use not null constraint in postgres when changing name type

* fix(test): updated test_autoincremented_doctype_transition with transitioning when no data is present

* fix(test): updated test_cast_name

probably messed up during rebase

* fix(test): used rollback upon error in transaction for postgres

* chore: use frappe.db.x methods for sequences

* minor: use temporary sequences in test

* minor: use generate_hash for sequence naming in sequence tests

* chore: replace sequence imports with frappe.db.x

* chore: move out casting name fields to a separate method

* refactor: cast_name

more explicit cases for casts and added docstring

* fix: added space in test_cast_name

* chore: fix linter

* chore: better naming for can_change_name_column_type

* chore: add comment for autoincremented_site_status_map

* chore: update/add docstrings
2022-04-29 15:06:03 +05:30

54 lines
2 KiB
Python

import psycopg2
import pymysql
import frappe
from frappe.tests.utils import FrappeTestCase
class TestSequence(FrappeTestCase):
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 pymysql.err.OperationalError as e:
self.assertEqual(e.args[0], 4084)
except psycopg2.errors.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))