test_bag_to_csv.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 Test: grep input bags to CSV output.
5 
6 ------------------------------------------------------------------------------
7 This file is part of grepros - grep for ROS bag files and live topics.
8 Released under the BSD License.
9 
10 @author Erki Suurjaak
11 @created 22.12.2021
12 @modified 25.12.2021
13 ------------------------------------------------------------------------------
14 """
15 import glob
16 import logging
17 import os
18 import sys
19 
20 sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
21 from test import testbase
22 
23 logger = logging.getLogger()
24 
25 
27  """Tests grepping from input bags and writing matches to CSV files."""
28 
29 
30  NAME = os.path.splitext(os.path.basename(__file__))[0]
31 
32 
33  OUTPUT_LABEL = "CSV"
34 
35 
36  OUTPUT_SUFFIX = ".csv"
37 
38  def setUp(self):
39  """Collects bags in data directory, assembles command."""
40  super().setUp()
41  self._cmd = self.CMD_BASE + ["--no-console-output", "--write", self._outname]
42 
43  def tearDown(self):
44  """Terminates subprocess and deletes temporary output files, if any."""
45  super().tearDown()
46  for filename in glob.glob("%s*%s" % os.path.splitext(self._outname)):
47  try: os.unlink(filename)
48  except Exception: pass
49 
50  def test_grepros(self):
51  """Runs grepros on bags in data directory, verifies CSV output."""
52  self.verify_bags()
53  self.run_command()
54 
55  logger.info("Verifying topics and messages.")
56  filebase, fileext = os.path.splitext(self._outname)
57  for bag in self._bags:
58  bagname = os.path.splitext(os.path.basename(bag))[0]
59  fulltext = ""
60  for topicbase in self.EXPECTED_TOPIC_BASES:
61  topic = topicbase + "/" + bagname
62  value = " ".join(self.EXPECTED_WORDS + [bagname])
63 
64  filename = "%s.%s%s" % (filebase, topic.lstrip("/").replace("/", "__"), fileext)
65  self.assertTrue(os.path.isfile(filename),
66  "Expected output file not written: %s" % filename)
67  logger.info("Reading data from written %s %s.",
68  self.OUTPUT_LABEL, os.path.basename(filename))
69  with open(filename) as f:
70  ftext = f.read()
71  self.assertIn(value, ftext, "Expected message value not in output.")
72  fulltext += "\n\n" + ftext
73  for topicbase in self.SKIPPED_TOPIC_BASES:
74  topic = topicbase + "/" + bagname
75  filename = "%s.%s%s" % (filebase, topic.lstrip("/").replace("/", "__"), fileext)
76  self.assertFalse(os.path.isfile(filename),
77  "Unexpected output file written: %s" % filename)
78 
79 
80 if "__main__" == __name__:
81  TestBagInputCsvOutput.run_rostest()
test.testbase.TestBase
Definition: testbase.py:46
test.test_bag_to_csv.TestBagInputCsvOutput._cmd
_cmd
Definition: test_bag_to_csv.py:41
test.testbase.TestBase.CMD_BASE
list CMD_BASE
Base command for running grepros.
Definition: testbase.py:71
test.test_bag_to_csv.TestBagInputCsvOutput.setUp
def setUp(self)
Definition: test_bag_to_csv.py:38
test.test_bag_to_csv.TestBagInputCsvOutput.OUTPUT_LABEL
string OUTPUT_LABEL
Name used in logging.
Definition: test_bag_to_csv.py:33
test.test_bag_to_csv.TestBagInputCsvOutput.tearDown
def tearDown(self)
Definition: test_bag_to_csv.py:43
test.testbase.TestBase.EXPECTED_WORDS
list EXPECTED_WORDS
Words expected in matched messages, bag name appended.
Definition: testbase.py:77
test.test_bag_to_csv.TestBagInputCsvOutput.test_grepros
def test_grepros(self)
Definition: test_bag_to_csv.py:50
test.test_bag_to_csv.TestBagInputCsvOutput
Definition: test_bag_to_csv.py:26
test.testbase.TestBase.SKIPPED_TOPIC_BASES
list SKIPPED_TOPIC_BASES
Topics not expected in output, bag name appended.
Definition: testbase.py:86
test.testbase.TestBase.EXPECTED_TOPIC_BASES
list EXPECTED_TOPIC_BASES
Topics expected in output, bag name appended.
Definition: testbase.py:83
test.testbase.TestBase._outname
_outname
Definition: testbase.py:97
test.testbase.TestBase._bags
_bags
Definition: testbase.py:94
test.testbase.TestBase.verify_bags
def verify_bags(self)
Definition: testbase.py:169
test.testbase.TestBase.run_command
def run_command(self, communicate=True)
Definition: testbase.py:154


grepros
Author(s): Erki Suurjaak
autogenerated on Sat Jan 6 2024 03:11:29