Exemple #1
0
    def setUpClass(cls, *args, **kwargs):
        """
        Create the computers and setup a codes
        """
        super(TestVerdiCodeCommands, cls).setUpClass()

        # Setup computer #1
        from aiida.cmdline.commands.computer import Computer
        cmd_comp = Computer()
        with mock.patch('__builtin__.raw_input',
                        side_effect=computer_setup_input_1):
            with Capturing():
                cmd_comp.computer_setup()

        # Setup a code for computer #1
        from aiida.cmdline.commands.code import Code
        code_cmd = Code()
        with mock.patch('__builtin__.raw_input',
                        side_effect=code_setup_input_1):
            with Capturing():
                code_cmd.code_setup()

        # Setup computer #2
        with mock.patch('__builtin__.raw_input',
                        side_effect=computer_setup_input_2):
            with Capturing():
                cmd_comp.computer_setup()

        # Setup a code for computer #2
        with mock.patch('__builtin__.raw_input',
                        side_effect=code_setup_input_2):
            with Capturing():
                code_cmd.code_setup()
Exemple #2
0
    def test_calculation_list(self):
        """
        Do some calculation listing to ensure that verdi calculation list
        works and gives at least to some extent the expected results.
        """
        from aiida.cmdline.commands.calculation import Calculation
        calc_cmd = Calculation()

        with Capturing() as output:
            calc_cmd.calculation_list()

        out_str = ''.join(output)
        self.assertTrue(
            calc_states.TOSUBMIT in out_str,
            "The TOSUBMIT calculations should be part fo the "
            "simple calculation list.")
        self.assertTrue(
            calc_states.COMPUTED in out_str,
            "The COMPUTED calculations should be part fo the "
            "simple calculation list.")
        self.assertFalse(
            calc_states.FINISHED in out_str,
            "The FINISHED calculations should not be part fo the "
            "simple calculation list.")

        with Capturing() as output:
            calc_cmd.calculation_list(*['-a'])

        out_str = ''.join(output)
        self.assertTrue(
            calc_states.FINISHED in out_str,
            "The FINISHED calculations should be part fo the "
            "simple calculation list.")
Exemple #3
0
    def test_trajectory_all_user_listing(self):
        from aiida.cmdline.commands.data import _Bands
        from aiida.cmdline.commands.data import _Structure
        from aiida.cmdline.commands.data import _Cif
        from aiida.cmdline.commands.data import _Trajectory

        sub_cmds = [_Bands, _Structure, _Cif, _Trajectory]
        for sub_cmd in sub_cmds:
            args_to_test = [['-A'], ['--all-users']]
            for arg in args_to_test:
                curr_scmd = sub_cmd()
                with Capturing() as output:
                    curr_scmd.list(*arg)

                out_str = ' '.join(output)

                for nid in (self.cmd_to_nodeid_map[sub_cmd] +
                            self.cmd_to_nodeid_map_for_nuser[sub_cmd]):
                    if str(nid) not in out_str:
                        self.fail(
                            "The data objects ({}) with ids {} and {} "
                            "were not found. ".format(
                                sub_cmd, str(self.cmd_to_nodeid_map[sub_cmd]
                                             [0]),
                                str(self.cmd_to_nodeid_map[sub_cmd][1])) +
                            "The output was {}".format(out_str))
