def test_iter(): table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) table.insert({'name': 'one', 'val': 1, 'lucky': False}) table.insert({'name': 'nine', 'val': 9, 'lucky': False}) table.insert({'name': 'thirteen', 'val': 13, 'lucky': True}) assert list(table) == [('one', 1, False), ('nine', 9, False), ('thirteen', 13, True)]
def test_insert_batch_replace(qtbot): table = sql.SqlTable('Foo', ['name', 'val', 'lucky'], constraints={'name': 'PRIMARY KEY'}) with qtbot.wait_signal(table.changed): table.insert_batch({ 'name': ['one', 'nine', 'thirteen'], 'val': [1, 9, 13], 'lucky': [False, False, True] }) with qtbot.wait_signal(table.changed): table.insert_batch( { 'name': ['one', 'nine'], 'val': [11, 19], 'lucky': [True, True] }, replace=True) assert list(table) == [('thirteen', 13, True), ('one', 11, True), ('nine', 19, True)] with pytest.raises(sql.BugError): table.insert_batch({ 'name': ['one', 'nine'], 'val': [11, 19], 'lucky': [True, True] })
def test_delete_all(qtbot): table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) table.insert({'name': 'one', 'val': 1, 'lucky': False}) table.insert({'name': 'nine', 'val': 9, 'lucky': False}) table.insert({'name': 'thirteen', 'val': 13, 'lucky': True}) with qtbot.wait_signal(table.changed): table.delete_all() assert list(table) == []
def test_len(): table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) assert len(table) == 0 table.insert({'name': 'one', 'val': 1, 'lucky': False}) assert len(table) == 1 table.insert({'name': 'nine', 'val': 9, 'lucky': False}) assert len(table) == 2 table.insert({'name': 'thirteen', 'val': 13, 'lucky': True}) assert len(table) == 3
def test_timestamp_fmt(fmt, expected, model_validator, config_stub, init_sql): """Validate the filtering and sorting results of set_pattern.""" config_stub.val.completion.timestamp_format = fmt hist = sql.SqlTable('CompletionHistory', ['url', 'title', 'last_atime']) atime = datetime.datetime(2018, 2, 27, 8, 30) hist.insert({'url': 'foo', 'title': '', 'last_atime': atime.timestamp()}) cat = histcategory.HistoryCategory() model_validator.set_model(cat) cat.set_pattern('') model_validator.validate([('foo', '', expected)])
def test_insert_batch(qtbot): table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) with qtbot.waitSignal(table.changed): table.insert_batch({'name': ['one', 'nine', 'thirteen'], 'val': [1, 9, 13], 'lucky': [False, False, True]}) assert list(table) == [('one', 1, False), ('nine', 9, False), ('thirteen', 13, True)]
def test_insert_replace(qtbot): table = sql.SqlTable('Foo', ['name', 'val', 'lucky'], constraints={'name': 'PRIMARY KEY'}) with qtbot.wait_signal(table.changed): table.insert({'name': 'one', 'val': 1, 'lucky': False}, replace=True) with qtbot.wait_signal(table.changed): table.insert({'name': 'one', 'val': 11, 'lucky': True}, replace=True) assert list(table) == [('one', 11, True)] with pytest.raises(sql.BugError): table.insert({'name': 'one', 'val': 11, 'lucky': True}, replace=False)
def test_bool_benchmark(benchmark): table = sql.SqlTable('Foo', ['number']) # Simulate a history table table.create_index('NumberIndex', 'number') table.insert_batch({'number': [str(i) for i in range(100_000)]}) def run(): assert table benchmark(run)
def test_delete(qtbot): table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) table.insert({'name': 'one', 'val': 1, 'lucky': False}) table.insert({'name': 'nine', 'val': 9, 'lucky': False}) table.insert({'name': 'thirteen', 'val': 13, 'lucky': True}) with pytest.raises(KeyError): table.delete('name', 'nope') with qtbot.wait_signal(table.changed): table.delete('name', 'thirteen') assert list(table) == [('one', 1, False), ('nine', 9, False)] with qtbot.wait_signal(table.changed): table.delete('lucky', False) assert not list(table)
def test_contains(): table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) table.insert({'name': 'one', 'val': 1, 'lucky': False}) table.insert({'name': 'nine', 'val': 9, 'lucky': False}) table.insert({'name': 'thirteen', 'val': 13, 'lucky': True}) name_query = table.contains_query('name') val_query = table.contains_query('val') lucky_query = table.contains_query('lucky') assert name_query.run(val='one').value() assert name_query.run(val='thirteen').value() assert val_query.run(val=9).value() assert lucky_query.run(val=False).value() assert lucky_query.run(val=True).value() assert not name_query.run(val='oone').value() assert not name_query.run(val=1).value() assert not name_query.run(val='*').value() assert not val_query.run(val=10).value()
def test_bool(): table = sql.SqlTable('Foo', ['name']) assert not table table.insert({'name': 'one'}) assert table
def hist(data_tmpdir, config_stub): db = sql.Database(str(data_tmpdir / 'test_histcategory.db')) config_stub.val.completion.timestamp_format = '%Y-%m-%d' config_stub.val.completion.web_history.max_items = -1 return sql.SqlTable(db, 'CompletionHistory', ['url', 'title', 'last_atime'])
def test_select(rows, sort_by, sort_order, limit, result): table = sql.SqlTable('Foo', ['a', 'b']) for row in rows: table.insert(row) assert list(table.select(sort_by, sort_order, limit)) == result
def hist(init_sql, config_stub): config_stub.val.completion.timestamp_format = '%Y-%m-%d' config_stub.val.completion.web_history.max_items = -1 return sql.SqlTable('CompletionHistory', ['url', 'title', 'last_atime'])
def test_delete_optional(qtbot): table = sql.SqlTable('Foo', ['name', 'val']) table.delete('name', 'doesnotexist', optional=True)
def test_init(): sql.SqlTable('Foo', ['name', 'val', 'lucky']) # should not error if table already exists sql.SqlTable('Foo', ['name', 'val', 'lucky'])
def test_insert(qtbot): table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) with qtbot.wait_signal(table.changed): table.insert({'name': 'one', 'val': 1, 'lucky': False}) with qtbot.wait_signal(table.changed): table.insert({'name': 'wan', 'val': 1, 'lucky': False})
def test_num_rows_affected(self, condition): table = sql.SqlTable('Foo', ['name']) table.insert({'name': 'helloworld'}) q = sql.Query(f'DELETE FROM Foo WHERE {condition}') q.run() assert q.rows_affected() == condition
def hist(init_sql, config_stub): config_stub.data['completion'] = { 'timestamp-format': '%Y-%m-%d', 'web-history-max-items': -1 } return sql.SqlTable('CompletionHistory', ['url', 'title', 'last_atime'])