class SQLStoreBuilder(object): """ Test-fixture-builder which can construct a PostgresStore. """ sharedService = None currentTestID = None SHARED_DB_PATH = "../_test_sql_db" def buildStore(self, testCase, notifierFactory): """ Do the necessary work to build a store for a particular test case. @return: a L{Deferred} which fires with an L{IDataStore}. """ currentTestID = testCase.id() dbRoot = CachingFilePath(self.SHARED_DB_PATH) if self.sharedService is None: ready = Deferred() def getReady(connectionFactory): attachmentRoot = dbRoot.child("attachments") try: attachmentRoot.createDirectory() except OSError: pass try: self.store = CommonDataStore( lambda label=None: connectionFactory( label or currentTestID ), notifierFactory, attachmentRoot ) except: ready.errback() raise else: self.cleanDatabase(testCase) ready.callback(self.store) return self.store self.sharedService = PostgresService( dbRoot, getReady, v1_schema, "caldav", resetSchema=True, testMode=True ) self.sharedService.startService() def startStopping(): log.msg("Starting stopping.") self.sharedService.unpauseMonitor() return self.sharedService.stopService() reactor.addSystemEventTrigger(#@UndefinedVariable "before", "shutdown", startStopping) result = ready else: self.store.notifierFactory = notifierFactory self.cleanDatabase(testCase) result = succeed(self.store) def cleanUp(): # FIXME: clean up any leaked connections and report them with an # immediate test failure. def stopit(): self.sharedService.pauseMonitor() return deferLater(reactor, 0.1, stopit) testCase.addCleanup(cleanUp) return result def cleanDatabase(self, testCase): cleanupConn = self.store.connectionFactory( "%s schema-cleanup" % (testCase.id(),) ) cursor = cleanupConn.cursor() tables = ['INVITE', 'RESOURCE_PROPERTY', 'ATTACHMENT', 'ADDRESSBOOK_OBJECT', 'CALENDAR_OBJECT', 'CALENDAR_BIND', 'ADDRESSBOOK_BIND', 'CALENDAR', 'ADDRESSBOOK', 'CALENDAR_HOME', 'ADDRESSBOOK_HOME', 'NOTIFICATION', 'NOTIFICATION_HOME'] for table in tables: try: cursor.execute("delete from "+table) except: log.err() cleanupConn.commit() cleanupConn.close()