def test_erdfilehandler_init(self):
        handler = ERDFileHandler(self.valid_erd_files, self.elem_4he)

        exp = [(f, s, False) for f, s in self.expected_values]
        self.assertEqual(exp, [f for f in handler])
        self.assertEqual(0, handler.get_active_atom_count())
        self.assertEqual(0, handler.get_old_atom_count())
    def test_max_seed(self):
        handler = ERDFileHandler([], self.elem_4he)

        self.assertEqual(None, handler.get_max_seed())

        handler.add_active_file(self.valid_erd_files[0])
        self.assertEqual(101, handler.get_max_seed())

        handler.add_active_file(self.valid_erd_files[1])
        self.assertEqual(102, handler.get_max_seed())
    def test_len(self):
        """Tests for handler's __len__ function"""
        handler = ERDFileHandler([], self.elem_4he)
        self.assertEqual(0, len(handler))
        for f in self.valid_erd_files:
            handler.add_active_file(f)

        self.assertEqual(len(self.valid_erd_files), len(handler))
        handler.update()
        handler.add_active_file("4He-Default.103.erd")
        self.assertEqual(len(self.valid_erd_files) + 1, len(handler))
    def test_atom_counts(self):
        """Tests atom counting by writing lines to temporary files"""
        with tempfile.TemporaryDirectory() as tmp_dir:
            # Create files in the tmp dir
            for file in self.valid_erd_files:
                write_line(os.path.join(tmp_dir, file))

            # Initialise a handler from the tmp_dir and add an active file
            handler = ERDFileHandler.from_directory(tmp_dir, self.elem_4he)
            handler.add_active_file(
                os.path.join(tmp_dir, "4He-Default.103.erd"))

            # Append a line to each file
            for erd_file, _, _ in handler:
                write_line(erd_file)

            self.assertEqual(1, handler.get_active_atom_count())
            self.assertEqual(4, handler.get_old_atom_count())
            self.assertEqual(5, handler.get_total_atom_count())

            # As the results of old files are cached, only counts in active
            # files are incremented
            for erd_file, _, _ in handler:
                write_line(erd_file)

            self.assertEqual(2, handler.get_active_atom_count())
            self.assertEqual(4, handler.get_old_atom_count())
            self.assertEqual(6, handler.get_total_atom_count())

            # If the handler is updated, active file is moved to old files
            handler.update()
            self.assertEqual(0, handler.get_active_atom_count())
            self.assertEqual(6, handler.get_old_atom_count())
            self.assertEqual(6, handler.get_total_atom_count())

            # Now the atom count will no longer update in the added file
            for erd_file, _, _ in handler:
                write_line(erd_file)

            self.assertEqual(0, handler.get_active_atom_count())
            self.assertEqual(6, handler.get_old_atom_count())
            self.assertEqual(6, handler.get_total_atom_count())

        # Assert that clearing also clears cache
        handler.clear()
        self.assertEqual(0, handler.get_active_atom_count())
        self.assertEqual(0, handler.get_old_atom_count())
        self.assertEqual(0, handler.get_total_atom_count())

        # Assert that tmp dir got deleted
        self.assertFalse(os.path.exists(tmp_dir))
    def test_thread_safety(self):
        """Tests that ErdFileHandler is thread safe."""
        n = 1000
        delay = 0.001
        handler = ERDFileHandler([], self.elem_4he)

        def adder():
            for i in range(n):
                handler.add_active_file(f"4He-Default.{i}.erd")

        self.assert_runs_ok(adder, handler.get_active_atom_count)
        self.assertEqual(n, len(handler))

        def updater():
            time.sleep(delay)
            handler.update()

        self.assert_runs_ok(handler.get_active_atom_count, updater)
        self.assertEqual(n, len(handler))

        def clearer():
            time.sleep(delay)
            handler.clear()

        self.assert_runs_ok(handler.get_old_atom_count, clearer)
        self.assertEqual(0, len(handler))

        # Add the files again and see if counting old atoms works when updating
        adder()
        self.assert_runs_ok(handler.get_old_atom_count, updater)
        self.assertEqual(n, len(handler))

        clearer()
        self.assertEqual(0, len(handler))

        self.assert_runs_ok(adder, updater)
        # TODO Updating and adding at the same time may cause the total count
        #  to be more than n, hence the less or equal comparison. A better
        #  multithreading test is needed to test this thing properly.
        self.assertTrue(n <= len(handler))
    def test_erdfilehandler_add(self):
        handler = ERDFileHandler(self.valid_erd_files, self.elem_4he)

        # already existing files, or files belonging to another
        # recoil element cannot be added
        self.assertRaises(
            ValueError,
            lambda: handler.add_active_file(self.valid_erd_files[0]))
        self.assertRaises(ValueError,
                          lambda: handler.add_active_file("4He-New.101.erd"))

        # new file can be added, but only once
        new_file = Path("4He-Default.103.erd")
        handler.add_active_file(new_file)

        self.assertRaises(ValueError,
                          lambda: handler.add_active_file(new_file))

        # new file appears as the first element when iterating
        # over the handler and its status is active
        exp = [(new_file, 103, True)] + [(f, s, False)
                                         for f, s, in self.expected_values]

        self.assertEqual(exp, [f for f in handler])
 def test_results_exists(self):
     handler = ERDFileHandler([], self.elem_4he)
     self.assertFalse(handler.results_exist())
     handler.add_active_file(self.valid_erd_files[0])
     self.assertTrue(handler.results_exist())
     handler.update()
     self.assertTrue(handler.results_exist())
     handler.clear()
     self.assertFalse(handler.results_exist())
 def test_clear(self):
     """Tests handler's clear method."""
     handler = ERDFileHandler(self.valid_erd_files, self.elem_4he)
     self.assertEqual(len(self.valid_erd_files), len(handler))
     handler.clear()
     self.assertEqual(0, len(handler))