Exemple #4
0
    def test_ask_backup_question(self):
        """
        This method checks that the combined use of query_string and
        query_yes_no by the ask_backup_question is done as expected.
        """
        from aiida.utils.capturing import Capturing

        # Capture the sysout for the following code
        with Capturing():
            # Test that a question that asks for an integer is working
            # The given answers are in order:
            # - a non-accepted empty answer
            # - an answer that can not be parsed based on the given type
            # - the final expected answer
            self.seq = -1
            answers = ["", "3fd43", "1", "yes"]
            utils.raw_input = lambda _: answers[self.array_counter()]
            self.assertEqual(utils.ask_question("", int, False),
                             int(answers[2]))

            # Test that a question that asks for a date is working correctly.
            # The behavior is similar to the above test.
            self.seq = -1
            answers = ["", "3fd43", "2015-07-28 20:48:53.197537+02:00", "yes"]
            utils.raw_input = lambda _: answers[self.array_counter()]
            self.assertEqual(utils.ask_question("", datetime.datetime, False),
                             parse(answers[2]))

            # Check that None is not allowed as answer
            question = ""
            answer = ""
            utils.raw_input = lambda x: answer if x == question else "y"
            self.assertEqual(utils.ask_question(question, int, True), None)
Exemple #5
0
    def test_trajectory_group_listing(self):
        from aiida.cmdline.commands.data import _Bands
        from aiida.cmdline.commands.data import _Structure
        from aiida.cmdline.commands.data import _Cif
        from aiida.cmdline.commands.data import _Trajectory

        args_to_test = [['-g', self.group_name],
                        ['--group-name', self.group_name],
                        ['-G', str(self.group_id)],
                        ['--group-pk', str(self.group_id)]]

        sub_cmds = [_Bands, _Structure, _Cif, _Trajectory]
        for sub_cmd in sub_cmds:
            for arg in args_to_test:
                curr_scmd = sub_cmd()
                with Capturing() as output:
                    curr_scmd.list(*arg)
                out_str = ' '.join(output)

                if str(self.cmd_to_nodeid_map_for_groups[sub_cmd]
                       ) not in out_str:
                    self.fail(
                        "The data object ({}) with id {} "
                        "was not found. ".format(
                            sub_cmd,
                            str(self.cmd_to_nodeid_map_for_groups[sub_cmd]) +
                            "The output was {}".format(out_str)))
Exemple #6
0
    def test_code_list(self):
        """
        Do some code listing test to ensure the correct behaviour of
        verdi code list
        """
        from aiida.cmdline.commands.code import Code
        code_cmd = Code()

        # Run a simple verdi code list, capture the output and check the result
        with Capturing() as output:
            code_cmd.code_list()
        out_str_1 = ''.join(output)
        self.assertTrue(
            computer_name_1 in out_str_1,
            "The computer 1 name should be included into "
            "this list")
        self.assertTrue(code_name_1 in out_str_1,
                        "The code 1 name should be included into this list")
        self.assertTrue(
            computer_name_2 in out_str_1,
            "The computer 2 name should be included into "
            "this list")
        self.assertTrue(code_name_2 in out_str_1,
                        "The code 2 name should be included into this list")

        # Run a verdi code list -a, capture the output and check if the result
        # is the same as the previous one
        with Capturing() as output:
            code_cmd.code_list(*['-a'])
        out_str_2 = ''.join(output)
        self.assertEqual(
            out_str_1, out_str_2,
            "verdi code list & verdi code list -a should provide "
            "the same output in this experiment.")

        # Run a verdi code list -c, capture the output and check the result
        with Capturing() as output:
            code_cmd.code_list(*['-c', computer_name_1])
        out_str = ''.join(output)
        self.assertTrue(
            computer_name_1 in out_str,
            "The computer 1 name should be included into "
            "this list")
        self.assertFalse(
            computer_name_2 in out_str,
            "The computer 2 name should not be included into "
            "this list")
        def run_async():
            yield run_until_paused(process)

            process.play()

            with Capturing():
                with self.assertRaises(RuntimeError):
                    result = yield process.future()
Exemple #8
0
    def run(self, suite, backend=BACKEND_DJANGO):
        """
        Run tests using fixture manager for specified backend.

        :param tests: A suite of tests, as returned e.g. by :py:meth:`unittest.TestLoader.discover`
        :param backend: Database backend to be used.
        """
        from aiida.utils.capturing import Capturing
        with Capturing():
            with fixture_manager(backend=backend):
                return super(TestRunner, self).run(suite)
