Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated get_ogr_db_string in inspectapp tests to support MySQL/Spatia…

…lite

The OGRInspectTest.test_time_field does still not succeed with these
databases (even when removing the postgis guard), but at least it's now
possible to setup a datasource.
  • Loading branch information...
commit 53df89c0feca59517d407a6c35fdb16cb6349310 1 parent 1628dfd
Claude Paroz authored April 15, 2013
27  django/contrib/gis/tests/inspectapp/tests.py
@@ -93,21 +93,24 @@ def test_time_field(self):
93 93
 
94 94
 
95 95
 def get_ogr_db_string():
96  
-    # Construct the DB string that GDAL will use to inspect the database.
97  
-    # GDAL will create its own connection to the database, so we re-use the
98  
-    # connection settings from the Django test.  This approach is a bit fragile
99  
-    # and cannot work on any other database other than PostgreSQL at the moment.
  96
+    """
  97
+    Construct the DB string that GDAL will use to inspect the database.
  98
+    GDAL will create its own connection to the database, so we re-use the
  99
+    connection settings from the Django test.
  100
+    """
100 101
     db = connections.databases['default']
101 102
 
102 103
     # Map from the django backend into the OGR driver name and database identifier
103 104
     # http://www.gdal.org/ogr/ogr_formats.html
104 105
     #
105  
-    # TODO: Support Oracle (OCI), MySQL, and SpatiaLite.
  106
+    # TODO: Support Oracle (OCI).
106 107
     drivers = {
107  
-        'django.contrib.gis.db.backends.postgis': ('PostgreSQL', 'PG'),
  108
+        'django.contrib.gis.db.backends.postgis': ('PostgreSQL', "PG:dbname='%(db_name)s'", ' '),
  109
+        'django.contrib.gis.db.backends.mysql': ('MySQL', 'MYSQL:"%(db_name)s"', ','),
  110
+        'django.contrib.gis.db.backends.spatialite': ('SQLite', '%(db_name)s', '')
108 111
     }
109 112
 
110  
-    drv_name, db_str = drivers[db['ENGINE']]
  113
+    drv_name, db_str, param_sep = drivers[db['ENGINE']]
111 114
 
112 115
     # Ensure that GDAL library has driver support for the database.
113 116
     try:
@@ -115,10 +118,12 @@ def get_ogr_db_string():
115 118
     except:
116 119
         return None
117 120
 
  121
+    # SQLite/Spatialite in-memory databases
  122
+    if db['NAME'] == ":memory:":
  123
+        return None
  124
+
118 125
     # Build the params of the OGR database connection string
119  
-    # TODO: connection strings are database-dependent, thus if
120  
-    #       we ever test other backends, this will need to change.
121  
-    params = ["dbname='%s'" % db['NAME']]
  126
+    params = [db_str % {'db_name': db['NAME']}]
122 127
     def add(key, template):
123 128
         value = db.get(key, None)
124 129
         # Don't add the parameter if it is not in django's settings
@@ -129,4 +134,4 @@ def add(key, template):
129 134
     add('USER', "user='%s'")
130 135
     add('PASSWORD', "password='%s'")
131 136
 
132  
-    return '%s:%s' % (db_str, ' '.join(params))
  137
+    return param_sep.join(params)

0 notes on commit 53df89c

Please sign in to comment.
Something went wrong with that request. Please try again.