Ejemplo n.º 1
0
    def test_can_write_record(self):
        writer = DatabaseRecordWriter(connection=self.connection)
        known_record_fields = {
            'command_id': 'command',
            'source': 'TEST',
            'event_type': 'foo',
            'payload': {"foo": "bar"},
            'timestamp': 1234
        }
        writer.write_record(known_record_fields)

        cursor = self.connection.execute("SELECT COUNT(*) FROM records")
        num_records = cursor.fetchone()
        self.assertEqual(num_records[0], 1)

        cursor.execute("SELECT * FROM records")
        record = dict(cursor.fetchone())
        for col_name, row_value in known_record_fields.items():
            # Normally our reader would take care of parsing the JSON from
            # the payload.
            if col_name == 'payload':
                record[col_name] = json.loads(record[col_name])
                self.assertEqual(record[col_name], row_value)

        self.assertTrue('id' in record.keys())
        self.assertTrue('request_id' in record.keys())
Ejemplo n.º 2
0
    def test_can_write_many_records(self):
        writer = DatabaseRecordWriter(connection=self.connection)
        known_record_fields = {
            'command_id': 'command',
            'source': 'TEST',
            'event_type': 'foo',
            'payload': '',
            'timestamp': 1234
        }
        records_to_write = 40
        for _ in range(records_to_write):
            writer.write_record(known_record_fields)

        cursor = self.connection.execute("SELECT COUNT(*) FROM records")
        num_records = cursor.fetchone()
        self.assertEqual(num_records[0], records_to_write)
Ejemplo n.º 3
0
    def test_can_read_most_recent_records(self):
        writer = DatabaseRecordWriter(self.connection)
        self._write_sequence_of_records(writer, [{
            'command_id': 'command a',
            'source': 'TEST',
            'event_type': 'foo',
            'payload': '',
            'timestamp': 3
        }, {
            'command_id': 'command a',
            'source': 'TEST',
            'event_type': 'bar',
            'payload': '',
            'timestamp': 1
        }])
        self._write_sequence_of_records(writer, [{
            'command_id': 'command b',
            'source': 'TEST',
            'event_type': 'baz',
            'payload': '',
            'timestamp': 2
        }])

        # Since the foo and bar events were written by the writer_a they all
        # share an id. foo was written at time 3 which makes it the most
        # recent, so when we call get_latest_records we should get the
        # foo and bar records only.
        reader = DatabaseRecordReader(self.connection)
        records = set(
            [record['event_type'] for record in reader.iter_latest_records()])
        self.assertEqual(set(['foo', 'bar']), records)
Ejemplo n.º 4
0
    def test_can_read_record(self):
        writer = DatabaseRecordWriter(self.connection)
        self._write_sequence_of_records(writer, [
            {
                'command_id': 'command a',
                'source': 'TEST',
                'event_type': 'foo',
                'payload': '',
                'timestamp': 3
            },
            {
                'command_id': 'command a',
                'source': 'TEST',
                'event_type': 'bar',
                'payload': '',
                'timestamp': 1
            },
            {
                'command_id': 'command a',
                'source': 'TEST',
                'event_type': 'baz',
                'payload': '',
                'timestamp': 4
            }
        ])
        self._write_sequence_of_records(writer, [
            {
                'command_id': 'command b',
                'source': 'TEST',
                'event_type': 'qux',
                'payload': '',
                'timestamp': 2
            },
            {
                'command_id': 'command b',
                'source': 'TEST',
                'event_type': 'zip',
                'payload': '',
                'timestamp': 6
            }
        ])
        reader = DatabaseRecordReader(self.connection)
        cursor = self.connection.execute(
            'select id from records where event_type = "foo" limit 1')
        identifier = cursor.fetchone()['id']

        # This should select only the three records from writer_a since we
        # are explicitly looking for the records that match the id of the
        # foo event record.
        records = [record for record in reader.iter_records(identifier)]
        self.assertEqual(len(records), 3)
        for record in records:
            record_id = record['id']
            self.assertEqual(record_id, identifier)
Ejemplo n.º 5
0
def attach_history_handler(session, parsed_args, **kwargs):
    if _should_enable_cli_history(session, parsed_args):
        LOG.debug('Enabling CLI history')

        history_filename = os.environ.get(HISTORY_FILENAME_ENV_VAR,
                                          DEFAULT_HISTORY_FILENAME)
        if not os.path.isdir(os.path.dirname(history_filename)):
            os.makedirs(os.path.dirname(history_filename))

        connection = DatabaseConnection(history_filename)
        writer = DatabaseRecordWriter(connection)
        record_builder = RecordBuilder()
        db_handler = DatabaseHistoryHandler(writer, record_builder)

        HISTORY_RECORDER.add_handler(db_handler)
        HISTORY_RECORDER.enable()
Ejemplo n.º 6
0
 def setUp(self):
     super(BaseThreadedDatabaseWriter, self).setUp()
     self.threads = []
     self.writer = DatabaseRecordWriter(self.connection)
Ejemplo n.º 7
0
 def setUp(self):
     self.db = DatabaseConnection(':memory:')
     self.writer = DatabaseRecordWriter(connection=self.db)
     self.record_builder = RecordBuilder()
     self.handler = DatabaseHistoryHandler(
         writer=self.writer, record_builder=self.record_builder)
Ejemplo n.º 8
0
 def test_can_close(self):
     connection = mock.Mock()
     writer = DatabaseRecordWriter(connection)
     writer.close()
     self.assertTrue(connection.close.called)
Ejemplo n.º 9
0
 def setUp(self):
     self.db = DatabaseConnection(':memory:')
     self.writer = DatabaseRecordWriter(self.db)