Exemple #9
0
    def test_trajectory_past_days_listing(self):
        from aiida.cmdline.commands.data import _Bands
        from aiida.cmdline.commands.data import _Structure
        from aiida.cmdline.commands.data import _Cif
        from aiida.cmdline.commands.data import _Trajectory

        sub_cmds = [_Bands, _Structure, _Cif, _Trajectory]
        for sub_cmd in sub_cmds:
            args_to_test = [['-p', '0'], ['--past-days', '0']]
            for arg in args_to_test:
                curr_scmd = sub_cmd()
                with Capturing() as output:
                    curr_scmd.list(*arg)
                out_str = ' '.join(output)

                # This should be an empty output
                for nid in self.cmd_to_nodeid_map[sub_cmd]:
                    if str(nid) in out_str:
                        self.fail("No data objects should be retrieved and "
                                  "some were retrieved. The (concatenation of "
                                  "the) output was: {}".format(out_str))

            args_to_test = [['-p', '1'], ['--past-days', '1']]
            for arg in args_to_test:
                curr_scmd = sub_cmd()
                with Capturing() as output:
                    curr_scmd.list(*arg)
                out_str = ' '.join(output)

                for nid in self.cmd_to_nodeid_map[sub_cmd]:
                    if str(nid) not in out_str:
                        self.fail(
                            "The data objects ({}) with ids {} and {} "
                            "were not found. ".format(
                                sub_cmd, str(self.cmd_to_nodeid_map[sub_cmd]
                                             [0]),
                                str(self.cmd_to_nodeid_map[sub_cmd][1])) +
                            "The output was {}".format(out_str))
Exemple #10
0
    def test_simple_run(self):
        """
        Run the workchain which should hit the exception and therefore end
        up in the EXCEPTED state
        """
        process = TestWorkChainAbortChildren.MainWorkChain()

        with Capturing():
            with self.assertRaises(RuntimeError):
                work.run(process)

        self.assertEquals(process.calc.is_finished_ok, False)
        self.assertEquals(process.calc.is_excepted, True)
        self.assertEquals(process.calc.is_killed, False)
Exemple #11
0
    def test_integration(self):
        import logging
        from aiida.utils.capturing import Capturing

        # Disable the logging messages
        logging.disable(logging.INFO)

        # Fill in the repository with data
        self.fill_repo()
        try:
            # Create a temp folder where the backup files will be placed
            # and the backup will be stored
            temp_folder = tempfile.mkdtemp()

            # Capture the sysout of the following command
            with Capturing():
                # Create the backup scripts
                backup_full_path = self.create_backup_scripts(temp_folder)

            # Put the backup folder in the path
            sys.path.append(backup_full_path)

            # Import the backup script - this action will also run it
            # It is assumed that the backup script ends with .py
            importlib.import_module(self._bs_instance._script_filename[:-3])

            # Check the backup
            from aiida import settings
            from filecmp import dircmp
            import os
            from aiida.common.utils import are_dir_trees_equal
            source_dir = os.path.join(settings.REPOSITORY_PATH,
                                      self._repo_rel_path)
            dest_dir = os.path.join(backup_full_path,
                                    self._bs_instance._file_backup_folder_rel,
                                    self._repo_rel_path)
            self.assertTrue(
                are_dir_trees_equal(source_dir, dest_dir),
                "The backed-up repository has differences "
                "to the original one.")
        finally:
            shutil.rmtree(temp_folder, ignore_errors=True)

            # Enable the logging messages
            logging.disable(logging.NOTSET)
Exemple #12
0
    def setUpClass(cls, *args, **kwargs):
        """
        Create a user
        """
        super(TestVerdiUserCommands, cls).setUpClass()

        # Setup user #1
        from aiida.cmdline.commands.user import do_configure

        with mock.patch('__builtin__.raw_input',
                        side_effect=computer_setup_input_1):
            with Capturing():
                do_configure(user_1['email'],
                             user_1['first_name'],
                             user_1['last_name'],
                             user_1['institution'],
                             no_password=True,
                             force_reconfigure=True)
