bag_csv_test.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 #
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2008, Willow Garage, Inc.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of the Willow Garage nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 
35 # Author: Kevin Watts
36 
37 PKG = 'diagnostic_analysis'
38 
39 import roslib; roslib.load_manifest(PKG)
40 import rostest
41 import unittest
42 
43 import rosbag
44 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
45 
46 import random
47 import tempfile
48 import time, os
49 import csv
50 
51 from diagnostic_analysis.exporter import LogExporter
52 from diagnostic_analysis.sparse import *
53 
54 row_count = 100
55 
56 ##\brief Make DiagnosticArray message for testing
57 def make_status_msg(count):
58  array = DiagnosticArray()
59  stat = DiagnosticStatus()
60  stat.level = 0
61  stat.message = 'OK'
62  stat.name = 'Unit Test'
63  stat.hardware_id = 'HW ID'
64  stat.values = [
65  KeyValue('Value A', str(count)),
66  KeyValue('Value B', str(count)),
67  KeyValue('Value C', str(count))]
68  array.status = [ stat ]
69  return array
70 
71 ##\brief Tests convert logfile to CSV and making sparse
72 class TestBagToCSV(unittest.TestCase):
73  def setUp(self):
74  # Make logfile with bogus messages
75  self.bag = tempfile.NamedTemporaryFile()
76 
77  rebagger = rosbag.Bag(self.bag.name, 'w')
78  for i in range(0, row_count):
79  rebagger.write("/diagnostics", make_status_msg(i))
80  rebagger.close()
81 
82  # Make CSV
83  self.exp = LogExporter(None, self.bag.name)
84  self.exp.process_log()
85  self.exp.finish_logfile()
86  self.filename = self.exp.get_filename('Unit Test')
87 
88  ## Make sparse CSV's
91 
92  ##\brief Tests that exported file exists and is not None
93  def test_file_exists(self):
94  self.assert_(self.filename is not None, "CSV file is None")
95  self.assert_(os.path.isfile(self.filename), "CSV file doesn't exist")
96 
97  ##\brief Test that CSV file has correct data, number of lines
98  def test_export(self):
99  # Read CSV, count rows
100  input_reader = csv.reader(open(self.filename, 'rb'))
101  count = -1
102  for row in input_reader:
103  if count == -1:
104  self.assert_(row[2].strip() == 'Message')
105  self.assert_(row[3].strip() == 'Hardware ID')
106  self.assert_(row[4].strip() == 'Value A')
107  count += 1
108  continue
109 
110  self.assert_(row[2].strip() == 'OK')
111  self.assert_(row[3].strip() == 'HW ID')
112  self.assert_(row[4].strip() == str(count))
113  count += 1
114 
115  self.assert_(count == row_count, "Row count doesn't match")
116 
117  ##\brief Tests that sparse CSV made with 'skip' option has correct number of lines
118  def test_sparse_skip(self):
119  self.assert_(len(open(self.skip_10).read().split('\n')) <= int(row_count / 10) + 2, "Length of sparse CSV (skipped) incorrect")
120 
121  ##\brief Tests that sparse CSV made with 'length' option has correct number of lines
123  self.assert_(len(open(self.length_10).read().split('\n')) == 12, "Length of sparse CSV incorrect")
124 
125  def tearDown(self):
126  self.bag.close()
127  os.remove(self.skip_10)
128  os.remove(self.length_10)
129 
130  self.exp.remove_files()
131 
132 
133 if __name__ == '__main__':
134  if True: # Use rostest for accurate results
135  rostest.unitrun(PKG, 'bag_csv_test', TestBagToCSV)
136  else:
137  # Manual test suite
138  suite = unittest.TestSuite()
139  suite.addTest(TestBagToCSV('test_file_exists'))
140  suite.addTest(TestBagToCSV('test_export'))
141  suite.addTest(TestBagToCSV('test_sparse_skip'))
142  suite.addTest(TestBagToCSV('test_sparse_length'))
143 
144  unittest.TextTestRunner(verbosity = 2).run(suite)
Tests convert logfile to CSV and making sparse.
Definition: bag_csv_test.py:72
def test_sparse_length(self)
Tests that sparse CSV made with &#39;length&#39; option has correct number of lines.
def test_file_exists(self)
Tests that exported file exists and is not None.
Definition: bag_csv_test.py:93
def test_sparse_skip(self)
Tests that sparse CSV made with &#39;skip&#39; option has correct number of lines.
def test_export(self)
Test that CSV file has correct data, number of lines.
Definition: bag_csv_test.py:98
def make_status_msg(count)
Make DiagnosticArray message for testing.
Definition: bag_csv_test.py:57
Converts and processes diagnostics logs to CSV format.
Definition: exporter.py:47
def make_sparse_length(csv_file, length)
Makes sparse CSV with the given number of rows.
Definition: sparse.py:70
def make_sparse_skip(csv_file, skip)
Makes sparse CSV by skipping every nth value.
Definition: sparse.py:48
skip_10
Make sparse CSV&#39;s.
Definition: bag_csv_test.py:89


diagnostic_analysis
Author(s): Kevin Watts, Brice Rebsamen , Eric Berger, Kevin Watts
autogenerated on Thu Oct 8 2020 03:19:33