def test_creates_table(self): connection = sqlite3.connect(':memory:') results = {'columns': [{'name': 'test1'}, {'name': 'test2'}], 'rows': []} table_name = 'query_123' create_table(connection, table_name, results) connection.execute('SELECT 1 FROM query_123')
def create_tables_from_child_queries(user, connection, query, child_queries): for i, child_query in enumerate(child_queries): _child_query = _load_query(user, child_query['query_id']) params = child_query.get('params', {}) if not params: params = get_default_params(_child_query) _rendered_child_query = pystache.render(_child_query.query_text, params) logger.debug('ResultsWithParams child query[{}]: {}'.format( i, _rendered_child_query)) results, error = _child_query.data_source.query_runner.run_query( _rendered_child_query, user) if error: raise ChildQueryExecutionError( 'Failed loading results for query id {}.'.format( i, _child_query.id)) results = json.loads(results) table_name = child_query['table'] create_table(connection, table_name, results) query = query.replace(child_query['token'], table_name, 1) return query
def test_creates_table_with_spaces_in_column_name(self): connection = sqlite3.connect(':memory:') results = {'columns': [{'name': 'two words'}, {'name': 'test2'}], 'rows': [ {'two words': 1, 'test2': 2}, {'test1': 3}]} table_name = 'query_123' create_table(connection, table_name, results) connection.execute('SELECT 1 FROM query_123')
def test_creates_table_with_non_ascii_in_column_name(self): connection = sqlite3.connect(':memory:') results = {'columns': [{'name': u'\xe4'}, {'name': 'test2'}], 'rows': [ {u'\xe4': 1, 'test2': 2}]} table_name = 'query_123' create_table(connection, table_name, results) connection.execute('SELECT 1 FROM query_123')
def test_loads_list_and_dict_results(self): connection = sqlite3.connect(":memory:") rows = [{"test1": [1, 2, 3]}, {"test2": {"a": "b"}}] results = {"columns": [{"name": "test1"}, {"name": "test2"}], "rows": rows} table_name = "query_123" create_table(connection, table_name, results) self.assertEqual(len(list(connection.execute("SELECT * FROM query_123"))), 2)
def test_creates_table_with_colons_in_column_name(self): connection = sqlite3.connect(':memory:') results = {'columns': [{'name': 'ga:newUsers'}, { 'name': 'test2'}], 'rows': [{'ga:newUsers': 123, 'test2': 2}]} table_name = 'query_123' create_table(connection, table_name, results) connection.execute('SELECT 1 FROM query_123')
def test_creates_table_with_non_ascii_in_column_name(self): connection = sqlite3.connect(":memory:") results = { "columns": [{"name": "\xe4"}, {"name": "test2"}], "rows": [{"\xe4": 1, "test2": 2}], } table_name = "query_123" create_table(connection, table_name, results) connection.execute("SELECT 1 FROM query_123")
def test_creates_table_with_missing_columns(self): connection = sqlite3.connect(":memory:") results = { "columns": [{"name": "test1"}, {"name": "test2"}], "rows": [{"test1": 1, "test2": 2}, {"test1": 3}], } table_name = "query_123" create_table(connection, table_name, results) connection.execute("SELECT 1 FROM query_123")
def test_creates_table_with_double_quotes_in_column_name(self): connection = sqlite3.connect(":memory:") results = { "columns": [{"name": "ga:newUsers"}, {"name": '"test2"'}], "rows": [{"ga:newUsers": 123, '"test2"': 2}], } table_name = "query_123" create_table(connection, table_name, results) connection.execute("SELECT 1 FROM query_123")
def test_loads_results(self): connection = sqlite3.connect(':memory:') rows = [{'test1': 1, 'test2': 'test'}, {'test1': 2, 'test2': 'test2'}] results = {'columns': [{'name': 'test1'}, {'name': 'test2'}], 'rows': rows} table_name = 'query_123' create_table(connection, table_name, results) self.assertEquals( len(list(connection.execute('SELECT * FROM query_123'))), 2)
def test_creates_table_with_dashes_in_column_name(self): connection = sqlite3.connect(":memory:") results = { "columns": [{"name": "two-words"}, {"name": "test2"}], "rows": [{"two-words": 1, "test2": 2}], } table_name = "query_123" create_table(connection, table_name, results) connection.execute("SELECT 1 FROM query_123") connection.execute('SELECT "two-words" FROM query_123')
def test_shows_meaningful_error_on_failure_to_create_table(self): connection = sqlite3.connect(':memory:') results = {'columns': [], 'rows': []} table_name = 'query_123' with pytest.raises(CreateTableError): create_table(connection, table_name, results)
def test_shows_meaningful_error_on_failure_to_create_table(self): connection = sqlite3.connect(":memory:") results = {"columns": [], "rows": []} table_name = "query_123" with pytest.raises(CreateTableError): create_table(connection, table_name, results)