Exemple #13
0
    def test_query_yes_no(self):
        """
        This method tests the query_yes_no method behaves as expected. To
        perform this, a lambda function is used to simulate the user input.
        """
        from aiida.utils.capturing import Capturing

        # Capture the sysout for the following code
        with Capturing():
            # Check the yes
            utils.raw_input = lambda _: "y"
            self.assertTrue(utils.query_yes_no("", "yes"))

            utils.raw_input = lambda _: "yes"
            self.assertTrue(utils.query_yes_no("", "yes"))

            # Check the no
            utils.raw_input = lambda _: "no"
            self.assertFalse(utils.query_yes_no("", "yes"))

            utils.raw_input = lambda _: "n"
            self.assertFalse(utils.query_yes_no("", "yes"))

            # Check the empty default value that should
            # lead to an error
            with self.assertRaises(ValueError):
                utils.query_yes_no("", "")

            # Check that a None default value and no answer from
            # the user should lead to the repetition of the query until
            # it is answered properly
            self.seq = -1
            answers = ["", "", "", "yes"]
            utils.raw_input = lambda _: answers[self.array_counter()]
            self.assertTrue(utils.query_yes_no("", None))
            self.assertEqual(self.seq, len(answers) - 1)

            # Check that the default answer is returned
            # when the user doesn't give an answer
            utils.raw_input = lambda _: ""
            self.assertTrue(utils.query_yes_no("", "yes"))

            utils.raw_input = lambda _: ""
            self.assertFalse(utils.query_yes_no("", "no"))
Exemple #14
0
    def test_create_use_destroy_profile(self):
        """
        Test temporary test profile creation

        * The profile gets created, the dbenv loaded
        * Data can be stored in the db
        * reset_db deletes all data added after profile creation
        * destroy_all removes all traces of the test run
        """
        from aiida.common import setup as aiida_cfg
        from aiida import is_dbenv_loaded
        with Capturing() as output:
            self.fixture_manager.create_profile()
        self.assertTrue(self.fixture_manager.root_dir_ok, msg=output)
        self.assertTrue(self.fixture_manager.config_dir_ok, msg=output)
        self.assertTrue(self.fixture_manager.repo_ok, msg=output)
        self.assertEqual(aiida_cfg.AIIDA_CONFIG_FOLDER,
                         self.fixture_manager.config_dir,
                         msg=output)
        self.assertTrue(is_dbenv_loaded())

        from aiida.orm import DataFactory, load_node
        data = DataFactory('parameter')(dict={'key': 'value'})
        data.store()
        data_pk = data.pk
        self.assertTrue(load_node(data_pk))

        with self.assertRaises(FixtureError):
            self.test_create_aiida_db()

        self.fixture_manager.reset_db()
        with self.assertRaises(Exception):
            load_node(data_pk)

        temp_dir = self.fixture_manager.root_dir
        self.fixture_manager.destroy_all()
        with self.assertRaises(Exception):
            self.fixture_manager.postgres.db_exists(
                self.fixture_manager.db_name)
        self.assertFalse(os.path.exists(temp_dir))
        self.assertIsNone(self.fixture_manager.root_dir)
        self.assertIsNone(self.fixture_manager.pg_cluster)
