def table_data_from_db(table_name, table_data_repr): if table_data_repr is None: return actions.TableData(table_name, [], {}) table_data_parsed = marshal.loads(table_data_repr) id_col = table_data_parsed.pop("id") return actions.TableData( table_name, id_col, actions.decode_bulk_values(table_data_parsed, _decode_db_value))
def table_data_from_db(table_name, table_data_repr): if table_data_repr is None: return actions.TableData(table_name, [], {}) table_data_parsed = marshal.loads(table_data_repr) table_data_parsed = { key.decode("utf8"): value for key, value in table_data_parsed.items() } id_col = table_data_parsed.pop("id") return actions.TableData( table_name, id_col, actions.decode_bulk_values(table_data_parsed, _decode_db_value))
def _prep_data(cls, table_id, row_ids, columns): def sort(col): return [v for r, v in sorted(zip(row_ids, col))] sorted_data = actions.TableData(table_id, sorted(row_ids), {c: sort(col) for c, col in columns.iteritems()}) return actions.encode_objects(testutil.replace_nans(sorted_data))
def _strip_formulas_table(self, table_id, row_ids, columns): return actions.TableData( table_id, row_ids, { col_id: col for col_id, col in six.iteritems(columns) if not self. _table_data_set.get_col_info(table_id, col_id)["isFormula"] })
def ReplaceTableData(self, table_id, row_ids, column_values): old_data = self._engine.fetch_table(table_id, formulas=False) self._engine.out_actions.undo.append( actions.ReplaceTableData(*old_data)) self._engine.out_actions.summary.remove_records(table_id, old_data[1]) self._engine.out_actions.summary.add_records(table_id, row_ids) self._engine.load_table( actions.TableData(table_id, row_ids, column_values))
def table_data_from_rows(table_id, col_names, rows): """ Returns a TableData object built from a table_id, a list of column names, and corresponding row-oriented data. """ column_values = {} for i, col in enumerate(col_names): # Strip leading @ from column headers column_values[col.lstrip('@')] = [row[i] for row in rows] return actions.TableData(table_id, column_values.pop('id'), column_values)
def assertTableData(self, table_name, data=[], cols="all", rows="all", sort=None): """ Verify some or all of the data in the table named `table_name`. - data: an array of rows, with first row containing column names starting with "id", and other rows also all starting with row_id. - cols: may be "all" (default) to match all columns, or "subset" to match only those listed. - rows: may be "all" (default) to match all rows, or "subset" to match only those listed, or a function called with a Record to return whether to include it. - sort: optionally a key function called with a Record, for sorting observed rows. """ assert data[0][ 0] == 'id', "assertRecords requires 'id' as the first column" col_names = data[0] row_data = data[1:] expected = testutil.table_data_from_rows(table_name, col_names, row_data) table = self.engine.tables[table_name] columns = [ c for c in table.all_columns.values() if c.col_id != "id" and not column.is_virtual_column(c.col_id) ] if cols == "all": pass elif cols == "subset": columns = [c for c in columns if c.col_id in col_names] else: raise ValueError("assertRecords: invalid value for cols: %s" % (cols, )) if rows == "all": row_ids = list(table.row_ids) elif rows == "subset": row_ids = [row[0] for row in row_data] elif callable(rows): row_ids = [r.id for r in table.user_table.all if rows(r)] else: raise ValueError("assertRecords: invalid value for rows: %s" % (rows, )) if sort: row_ids.sort(key=lambda r: sort(table.get_record(r))) observed_col_data = { c.col_id: [c.raw_get(r) for r in row_ids] for c in columns if c.col_id != "id" } observed = actions.TableData(table_name, row_ids, observed_col_data) self.assertEqualDocData({table_name: observed}, {table_name: expected}, col_names=col_names)
def RenameTable(self, old_table_id, new_table_id): table_data = self.all_tables.pop(old_table_id) self.all_tables[new_table_id] = actions.TableData( new_table_id, table_data.row_ids, table_data.columns) self._schema[new_table_id] = self._schema.pop(old_table_id)
def AddTable(self, table_id, columns): self.all_tables[table_id] = actions.TableData( table_id, [], {c['id']: [] for c in columns}) self._schema[table_id] = {c['id']: c.copy() for c in columns}