示例#1
0
    def create_profile(self):
        """Not actually create the profile but reuse the test profile"""
        test_profile = os.environ.get('AIIDA_TEST_PROFILE', None)
        if not test_profile:
            return super(ExistingProfileFixtureManager, self).create_profile()

        from aiida import load_dbenv
        load_dbenv(profile=test_profile)
        check_if_tests_can_run()

        # Running on test profile
        self._is_running_on_test_profile = True

        from aiida.backends.djsite.db.testbase import DjangoTests
        self._test_case = DjangoTests()
示例#2
0
    def __init__(self, profile_name):
        """
        Use an existing profile.

        :param profile_name: Name of the profile to be loaded
        """
        from aiida import load_profile
        from aiida.backends.testbase import check_if_tests_can_run

        self._profile = None
        self._user = None

        try:
            self._profile = load_profile(profile_name)
            manager.get_manager()._load_backend(schema_check=False)  # pylint: disable=protected-access
        except Exception:
            raise TestManagerError('Unable to load test profile \'{}\'.'.format(profile_name))
        check_if_tests_can_run()

        self._select_db_test_case(backend=self._profile.database_backend)
示例#3
0
def devel_tests(paths, verbose):  # pylint: disable=too-many-locals,too-many-statements,too-many-branches
    """Run the unittest suite or parts of it."""
    import os
    import unittest
    import warnings

    import aiida
    from aiida.backends.testbase import run_aiida_db_tests
    from aiida.backends.testbase import check_if_tests_can_run
    from aiida.manage import configuration
    from aiida.manage import tests
    from aiida.common.warnings import AiidaTestWarning

    test_failures = []
    test_errors = []
    test_skipped = []
    tot_num_tests = 0
    db_test_list = []
    test_folders = []
    do_db = False

    if tests.get_test_profile_name():
        warnings.warn(  # pylint: disable=no-member
            'Ignoring `AIIDA_TEST_PROFILE` environment variable.'
            ' Use `verdi -p test_profile devel tests` to select the test profile for aiida-core tests.',
            AiidaTestWarning)

    if paths:
        for path in paths:
            if path in get_valid_test_paths():
                dbtests = get_valid_test_paths()[path]
                # Anything that has been added is a DB test
                if dbtests is not None:
                    do_db = True
                    for dbtest in dbtests:
                        db_test_list.append(dbtest)
                else:
                    test_folders.append(path)
            else:
                valid_tests = '\n'.join(
                    '  * {}'.format(a)
                    for a in sorted(get_valid_test_paths().keys()))
                echo.echo_critical(
                    '{} is not a valid test, allowed test folders are:\n{}'.
                    format(path, valid_tests))
    else:
        # Without arguments, run all tests
        do_db = True
        for key, value in get_valid_test_paths().items():
            if value is None:
                # Non-db tests
                test_folders.append(key)
            else:
                # DB test
                for dbtest in value:
                    db_test_list.append(dbtest)

    for test_folder in test_folders:
        echo.echo('v' * 75)
        echo.echo('>>> Tests for module {} <<<'.format(test_folder.ljust(50)))
        echo.echo('^' * 75)
        testsuite = unittest.defaultTestLoader.discover(
            test_folder, top_level_dir=os.path.dirname(aiida.__file__))
        test_runner = unittest.TextTestRunner()
        test_results = test_runner.run(testsuite)
        test_failures.extend(test_results.failures)
        test_errors.extend(test_results.errors)
        test_skipped.extend(test_results.skipped)
        tot_num_tests += test_results.testsRun

    if do_db:
        # Even if each test would fail if we are not in a test profile,
        # it's still better to not even run them in the case the profile
        # is not a test one.
        try:
            check_if_tests_can_run()
        except TestsNotAllowedError as exception:
            echo.echo_critical(str(exception))

        echo.echo('v' * 75)
        echo.echo('>>> Tests for {} db application'.format(
            configuration.PROFILE.database_backend))
        echo.echo('^' * 75)
        db_results = run_aiida_db_tests(db_test_list, verbose)
        test_skipped.extend(db_results.skipped)
        test_failures.extend(db_results.failures)
        test_errors.extend(db_results.errors)
        tot_num_tests += db_results.testsRun

    echo.echo('Final summary of the run of tests:')
    echo.echo('* Tests skipped: {}'.format(len(test_skipped)))
    if test_skipped:
        echo.echo('  Reasons for skipping:')
        for reason in sorted(set(_[1] for _ in test_skipped)):
            echo.echo('  - {}'.format(reason))

    echo.echo('* Tests run:     {}'.format(tot_num_tests))
    echo.echo('* Tests failed:  {}'.format(len(test_failures)))
    echo.echo('* Tests errored: {}'.format(len(test_errors)))

    # If there was any failure report it with the right exit code
    if test_failures or test_errors:
        sys.exit(len(test_failures) + len(test_errors))
