[glom] LDTP: Delete created centrally hosted database at the end of the test



commit 4c3eea4877d0712ef981fc8e6591e52f5067e827
Author: Armin Burgmeier <armin arbur net>
Date:   Wed Jun 24 18:45:54 2009 +0200

    LDTP: Delete created centrally hosted database at the end of the test
    
    	* ldtp/common.py: Work around bug #586291, added a function to enter
    	the connection credentials into the connection dialog if using a
    	centrally hosted database. Added mapping of backend name to button
    	texts in the database creation dialog.
    
    	* ldtp/database-creation/postgres-central.xml:
    	* ldtp/database-creation/postgres-self.xml:
    	* ldtp/database-creation/sqlite.xml: Removed the button texts from
    	the data XML files.
    
    	* ldtp/database-creation/create-db.py: Generalized the script so that
    	it also handles central hosting. Delete the newly created centrally
    	hosted database at the end. This requires pygda from git for now.
    
    	* ldtp/database-creation/create-central-db.py: Removed, as central
    	hosting is also handled by create-db.py now.

 ChangeLog                                   |   19 +++++
 ldtp/common.py                              |   89 +++++++++++++++++--------
 ldtp/database-creation/create-central-db.py |   95 ---------------------------
 ldtp/database-creation/create-db.py         |   58 +++++++++++++----
 ldtp/database-creation/postgres-central.xml |    7 +--
 ldtp/database-creation/postgres-self.xml    |    4 +-
 ldtp/database-creation/sqlite.xml           |    2 -
 ldtp/test.xml                               |    6 +-
 8 files changed, 131 insertions(+), 149 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 85b5b57..7fd17fd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,24 @@
 2009-06-24  Armin Burgmeier  <armin openismus com>
 
+	* ldtp/common.py: Work around bug #586291, added a function to enter
+	the connection credentials into the connection dialog if using a
+	centrally hosted database. Added mapping of backend name to button
+	texts in the database creation dialog.
+
+	* ldtp/database-creation/postgres-central.xml:
+	* ldtp/database-creation/postgres-self.xml:
+	* ldtp/database-creation/sqlite.xml: Removed the button texts from
+	the data XML files.
+
+	* ldtp/database-creation/create-db.py: Generalized the script so that
+	it also handles central hosting. Delete the newly created centrally
+	hosted database at the end. This requires pygda from git for now.
+
+	* ldtp/database-creation/create-central-db.py: Removed, as central
+	hosting is also handled by create-db.py now.
+
+2009-06-24  Armin Burgmeier  <armin openismus com>
+
 	* glom/glom.glade: Added a title to the connection dialog window, and
 	added accessible names for the entries, to be able to use them via
 	LDTP.
diff --git a/ldtp/common.py b/ldtp/common.py
index 88d0ef4..8edb8d0 100644
--- a/ldtp/common.py
+++ b/ldtp/common.py
@@ -7,16 +7,21 @@ import ldtputils
 main_window = '*Glom*'
 initial_dialog = 'WelcometoGlom'
 
-error_message = ''
-error_happened = threading.Event()
-error_happened.clear()
-
-def error_cb():
-	global error_message
-	if not error_happened.isSet():
-		# TODO: Read the actual error message from the dialog window
-		error_message = 'Failed to create new database'
-		error_happened.set()
+central_info = []
+
+# Radio button texts in the database creation dialog
+backend_create_db_button_texts = {
+	'PostgresCentral': [
+		'CreatedatabaseonanexternalPostgreSQLdatabaseserver,tobespecifiedinthenextstep.',
+		'Createdatabaseonanexternaldatabaseserver,tobespecifiedinthenextstep.'
+	], 'PostgresSelf': [
+		'CreatePostgreSQLdatabaseinitsownfolder,tobehostedbythiscomputer.',
+		'Createdatabaseinitsownfolder,tobehostedbythiscomputer.'
+	], 'SQLite': [
+		'CreateSQLitedatabaseinitsownfolder,tobehostedbythiscomputer.',
+		'Createdatabaseinitsownfolder,tobehostedbythiscomputer,usingSQLite.'
+	]
+}
 
 def launch_glom():
 	# Start glom:
@@ -40,30 +45,53 @@ def exit_glom():
 # Reads hostname, username and password to use for access to a
 # centrally hosted database server
 def read_central_info():
+	global central_info
+	if len(central_info) > 0:
+		return central_info
+
 	info = ldtputils.LdtpDataFileParser('central-info.xml')
 	ret = [info.gettagvalue('hostname'), info.gettagvalue('username'), info.gettagvalue('password')]
+
 	if len(ret[0]) == 0 or len(ret[1]) == 0 or len(ret[2]) == 0:
 		raise ldtp.LdtpExecutionError('Connection details for centrally hosted database not provided. See the README file for how to provide the details.')
