Exemplo n.º 1
0
    def test_read_single(self):
        """Test ReadFile() read single file."""

        # Create file reader object.
        fname = os.path.join(LOG_PATH, 'UnitTestMessageA.log')

        # Create file readers with different message types.
        file_readers = [
            ReadFile(fname),
            ReadFile(fname, message=True),
            ReadFile(fname, message=UnitTestMessageB)
        ]

        # Type returned by each file reader.
        #
        # Note: We can cast UnitTestMessageA data into UnitTestMessageB data
        #       because the data structure is the same.)
        types = [dict, UnitTestMessageA, UnitTestMessageB]

        # Iterate through readers.
        for rf, dtype in zip(file_readers, types):

            # Ensure items in file can be read correctly.
            for i in range(10):
                self.assertTrue(rf.is_data_pending())
                message = rf.read()
                self.assertEqual(type(message['payload']), dtype)
                self.assertEqual(round(100 * message['elapsed_time']), i)
                self.assertEqual(round(100 * message['payload']['timestamp']),
                                 i)

            # Ensure None is returned when all data has been read.
            self.assertFalse(rf.is_data_pending())
            message = rf.read()
            self.assertEqual(message, None)
Exemplo n.º 2
0
    def test_type(self):
        """Test ReadFile() type specification."""

        # Create file reader object.
        fname = os.path.join(LOG_PATH, 'UnitTestMessageA.log')

        # Load raw data.
        rf = ReadFile(fname)
        self.assertEqual(rf.header['type'], UnitTestMessageA)
        self.assertEqual(type(rf.read()['payload']), dict)

        # Load data into message object specified in the log header.
        rf = ReadFile(fname, message=True)
        self.assertEqual(rf.header['type'], UnitTestMessageA)
        self.assertEqual(type(rf.read()['payload']), UnitTestMessageA)

        # Load data into message object specified by MCL object.
        rf = ReadFile(fname, message=UnitTestMessageA)
        self.assertEqual(rf.header['type'], UnitTestMessageA)
        self.assertEqual(type(rf.read()['payload']), UnitTestMessageA)

        # Load data into message object specified by string.
        rf = ReadFile(fname, message='UnitTestMessageB')
        self.assertEqual(rf.header['type'], UnitTestMessageA)
        self.assertEqual(type(rf.read()['payload']), UnitTestMessageB)
Exemplo n.º 3
0
    def test_read_raw(self):
        """Test ReadFile() read raw file."""

        rf = ReadFile(os.path.join(LOG_PATH, 'RawUnitTestData.log'))
        self.assertEqual(rf.min_time, None)
        self.assertEqual(rf.max_time, None)
        self.assertEqual(rf.header['version'], '1.0')
        self.assertEqual(rf.header['revision'], None)
        self.assertEqual(rf.header['created'], '1970-01-01 00:00:00')
        self.assertEqual(rf.header['type'], None)

        # Verify content.
        for i in range(10):
            message = rf.read()
            self.assertEqual(message['elapsed_time'], i + 1)
            self.assertEqual(message['payload'], (i * range(i)))
Exemplo n.º 4
0
    def test_reset(self):
        """Test ReadFile() reset."""

        # Create file reader object.
        fname = os.path.join(LOG_PATH, 'UnitTestMessageA.log')
        rf = ReadFile(fname)

        # Read first few items.
        for j in range(2):
            for i in range(10):
                message = rf.read()
                self.assertEqual(round(100 * message['elapsed_time']), i)
                self.assertEqual(round(100 * message['payload']['timestamp']), i)

            # Reset object (ensure data is read from beginning on next loop).
            rf.reset()
Exemplo n.º 5
0
    def test_read_raw(self):
        """Test ReadFile() read raw file."""

        rf = ReadFile(os.path.join(LOG_PATH, 'RawUnitTestData.log'))
        self.assertEqual(rf.min_time, None)
        self.assertEqual(rf.max_time, None)
        self.assertEqual(rf.header['version'], '1.0')
        self.assertEqual(rf.header['revision'], None)
        self.assertEqual(rf.header['created'], '1970-01-01 00:00:00')
        self.assertEqual(rf.header['type'], None)

        # Verify content.
        for i in range(10):
            message = rf.read()
            self.assertEqual(message['elapsed_time'], i + 1)
            self.assertEqual(message['payload'], (i * range(i)))
