def test_startService_withDumpFile(self): """ Assuming a properly configured environment ($PATH points at an 'initdb' and 'postgres', $PYTHONPATH includes pgdb), starting a L{PostgresService} will start the service passed to it, after importing an existing dump file. """ test = self class SimpleService1(Service): instances = [] ready = Deferred() def __init__(self, connectionFactory, storageService): self.connection = connectionFactory() test.addCleanup(self.connection.close) self.instances.append(self) def startService(self): cursor = self.connection.cursor() try: cursor.execute( "insert into import_test_table values ('value2')" ) except: self.ready.errback() else: self.ready.callback(None) finally: cursor.close() # The SQL in importFile.sql will get executed, including the insertion of "value1" importFileName = CachingFilePath(__file__).parent().child("importFile.sql").path svc = PostgresService( CachingFilePath("postgres_3.pgdb"), SimpleService1, "", databaseName="dummy_db", testMode=True, importFileName=importFileName ) svc.startService() self.addCleanup(svc.stopService) yield SimpleService1.ready connection = SimpleService1.instances[0].connection cursor = connection.cursor() cursor.execute("select * from import_test_table") values = cursor.fetchall() self.assertEquals(values, [["value1"], ["value2"]])
def test_startService_Socket(self): """ Assuming a properly configured environment ($PATH points at an 'initdb' and 'postgres', $PYTHONPATH includes pgdb), starting a L{PostgresService} will start the service passed to it, after executing the schema. """ test = self class SimpleService2(Service): instances = [] ready = Deferred() def __init__(self, connectionFactory, storageService): self.connection = connectionFactory() test.addCleanup(self.connection.close) self.instances.append(self) def startService(self): cursor = self.connection.cursor() try: cursor.execute( "insert into test_dummy_table values ('dummy')" ) except: self.ready.errback() else: self.ready.callback(None) finally: cursor.close() svc = PostgresService( CachingFilePath("postgres_2.pgdb"), SimpleService2, "create table TEST_DUMMY_TABLE (stub varchar)", databaseName="dummy_db", listenAddresses=['127.0.0.1', ], testMode=True ) svc.startService() self.addCleanup(svc.stopService) yield SimpleService2.ready connection = SimpleService2.instances[0].connection cursor = connection.cursor() cursor.execute("select * from test_dummy_table") values = cursor.fetchall() self.assertEquals(map(list, values), [["dummy"]])
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()