|
| 1 | +from tortoise.backends.asyncpg.client import AsyncpgDBClient |
| 2 | +from tortoise.backends.base.db_url import expand_db_url |
| 3 | +from tortoise.backends.sqlite.client import SqliteClient |
| 4 | +from tortoise.contrib import test |
| 5 | +from tortoise.exceptions import ConfigurationError |
| 6 | + |
| 7 | + |
| 8 | +class TestDBUrl(test.SimpleTestCase): |
| 9 | + |
| 10 | + def test_unknown_scheme(self): |
| 11 | + with self.assertRaises(ConfigurationError): |
| 12 | + expand_db_url('moo://baa') |
| 13 | + |
| 14 | + def test_sqlite_basic(self): |
| 15 | + res = expand_db_url('sqlite:///tmp/test.sqlite') |
| 16 | + self.assertEqual(res, { |
| 17 | + 'client': SqliteClient, |
| 18 | + 'params': { |
| 19 | + 'filename': '/tmp/test.sqlite', # nosec |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + def test_sqlite_testing(self): |
| 24 | + res = expand_db_url('sqlite:///tmp/test-{}.sqlite', testing=True) |
| 25 | + self.assertIn('/tmp/test-', res['params']['filename']) # nosec |
| 26 | + self.assertIn('.sqlite', res['params']['filename']) |
| 27 | + self.assertNotEqual('sqlite:///tmp/test-{}.sqlite', res['params']['filename']) |
| 28 | + self.assertEqual(res, { |
| 29 | + 'client': SqliteClient, |
| 30 | + 'params': { |
| 31 | + 'filename': res['params']['filename'], |
| 32 | + 'single_connection': True, |
| 33 | + } |
| 34 | + }) |
| 35 | + |
| 36 | + def test_sqlite_params(self): |
| 37 | + res = expand_db_url('sqlite:///tmp/test.sqlite?AHA=5&moo=yes') |
| 38 | + self.assertEqual(res, { |
| 39 | + 'client': SqliteClient, |
| 40 | + 'params': { |
| 41 | + 'filename': '/tmp/test.sqlite', # nosec |
| 42 | + 'AHA': '5', |
| 43 | + 'moo': 'yes', |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + def test_sqlite_invalid(self): |
| 48 | + with self.assertRaises(ConfigurationError): |
| 49 | + expand_db_url('sqlite://') |
| 50 | + |
| 51 | + def test_postgres_basic(self): |
| 52 | + res = expand_db_url('postgres://postgres:@127.0.0.1:5432/test') |
| 53 | + self.assertEqual(res, { |
| 54 | + 'client': AsyncpgDBClient, |
| 55 | + 'params': { |
| 56 | + 'database': 'test', |
| 57 | + 'host': '127.0.0.1', |
| 58 | + 'password': '', |
| 59 | + 'port': '5432', |
| 60 | + 'user': 'postgres', |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + def test_postgres_nonint_port(self): |
| 65 | + with self.assertRaises(ConfigurationError): |
| 66 | + expand_db_url('postgres://postgres:@127.0.0.1:moo/test') |
| 67 | + |
| 68 | + def test_postgres_testing(self): |
| 69 | + res = expand_db_url('postgres://postgres:@127.0.0.1:5432/test_\{\}', testing=True) |
| 70 | + self.assertIn('test_', res['params']['database']) |
| 71 | + self.assertNotEqual('test_{}', res['params']['database']) |
| 72 | + self.assertEqual(res, { |
| 73 | + 'client': AsyncpgDBClient, |
| 74 | + 'params': { |
| 75 | + 'database': res['params']['database'], |
| 76 | + 'host': '127.0.0.1', |
| 77 | + 'password': '', |
| 78 | + 'port': '5432', |
| 79 | + 'user': 'postgres', |
| 80 | + 'single_connection': True, |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + def test_postgres_params(self): |
| 85 | + res = expand_db_url('postgres://postgres:@127.0.0.1:5432/test?AHA=5&moo=yes') |
| 86 | + self.assertEqual(res, { |
| 87 | + 'client': AsyncpgDBClient, |
| 88 | + 'params': { |
| 89 | + 'database': 'test', |
| 90 | + 'host': '127.0.0.1', |
| 91 | + 'password': '', |
| 92 | + 'port': '5432', |
| 93 | + 'user': 'postgres', |
| 94 | + 'AHA': '5', |
| 95 | + 'moo': 'yes', |
| 96 | + } |
| 97 | + }) |
0 commit comments