seitime-frappe/frappe/utils/make_random.py
Conor 40ba1ac9ba fix: Postgres Compatibility
* Handle inconsistencies in type handling in DatabaseQuery & Database
  APIs
* Update incompatible queries with frappe.qb notation
* Fixed use cases discovered by failing ERPNext CI tests

fix: db independent syntax for user_type

fix: handle postgres datetime values

feat: add ability to auto commit on db inserts

feat: add ability to escape underscore in postgres

fix: handle missing data in test runner bootstrapping

fix: db independent syntax for queries

fix: refactor to use qb

fix: update cache for language

fix: use pluck in email_queue

Co-authored-by: gavin <gavin18d@gmail.com>

fix: don't auto insert on tests for make_property_setter

fix: remove auto_commit in custom_field insertion

fix: remove auto_commit functionality

fix: review comments

fix: revert link validation

fix: style suggestion for readability

Co-authored-by: gavin <gavin18d@gmail.com>

fix: revert .lower() in link validation

fix: add rollback for setup_wizard

Revert "fix: add rollback for setup_wizard"

This reverts commit 83b3b0913db17718ccd5edae01858cff15603829.

Revert "feat: add ability to escape underscore in postgres"

This reverts commit 8ed9c2aa3306438e94bb813f60e65b416d0b947b.

fix: more concise representation of order fields

Co-authored-by: gavin <gavin18d@gmail.com>
2022-01-07 10:44:57 +05:30

57 lines
1.4 KiB
Python

import frappe, random
settings = frappe._dict(
prob = {
"default": { "make": 0.6, "qty": (1,5) },
}
)
def add_random_children(doc, fieldname, rows, randomize, unique=None):
nrows = rows
if rows > 1:
nrows = random.randrange(1, rows)
for i in range(nrows):
d = {}
for key, val in randomize.items():
if isinstance(val[0], str):
d[key] = get_random(*val)
else:
d[key] = random.randrange(*val)
if unique:
if not doc.get(fieldname, {unique:d[unique]}):
doc.append(fieldname, d)
else:
doc.append(fieldname, d)
def get_random(doctype, filters=None, doc=False):
condition = []
if filters:
for key, val in filters.items():
condition.append("%s='%s'" % (key, str(val).replace("'", "\'")))
if condition:
condition = " where " + " and ".join(condition)
else:
condition = ""
out = frappe.db.multisql({
'mariadb': """select name from `tab%s` %s
order by RAND() limit 1 offset 0""" % (doctype, condition),
'postgres': """select name from `tab%s` %s
order by RANDOM() limit 1 offset 0""" % (doctype, condition)
})
out = out and out[0][0] or None
if doc and out:
return frappe.get_doc(doctype, out)
else:
return out
def can_make(doctype):
return random.random() < settings.prob.get(doctype, settings.prob["default"])["make"]
def how_many(doctype):
return random.randrange(*settings.prob.get(doctype, settings.prob["default"])["qty"])