Exemplo n.º 6
0
    def test_reset(self):
        """Test ReadFile() reset."""

        # Create file reader object.
        fname = os.path.join(LOG_PATH, 'UnitTestMessageA.log')
        rf = ReadFile(fname)

        # Read first few items.
        for j in range(2):
            for i in range(10):
                message = rf.read()
                self.assertEqual(round(100 * message['elapsed_time']), i)
                self.assertEqual(round(100 * message['payload']['timestamp']),
                                 i)

            # Reset object (ensure data is read from beginning on next loop).
            rf.reset()
Exemplo n.º 7
0
    def test_initialisation(self):
        """Test ReadFile() type specification."""

        # Create file reader object.
        fname = os.path.join(LOG_PATH, 'UnitTestMessageA.log')
        ReadFile(fname)

        # Ensure failure on files that do not exit.
        with self.assertRaises(IOError):
            ReadFile('missing_file.log')

        # Ensure failure on non-numeric minimum times.
        with self.assertRaises(TypeError):
            ReadFile(fname, min_time='a')

        # Ensure failure on non-numeric maximum times.
        with self.assertRaises(TypeError):
            ReadFile(fname, max_time='a')

        # Ensure failure on smaller max time than min time.
        with self.assertRaises(ValueError):
            ReadFile(fname, min_time=10, max_time=5)

        # Ensure initialisation fails on non-boolean or string input for
        # message.
        with self.assertRaises(TypeError):
            ReadFile(fname, message=list())

        # Attempt to load message that does not exist.
        with self.assertRaises(TypeError):
            ReadFile(fname, message='missing_message')
Exemplo n.º 8
0
    def test_header(self):
        """Test ReadFile() header."""

        rf = ReadFile(os.path.join(LOG_PATH, 'UnitTestMessageA.log'))
        self.assertEqual(rf.min_time, None)
        self.assertEqual(rf.max_time, None)
        self.assertEqual(rf.header['version'], '1.0')
        self.assertEqual(rf.header['revision'],
                         'ffff000000000000000000000000000000000000')
        self.assertEqual(rf.header['created'], '1970-01-01 00:00:00')
        self.assertEqual(rf.header['type'], UnitTestMessageA)
Exemplo n.º 9
0
    def test_read_partial(self):
        """Test ReadFile() read one split file."""

        # Path of a single split log file.
        fname = os.path.join(SPT_PATH, 'UnitTestMessageA_001.log')
        rf = ReadFile(fname)

        # There is no header block to read. Ensure object returns None.
        self.assertEqual(rf.header, None)

        # Ensure items in split log-files can be read correctly.
        for i in range(3, 6):
            self.assertTrue(rf.is_data_pending())
            message = rf.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)

        # Ensure None is returned when all data has been read.
        self.assertFalse(rf.is_data_pending())
        message = rf.read()
        self.assertEqual(message, None)
Exemplo n.º 10
0
    def test_read_split(self):
        """Test ReadFile() read split files."""

        # Prefix of split log files.
        fname = os.path.join(SPT_PATH, 'UnitTestMessageA')
        rf = ReadFile(fname, message=True)

        # Ensure object can parse the header block.
        self.assertNotEqual(rf.header, None)
        self.assertEqual(rf.header['type'], UnitTestMessageA)

        # Ensure items in split log-files can be read correctly.
        for i in range(10):
            self.assertTrue(rf.is_data_pending())
            message = rf.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)

        # Ensure None is returned when all data has been read.
        self.assertFalse(rf.is_data_pending())
        message = rf.read()
        self.assertEqual(message, None)
