Exemplo n.º 1
0
    def setup(self):
        self.root = RootTask(should_stop=Event(), should_pause=Event())
        self.task = SaveTask(name='Test')
        self.root.add_child_task(0, self.task)

        self.root.write_in_database('int', 1)
        self.root.write_in_database('float', 2.0)
        self.root.write_in_database('str', 'a')
Exemplo n.º 2
0
class TestSaveTask(object):

    def setup(self):
        self.root = RootTask(should_stop=Event(), should_pause=Event())
        self.task = SaveTask(name='Test')
        self.root.add_child_task(0, self.task)

        self.root.write_in_database('int', 1)
        self.root.write_in_database('float', 2.0)
        self.root.write_in_database('str', 'a')

    def test_saving_target_observer(self):
        """Test that changing the target does change the database content.

        """
        self.task.saving_target = 'Array'

        assert self.task.get_from_database('Test_array') == np.array([1.0])

        self.task.saving_target = 'File'

        aux = self.task.list_accessible_database_entries()
        assert 'Test_array' not in aux

        self.task.saving_target = 'File and array'

        assert self.task.get_from_database('Test_array') == np.array([1.0])

    def test_check1(self, tmpdir):
        """Test everything ok in file mode (no array size).

        """
        task = self.task
        task.saving_target = 'File'
        task.folder = str(tmpdir)
        task.filename = 'test{int}.txt'
        task.file_mode = 'New'
        task.header = 'test'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tata', '{float}')])
        file_path = os.path.join(str(tmpdir), 'test1.txt')

        test, traceback = task.check()
        assert test and not traceback
        assert not os.path.isfile(file_path)
        assert not task.initialized

        task.file_mode = 'Add'

        test, traceback = task.check()
        assert test and not traceback
        assert os.path.isfile(file_path)

    def test_check2(self):
        """Test everything ok in array mode (assert database state).

        """
        task = self.task
        task.saving_target = 'Array'
        task.array_size = '1000*{float}'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tata', '{float}')])

        test, traceback = task.check()
        assert test and not traceback
        array = task.get_from_database('Test_array')
        assert array.dtype.names == ('toto', 'tata')

    def test_check3(self, tmpdir):
        """Test everything is ok in file & array mode.

        """
        task = self.task
        task.saving_target = 'File and array'
        task.folder = str(tmpdir)
        task.filename = 'test_rr.txt'
        task.file_mode = 'New'
        task.header = 'test'
        task.array_size = '1000*{float}'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tata', '{float}')])
        file_path = os.path.join(str(tmpdir), 'test_rr.txt')

        test, traceback = task.check()
        assert test and not traceback
        assert not os.path.isfile(file_path)
        array = task.get_from_database('Test_array')
        assert array.dtype.names == ('toto', 'tata')

    def test_check4(self, tmpdir):
        """Test check issues in file mode : folder.

        """
        task = self.task
        task.saving_target = 'File'
        task.folder = str(tmpdir) + '{tt}'

        test, traceback = task.check()
        assert not test
        assert len(traceback) == 1

    def test_check5(self, tmpdir):
        """Test check issues in file mode : file.

        """
        task = self.task
        task.saving_target = 'File'
        task.folder = str(tmpdir)
        task.filename = 'test{tt}.txt'

        test, traceback = task.check()
        assert not test
        assert len(traceback) == 1

    def test_check6(self, tmpdir):
        """Test check issues in file mode : array_size.

        """
        task = self.task
        task.saving_target = 'File'
        task.folder = str(tmpdir)
        task.filename = 'test.txt'
        task.file_mode = 'New'
        task.header = 'test'
        task.array_size = '1000*'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tata', '{float}')])
        file_path = os.path.join(str(tmpdir), 'test.txt')

        test, traceback = task.check()
        assert not test
        assert len(traceback) == 1
        assert not os.path.isfile(file_path)

    def test_check6bis(self, tmpdir):
        """Test check issues in file mode : header formatting.

        """
        task = self.task
        task.saving_target = 'File'
        task.folder = str(tmpdir)
        task.filename = 'test.txt'
        task.file_mode = 'New'
        task.header = 'test {*}'
        task.array_size = '1000'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tata', '{float}')])
        file_path = os.path.join(str(tmpdir), 'test.txt')

        test, traceback = task.check()
        assert not test
        assert len(traceback) == 1
        assert not os.path.isfile(file_path)

    def test_check7(self):
        """Test check issues in array mode  : wrong array_size.

        """
        task = self.task
        task.saving_target = 'Array'
        task.array_size = '1000*{float}*'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tata', '{float}')])

        test, traceback = task.check()
        assert not test
        assert len(traceback) == 1
        assert self.task.get_from_database('Test_array') == np.array([1.0])

    def test_check8(self):
        """Test check issues in array mode : absent array_size.

        """
        task = self.task
        task.saving_target = 'Array'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tata', '{float}')])

        test, traceback = task.check()
        assert not test
        assert len(traceback) == 1
        assert self.task.get_from_database('Test_array') == np.array([1.0])

    def test_check9(self):
        """Test check issues in entrie.

        """
        task = self.task
        task.saving_target = 'Array'
        task.array_size = '1000*{float}'
        task.saved_values = OrderedDict([('toto', '*{str}'),
                                         ('tat{str}', '{float}@')])

        test, traceback = task.check()
        assert not test
        assert len(traceback) == 2

    def test_check9bis(self):
        """Test check issues in label.

        """
        task = self.task
        task.saving_target = 'Array'
        task.array_size = '1000*{float}'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tat{str*}', '{float}')])

        test, traceback = task.check()
        assert not test
        assert len(traceback) == 1

    def test_check10(self, tmpdir):
        """Test warning in case the file already exists in new mode.

        """
        task = self.task
        task.saving_target = 'File'
        task.folder = str(tmpdir)
        task.filename = 'test_e.txt'
        task.file_mode = 'New'
        task.header = 'test'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tat{str}', '{float}')])

        file_path = os.path.join(str(tmpdir), 'test_e.txt')
        with open(file_path, 'w'):
            pass

        assert os.path.isfile(file_path)
        test, traceback = task.check()
        assert test and traceback
        assert os.path.isfile(file_path)

    def test_perform1(self, tmpdir):
        """Test performing in mode file. (Call three times perform)

        """
        task = self.task
        task.saving_target = 'File'
        task.folder = str(tmpdir)
        task.filename = 'test_perform{int}.txt'
        task.file_mode = 'Add'
        task.header = 'test {str}'
        task.array_size = '3'
        task.saved_values = OrderedDict([('toto', '{str}'),
                                         ('tat{str}', '{float}')])

        file_path = os.path.join(str(tmpdir), 'test_perform1.txt')

        with open(file_path, 'w') as f:
            f.write('test\n')

        task.perform()

        assert task.initialized
        assert task.file_object
        assert task.line_index == 1
        with open(file_path) as f:
            a = f.readlines()
            assert a == ['test\n', '# test a\n', 'toto\ttata\n', 'a\t2.0\n']

        task.perform()

        assert task.initialized
        assert task.line_index == 2
        with open(file_path) as f:
            a = f.readlines()
            assert (a == ['test\n', '# test a\n', 'toto\ttata\n', 'a\t2.0\n',
                          'a\t2.0\n'])

        task.perform()

        assert not task.initialized
        assert task.line_index == 3
        with open(file_path) as f:
            a = f.readlines()
            assert a == ['test\n', '# test a\n', 'toto\ttata\n',
                         'a\t2.0\n', 'a\t2.0\n', 'a\t2.0\n']

    def test_perform2(self):
        """Test performing in array mode. (Call three times perform)

        """
        task = self.task
        task.saving_target = 'Array'
        task.array_size = '3'
        task.saved_values = OrderedDict([('toto', '{int}'),
                                         ('tat{str}', '{float}')])

        task.perform()

        assert task.initialized
        assert task.line_index == 1

        task.perform()

        assert task.initialized
        assert task.line_index == 2

        task.perform()

        assert not task.initialized
        assert task.line_index == 3

        dtype = np.dtype({'names': [task.format_string(s)
                                    for s in task.saved_values],
                          'formats': ['f8']*len(task.saved_values)})
        array = np.empty((3),  dtype)
        array[0] = (1, 2.0)
        array[1] = (1, 2.0)
        array[2] = (1, 2.0)
        np.testing.assert_array_equal(task.array, array)