Example #1
0
 def setup(self):
     self.root = RootTask(should_stop=Event(), should_pause=Event())
     self.task = ArrayExtremaTask(name="Test")
     self.root.add_child_task(0, self.task)
     array = np.zeros((5,), dtype={"names": ["var1", "var2"], "formats": ["f8", "f8"]})
     array["var1"][1] = -1
     array["var1"][3] = 1
     self.root.write_in_database("array", array)
Example #2
0
class TestArrayExtremaTask(object):
    def setup(self):
        self.root = RootTask(should_stop=Event(), should_pause=Event())
        self.task = ArrayExtremaTask(name="Test")
        self.root.add_child_task(0, self.task)
        array = np.zeros((5,), dtype={"names": ["var1", "var2"], "formats": ["f8", "f8"]})
        array["var1"][1] = -1
        array["var1"][3] = 1
        self.root.write_in_database("array", array)

    def test_mode_observation(self):
        """Check that the database is correctly updated when the mode change.

        """
        self.task.mode = "Min"

        assert self.task.get_from_database("Test_min_ind") == 0
        assert self.task.get_from_database("Test_min_value") == 1.0
        aux = self.task.list_accessible_database_entries()
        assert "Test_max_ind" not in aux
        assert "Test_max_value" not in aux

        self.task.mode = "Max"

        assert self.task.get_from_database("Test_max_ind") == 0
        assert self.task.get_from_database("Test_max_value") == 2.0
        aux = self.task.list_accessible_database_entries()
        assert "Test_min_ind" not in aux
        assert "Test_min_value" not in aux

        self.task.mode = "Max & min"

        assert self.task.get_from_database("Test_min_ind") == 0
        assert self.task.get_from_database("Test_min_value") == 1.0
        assert self.task.get_from_database("Test_max_ind") == 0
        assert self.task.get_from_database("Test_max_value") == 2.0

    def test_check1(self):
        """Simply test that everything is ok if the array exists in the
        database.

        """
        self.root.write_in_database("array", np.zeros((5,)))
        self.task.target_array = "{array}"

        test, traceback = self.task.check()
        assert test
        assert not traceback

    def test_check2(self):
        """Simply test that everything is ok if the array exists in the
        database and the column name is ok.

        """
        self.task.target_array = "{array}"
        self.task.column_name = "var1"

        test, traceback = self.task.check()
        assert test
        assert not traceback

    def test_check3(self):
        """Test handling a wrong array name.

        """
        self.task.target_array = "*{array}"
        self.task.column_name = "var3"

        test, traceback = self.task.check()
        assert not test
        assert len(traceback) == 1
        assert "root/Test-target_array" in traceback

    def test_check4(self):
        """Test handling an array without names when a name is given.

        """
        self.root.write_in_database("array", np.zeros((5,)))
        self.task.target_array = "{array}"
        self.task.column_name = "var1"

        test, traceback = self.task.check()
        assert not test
        assert len(traceback) == 1
        assert "root/Test" in traceback

    def test_check5(self):
        """Test handling an array with names when no name is given.

        """
        self.task.target_array = "{array}"

        test, traceback = self.task.check()
        assert not test
        assert len(traceback) == 1
        assert "root/Test" in traceback

    def test_check6(self):
        """Test handling a wrong column name.

        """
        self.task.target_array = "{array}"
        self.task.column_name = "var3"

        test, traceback = self.task.check()
        assert not test
        assert len(traceback) == 1
        assert "root/Test" in traceback

    def test_check7(self):
        """Test handling a 2d array without names.

        """
        self.task.target_array = "{array}"

        array = np.zeros((5, 5))
        self.root.write_in_database("array", array)

        test, traceback = self.task.check()
        assert not test
        assert len(traceback) == 1
        assert "root/Test" in traceback

    def test_perform1(self):
        """Test performing when mode is 'Max'.

        """
        self.task.mode = "Max"
        self.task.target_array = "{array}"
        self.task.column_name = "var1"
        self.root.prepare()

        self.task.perform()

        assert self.task.get_from_database("Test_max_ind") == 3
        assert self.task.get_from_database("Test_max_value") == 1.0

    def test_perform2(self):
        """Test performing when mode is 'Min'.

        """
        self.task.mode = "Min"
        self.task.target_array = "{array}"
        self.task.column_name = "var1"
        self.root.prepare()

        self.task.perform()

        assert self.task.get_from_database("Test_min_ind") == 1
        assert self.task.get_from_database("Test_min_value") == -1.0

    def test_perform3(self):
        """Test performing when mode is 'Max & min'.

        """
        self.task.mode = "Max & min"
        self.task.target_array = "{array}"
        self.task.column_name = "var1"
        self.root.prepare()

        self.task.perform()

        assert self.task.get_from_database("Test_max_ind") == 3
        assert self.task.get_from_database("Test_max_value") == 1.0
        assert self.task.get_from_database("Test_min_ind") == 1
        assert self.task.get_from_database("Test_min_value") == -1.0

    def test_perform4(self):
        """Test performing when no column name is given.

        """
        self.root.write_in_database("array", np.zeros((5,)))
        self.task.mode = "Max"
        self.task.target_array = "{array}"
        self.root.prepare()

        self.task.perform()

        assert self.task.get_from_database("Test_max_ind") == 0
        assert self.task.get_from_database("Test_max_value") == 0.0