def testInvalidArrayInput(self): data = OrderedDict([('id', '1'), ('name', 'Cooper Dogerson'), ('arr', '[invalid]')]) table = util.Table.FromDdl(_DATABASE_DDL, 'queryTest') error_message = re.escape( 'Column name [arr] has an invalid array input: [invalid]. ' '`--flags-file` should be used to specify array values.') with self.assertRaisesRegex(util.InvalidArrayInputError, error_message): util.ValidateArrayInput(table, data)
def CreateInsertMutationFromCSVRow(table, data, columns): """Create an INSERT mutation from a CSV row of data. Args: table: A write_util.Table object data: A list containing data from a single row from a CSV file. Each element corresponds to a string. columns: An ordered dictionary containing column names {col -> data_type} Returns: A single INSERT mutation """ col_to_data = collections.OrderedDict() for col, data_cell in zip(columns, data): col_to_data[col] = encoding.Decode(data_cell) valid_data = write_util.ValidateArrayInput(table, col_to_data) mutation = database_sessions.MutationFactory.Insert(table, valid_data) return mutation
def Run(self, args): """This is what gets called when the user runs this command.""" database_ref = args.CONCEPTS.database.Parse() # DDL(Data Definition Language) is needed to get the schema of the current # database and table so that we know the type of each column (e.g. INT64) # user wants to delete. ddl = databases.GetDdl(database_ref) table = write_util.Table.FromDdl(ddl, args.table) data = write_util.ValidateArrayInput(table, args.data) mutation = database_sessions.MutationFactory.Update(table, data) # To commit a transaction in a session, we need to create one and delete it # at the end. session_name = database_sessions.Create(database_ref) session = resources.REGISTRY.ParseRelativeName( relative_name=session_name.name, collection='spanner.projects.instances.databases.sessions') try: return database_sessions.Commit(session, [mutation]) finally: database_sessions.Delete(session)
def testValidArrayInput(self): data = OrderedDict([('id', '1'), ('name', 'Cooper Dogerson'), ('arr', ['hello, I am valid', 'also valid'])]) table = util.Table.FromDdl(_DATABASE_DDL, 'queryTest') self.assertEqual(data, util.ValidateArrayInput(table, data))