def testCreateTableIfNotExists_alreadyExists(self):
     df = pandas_sqlite.DataFrame([('bug_id', int), ('summary', str),
                                   ('status', str)],
                                  index='bug_id')
     con = sqlite3.connect(':memory:')
     try:
         self.assertFalse(pandas.io.sql.has_table('bugs', con))
         pandas_sqlite.CreateTableIfNotExists(con, 'bugs', df)
         self.assertTrue(pandas.io.sql.has_table('bugs', con))
         # It's fine to call a second time.
         pandas_sqlite.CreateTableIfNotExists(con, 'bugs', df)
         self.assertTrue(pandas.io.sql.has_table('bugs', con))
     finally:
         con.close()
    def testInsertOrReplaceRecords_existingRecords(self):
        column_types = (('bug_id', int), ('summary', str), ('status', str))
        rows1 = [(123, 'Some bug', 'Started'),
                 (456, 'Another bug', 'Assigned')]
        df1 = pandas_sqlite.DataFrame(column_types, index='bug_id', rows=rows1)
        rows2 = [(123, 'Some bug', 'Fixed'), (789, 'A new bug', 'Untriaged')]
        df2 = pandas_sqlite.DataFrame(column_types, index='bug_id', rows=rows2)
        con = sqlite3.connect(':memory:')
        try:
            pandas_sqlite.CreateTableIfNotExists(con, 'bugs', df1)

            # Write first data frame to database.
            pandas_sqlite.InsertOrReplaceRecords(con, 'bugs', df1)
            df = pandas.read_sql('SELECT * FROM bugs', con, index_col='bug_id')
            self.assertEqual(len(df), 2)
            self.assertEqual(df.loc[123]['status'], 'Started')

            # Write second data frame to database.
            pandas_sqlite.InsertOrReplaceRecords(con, 'bugs', df2)
            df = pandas.read_sql('SELECT * FROM bugs', con, index_col='bug_id')
            self.assertEqual(len(df), 3)  # Only one extra record added.
            self.assertEqual(df.loc[123]['status'],
                             'Fixed')  # Bug is now fixed.
            self.assertItemsEqual(df.index, (123, 456, 789))
        finally:
            con.close()
Esempio n. 3
0
 def testCreateTableIfNotExists_newTable(self):
     column_types = (('bug_id', int), ('summary', str), ('status', str))
     index = 'bug_id'
     con = sqlite3.connect(':memory:')
     try:
         self.assertFalse(pandas.io.sql.has_table('bugs', con))
         pandas_sqlite.CreateTableIfNotExists(con, 'bugs', column_types,
                                              index)
         self.assertTrue(pandas.io.sql.has_table('bugs', con))
     finally:
         con.close()
Esempio n. 4
0
 def testWorkerPoolRun(self):
     tempdir = tempfile.mkdtemp()
     try:
         args = argparse.Namespace()
         args.database_file = os.path.join(tempdir, 'test.db')
         args.processes = 3
         items = range(20)  # We'll write these in the database.
         con = sqlite3.connect(args.database_file)
         pandas_sqlite.CreateTableIfNotExists(con, 'items', [('item', int)])
         with open(os.devnull, 'w') as devnull:
             worker_pool.Run('Processing:',
                             TestWorker,
                             args,
                             items,
                             stream=devnull)
         df = pandas.read_sql('SELECT * FROM items', con)
         # Check all of our items were written.
         self.assertItemsEqual(df['item'], items)
     finally:
         shutil.rmtree(tempdir)
Esempio n. 5
0
def CreateIfNeeded(con):
    """Creates soundwave tables in the database, if they don't already exist."""
    for m in (alerts, bugs, timeseries):
        pandas_sqlite.CreateTableIfNotExists(con, m.TABLE_NAME, m.COLUMN_TYPES,
                                             m.INDEX)
def _CreateTablesIfNeeded(con):
    """Creates soundwave tables in the database, if they don't already exist."""
    for m in (alerts, bugs, timeseries):
        pandas_sqlite.CreateTableIfNotExists(con, m.TABLE_NAME, m.DataFrame())
Esempio n. 7
0
def CreateIfNeeded(con):
    """Creates soundwave tables in the database, if they don't already exist."""
    # TODO(#4442): Also create alerts and bug tables.
    for m in (timeseries, ):
        pandas_sqlite.CreateTableIfNotExists(con, m.TABLE_NAME, m.COLUMN_TYPES,
                                             m.INDEX)