bh: thuban/test test_postgis_db.py, 1.13.2.1, 1.13.2.2 postgissupport.py, 1.7.2.2, 1.7.2.3

cvs@intevation.de cvs at intevation.de
Thu Dec 16 21:34:05 CET 2004


Author: bh

Update of /thubanrepository/thuban/test
In directory doto:/tmp/cvs-serv21002/test

Modified Files:
      Tag: thuban-1-0-branch
	test_postgis_db.py postgissupport.py 
Log Message:
* test/postgissupport.py (find_postgis_sql): Different postgis
versions put the postgis.sql file into slightly different places
so we have to look in both.  The updated doc string describes this
is more detail.

* test/test_postgis_db.py
(TestPostGISSpecialCases.test_column_name_quoting): The return
value of UniqueValues is unsorted, so it has to be sorted for
comparison.


Index: test_postgis_db.py
===================================================================
RCS file: /thubanrepository/thuban/test/test_postgis_db.py,v
retrieving revision 1.13.2.1
retrieving revision 1.13.2.2
diff -u -d -r1.13.2.1 -r1.13.2.2
--- test_postgis_db.py	16 Dec 2004 19:17:51 -0000	1.13.2.1
+++ test_postgis_db.py	16 Dec 2004 20:34:03 -0000	1.13.2.2
@@ -216,7 +216,12 @@
         self.assertEquals(table.ReadValue(2, 1, row_is_ordinal = True), 3.0)
         self.assertEquals(table.ReadValue(2, 1, row_is_ordinal = False), 2.0)
         self.assertEquals(table.ValueRange("with space"), (1.0, 3.0))
-        self.assertEquals(table.UniqueValues("with \" quote"), [0, 1])
+
+        # The return value of UniqueValues is unsorted, so we need to
+        # sort it for comparison.
+        unique_values = table.UniqueValues("with \" quote")
+        unique_values.sort()
+        self.assertEquals(unique_values, [0, 1])
         self.assertEquals(table.SimpleQuery(table.Columns()[2], "==", 1),
                           [2, 3])
         self.assertEquals(table.SimpleQuery(table.Columns()[0], "==",

Index: postgissupport.py
===================================================================
RCS file: /thubanrepository/thuban/test/postgissupport.py,v
retrieving revision 1.7.2.2
retrieving revision 1.7.2.3
diff -u -d -r1.7.2.2 -r1.7.2.3
--- postgissupport.py	16 Dec 2004 19:21:53 -0000	1.7.2.2
+++ postgissupport.py	16 Dec 2004 20:34:03 -0000	1.7.2.3
@@ -17,6 +17,7 @@
 import popen2
 import shutil
 import traceback
+import re
 
 import support
 
@@ -297,17 +298,39 @@
         return " ".join(params)
 
     def execute_sql(self, dbname, user, sql):
-        """Execute the sql statament
+        """Execute the sql statament and return a result for SELECT statements
 
         The user parameter us used as in connection_params. The dbname
-        parameter must be the name of a database in the cluster.
+        parameter must be the name of a database in the cluster.  The
+        sql parameter is the SQL statement to execute as a string.  If
+        the string starts with 'select' (matched case insensitively) the
+        first row of the result will be returned.  Otherwise the return
+        value is None.
         """
         conn = psycopg.connect("dbname=%s " % dbname
                                + self.connection_string(user))
         cursor = conn.cursor()
         cursor.execute(sql)
+        if sql.lower().startswith("select"):
+            row = cursor.fetchone()
+        else:
+            row = None
         conn.commit()
         conn.close()
+        return row
+
+    def server_version(self):
+        """Return the server version as a tuple (major, minor, patch)
+
+        Each item in the tuple is an int.
+        """
+        result = self.execute_sql("template1", "admin", "SELECT version();")[0]
+        match = re.match(r"PostgreSQL (\d+\.\d+\.\d+)", result)
+        if match:
+            return tuple(map(int, match.group(1).split(".")))
+        else:
+            raise RutimeError("Cannot determine PostgreSQL server version"
+                              " from %r" % result)
 
     def require_authentication(self, required):
         """Switch authentication requirements on or off
@@ -319,10 +342,17 @@
         corresponding call to switch it off again in the test case'
         tearDown method or in a finally: block.
         """
+        # Starting with PostgreSQL 7.3 the pg_hba.conf file has an
+        # additional column with a username.  Query the server version
+        # and generate a file in the correct format.
+        if self.server_version() >= (7, 3):
+            user = "all"
+        else:
+            user = ""
         if required:
-            contents = "local all password\n"
+            contents = "local all %s password\n" % user
         else:
-            contents = "local all trust\n"
+            contents = "local all %s trust\n" % user
         f = open(os.path.join(self.dbdir, "pg_hba.conf"), "w")
         f.write(contents)
         f.close()
@@ -441,15 +471,29 @@
     """Return the name of the postgis_sql file
 
     A postgis installation usually has the postgis_sql file in
-    PostgreSQL's datadir (i.e. the directory where PostgreSQL keeps
+    PostgreSQL's $datadir (i.e. the directory where PostgreSQL keeps
     static files, not the directory containing the databases).
     Unfortunately there's no way to determine the name of this directory
     with pg_config so we assume here that it's
     $bindir/../share/postgresql/.
+
+    Furthermore, different versions of postgis place the file in
+    slightly different locations.  For instance:
+
+      postgis 0.7.5        $datadir/contrib/postgis.sql
+      postgis 0.8.1        $datadir/postgis.sql
+
+    To support both versions, we look in both places and return the
+    first one found (looking under contrib first).  If the file is not
+    found the return value is None.
     """
     bindir = run_config_script("pg_config --bindir").strip()
-    return os.path.join(bindir, "..", "share", "postgresql",
-                        "contrib", "postgis.sql")
+    datadir = os.path.join(bindir, "..", "share", "postgresql")
+    for filename in [os.path.join(datadir, "contrib", "postgis.sql"),
+                     os.path.join(datadir, "postgis.sql")]:
+        if os.path.exists(filename):
+            return filename
+
 
 _postgres_server = None
 def get_test_server():





More information about the Thuban-devel mailing list

This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)