Skip to content

Commit

Permalink
[IMP] yaml import: log as TEST only for test files
Browse files Browse the repository at this point in the history
  • Loading branch information
KangOl committed Aug 22, 2012
1 parent 16dc736 commit 610c8d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion openerp-server
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def run_test_file(dbname, test_file):
db, registry = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False)
cr = db.cursor()
_logger.info('loading test file %s', test_file)
openerp.tools.convert_yaml_import(cr, 'base', file(test_file), {}, 'test', True)
openerp.tools.convert_yaml_import(cr, 'base', file(test_file), 'test', {}, 'test', True)
cr.rollback()
cr.close()
except Exception:
Expand Down
6 changes: 3 additions & 3 deletions openerp/modules/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
# Copyright (C) 2010-2011 OpenERP s.a. (<http://openerp.com>).
# Copyright (C) 2010-2012 OpenERP s.a. (<http://openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -84,7 +84,7 @@ def load_test(module_name, idref, mode):
_load_data(cr, module_name, idref, mode, 'test')
return True
except Exception:
_logger.error(
_logger.exception(
'module %s: an exception occurred in a test', module_name)
return False
finally:
Expand Down Expand Up @@ -119,7 +119,7 @@ def _load_data(cr, module_name, idref, mode, kind):
elif ext == '.sql':
process_sql_file(cr, fp)
elif ext == '.yml':
tools.convert_yaml_import(cr, module_name, fp, idref, mode, noupdate, report)
tools.convert_yaml_import(cr, module_name, fp, kind, idref, mode, noupdate, report)
else:
tools.convert_xml_import(cr, module_name, fp, idref, mode, noupdate, report)
finally:
Expand Down
27 changes: 15 additions & 12 deletions openerp/tools/yaml_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __getitem__(self, key):
return dict.__getitem__(self, key)

class YamlInterpreter(object):
def __init__(self, cr, module, id_map, mode, filename, report=None, noupdate=False):
def __init__(self, cr, module, id_map, mode, filename, report=None, noupdate=False, loglevel=logging.DEBUG):
self.cr = cr
self.module = module
self.id_map = id_map
Expand All @@ -110,6 +110,7 @@ def __init__(self, cr, module, id_map, mode, filename, report=None, noupdate=Fal
report = assertion_report.assertion_report()
self.assertion_report = report
self.noupdate = noupdate
self.loglevel = loglevel
self.pool = pooler.get_pool(cr.dbname)
self.uid = 1
self.context = {} # opererp context
Expand All @@ -119,6 +120,9 @@ def __init__(self, cr, module, id_map, mode, filename, report=None, noupdate=Fal
'datetime': datetime,
'timedelta': timedelta}

def _log(self, *args, **kwargs):
_logger.log(self.loglevel, *args, **kwargs)

def _ref(self):
return lambda xml_id: self.get_id(xml_id)

Expand Down Expand Up @@ -484,12 +488,10 @@ def process_context(self, node):
self.noupdate = node.noupdate

def process_python(self, node):
def log(msg, *args):
_logger.log(logging.TEST, msg, *args)
python, statements = node.items()[0]
model = self.get_model(python.model)
statements = statements.replace("\r\n", "\n")
code_context = {'model': model, 'cr': self.cr, 'uid': self.uid, 'log': log, 'context': self.context}
code_context = {'model': model, 'cr': self.cr, 'uid': self.uid, 'log': self._log, 'context': self.context}
code_context.update({'self': model}) # remove me when no !python block test uses 'self' anymore
try:
code_obj = compile(statements, self.filename, 'exec')
Expand Down Expand Up @@ -720,7 +722,7 @@ def process_delete(self, node):
if len(ids):
self.pool.get(node.model).unlink(self.cr, self.uid, ids)
else:
_logger.log(logging.TEST, "Record not deleted.")
self._log("Record not deleted.")

def process_url(self, node):
self.validate_xml_id(node.id)
Expand Down Expand Up @@ -805,7 +807,7 @@ def process(self, yaml_string):

is_preceded_by_comment = False
for node in yaml.load(yaml_string):
is_preceded_by_comment = self._log(node, is_preceded_by_comment)
is_preceded_by_comment = self._log_node(node, is_preceded_by_comment)
try:
self._process_node(node)
except YamlImportException, e:
Expand Down Expand Up @@ -852,26 +854,27 @@ def _process_node(self, node):
else:
raise YamlImportException("Can not process YAML block: %s" % node)

def _log(self, node, is_preceded_by_comment):
def _log_node(self, node, is_preceded_by_comment):
if is_comment(node):
is_preceded_by_comment = True
_logger.log(logging.TEST, node)
self._log(node)
elif not is_preceded_by_comment:
if isinstance(node, types.DictionaryType):
msg = "Creating %s\n with %s"
args = node.items()[0]
_logger.log(logging.TEST, msg, *args)
self._log(msg, *args)
else:
_logger.log(logging.TEST, node)
self._log(node)
else:
is_preceded_by_comment = False
return is_preceded_by_comment

def yaml_import(cr, module, yamlfile, idref=None, mode='init', noupdate=False, report=None):
def yaml_import(cr, module, yamlfile, kind, idref=None, mode='init', noupdate=False, report=None):
if idref is None:
idref = {}
loglevel = logging.TEST if kind == 'test' else logging.DEBUG
yaml_string = yamlfile.read()
yaml_interpreter = YamlInterpreter(cr, module, idref, mode, filename=yamlfile.name, report=report, noupdate=noupdate)
yaml_interpreter = YamlInterpreter(cr, module, idref, mode, filename=yamlfile.name, report=report, noupdate=noupdate, loglevel=loglevel)
yaml_interpreter.process(yaml_string)

# keeps convention of convert.py
Expand Down

0 comments on commit 610c8d8

Please sign in to comment.