Helper and docs for offlinesales API (#5302)

This commit is contained in:
Raphael Michel
2025-07-17 17:01:23 +02:00
committed by GitHub
parent 614a086227
commit 3ddf759a1b
7 changed files with 278 additions and 37 deletions

View File

@@ -19,37 +19,3 @@
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
#
from django.db import DEFAULT_DB_ALIAS, connections
from django.test.utils import CaptureQueriesContext
class _AssertNumQueriesContext(CaptureQueriesContext):
# Inspired by /django/test/testcases.py
# but copied over to work without the unit test module
def __init__(self, num, connection):
self.num = num
super(_AssertNumQueriesContext, self).__init__(connection)
def __exit__(self, exc_type, exc_value, traceback):
super(_AssertNumQueriesContext, self).__exit__(exc_type, exc_value, traceback)
if exc_type is not None:
return
executed = len(self)
assert executed == self.num, "%d queries executed, %d expected\nCaptured queries were:\n%s" % (
executed, self.num,
'\n'.join(
query['sql'] for query in self.captured_queries
)
)
def assert_num_queries(num, func=None, *args, **kwargs):
using = kwargs.pop("using", DEFAULT_DB_ALIAS)
conn = connections[using]
context = _AssertNumQueriesContext(num, conn)
if func is None:
return context
with context:
func(*args, **kwargs)