moved test_objects to webnotes.test_objects for cross-module accessibility

This commit is contained in:
Anand Doshi 2013-02-07 16:27:02 +05:30
parent cdfde39722
commit b90bf0e0a2
2 changed files with 47 additions and 36 deletions

View file

@ -71,6 +71,7 @@ response = _dict({'message':'', 'exc':''})
debug_log = []
message_log = []
mute_emails = False
test_objects = {}
user_lang = False
lang = 'en'

View file

@ -1,73 +1,83 @@
from __future__ import unicode_literals
import sys
if __name__=="__main__":
import sys
sys.path.extend([".", "app", "lib"])
import webnotes
from webnotes.model.meta import get_link_fields
from webnotes.model.code import load_doctype_module
test_objects = {}
def make_test_records(doctype):
global test_objects
def make_test_records(doctype, verbose=0):
webnotes.mute_emails = True
if not webnotes.conn:
webnotes.connect()
# also include doctype itself
options_list = list(set([options for fieldname, options, label
in get_link_fields(doctype)] + [doctype]))
for options in options_list:
if options.startswith("link:"):
options = options[5:]
if options == "[Select]":
continue
if not options in test_objects:
test_objects[options] = []
make_test_records(options)
module = webnotes.conn.get_value("DocType", options, "module")
if options not in webnotes.test_objects:
webnotes.test_objects[options] = []
make_test_records(options, verbose)
# get methods for either [doctype].py or test_[doctype].py
doctype_module = load_doctype_module(options, module)
test_module = load_doctype_module(options, module, "test_")
load_module_and_make_records(options, verbose)
def load_module_and_make_records(options, verbose=0):
module = webnotes.conn.get_value("DocType", options, "module")
if hasattr(test_module, "make_test_records"):
test_objects[options] = test_module.make_test_records()
# get methods for either [doctype].py or test_[doctype].py
doctype_module = load_doctype_module(options, module)
test_module = load_doctype_module(options, module, "test_")
elif hasattr(doctype_module, "make_test_records"):
test_objects[options] = doctype_module.make_test_records()
if hasattr(test_module, "make_test_records"):
webnotes.test_objects[options] += test_module.make_test_records(verbose)
elif hasattr(test_module, "test_records"):
make_test_objects(test_module)
elif hasattr(doctype_module, "make_test_records"):
webnotes.test_objects[options] += doctype_module.make_test_records(verbose)
elif hasattr(doctype_module, "test_records"):
make_test_objects(doctype_module)
elif hasattr(test_module, "test_records"):
webnotes.test_objects[options] += make_test_objects(test_module)
else:
print "Please setup make_test_records for: " + options
print "-" * 60
print_mandatory_fields(options)
print
elif hasattr(doctype_module, "test_records"):
webnotes.test_objects[options] += make_test_objects(doctype_module)
def make_test_objects(moduleobj):
global test_objects
for doclist in moduleobj.test_records:
elif verbose:
print_mandatory_fields(options)
def make_test_objects(obj):
if isinstance(obj, list):
test_records = obj
else:
# obj is a module object
test_records = obj.test_records
records = []
for doclist in test_records:
d = webnotes.model_wrapper(doclist)
if webnotes.test_objects.get(d.doc.doctype):
# do not create test records, if already exists
return []
d.insert()
if not d.doc.doctype in test_objects:
test_objects[d.doc.doctype] = []
test_objects[d.doc.doctype].append(d.doc.name)
records.append(d.doc.name)
return records
def print_mandatory_fields(doctype):
print "Please setup make_test_records for: " + doctype
print "-" * 60
doctype_obj = webnotes.get_doctype(doctype)
print "Autoname: " + (doctype_obj[0].autoname or "")
print "Mandatory Fields: "
for d in doctype_obj.get({"reqd":1}):
print d.parent + ":" + d.fieldname + " | " + d.fieldtype + " | " + (d.options or "")
print
if __name__=="__main__":
webnotes.connect()
make_test_records(sys.argv[1])
make_test_records(sys.argv[1], verbose=1)