Michael Manfre avatar Michael Manfre committed aa3ce0b

Fix PORT setting to ensure it is actually include in the connection string. Fixes #117. Thanks Robert Ziebarth for reporting.

Comments (0)

Files changed (2)

sqlserver_ado/base.py

             port = int(settings.PORT)
         except ValueError:
             raise ImproperlyConfigured("DATABASE PORT must be a number.")
-        datasource = '%s,%i;Network Library=DBMSSOCN' % (db_host, port)
+        db_host = '%s,%i;Network Library=DBMSSOCN' % (db_host, port)
 
     # If no user is specified, use integrated security.
     if settings.USER != '':

tests/test_main/regressiontests/tests.py

 import datetime
 import decimal
+from django.core.exceptions import ImproperlyConfigured
 from django.db import models
 from django.test import TestCase
 
         a = list(Bug69Table1.objects.all().order_by('?'))
         b = list(Bug69Table1.objects.all().order_by('?'))
         
-        self.assertNotEquals(a, b)
+        self.assertNotEquals(a, b)
+
+class ConnectionStringTestCase(TestCase):
+    def assertInString(self, conn_string, pattern):
+        """
+        Asserts that the pattern is found in the string.
+        """
+        found = conn_string.find(pattern) != -1
+        self.assertTrue(found,
+            "pattern \"%s\" was not found in connection string \"%s\"" % (pattern, conn_string))
+
+    def assertNotInString(self, conn_string, pattern):
+        """
+        Asserts that the pattern is found in the string.
+        """
+        found = conn_string.find(pattern) != -1
+        self.assertFalse(found,
+            "pattern \"%s\" was found in connection string \"%s\"" % (pattern, conn_string))
+
+    def get_conn_string(self, data={}):
+        db_settings = {
+           'NAME': 'db_name',
+           'ENGINE': 'sqlserver_ado',
+           'HOST': 'myhost',
+           'PORT': '',
+           'USER': '',
+           'PASSWORD': '',
+           'OPTIONS' : {
+               'provider': 'SQLOLEDB',
+               'use_mars': True,
+           },
+        }
+        db_settings.update(data)
+        from sqlserver_ado.base import make_connection_string
+        return make_connection_string(db_settings)
+
+    def test_default(self):
+        conn_string = self.get_conn_string()
+        self.assertInString(conn_string, 'Initial Catalog=db_name')
+        self.assertInString(conn_string, '=myhost;')
+        self.assertInString(conn_string, 'Integrated Security=SSPI')
+        self.assertInString(conn_string, 'PROVIDER=SQLOLEDB')
+        self.assertNotInString(conn_string, 'UID=')
+        self.assertNotInString(conn_string, 'PWD=')
+        self.assertInString(conn_string, 'MARS Connection=True')
+
+    def test_require_database_name(self):
+        """Database NAME setting is required"""
+        self.assertRaises(ImproperlyConfigured, self.get_conn_string, {'NAME': ''})
+
+    def test_user_pass(self):
+        """Validate username and password in connection string"""
+        conn_string = self.get_conn_string({'USER': 'myuser', 'PASSWORD': 'mypass'})
+        self.assertInString(conn_string, 'UID=myuser;')
+        self.assertInString(conn_string, 'PWD=mypass;')
+        self.assertNotInString(conn_string, 'Integrated Security=SSPI')
+
+    def test_port(self):
+        """Test the PORT setting to make sure it properly updates the connection string"""
+        self.assertRaises(ImproperlyConfigured, self.get_conn_string,
+            {'HOST': 'myhost', 'PORT': 1433})
+        self.assertRaises(ImproperlyConfigured, self.get_conn_string, {'HOST': 'myhost', 'PORT': 'a'})
+
+        conn_string = self.get_conn_string({'HOST': '127.0.0.1', 'PORT': 1433})
+        self.assertInString(conn_string, '=127.0.0.1,1433;')
+
+    def test_extra_params(self):
+        """Test extra_params OPTIONS"""
+        extras = 'Some=Extra;Stuff Goes=here'
+        conn_string = self.get_conn_string({'OPTIONS': {'extra_params': extras}})
+        self.assertInString(conn_string, extras)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.