Пример #1
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)
Пример #2
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)
Пример #3
0
class HistorySubcommand(BasicCommand):
    def __init__(self, session, db_reader=None, output_stream_factory=None):
        super(HistorySubcommand, self).__init__(session)
        self._db_reader = db_reader
        self._output_stream_factory = output_stream_factory
        if output_stream_factory is None:
            self._output_stream_factory = OutputStreamFactory()

    def _connect_to_history_db(self):
        if self._db_reader is None:
            connection = DatabaseConnection(self._get_history_db_filename())
            self._db_reader = DatabaseRecordReader(connection)

    def _close_history_db(self):
        self._db_reader.close()

    def _get_history_db_filename(self):
        filename = os.environ.get(
            HISTORY_FILENAME_ENV_VAR, DEFAULT_HISTORY_FILENAME)
        if not os.path.exists(filename):
            raise RuntimeError(
                'Could not locate history. Make sure cli_history is set to '
                'enabled in the ~/.aws/config file'
            )
        return filename

    def _should_use_color(self, parsed_globals):
        if parsed_globals.color == 'on':
            return True
        elif parsed_globals.color == 'off':
            return False
        return is_a_tty() and not is_windows

    def _get_output_stream(self, preferred_pager=None):
        if is_a_tty():
            return self._output_stream_factory.get_pager_stream(
                preferred_pager)
        return self._output_stream_factory.get_stdout_stream()
Пример #4
0
class HistorySubcommand(BasicCommand):
    def __init__(self, session, db_reader=None, output_stream_factory=None):
        super(HistorySubcommand, self).__init__(session)
        self._db_reader = db_reader
        self._output_stream_factory = output_stream_factory
        if output_stream_factory is None:
            self._output_stream_factory = \
                self._get_default_output_stream_factory()

    def _get_default_output_stream_factory(self):
        return OutputStreamFactory(self._session)

    def _connect_to_history_db(self):
        if self._db_reader is None:
            connection = DatabaseConnection(self._get_history_db_filename())
            self._db_reader = DatabaseRecordReader(connection)

    def _close_history_db(self):
        self._db_reader.close()

    def _get_history_db_filename(self):
        filename = os.environ.get(HISTORY_FILENAME_ENV_VAR,
                                  DEFAULT_HISTORY_FILENAME)
        if not os.path.exists(filename):
            raise RuntimeError(
                'Could not locate history. Make sure cli_history is set to '
                'enabled in the ~/.aws/config file')
        return filename

    def _should_use_color(self, parsed_globals):
        if parsed_globals.color == 'on':
            return True
        elif parsed_globals.color == 'off':
            return False
        return is_a_tty() and not is_windows

    def _get_output_stream(self):
        return self._output_stream_factory.get_output_stream()
Пример #5
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)
Пример #6
0
class TestDatabaseRecordReader(BaseDatabaseRecordTester):
    def setUp(self):
        self.fake_connection = FakeDatabaseConnection()
        self.reader = DatabaseRecordReader(self.fake_connection)

    def test_can_close(self):
        self.reader.close()
        self.assertTrue(self.fake_connection.closed)

    def test_row_factory_set(self):
        self.assertEqual(self.fake_connection.row_factory,
                         self.reader._row_factory)

    def test_iter_latest_records_performs_correct_query(self):
        expected_query = (
            '    SELECT * FROM records\n'
            '        WHERE id =\n'
            '        (SELECT id FROM records WHERE timestamp =\n'
            '        (SELECT max(timestamp) FROM records)) ORDER BY timestamp;'
        )
        [_ for _ in self.reader.iter_latest_records()]
        self.assertEqual(self.fake_connection.execute.call_args[0][0].strip(),
                         expected_query.strip())

    def test_iter_latest_records_does_iter_records(self):
        records_to_get = [1, 2, 3]
        self.fake_connection.execute.return_value.__iter__.return_value = iter(
            records_to_get)
        records = [r for r in self.reader.iter_latest_records()]
        self.assertEqual(records, records_to_get)

    def test_iter_records_performs_correct_query(self):
        expected_query = ('SELECT * from records where id = ? '
                          'ORDER BY timestamp')
        [_ for _ in self.reader.iter_records('fake_id')]
        self.assertEqual(self.fake_connection.execute.call_args[0][0].strip(),
                         expected_query.strip())

    def test_iter_records_does_iter_records(self):
        records_to_get = [1, 2, 3]
        self.fake_connection.execute.return_value.__iter__.return_value = iter(
            records_to_get)
        records = [r for r in self.reader.iter_records('fake_id')]
        self.assertEqual(records, records_to_get)
Пример #7
0
 def test_yields_nothing_no_recent_records(self):
     reader = DatabaseRecordReader(self.connection)
     records = [record for record in reader.iter_latest_records()]
     self.assertEqual(len(records), 0)
Пример #8
0
 def test_yields_nothing_if_no_matching_record_id(self):
     reader = DatabaseRecordReader(self.connection)
     records = [record for record in reader.iter_records('fake_id')]
     self.assertEqual(len(records), 0)
Пример #9
0
 def setUp(self):
     self.fake_connection = FakeDatabaseConnection()
     self.reader = DatabaseRecordReader(self.fake_connection)
Пример #10
0
 def _connect_to_history_db(self):
     if self._db_reader is None:
         connection = DatabaseConnection(self._get_history_db_filename())
         self._db_reader = DatabaseRecordReader(connection)
 def _connect_to_history_db(self):
     if self._db_reader is None:
         connection = DatabaseConnection(self._get_history_db_filename())
         self._db_reader = DatabaseRecordReader(connection)