def setUp(self): """Create test data.""" self.table = 'people' self.db_file = create_db(self.table) self.datasource = SQLiteDataSource( self.db_file, table=self.table, config=[ { 'column': 'First name', 'type': 'str' }, { 'column': 'Last name', 'type': 'str' }, { 'column': 'Age', 'type': 'int' }, { 'column': 'Start', 'type': 'int', 'encoding': 'timestamp' }, { 'column': 'Image', 'type': 'int', 'encoding': 'image' }, ], ) self.datasource.MAX_RECS = 2
def test_explicit_query(self): """Test using an explicit query for the data source.""" # Important to not ensure "selected" column if there is no primary # key returned by the query. datasource = SQLiteDataSource( self.db_file, query='SELECT first_name, age FROM people', ensure_selected_column=False) rows = datasource.load() data = {tuple(row.data) for row in rows} reference_data = {(record[1], record[3]) for record in TEST_DATA[self.table]['data']} self.assertEqual(data, reference_data)
def test_explicit_query(self): """Test using an explicit query for the data source.""" # Important to not ensure "selected" column if there is no primary # key returned by the query. datasource = SQLiteDataSource( self.db_file, query='SELECT first_name, age FROM people', ensure_selected_column=False ) rows = datasource.load() data = {tuple(row.data) for row in rows} reference_data = {(record[1], record[3]) for record in TEST_DATA[self.table]['data']} self.assertEqual(data, reference_data)
def setUp(self): # noqa """Create test data.""" self.table = 'people' self.db_file = create_db(self.table) self.datasource = SQLiteDataSource(self.db_file, self.table, None, [ { 'column': 'First name', 'type': 'str' }, { 'column': 'Last name', 'type': 'str' }, { 'column': 'Age', 'type': 'int' }, { 'column': 'Start', 'type': 'int', 'encoding': 'timestamp' }, { 'column': 'Image', 'type': 'buffer', 'encoding': 'image' }, ]) self.datasource.MAX_RECS = 2 # 2 records per page win = mock.MagicMock() datagrid_container = DataGridContainer(win) self.datagrid_controller = DataGridController(datagrid_container, self.datasource) self.model = self.datagrid_controller.model
def setUp(self): # noqa """Create test data.""" self.datagrid_model = DataGridModel( data_source=SQLiteDataSource('', 'test', ensure_selected_column=False), get_media_callback=mock.MagicMock(), decode_fallback=mock.MagicMock())
def select_table(selection): model, iterator = selection.get_selected() if iterator: table_name, config = model[iterator] controller.bind_datasource( SQLiteDataSource(db_path, table_name, config=config, ensure_selected_column=False, display_all=True, persist_columns_visibility=False))
def setUp(self): """Create test data.""" self.table = 'people' self.db_file = create_db(self.table) self.datasource = SQLiteDataSource( self.db_file, table=self.table, config=[ {'column': 'First name', 'type': 'str'}, {'column': 'Last name', 'type': 'str'}, {'column': 'Age', 'type': 'int'}, {'column': 'Start', 'type': 'int', 'encoding': 'timestamp'}, {'column': 'Image', 'type': 'int', 'encoding': 'image'}, ], ) self.datasource.MAX_RECS = 2
class SQLiteDataSourceTest(unittest.TestCase): """Test SQLiteDataSource.""" def setUp(self): """Create test data.""" self.table = 'people' self.db_file = create_db(self.table) self.datasource = SQLiteDataSource( self.db_file, table=self.table, config=[ { 'column': 'First name', 'type': 'str' }, { 'column': 'Last name', 'type': 'str' }, { 'column': 'Age', 'type': 'int' }, { 'column': 'Start', 'type': 'int', 'encoding': 'timestamp' }, { 'column': 'Image', 'type': 'int', 'encoding': 'image' }, ], ) self.datasource.MAX_RECS = 2 def tearDown(self): """Remove test data file.""" os.unlink(self.db_file) def test_load(self): """Load first page of records and get total.""" rows = self.datasource.load() self.assertEqual(len(rows), 2) self.assertEqual(self.datasource.total_recs, 4) def test_load_with_params(self): """Filter and order records.""" param = { 'where': { 'age': { 'param': 30, 'operator': '>' } }, 'order_by': 'age', 'desc': True } rows = self.datasource.load(param) self.assertEqual(len(rows), 2) self.assertEqual(rows[0].data[2], 'Goldman') def test_load_with_where_param(self): """Filter results with a search param.""" param = { 'where': { 'search': { 'param': 'gold', }, }, } rows = self.datasource.load(param) self.assertEqual(len(rows), 2) self.assertEqual({('Oscar', 'Goldman'), ('Monica', 'Goldman')}, {(row.data[1], row.data[2]) for row in rows}) def test_load_paging(self): """Load first and second pages of records.""" self.datasource.load() # initial load is always without paging rows = self.datasource.load({'page': 1}) self.assertEqual(len(rows), 2) self.assertEqual(rows[0].data[2], 'Goldman') def test_update(self): """Update __selected in first record in data set.""" self.datasource.update({'__selected': True}, [1]) # ^^ update row with id 1 rows = self.datasource.load() self.assertEqual(rows[0].data[0], 1) def test_get_all_record_ids(self): """Get all record ids for a particular query.""" param = {'where': {'age': {'param': 30, 'operator': '>'}}} ids = self.datasource.get_all_record_ids(param) self.assertEqual(ids, [2, 3, 4]) def test_visible_columns(self): """Set visible columns and ensure they're persisted.""" self.datasource.set_visible_columns(['last_name']) self.assertEqual(self.datasource.get_visible_columns(), ['last_name']) def test_get_single_record(self): """Retrieve a single record as a tuple of values.""" row = self.datasource.get_single_record(1) self.assertEqual(row[0], 1) self.assertEqual(row[1], 'Dee') self.assertEqual(row[2], 'Timberlake') def test_select(self): """Get data without class instance or paging.""" db_file = self.datasource.db_file with contextlib.closing(sqlite3.connect(db_file)) as conn: results = list(self.datasource.select(conn, self.datasource.table)) self.assertEqual(len(results), 4) def test_explicit_query(self): """Test using an explicit query for the data source.""" # Important to not ensure "selected" column if there is no primary # key returned by the query. datasource = SQLiteDataSource( self.db_file, query='SELECT first_name, age FROM people', ensure_selected_column=False) rows = datasource.load() data = {tuple(row.data) for row in rows} reference_data = {(record[1], record[3]) for record in TEST_DATA[self.table]['data']} self.assertEqual(data, reference_data)
DataGridController, ) logger = logging.getLogger(__name__) if __name__ == '__main__': setup_logging_to_stdout() setup_gtk_show_rules_hint() logger.info("Starting a datagrid_gtk3 example.") path = os.path.join(os.path.dirname(os.path.realpath(__file__))) db_path = os.path.join(path, 'selectable.sqlite') data_source = SQLiteDataSource(db_path, 'fruits', ensure_selected_column=True) win = Gtk.Window() datagrid_container = DataGridContainer(win) controller = DataGridController(datagrid_container, data_source, has_checkboxes=True) datagrid_container.grid_vbox.reparent(win) win.set_default_size(600, 400) win.connect("delete-event", lambda *args: Gtk.main_quit()) win.show() try:
class SQLiteDataSourceTest(unittest.TestCase): """Test SQLiteDataSource.""" def setUp(self): """Create test data.""" self.table = 'people' self.db_file = create_db(self.table) self.datasource = SQLiteDataSource( self.db_file, table=self.table, config=[ {'column': 'First name', 'type': 'str'}, {'column': 'Last name', 'type': 'str'}, {'column': 'Age', 'type': 'int'}, {'column': 'Start', 'type': 'int', 'encoding': 'timestamp'}, {'column': 'Image', 'type': 'int', 'encoding': 'image'}, ], ) self.datasource.MAX_RECS = 2 def tearDown(self): """Remove test data file.""" os.unlink(self.db_file) def test_load(self): """Load first page of records and get total.""" rows = self.datasource.load() self.assertEqual(len(rows), 2) self.assertEqual(self.datasource.total_recs, 4) def test_load_with_params(self): """Filter and order records.""" param = { 'where': { 'age': { 'param': 30, 'operator': '>' } }, 'order_by': 'age', 'desc': True } rows = self.datasource.load(param) self.assertEqual(len(rows), 2) self.assertEqual(rows[0].data[2], 'Goldman') def test_load_with_where_param(self): """Filter results with a search param.""" param = { 'where': { 'search': { 'param': 'gold', }, }, } rows = self.datasource.load(param) self.assertEqual(len(rows), 2) self.assertEqual( {('Oscar', 'Goldman'), ('Monica', 'Goldman')}, {(row.data[1], row.data[2]) for row in rows}) def test_load_paging(self): """Load first and second pages of records.""" self.datasource.load() # initial load is always without paging rows = self.datasource.load({'page': 1}) self.assertEqual(len(rows), 2) self.assertEqual(rows[0].data[2], 'Goldman') def test_update(self): """Update __selected in first record in data set.""" self.datasource.update({'__selected': True}, [1]) # ^^ update row with id 1 rows = self.datasource.load() self.assertEqual(rows[0].data[0], 1) def test_get_all_record_ids(self): """Get all record ids for a particular query.""" param = { 'where': { 'age': { 'param': 30, 'operator': '>' } } } ids = self.datasource.get_all_record_ids(param) self.assertEqual(ids, [2, 3, 4]) def test_visible_columns(self): """Set visible columns and ensure they're persisted.""" self.datasource.set_visible_columns(['last_name']) self.assertEqual(self.datasource.get_visible_columns(), ['last_name']) def test_get_single_record(self): """Retrieve a single record as a tuple of values.""" row = self.datasource.get_single_record(1) self.assertEqual(row[0], 1) self.assertEqual(row[1], 'Dee') self.assertEqual(row[2], 'Timberlake') def test_select(self): """Get data without class instance or paging.""" db_file = self.datasource.db_file with contextlib.closing(sqlite3.connect(db_file)) as conn: results = list(self.datasource.select(conn, self.datasource.table)) self.assertEqual(len(results), 4) def test_explicit_query(self): """Test using an explicit query for the data source.""" # Important to not ensure "selected" column if there is no primary # key returned by the query. datasource = SQLiteDataSource( self.db_file, query='SELECT first_name, age FROM people', ensure_selected_column=False ) rows = datasource.load() data = {tuple(row.data) for row in rows} reference_data = {(record[1], record[3]) for record in TEST_DATA[self.table]['data']} self.assertEqual(data, reference_data)