-	return [ret[0], ret[1], ret[2]]
+
+	central_info = [ret[0][0], ret[1][0], ret[2][0]]
+	return central_info
+
+def enter_connection_credentials(backend_name):
+	if backend_name == 'PostgresCentral':
+		if ldtp.waittillguiexist('Connection Details') == 0:
+			raise ldtp.LdtpExecutionError('Connection details dialog does not show up')
+
+		(hostname, username, password) = read_central_info()
+
+		# Set connection details
+		ldtp.settextvalue('Connection Details', 'txtHost', hostname)
+		ldtp.settextvalue('Connection Details', 'txtUser', username)
+		ldtp.settextvalue('Connection Details', 'txtPassword', password)
+
+		# Acknowledge the dialog
+		ldtp.click('Connection Details', 'btnConnect')
+
+		# Make sure it's gone
+		if ldtp.waittillguinotexist('Connection Details') == 0:
+			raise ldtp.LdtpExecutionError('Connection details dialog does not disappear')
 
 # Selects one of the backends in button_texts in the database creation dialog
-def select_backend(backend_name, button_texts):
+def select_backend(backend_name):
+	try:
+		button_texts = backend_create_db_button_texts[backend_name]
+	except KeyError:
+		raise ldtp.LdtpExecutionError('Backend "' + backend + '" does not exist')
+
 	for text in button_texts:
 		if ldtp.objectexist('Creating From Example File', 'rbtn' + text):
 			ldtp.click('Creating From Example File', 'rbtn' + text)
 			break
 	else:
-		raise ldtp.LdtpExecutionError('Backend ' + backend + ' not supported')
+		raise ldtp.LdtpExecutionError('Backend "' + backend + '" not supported')
 
 def wait_for_database_open():
-	# Be notified when an error dialog pops up
-	ldtp.onwindowcreate('Warning', error_cb)
-	ldtp.onwindowcreate('Error', error_cb)
-
-	# Maybe one exists already:
-	if ldtp.guiexist('Warning') or ldtp.guiexist('Error'):
-		error_cb()
-
 	# Wait for the list view to pop up in the main Glom window:
 	# Note that the Window title of the Glom Window changes when the file
 	# has loaded. If we use wildcards for the Window title (*Glom*) here,
@@ -73,16 +101,21 @@ def wait_for_database_open():
 	# TODO: Or maybe this has been fixed in LDTP in the meanwhile,
 	# see bug #583021.
 	while not ldtp.guiexist('Glom-SmallBusinessExample') or not ldtp.objectexist('Glom-SmallBusinessExample', 'ptlListOrDetailsView'):
-		if error_happened.isSet():
-			raise ldtp.LdtpExecutionError(error_message)
+		# onwindowcreate calls the callback in a new thread, which
+		# does not really help us since we don't have a mainloop the
+		# callback thread could notify, so we would need to have to
+		# poll an event anyway. Instead, we can simply poll directly
+		# the existance of an error dialog.
+		# Plus, there seems to be a bug in LDTP when running a test
+		# sequence of multiple tests using onwindowcreate:
+		# http://bugzilla.gnome.org/show_bug.cgi?id=586291.
+		if ldtp.guiexist('Warning') or ldtp.guiexist('Error'):
+			# TODO: Read error message from error dialog
+			raise ldtp.LdtpExecutionError('Failed to create new database')
+
 		# Wait a bit and then try again:
 		ldtp.wait()
 
-	# TODO: This doesn't seem to work currently, maybe has something to
-	# do with bug #586291.
-#	ldtp.removecallback('Warning')
-#	ldtp.removecallback('Error')
-
 def check_small_business_integrity():
 	if(ldtp.getrowcount(main_window, 'tblTableContent') != 9):
 		raise ldtp.LdtpExecutionError("Newly created database does not contain all 8 rows"); # Note there is one placeholder row
diff --git a/ldtp/database-creation/create-db.py b/ldtp/database-creation/create-db.py
index 6f6b9fc..d846056 100755
--- a/ldtp/database-creation/create-db.py
+++ b/ldtp/database-creation/create-db.py
@@ -8,10 +8,45 @@ import common
 import os
 import shutil
 
+import xml.parsers.expat
+import gda
+
+def delete_database(backend):
+	if backend == 'PostgresCentral':
+		# Read glom file to find out database name and port
+		xml_info = []
+
+		def start_element(name, attrs):
+			if name == 'connection':
+				xml_info.append(attrs['database'])
+				xml_info.append(attrs['port'])
+
+		file = open('TestDatabase/Test.glom', 'r')
+		content = file.read(1024)
+
+		parser = xml.parsers.expat.ParserCreate()
+		parser.StartElementHandler = start_element
+		parser.Parse(content)
+
+		if len(xml_info) < 2:
+			raise LdtpExecutionError('Glom file does not contain port or database of centrally hosted database')
+
+		(database, port) = (xml_info[0], xml_info[1])
+		(hostname, username, password) = common.read_central_info()
+
+		# Remove the database from the central PostgreSQL server
+		op = gda.gda_prepare_drop_database('PostgreSQL', database)
+		op.set_value_at('/SERVER_CNX_P/HOST', hostname)
+		op.set_value_at('/SERVER_CNX_P/PORT', port)
+		op.set_value_at('/SERVER_CNX_P/ADM_LOGIN', username)
+		op.set_value_at('/SERVER_CNX_P/ADM_PASSWORD', password)
+		gda.gda_perform_drop_database('PostgreSQL', op)
+
+       	shutil.rmtree('TestDatabase')
+
 try:
