Best Python code snippet using autotest_python
models.py
Source:models.py
...14 field_names.append(field)15 else:16 field_names.append(self._get_key_unless_is_function(field))17 return field_names18 def _get_group_query_sql(self, query, group_by, extra_select_fields):19 group_fields = self._get_field_names(group_by, extra_select_fields)20 select_fields = [field for field in group_fields21 if field not in extra_select_fields]22 for field_name, field_sql in extra_select_fields.iteritems():23 field_sql = self._get_key_unless_is_function(field_sql)24 select_fields.append(field_sql + ' AS ' + field_name)25 # add the extra fields to the query selects, so they'll be sortable26 # and Django won't mess with any of them27 query._select[field_name] = field_sql28 _, where, params = query._get_sql_clause()29 # insert GROUP BY clause into query30 group_by_clause = ' GROUP BY ' + ', '.join(group_fields)31 group_by_position = where.rfind('ORDER BY')32 if group_by_position == -1:33 group_by_position = len(where)34 where = (where[:group_by_position] +35 group_by_clause + ' ' +36 where[group_by_position:])37 return ('SELECT ' + ', '.join(select_fields) + where), params38 def _get_column_names(self, cursor):39 """\40 Gets the column names from the cursor description. This method exists41 so that it can be mocked in the unit test for sqlite3 compatibility."42 """43 return [column_info[0] for column_info in cursor.description]44 def execute_group_query(self, query, group_by, extra_select_fields=[]):45 """46 Performs the given query grouped by the fields in group_by with the47 given extra select fields added. extra_select_fields should be a dict48 mapping field alias to field SQL. Usually, the extra fields will use49 group aggregation functions. Returns a list of dicts, where each dict50 corresponds to single row and contains a key for each grouped field as51 well as all of the extra select fields.52 """53 sql, params = self._get_group_query_sql(query, group_by,54 extra_select_fields)55 cursor = readonly_connection.connection().cursor()56 cursor.execute(sql, params)57 field_names = self._get_column_names(cursor)58 row_dicts = [dict(zip(field_names, row)) for row in cursor.fetchall()]59 return row_dicts60 def get_count_sql(self, query):61 """62 Get the SQL to properly select a per-group count of unique matches for63 a grouped query. Returns a tuple (field alias, field SQL)64 """65 if query._distinct:66 pk_field = self.get_key_on_this_table()67 count_sql = 'COUNT(DISTINCT %s)' % pk_field...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!