示例#4
0
    def run_tests(self, *args):
        import unittest
        from aiida.backends import settings
        from aiida.backends.testbase import run_aiida_db_tests
        from aiida.backends.testbase import check_if_tests_can_run

        # For final summary
        test_failures = []
        test_errors = []
        test_skipped = []
        tot_num_tests = 0

        db_test_list = []
        test_folders = []
        do_db = False
        if args:
            for arg in args:
                if arg in self.allowed_test_folders:
                    dbtests = self.allowed_test_folders[arg]
                    # Anything that has been added is a DB test
                    if dbtests is not None:
                        do_db = True
                        for dbtest in dbtests:
                            db_test_list.append(dbtest)
                    else:
                        test_folders.append(arg)
                else:
                    print >> sys.stderr, (
                        "{} is not a valid test. "
                        "Allowed test folders are:".format(arg))
                    print >> sys.stderr, "\n".join(
                        '  * {}'.format(a) for a in sorted(
                            self.allowed_test_folders.keys()))
                    sys.exit(1)
        else:
            # Without arguments, run all tests
            do_db = True
            for k, v in self.allowed_test_folders.iteritems():
                if v is None:
                    # Non-db tests
                    test_folders.append(k)
                else:
                    # DB test
                    for dbtest in v:
                        db_test_list.append(dbtest)

        for test_folder in test_folders:
            print "v" * 75
            print ">>> Tests for module {} <<<".format(test_folder.ljust(50))
            print "^" * 75
            testsuite = unittest.defaultTestLoader.discover(
                test_folder, top_level_dir=os.path.dirname(aiida.__file__))
            test_runner = unittest.TextTestRunner()
            test_results = test_runner.run(testsuite)
            test_failures.extend(test_results.failures)
            test_errors.extend(test_results.errors)
            test_skipped.extend(test_results.skipped)
            tot_num_tests += test_results.testsRun

        if do_db:
            if not is_dbenv_loaded():
                load_dbenv()

            # Even if each test would fail if we are not in a test profile,
            # it's still better to not even run them in the case the profile
            # is not a test one.
            try:
                check_if_tests_can_run()
            except TestsNotAllowedError as e:
                print >> sys.stderr, e.message
                sys.exit(1)

            # project_dir = os.path.join(os.path.dirname(aiida.__file__), '..')
            # testsuite = unittest.defaultTestLoader.discover('test', top_level_dir=project_dir)
            # test_runner = unittest.TextTestRunner()
            # test_runner.run(testsuite)

            print "v" * 75
            print (">>> Tests for {} db application".format(settings.BACKEND))
            print "^" * 75
            db_results = run_aiida_db_tests(db_test_list)
            test_skipped.extend(db_results.skipped)
            test_failures.extend(db_results.failures)
            test_errors.extend(db_results.errors)
            tot_num_tests += db_results.testsRun

        print "Final summary of the run of tests:"
        print "* Tests skipped: {}".format(len(test_skipped))
        if test_skipped:
            print "  Reasons for skipping:"
            for reason in sorted(set([_[1] for _ in test_skipped])):
                print "  - {}".format(reason)

        print "* Tests run:     {}".format(tot_num_tests)
        # This count is wrong, sometimes a test can both error and fail 
        # apparently, and you can get negative numbers...
        #print "* Tests OK:      {}".format(tot_num_tests - len(test_errors) - len(test_failures))
        print "* Tests failed:  {}".format(len(test_failures))
        print "* Tests errored: {}".format(len(test_errors))



        # If there was any failure report it with the
        # right exit code
        if test_failures or test_errors:
            sys.exit(len(test_failures) + len(test_errors))
示例#5
0
        # Since I apply the replace rule, the last set of links should appear:

        instances = created_dict['instances']
        adjacency = created_dict['adjacency']

        touples_should = set()
        [touples_should.add((pk1, pk2))
                for idx1,pk1 in enumerate(instances)
                for idx2,pk2 in enumerate(instances)
                if adjacency[idx1, idx2]
                and pk1 in created_dict['depth_dict'][self.DEPTH-2]
                and pk2 in created_dict['depth_dict'][self.DEPTH-1]
            ]

        touples_are = set(zip(*zip(*res['nodes_nodes']._set)[:2]))
        self.assertEqual(touples_are, touples_should)

if __name__ == '__main__':
    from unittest import TestSuite, TextTestRunner
    try:
        check_if_tests_can_run()
    except TestsNotAllowedError as e:
        print >> sys.stderr, e.message
        sys.exit(1)

    test_suite = TestSuite()
    test_suite.addTest(TestNodes())
    test_suite.addTest(TestGroups())
    test_suite.addTest(TestEdges())
    results = TextTestRunner(failfast=False, verbosity=2).run(test_suite)