Exemplo n.º 1
0
def checkOneClass(aClass,aType):
  global me
  connection = psycopg2.connect(me.dsn)
  cursor = connection.cursor()
  table = aClass(logger = me.logger)
  expectedList = []
  expectedTableClasses = schema.getOrderedSetupList([aClass])
  for t in expectedTableClasses:
    expectedList.append(t(logger = me.logger))
  try:
    schema.teardownDatabase(me.config,me.logger)
    matchingTables = [x for x in socorro_psg.tablesMatchingPattern(table.name+'%',cursor) if not x.endswith('_id_seq')]
    assert [] == matchingTables ,'For class %s saw %s'%(table.name,matchingTables)
    # call create
    before = set(socorro_psg.tablesMatchingPattern('%',cursor))
    ignore = [x for x in before if (x.startswith('pg_toast') or x.endswith('id_seq'))]
    before -= set(ignore)
    table.create(cursor)
    connection.commit()
    after = set(socorro_psg.tablesMatchingPattern('%',cursor))
    ignore = [x for x in after if (x.startswith('pg_toast') or x.endswith('id_seq'))]
    after -= set(ignore)
    expectedDiff = hardCodedSchemaClasses[aClass][1]
    assert expectedDiff ==  after - before, 'for %s: after-before=\n   got:%s\nwanted:%s'%(table.name,after-before,expectedDiff)
    # call drop
    table.drop(cursor)
    connection.commit()
    afterDrop = set(socorro_psg.tablesMatchingPattern('%',cursor))
    assert not table.name in afterDrop
  finally:
    cursor.execute("DROP TABLE IF EXISTS %s CASCADE"%(','.join([x.name for x in expectedList])))
    connection.commit()
    connection.close()
Exemplo n.º 2
0
def testSetupAndTeardownDatabase():
  """
  testSetupAndTeardownDatabase():
   - test that when we setupDatabase, we get all the expected tables
   - test that when we teardownDatabase, we remove all the expected tables
  """
  global me
  tcon,tcur = schema.connectToDatabase(me.config,me.logger)
  tcur.execute("DROP TABLE IF EXISTS %s CASCADE"%','.join(me.expectedTableNames))
  tcon.commit()
  try:
    schema.setupDatabase(me.config,me.logger)
    try:
      for t in me.expectedTableNames:
        # next line raises if the table does not exist
        tcur.execute("SELECT count(*) from %s"%t)
        tcon.commit()
        count = tcur.fetchone()[0]
        assert 0 == count
    finally:
      schema.teardownDatabase(me.config,me.logger)
    for t in me.expectedTableNames:
      try:
        tcur.execute("SELECT count(*) from %s"%t)
        assert False, 'Expected table %s does not exist'%t
      except psycopg2.ProgrammingError:
        tcon.rollback()
      except Exception,x:
        assert False, 'Expected psycopg2.ProgrammingError, not %s: %s'%(type(x),x)
  finally:
    tcon.close()
Exemplo n.º 3
0
def checkOneClass(aClass,aType):
  global me
  connection = psycopg2.connect(me.dsn)
  cursor = connection.cursor()
  table = aClass(logger = me.logger)
  expectedList = []
  expectedTableClasses = schema.getOrderedSetupList([aClass])
  for t in expectedTableClasses:
    expectedList.append(t(logger = me.logger))
  try:
    schema.teardownDatabase(me.config,me.logger)
    matchingTables = [x for x in socorro_psg.tablesMatchingPattern(table.name+'%',cursor) if not x.endswith('_id_seq')]
    assert [] == matchingTables ,'For class %s saw %s'%(table.name,matchingTables)
    # call create
    before = set(socorro_psg.tablesMatchingPattern('%',cursor))
    print 'creating: ', table.name
    table.create(cursor)
    connection.commit()
    after = set(socorro_psg.tablesMatchingPattern('%',cursor))
    expected = me.hardCodedSchemaClasses[aClass][1] - set(['release_enum'])
    assertSameTableDiff(table.name,expected,before,after)
    # call drop
    table.drop(cursor)
    connection.commit()
    afterDrop = set(socorro_psg.tablesMatchingPattern('%',cursor))
    assert not table.name in afterDrop
  finally:
    cursor.execute("DROP TABLE IF EXISTS %s CASCADE"%(','.join([x.name for x in expectedList])))
    connection.commit()
    connection.close()
Exemplo n.º 4
0
def testSetupAndTeardownDatabase():
    """
  testSetupAndTeardownDatabase():
   - test that when we setupDatabase, we get all the expected tables
   - test that when we teardownDatabase, we remove all the expected tables
  """
    global me
    tcon, tcur = schema.connectToDatabase(me.config, me.logger)
    tcur.execute("DROP TABLE IF EXISTS %s CASCADE" %
                 ','.join(me.expectedTableNames))
    tcon.commit()
    try:
        schema.setupDatabase(me.config, me.logger)
        try:
            for t in me.expectedTableNames:
                if '_enum' in t:
                    continue
                # next line raises if the table does not exist
                tcur.execute("SELECT count(*) from %s" % t)
                tcon.commit()
                count = tcur.fetchone()[0]
                assert 0 == count
        finally:
            schema.teardownDatabase(me.config, me.logger)
        for t in me.expectedTableNames:
            try:
                tcur.execute("SELECT count(*) from %s" % t)
                assert False, 'Expected table %s does not exist' % t
            except psycopg2.ProgrammingError:
                tcon.rollback()
            except Exception, x:
                assert False, 'Expected psycopg2.ProgrammingError, not %s: %s' % (
                    type(x), x)
    finally:
        tcon.close()
Exemplo n.º 5
0
def checkOneClass(aClass, aType):
    global me
    connection = psycopg2.connect(me.dsn)
    cursor = connection.cursor()
    table = aClass(logger=me.logger)
    expectedList = []
    expectedTableClasses = schema.getOrderedSetupList([aClass])
    for t in expectedTableClasses:
        expectedList.append(t(logger=me.logger))
    try:
        schema.teardownDatabase(me.config, me.logger)
        matchingTables = [
            x
            for x in socorro_psg.tablesMatchingPattern(table.name +
                                                       '%', cursor)
            if not x.endswith('_id_seq')
        ]
        assert [] == matchingTables, 'For class %s saw %s' % (table.name,
                                                              matchingTables)
        # call create
        before = set(socorro_psg.tablesMatchingPattern('%', cursor))
        print 'creating: ', table.name
        table.create(cursor)
        connection.commit()
        after = set(socorro_psg.tablesMatchingPattern('%', cursor))
        expected = me.hardCodedSchemaClasses[aClass][1] - set(['release_enum'])
        assertSameTableDiff(table.name, expected, before, after)
        # call drop
        table.drop(cursor)
        connection.commit()
        afterDrop = set(socorro_psg.tablesMatchingPattern('%', cursor))
        assert not table.name in afterDrop
    finally:
        cursor.execute("DROP TABLE IF EXISTS %s CASCADE" %
                       (','.join([x.name for x in expectedList])))
        connection.commit()
        connection.close()
Exemplo n.º 6
0
 def removeDB(self, config, logger):
     """Convenience: Forward to schema to get actual work done each thing in exactly one place"""
     db_schema.teardownDatabase(config, logger)
     self.removePriorityTables(config, logger)
Exemplo n.º 7
0
 def removeDB(self, config, logger):
   """Convenience: Forward to schema to get actual work done each thing in exactly one place"""
   db_schema.teardownDatabase(config, logger)
   self.removePriorityTables(config,logger)