Exemplo n.º 11
0
    def test_read_time(self):
        """Test ReadFile() read min/max time."""

        # Define time interval of interest.
        min_time = 0.035
        max_time = 0.085

        # Create file reader object.
        fname = os.path.join(LOG_PATH, 'UnitTestMessageA.log')
        rf = ReadFile(fname, min_time=min_time, max_time=max_time)

        # Ensure time ranges were set.
        self.assertEqual(rf.min_time, min_time)
        self.assertEqual(rf.max_time, max_time)

        # Ensure object can filter items by time .
        for i in range(4, 9):
            message = rf.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)

        # Ensure None is returned when all data has been read.
        message = rf.read()
        self.assertEqual(message, None)
Exemplo n.º 12
0
    def test_read_time(self):
        """Test ReadFile() read min/max time."""

        # Define time interval of interest.
        min_time = 0.035
        max_time = 0.085

        # Create file reader object.
        fname = os.path.join(LOG_PATH, 'UnitTestMessageA.log')
        rf = ReadFile(fname, min_time=min_time, max_time=max_time)

        # Ensure time ranges were set.
        self.assertEqual(rf.min_time, min_time)
        self.assertEqual(rf.max_time, max_time)

        # Ensure object can filter items by time .
        for i in range(4, 9):
            message = rf.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)

        # Ensure None is returned when all data has been read.
        message = rf.read()
        self.assertEqual(message, None)
Exemplo n.º 13
0
    def test_read_partial(self):
        """Test ReadFile() read one split file."""

        # Path of a single split log file.
        fname = os.path.join(SPT_PATH, 'UnitTestMessageA_001.log')
        rf = ReadFile(fname)

        # There is no header block to read. Ensure object returns None.
        self.assertEqual(rf.header, None)

        # Ensure items in split log-files can be read correctly.
        for i in range(3, 6):
            self.assertTrue(rf.is_data_pending())
            message = rf.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)

        # Ensure None is returned when all data has been read.
        self.assertFalse(rf.is_data_pending())
        message = rf.read()
        self.assertEqual(message, None)
Exemplo n.º 14
0
    def test_read_split(self):
        """Test ReadFile() read split files."""

        # Prefix of split log files.
        fname = os.path.join(SPT_PATH, 'UnitTestMessageA')
        rf = ReadFile(fname, message=True)

        # Ensure object can parse the header block.
        self.assertNotEqual(rf.header, None)
        self.assertEqual(rf.header['type'], UnitTestMessageA)

        # Ensure items in split log-files can be read correctly.
        for i in range(10):
            self.assertTrue(rf.is_data_pending())
            message = rf.read()
            self.assertEqual(round(100 * message['elapsed_time']), i)
            self.assertEqual(round(100 * message['payload']['timestamp']), i)

        # Ensure None is returned when all data has been read.
        self.assertFalse(rf.is_data_pending())
        message = rf.read()
        self.assertEqual(message, None)
Exemplo n.º 15
0
    def test_type(self):
        """Test ReadFile() type specification."""

        # Create file reader object.
        fname = os.path.join(LOG_PATH, 'UnitTestMessageA.log')

        # Load raw data.
        rf = ReadFile(fname)
        self.assertEqual(rf.header['type'], UnitTestMessageA)
        self.assertEqual(type(rf.read()['payload']), dict)

        # Load data into message object specified in the log header.
        rf = ReadFile(fname, message=True)
        self.assertEqual(rf.header['type'], UnitTestMessageA)
        self.assertEqual(type(rf.read()['payload']), UnitTestMessageA)

        # Load data into message object specified by MCL object.
        rf = ReadFile(fname, message=UnitTestMessageA)
        self.assertEqual(rf.header['type'], UnitTestMessageA)
        self.assertEqual(type(rf.read()['payload']), UnitTestMessageA)

        # Load data into message object specified by string.
        rf = ReadFile(fname, message='UnitTestMessageB')
        self.assertEqual(rf.header['type'], UnitTestMessageA)
        self.assertEqual(type(rf.read()['payload']), UnitTestMessageB)