def __init__(self, tests=()): super(OOTestSuite, self).__init__(tests) self.config = config_from_environment('DESTRAL', ['module'], use_template=True) ooconfig = {} self.config['use_template'] = False ooconfig['demo'] = {'all': 1} if self.config['module'] == 'base': ooconfig.setdefault('update', {}) ooconfig['update'].update({'base': 1}) self.openerp = OpenERPService(**ooconfig) self.drop_database = True
class OOTestCase(unittest.TestCase): """Base class to inherit test cases from for OpenERP Testing Framework. """ @property def database(self): return self.openerp.db_name def setUp(self): self.config = config_from_environment('DESTRAL', ['module']) self.openerp = OpenERPService() self.drop_database = False if not self.openerp.db_name: self.openerp.db_name = self.openerp.create_database() self.drop_database = True self.openerp.install_module(self.config['module']) def test_all_views(self): logger.info('Testing views for module %s', self.config['module']) imd_obj = self.openerp.pool.get('ir.model.data') view_obj = self.openerp.pool.get('ir.ui.view') with Transaction().start(self.database) as txn: imd_ids = imd_obj.search(txn.cursor, txn.user, [ ('model', '=', 'ir.ui.view'), ('module', '=', self.config['module']) ]) if imd_ids: views = {} for imd in imd_obj.read(txn.cursor, txn.user, imd_ids): view_xml_name = '{}.{}'.format(imd['module'], imd['name']) views[imd['res_id']] = view_xml_name view_ids = views.keys() logger.info('Testing %s views...', len(view_ids)) for view in view_obj.browse(txn.cursor, txn.user, view_ids): view_xml_name = views[view.id] model = self.openerp.pool.get(view.model) if model is None: # Check if model exists raise Exception( 'View (xml id: %s) references model %s which does ' 'not exist' % (view_xml_name, view.model) ) logger.info('Testing view %s (id: %s)', view.name, view.id) model.fields_view_get(txn.cursor, txn.user, view.id, view.type) def tearDown(self): if self.drop_database: self.openerp.drop_database() self.openerp.config['db_name'] = False
def get_dependencies(module, addons_path=None, deps=None): """Get all the dependencies of a module without database Using `__terp__.py` files and is used to check requirements.txt in the dependencies. :param module: Module to find the dependencies :param addons_path: Path to find the modules :return: a listt of dependencies. """ if deps is None: deps = [] if addons_path is None: from destral.openerp import OpenERPService service = OpenERPService() addons_path = service.config['addons_path'] pj = os.path.join module_path = pj(addons_path, module) if not os.path.exists(module_path): raise Exception('Module \'{}\' not found in {}'.format( module, addons_path)) terp_path = pj(module_path, '__terp__.py') if not os.path.exists(terp_path): raise Exception( 'Module {} is not a valid module. Missing __terp__.py file'.format( module)) with open(terp_path, 'r') as terp_file: terp = literal_eval(terp_file.read()) for dep in terp['depends']: if dep not in deps: deps.append(dep) deps += get_dependencies(dep, addons_path, deps) return list(set(deps))
class OOTestCase(unittest.TestCase): """Base class to inherit test cases from for OpenERP Testing Framework. """ @property def database(self): return self.openerp.db_name def setUp(self): self.config = config_from_environment('DESTRAL', ['module']) self.openerp = OpenERPService() if not self.openerp.db_name: self.openerp.db_name = self.openerp.create_database() self.openerp.install_module(self.config['module']) def test_all_views(self): logger.info('Testing views for module %s', self.config['module']) imd_obj = self.openerp.pool.get('ir.model.data') view_obj = self.openerp.pool.get('ir.ui.view') with Transaction().start(self.database) as txn: imd_ids = imd_obj.search(txn.cursor, txn.user, [ ('model', '=', 'ir.ui.view'), ('module', '=', self.config['module']) ]) if imd_ids: views = {} for imd in imd_obj.read(txn.cursor, txn.user, imd_ids): view_xml_name = '{}.{}'.format(imd['module'], imd['name']) views[imd['res_id']] = view_xml_name view_ids = views.keys() logger.info('Testing %s views...', len(view_ids)) for view in view_obj.browse(txn.cursor, txn.user, view_ids): view_xml_name = views[view.id] model = self.openerp.pool.get(view.model) if model is None: # Check if model exists raise Exception( 'View (xml id: %s) references model %s which does ' 'not exist' % (view_xml_name, view.model) ) logger.info('Testing view %s (id: %s)', view.name, view.id) model.fields_view_get(txn.cursor, txn.user, view.id, view.type) def tearDown(self): self.openerp.drop_database() self.openerp.config['db_name'] = False
class OOTestSuite(unittest.TestSuite): def __init__(self, tests=()): super(OOTestSuite, self).__init__(tests) self.config = config_from_environment('DESTRAL', ['module'], use_template=True) ooconfig = {} self.config['use_template'] = False ooconfig['demo'] = {'all': 1} if self.config['module'] == 'base': ooconfig.setdefault('update', {}) ooconfig['update'].update({'base': 1}) self.openerp = OpenERPService(**ooconfig) self.drop_database = True def run(self, result, debug=False): """Run the test suite * Sets the config using environment variables prefixed with `DESTRAL_`. * Creates a new OpenERP service. * Installs the module to test if a database is not defined. """ module_suite = not result._testRunEntered if module_suite: if not self.openerp.db_name: self.openerp.db_name = self.openerp.create_database( self.config['use_template']) else: self.drop_database = False result.db_name = self.openerp.db_name self.openerp.install_module(self.config['module']) else: self.openerp.db_name = result.db_name res = super(OOTestSuite, self).run(result, debug) module_suite = not result._testRunEntered if module_suite: if self.drop_database: self.openerp.drop_database() self.openerp.db_name = False return res def _handleClassSetUp(self, test, result): test_class = test.__class__ test_class.openerp = self.openerp test_class.config = self.config super(OOTestSuite, self)._handleClassSetUp(test, result)
def setUp(self): self.config = config_from_environment('DESTRAL', ['module']) self.openerp = OpenERPService() self.drop_database = False if not self.openerp.db_name: self.openerp.db_name = self.openerp.create_database() self.drop_database = True self.openerp.install_module(self.config['module'])
def start(self, database_name, user=1, context=None): self._assert_stopped() self.service = OpenERPService(db_name=database_name) self.pool = self.service.pool self.cursor = self.service.db.cursor() self.user = user self.context = context if context is not None else self.get_context() return self
class OOTestSuite(unittest.TestSuite): def __init__(self, tests=()): super(OOTestSuite, self).__init__(tests) self.config = config_from_environment( 'DESTRAL', ['module', 'testing_langs'], use_template=True, testing_langs=[] ) ooconfig = {} self.config['use_template'] = False ooconfig['demo'] = {'all': 1} if self.config['module'] == 'base': ooconfig.setdefault('update', {}) ooconfig['update'].update({'base': 1}) self.openerp = OpenERPService(**ooconfig) self.drop_database = True def run(self, result, debug=False): """Run the test suite * Sets the config using environment variables prefixed with `DESTRAL_`. * Creates a new OpenERP service. * Installs the module to test if a database is not defined. """ module_suite = not result._testRunEntered if module_suite: if not self.openerp.db_name: self.openerp.db_name = self.openerp.create_database( self.config['use_template'] ) else: self.drop_database = False result.db_name = self.openerp.db_name self.openerp.install_module(self.config['module']) else: self.openerp.db_name = result.db_name res = super(OOTestSuite, self).run(result, debug) module_suite = not result._testRunEntered if module_suite: if self.drop_database: self.openerp.drop_database() self.openerp.db_name = False else: logger.info('Not dropping database %s', self.openerp.db_name) self.openerp.enable_admin() return res def _handleClassSetUp(self, test, result): test_class = test.__class__ test_class.openerp = self.openerp test_class.config = self.config super(OOTestSuite, self)._handleClassSetUp(test, result)
def __init__(self, tests=()): super(OOTestSuite, self).__init__(tests) self.config = config_from_environment( 'DESTRAL', ['module', 'testing_langs'], use_template=True, testing_langs=[] ) ooconfig = {} self.config['use_template'] = False ooconfig['demo'] = {'all': 1} if self.config['module'] == 'base': ooconfig.setdefault('update', {}) ooconfig['update'].update({'base': 1}) self.openerp = OpenERPService(**ooconfig) self.drop_database = True
def start(self, database_name, user=1, context=None): """Start a new transaction :param database_name: Database name :param user: User id :param context: Context to be used """ self._assert_stopped() self.service = OpenERPService(db_name=database_name) self.pool = self.service.pool self.cursor = self.service.db.cursor() self.user = user self.context = context if context is not None else self.get_context() return self
def destral(modules, tests): sys.argv = sys.argv[:1] service = OpenERPService() if not modules: paths = subprocess.check_output( ["git", "diff", "--name-only", "HEAD~1..HEAD"]) paths = [x for x in paths.split('\n') if x] modules_to_test = [] for path in paths: module = detect_module(path) if module and module not in modules_to_test: modules_to_test.append(module) else: modules_to_test = modules[:] results = [] for module in modules_to_test: req = os.path.join(service.config['addons_path'], module, 'requirements.txt') pip = os.path.join(sys.prefix, 'bin', 'pip') if os.path.exists(req) and os.path.exists(pip): logger.info('Requirements file %s found. Installing...', req) subprocess.check_call([pip, "install", "-r", req]) logger.info('Testing module %s', module) os.environ['DESTRAL_MODULE'] = module tests_module = 'addons.{}.tests'.format(module) logger.debug('Test module: %s', tests_module) if tests: tests = ['{}.{}'.format(tests_module, t) for t in tests] suite = unittest.TestLoader().loadTestsFromNames(tests) else: try: suite = unittest.TestLoader().loadTestsFromName(tests_module) except AttributeError, e: logger.debug('Test suits not found...%s', e) suite = unittest.TestSuite() if not suite.countTestCases(): suite = unittest.TestLoader().loadTestsFromName('destral.testing') result = unittest.TextTestRunner(verbosity=2).run(suite) if not result.wasSuccessful(): results.append(False) else: results.append(True)
def start(self, database_name, user=1, context=None): """Start a new transaction :param database_name: Database name :param user: User id :param context: Context to be used """ self._assert_stopped() self.service = OpenERPService(db_name=database_name) self.pool = self.service.pool self.cursor = self.service.db.cursor() self.user = user try: receivers = DB_CURSOR_EXECUTE.receivers DB_CURSOR_EXECUTE.receivers = {} self.context = context if context is not None else self.get_context( ) finally: DB_CURSOR_EXECUTE.receivers = receivers return self
def destral(modules, tests, all_tests=None, enable_coverage=None, report_coverage=None, report_junitxml=None, dropdb=None, requirements=None, **kwargs): enable_lint = kwargs.pop('enable_lint') database = kwargs.pop('database') if database: os.environ['OPENERP_DB_NAME'] = database sys.argv = sys.argv[:1] service = OpenERPService() if report_junitxml: os.environ['DESTRAL_JUNITXML'] = report_junitxml else: report_junitxml = os.environ.get('DESTRAL_JUNITXML', False) if report_junitxml: junitxml_directory = os.path.abspath(report_junitxml) if not os.path.isdir(junitxml_directory): os.makedirs(junitxml_directory) if not modules: ci_pull_request = os.environ.get('CI_PULL_REQUEST') token = os.environ.get('GITHUB_TOKEN') repository = os.environ.get('CI_REPO') if ci_pull_request and token and repository: try: int(ci_pull_request) except: # If CI_PULL_REQUEST contains URL instead of PR number, get it ci_pull_request = ci_pull_request.split('/')[-1] url = 'https://api.github.com/repos/{repo}/pulls/{pr_number}'.format( repo=repository, pr_number=ci_pull_request) req = requests.get(url, headers={ 'Authorization': 'token {0}'.format(token), 'Accept': 'application/vnd.github.patch' }) paths = find_files(req.text) logger.info('Files from Pull Request: {0}: {1}'.format( ci_pull_request, ', '.join(paths))) else: paths = subprocess.check_output( ["git", "diff", "--name-only", "HEAD~1..HEAD"]).decode('utf-8') paths = [x for x in paths.split('\n') if x] logger.info('Files from last commit: {}'.format(', '.join(paths))) modules_to_test = [] for path in paths: module = detect_module(path) if module and module not in modules_to_test: modules_to_test.append(module) else: modules_to_test = modules[:] results = [] addons_path = service.config['addons_path'] root_path = service.config['root_path'] if not modules_to_test: coverage_config = {'source': [root_path], 'omit': ['*/addons/*/*']} else: coverage_config = { 'source': coverage_modules_path(modules_to_test, addons_path), 'omit': ['*/__terp__.py'] } coverage = OOCoverage(**coverage_config) coverage.enabled = (enable_coverage or report_coverage) junitxml_suites = [] coverage.start() server_spec_suite = get_spec_suite(root_path) if server_spec_suite: logging.info('Spec testing for server') report = run_spec_suite(server_spec_suite) results.append(not len(report.failed_examples) > 0) if report_junitxml: junitxml_suites += report.create_report_suites() coverage.stop() logger.info('Modules to test: {}'.format(','.join(modules_to_test))) for module in modules_to_test: with RestorePatchedRegisterAll(): if requirements: install_requirements(module, addons_path) spec_suite = get_spec_suite(os.path.join(addons_path, module)) if spec_suite: logger.info('Spec testing module %s', module) coverage.start() report = run_spec_suite(spec_suite) coverage.stop() results.append(not len(report.failed_examples) > 0) if report_junitxml: junitxml_suites += report.create_report_suites() logger.info('Unit testing module %s', module) os.environ['DESTRAL_MODULE'] = module coverage.start() suite = get_unittest_suite(module, tests) suite.drop_database = dropdb suite.config['all_tests'] = all_tests if all_tests: for m in get_dependencies(module, addons_path): for test in get_unittest_suite(m): if test not in suite: suite.addTest(test) result = run_unittest_suite(suite) coverage.stop() results.append(result.wasSuccessful()) if report_junitxml: junitxml_suites.append(result.get_test_suite(module)) if report_junitxml: from junit_xml import TestSuite for suite in junitxml_suites: with open(os.path.join(report_junitxml, suite.name + '.xml'), 'w') as report_file: report_file.write(TestSuite.to_xml_string([suite])) logger.info('Saved report XML on {}/'.format(report_junitxml)) if report_coverage: coverage.report() if enable_coverage: coverage.save() if enable_lint: modules_path = [ '{}/{}'.format(addons_path, m) for m in modules_to_test ] if modules_path: run_linter(modules_path) if not all(results): sys.exit(1)
def destral(modules, tests, enable_coverage=None, report_coverage=None, dropdb=None): sys.argv = sys.argv[:1] service = OpenERPService() if not modules: ci_pull_request = os.environ.get('CI_PULL_REQUEST') token = os.environ.get('GITHUB_TOKEN') if ci_pull_request and token: url = 'https://api.github.com/repos/{repo}/pulls/{pr_number}'.format( repo=os.environ.get('CI_REPO'), pr_number=ci_pull_request) req = urllib2.Request(url, headers={ 'Authorization': 'token {0}'.format(token), 'Accept': 'application/vnd.github.patch' }) f = urllib2.urlopen(req) paths = find_files(f.read()) logger.info('Files from Pull Request: {0}: {1}'.format( ci_pull_request, ', '.join(paths))) else: paths = subprocess.check_output( ["git", "diff", "--name-only", "HEAD~1..HEAD"]) paths = [x for x in paths.split('\n') if x] modules_to_test = [] for path in paths: module = detect_module(path) if module and module not in modules_to_test: modules_to_test.append(module) else: modules_to_test = modules[:] results = [] addons_path = service.config['addons_path'] root_path = service.config['root_path'] if not modules_to_test: coverage_config = {'source': [root_path], 'omit': ['*/addons/*/*']} else: coverage_config = { 'source': coverage_modules_path(modules_to_test, addons_path), 'omit': ['*/__terp__.py'] } coverage = OOCoverage(**coverage_config) coverage.enabled = (enable_coverage or report_coverage) coverage.start() server_spec_suite = get_spec_suite(root_path) if server_spec_suite: logging.info('Spec testing for server') report = run_spec_suite(server_spec_suite) results.append(not len(report.failed_examples) > 0) coverage.stop() for module in modules_to_test: with RestorePatchedRegisterAll(): install_requirements(module, addons_path) spec_suite = get_spec_suite(os.path.join(addons_path, module)) if spec_suite: logger.info('Spec testing module %s', module) coverage.start() report = run_spec_suite(spec_suite) coverage.stop() results.append(not len(report.failed_examples) > 0) logger.info('Unit testing module %s', module) os.environ['DESTRAL_MODULE'] = module coverage.start() suite = get_unittest_suite(module, tests) suite.drop_database = dropdb result = run_unittest_suite(suite) coverage.stop() results.append(result.wasSuccessful()) if report_coverage: coverage.report() if enable_coverage: coverage.save() if not all(results): sys.exit(1)
def setUp(self): self.config = config_from_environment('DESTRAL', ['module']) self.openerp = OpenERPService() if not self.openerp.db_name: self.openerp.db_name = self.openerp.create_database() self.openerp.install_module(self.config['module'])