test_bag_to_sqlite.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 Test: grep input bags to SQLite 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 23.12.2021
12 @modified 24.12.2021
13 ------------------------------------------------------------------------------
14 """
15 import logging
16 import os
17 import sqlite3
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 SQLite file."""
28 
29 
30  NAME = os.path.splitext(os.path.basename(__file__))[0]
31 
32 
33  OUTPUT_LABEL = "SQLite"
34 
35 
36  OUTPUT_SUFFIX = ".sqlite"
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 test_grepros(self):
44  """Runs grepros on bags in data directory, verifies SQLite output."""
45  self.verify_bags()
46  self.run_command()
47  self.assertTrue(os.path.isfile(self._outname), "Expected output file not written.")
48 
49  logger.info("Reading data from written %s.", self.OUTPUT_LABEL)
50  db = self._outfile = sqlite3.connect(self._outname)
51  db.row_factory = lambda cursor, row: dict(sqlite3.Row(cursor, row))
52  messages = {} # {topic: [msg, ]}
53  for msg in db.execute("SELECT * FROM messages").fetchall():
54  messages.setdefault(msg["topic"], []).append(msg)
55  db.close()
56 
57  fulltext = "\n".join(str(m) for mm in messages.values() for m in mm)
58  self.verify_topics(fulltext, fulltext)
59 
60 
61 if "__main__" == __name__:
62  TestBagInputSqliteOutput.run_rostest()
test.testbase.TestBase
Definition: testbase.py:46
test.test_bag_to_sqlite.TestBagInputSqliteOutput._outfile
_outfile
Definition: test_bag_to_sqlite.py:50
test.test_bag_to_sqlite.TestBagInputSqliteOutput
Definition: test_bag_to_sqlite.py:26
test.testbase.TestBase.CMD_BASE
list CMD_BASE
Base command for running grepros.
Definition: testbase.py:71
test.testbase.TestBase.verify_topics
def verify_topics(self, topics, messages=None)
Definition: testbase.py:176
test.test_bag_to_sqlite.TestBagInputSqliteOutput.OUTPUT_LABEL
string OUTPUT_LABEL
Name used in logging.
Definition: test_bag_to_sqlite.py:33
test.test_bag_to_sqlite.TestBagInputSqliteOutput.test_grepros
def test_grepros(self)
Definition: test_bag_to_sqlite.py:43
test.test_bag_to_sqlite.TestBagInputSqliteOutput.setUp
def setUp(self)
Definition: test_bag_to_sqlite.py:38
test.testbase.TestBase._outname
_outname
Definition: testbase.py:97
test.test_bag_to_sqlite.TestBagInputSqliteOutput._cmd
_cmd
Definition: test_bag_to_sqlite.py:41
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