diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2016-03-03 16:52:53 +0000 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2016-03-03 17:09:15 +0000 |
commit | 7aab934ae5950c6fa1bcd25ed053857538624310 (patch) | |
tree | efc3678ccd25a811d58af28b0750d5d2857ae3c7 /tests | |
parent | 7155d06cdcc15064d28f8667e72d8f61b9b31e90 (diff) | |
download | psycopg2-7aab934ae5950c6fa1bcd25ed053857538624310.tar.gz |
Validate output result from make_dsn()
The output is not necessarily munged anyway: if no keyword is passed,
validate the input but return it untouched.
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test_connection.py | 51 | ||||
-rwxr-xr-x | tests/test_module.py | 34 |
2 files changed, 43 insertions, 42 deletions
diff --git a/tests/test_connection.py b/tests/test_connection.py index f2ea3d6..0158f5c 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -388,53 +388,64 @@ class MakeDsnTestCase(ConnectingTestCase): dsn = ext.make_dsn('') self.assertEqual(dsn, '') + def test_params_validation(self): + self.assertRaises(psycopg2.ProgrammingError, + ext.make_dsn, 'dbnamo=a') + self.assertRaises(psycopg2.ProgrammingError, + ext.make_dsn, dbnamo='a') + self.assertRaises(psycopg2.ProgrammingError, + ext.make_dsn, 'dbname=a', nosuchparam='b') + def test_empty_param(self): - dsn = ext.make_dsn(database='sony', password='') + dsn = ext.make_dsn(dbname='sony', password='') self.assertDsnEqual(dsn, "dbname=sony password=''") def test_escape(self): - dsn = ext.make_dsn(database='hello world') + dsn = ext.make_dsn(dbname='hello world') self.assertEqual(dsn, "dbname='hello world'") - dsn = ext.make_dsn(database=r'back\slash') + dsn = ext.make_dsn(dbname=r'back\slash') self.assertEqual(dsn, r"dbname=back\\slash") - dsn = ext.make_dsn(database="quo'te") + dsn = ext.make_dsn(dbname="quo'te") self.assertEqual(dsn, r"dbname=quo\'te") - dsn = ext.make_dsn(database="with\ttab") + dsn = ext.make_dsn(dbname="with\ttab") self.assertEqual(dsn, "dbname='with\ttab'") - dsn = ext.make_dsn(database=r"\every thing'") + dsn = ext.make_dsn(dbname=r"\every thing'") self.assertEqual(dsn, r"dbname='\\every thing\''") + def test_database_is_a_keyword(self): + self.assertEqual(ext.make_dsn(database='sigh'), "dbname=sigh") + def test_params_merging(self): - dsn = ext.make_dsn('dbname=foo', database='bar') - self.assertEqual(dsn, 'dbname=bar') + dsn = ext.make_dsn('dbname=foo host=bar', host='baz') + self.assertDsnEqual(dsn, 'dbname=foo host=baz') dsn = ext.make_dsn('dbname=foo', user='postgres') self.assertDsnEqual(dsn, 'dbname=foo user=postgres') def test_no_dsn_munging(self): - dsn = ext.make_dsn('nosuchparam=whatevs') - self.assertEqual(dsn, 'nosuchparam=whatevs') - - dsn = ext.make_dsn(nosuchparam='whatevs') - self.assertEqual(dsn, 'nosuchparam=whatevs') - - self.assertRaises(psycopg2.ProgrammingError, - ext.make_dsn, 'nosuchparam=whatevs', andthis='either') + dsnin = 'dbname=a host=b user=c password=d' + dsn = ext.make_dsn(dsnin) + self.assertEqual(dsn, dsnin) @skip_before_libpq(9, 2) def test_url_is_cool(self): - dsn = ext.make_dsn('postgresql://tester:secret@/test') - self.assertEqual(dsn, 'postgresql://tester:secret@/test') + url = 'postgresql://tester:secret@/test?application_name=wat' + dsn = ext.make_dsn(url) + self.assertEqual(dsn, url) - dsn = ext.make_dsn('postgresql://tester:secret@/test', - application_name='woot') + dsn = ext.make_dsn(url, application_name='woot') self.assertDsnEqual(dsn, 'dbname=test user=tester password=secret application_name=woot') + self.assertRaises(psycopg2.ProgrammingError, + ext.make_dsn, 'postgresql://tester:secret@/test?nosuch=param') + self.assertRaises(psycopg2.ProgrammingError, + ext.make_dsn, url, nosuch="param") + class IsolationLevelsTestCase(ConnectingTestCase): diff --git a/tests/test_module.py b/tests/test_module.py index 9f0adcc..a6918cb 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -62,8 +62,8 @@ class ConnectTestCase(unittest.TestCase): self.assertEqual(self.args[2], False) def test_dsn(self): - psycopg2.connect('dbname=blah x=y') - self.assertEqual(self.args[0], 'dbname=blah x=y') + psycopg2.connect('dbname=blah host=y') + self.assertEqual(self.args[0], 'dbname=blah host=y') self.assertEqual(self.args[1], None) self.assertEqual(self.args[2], False) @@ -88,31 +88,31 @@ class ConnectTestCase(unittest.TestCase): self.assertEqual(len(self.args[0].split()), 4) def test_generic_keywords(self): - psycopg2.connect(foo='bar') - self.assertEqual(self.args[0], 'foo=bar') + psycopg2.connect(options='stuff') + self.assertEqual(self.args[0], 'options=stuff') def test_factory(self): def f(dsn, async=False): pass - psycopg2.connect(database='foo', bar='baz', connection_factory=f) - self.assertDsnEqual(self.args[0], 'dbname=foo bar=baz') + psycopg2.connect(database='foo', host='baz', connection_factory=f) + self.assertDsnEqual(self.args[0], 'dbname=foo host=baz') self.assertEqual(self.args[1], f) self.assertEqual(self.args[2], False) - psycopg2.connect("dbname=foo bar=baz", connection_factory=f) - self.assertDsnEqual(self.args[0], 'dbname=foo bar=baz') + psycopg2.connect("dbname=foo host=baz", connection_factory=f) + self.assertDsnEqual(self.args[0], 'dbname=foo host=baz') self.assertEqual(self.args[1], f) self.assertEqual(self.args[2], False) def test_async(self): - psycopg2.connect(database='foo', bar='baz', async=1) - self.assertDsnEqual(self.args[0], 'dbname=foo bar=baz') + psycopg2.connect(database='foo', host='baz', async=1) + self.assertDsnEqual(self.args[0], 'dbname=foo host=baz') self.assertEqual(self.args[1], None) self.assert_(self.args[2]) - psycopg2.connect("dbname=foo bar=baz", async=True) - self.assertDsnEqual(self.args[0], 'dbname=foo bar=baz') + psycopg2.connect("dbname=foo host=baz", async=True) + self.assertDsnEqual(self.args[0], 'dbname=foo host=baz') self.assertEqual(self.args[1], None) self.assert_(self.args[2]) @@ -143,16 +143,6 @@ class ConnectTestCase(unittest.TestCase): psycopg2.connect('dbname=foo', user='postgres') self.assertDsnEqual(self.args[0], 'dbname=foo user=postgres') - def test_no_dsn_munging(self): - psycopg2.connect('nosuchparam=whatevs') - self.assertEqual(self.args[0], 'nosuchparam=whatevs') - - psycopg2.connect(nosuchparam='whatevs') - self.assertEqual(self.args[0], 'nosuchparam=whatevs') - - self.assertRaises(psycopg2.ProgrammingError, - psycopg2.connect, 'nosuchparam=whatevs', andthis='either') - class ExceptionsTestCase(ConnectingTestCase): def test_attributes(self): |