-	xml = LdtpDataFileParser(datafilename)
-	backend = xml.gettagvalue('backend')[0]
-	button_texts = xml.gettagvalue('button_text')
+	parser = LdtpDataFileParser(datafilename)
+	backend = parser.gettagvalue('backend')[0]
 
 	common.launch_glom()
 
@@ -46,15 +81,14 @@ try:
 	settextvalue('Creating From Example File', 'txtName', 'Test');
 
 	# Make sure we use the correct backend:
-	common.select_backend(backend, button_texts)
-
-	# Make sure the Glom main window still exists
-	if not guiexist(common.main_window):
-		raise LdtpExecutionError('The Glom main window does not exist anymore')
+	common.select_backend(backend)
 
 	# Acknowledge the dialog:
 	click('Creating From Example File', 'btnSave')
 
+	# Enter the connection credentials for the centrally hosted database
+	common.enter_connection_credentials(backend)
+
 	# Wait until the database has been created:
 	common.wait_for_database_open()
 
@@ -69,15 +103,15 @@ try:
 	wait(2)
 
 	# Remove the test database again
-       	shutil.rmtree('TestDatabase')
+	delete_database(backend)
 
 except LdtpExecutionError, msg:
 	log(msg, 'fail')
 
 	# Remove the created directory also on error, so that the test
 	# does not fail because of the database already existing next time
-	shutil.rmtree('TestDatabase')
+	delete_database(backend)
 
-        raise LdtpExecutionError (msg)
-        # TODO: Terminate the Glom application
+	raise LdtpExecutionError (msg)
+	# TODO: Terminate the Glom application
 	# os.kill(whatpid?, signal.SIGTERM)
diff --git a/ldtp/database-creation/postgres-central.xml b/ldtp/database-creation/postgres-central.xml
index d2ecf42..97f2938 100644
--- a/ldtp/database-creation/postgres-central.xml
+++ b/ldtp/database-creation/postgres-central.xml
@@ -1,8 +1,3 @@
 <data>
-	<backend>Postgres Central-Hosted</backend>
-	<button_text>CreatedatabaseonanexternalPostgreSQLdatabaseserver,tobespecifiedinthenextstep.</button_text>
-	<button_text>Createdatabaseonanexternaldatabaseserver,tobespecifiedinthenextstep.</button_text>
-	<hostname>localhost</hostname>
-	<username>ck</username>
-	<password>078175511</password>
+	<backend>PostgresCentral</backend>
 </data>
diff --git a/ldtp/database-creation/postgres-self.xml b/ldtp/database-creation/postgres-self.xml
index 6598191..6d7c7e7 100644
--- a/ldtp/database-creation/postgres-self.xml
+++ b/ldtp/database-creation/postgres-self.xml
@@ -1,5 +1,3 @@
 <data>
-	<backend>Postgres Self-Hosted</backend>
-	<button_text>CreatePostgreSQLdatabaseinitsownfolder,tobehostedbythiscomputer.</button_text>
-	<button_text>Createdatabaseinitsownfolder,tobehostedbythiscomputer.</button_text>
+	<backend>PostgresSelf</backend>
 </data>
diff --git a/ldtp/database-creation/sqlite.xml b/ldtp/database-creation/sqlite.xml
index 83a0b4c..b241149 100644
--- a/ldtp/database-creation/sqlite.xml
+++ b/ldtp/database-creation/sqlite.xml
@@ -1,5 +1,3 @@
 <data>
 	<backend>SQLite</backend>
-	<button_text>CreateSQLitedatabaseinitsownfolder,tobehostedbythiscomputer.</button_text>
-	<button_text>Createdatabaseinitsownfolder,tobehostedbythiscomputer,usingSQLite.</button_text>
 </data>
diff --git a/ldtp/test.xml b/ldtp/test.xml
index 40b4eec..201ff8f 100644
--- a/ldtp/test.xml
+++ b/ldtp/test.xml
@@ -6,12 +6,12 @@
 		<testcaseid>database-creation</testcaseid>
 		<comment>Test whether creation of a new database work for all available backends</comment>
 		<script>
-			<name>database-creation/create-central-db.py</name>
-			<data>database-creation/postgres-central.xml</data>
+			<name>database-creation/create-db.py</name>
+			<data>database-creation/postgres-self.xml</data>
 		</script>
 		<script>
 			<name>database-creation/create-db.py</name>
-			<data>database-creation/postgres-self.xml</data>
+			<data>database-creation/postgres-central.xml</data>
 		</script>
 		<script>
 			<name>database-creation/create-db.py</name>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]