Exemple #15
0
    def test_trajectory_simple_listing(self):
        from aiida.cmdline.commands.data import _Bands
        from aiida.cmdline.commands.data import _Structure
        from aiida.cmdline.commands.data import _Cif
        from aiida.cmdline.commands.data import _Trajectory

        sub_cmds = [_Bands, _Structure, _Cif, _Trajectory]
        for sub_cmd in sub_cmds:
            with Capturing() as output:
                sub_cmd().list()

            out_str = ' '.join(output)

            for nid in self.cmd_to_nodeid_map[sub_cmd]:
                if str(nid) not in out_str:
                    self.fail(
                        "The data objects ({}) with ids {} and {} "
                        "were not found. ".format(
                            sub_cmd, str(self.cmd_to_nodeid_map[sub_cmd][0]),
                            str(self.cmd_to_nodeid_map[sub_cmd][1])) +
                        "The output was {}".format(out_str))
Exemple #16
0
 def setUpClass(cls):
     cls.fixture_manager = FixtureManager()
     cls.fixture_manager.backend = cls.BACKEND
     with Capturing():
         cls.fixture_manager.create_profile()
Exemple #17
0
    def test_full_backup_setup_script(self):
        """
        This method is a full test of the backup setup script. It launches it,
        replies to all the question as the user would do and in the end it
        checks that the correct files were created with the right content.
        """
        from aiida.utils.capturing import Capturing

        # Create a temp folder where the backup files will be placed
        temp_folder = tempfile.mkdtemp()

        try:
            temp_aiida_folder = os.path.join(temp_folder, ".aiida")
            # The predefined answers for the setup script

            ac = utils.ArrayCounter()
            self.seq = -1
            answers = [
                temp_aiida_folder,  # the backup folder path
                "",  # should the folder be created?
                "",  # destination folder of the backup
                "",  # should the folder be created?
                "n",  # print config explanation?
                "",  # configure the backup conf file now?
                "2014-07-18 13:54:53.688484+00:00",  # start date of backup?
                "",  # is it correct?
                "",  # days to backup?
                "",  # is it correct?
                "2015-04-11 13:55:53.688484+00:00",  # end date of backup
                "",  # is it correct?
                "1",  # periodicity
                "",  # is it correct?
                "2",  # threshold?
                ""
            ]  # is it correct?
            utils.raw_input = lambda _: answers[ac.array_counter()]

            # Run the setup script and catch the sysout
            with Capturing():
                backup_setup.BackupSetup().run()

            # Get the backup configuration files & dirs
            backup_conf_records = [f for f in os.listdir(temp_aiida_folder)]
            # Check if all files & dirs are there
            self.assertTrue(
                backup_conf_records is not None
                and len(backup_conf_records) == 4
                and "backup_dest" in backup_conf_records
                and "backup_info.json.tmpl" in backup_conf_records
                and "start_backup.py" in backup_conf_records
                and "backup_info.json" in backup_conf_records,
                "The created backup folder doesn't have the "
                "expected files. "
                "It contains: {}.".format(backup_conf_records))

            # Check the content of the main backup configuration file
            with open(os.path.join(temp_aiida_folder,
                                   "backup_info.json")) as conf_jfile:
                conf_cont = json.load(conf_jfile)
                self.assertEqual(
                    conf_cont[AbstractBackup.OLDEST_OBJECT_BK_KEY],
                    "2014-07-18 13:54:53.688484+00:00")
                self.assertEqual(conf_cont[AbstractBackup.DAYS_TO_BACKUP_KEY],
                                 None)
                self.assertEqual(
                    conf_cont[AbstractBackup.END_DATE_OF_BACKUP_KEY],
                    "2015-04-11 13:55:53.688484+00:00")
                self.assertEqual(conf_cont[AbstractBackup.PERIODICITY_KEY], 1)
                self.assertEqual(
                    conf_cont[AbstractBackup.BACKUP_LENGTH_THRESHOLD_KEY], 2)
        finally:
            shutil.rmtree(temp_folder, ignore_errors=True)
Exemple #18
0
 def setUpClass(cls):
     from aiida.utils.capturing import Capturing
     cls.fixture_manager = fixture_manager
     if not fixture_manager.has_profile_open():
         with Capturing():
             cls.fixture_manager.create_profile()