# Path: norm/uri.py
# def parseURI(uri):
#     """
#     Parse a database uri into component parts.
#     """
#     ret = {}
#     r = urlparse(uri)
#     ret['scheme'] = r.scheme
#     if r.scheme == 'sqlite':
#         # sqlite
#         ret['file'] = uri.split(':')[1]
#     else:
#         # postgres
#         parts = r.path.lstrip('/').split('?')
#         ret['db'] = parts[0]
#         if r.username:
#             ret['user'] = r.username
#         if r.hostname:
#             ret['host'] = r.hostname
#         if r.port:
#             ret['port'] = r.port
#         if r.password:
#             ret['password'] = r.password
#         if len(parts) == 2:
#             query = parts[1]
#             for k,v in parse_qs(query).items():
#                 ret[k] = v[-1]
#     return ret
# 
# def mkConnStr(parsed_uri):
#     """
#     Turn a parsed uri into a connection string suitable for the db module
#     """
#     if parsed_uri['scheme'] == 'sqlite':
#         return parsed_uri['file'] or ':memory:'
# 
#     # postgres
#     parts = ['dbname=%s' % parsed_uri['db']]
#     for attr in ['host', 'port', 'user', 'password', 'sslmode']:
#         if attr in parsed_uri:
#             parts.append('%s=%s' % (attr, parsed_uri[attr]))
#     return ' '.join(parts)


from twisted.trial.unittest import TestCase
from norm.uri import parseURI, mkConnStr

        p = parseURI('postgres://user:password@')
        self.assertEqual(p['user'], 'user')
        self.assertEqual(p['password'], 'password')

        p = parseURI('postgres:///postgres?sslmode=require')
        self.assertEqual(p['db'], 'postgres')
        self.assertEqual(p['sslmode'], 'require')



class makeConnStrTest(TestCase):


    def t(self, i, expected):
        parsed = parseURI(i)
        output = mkConnStr(parsed)
        self.assertEqual(output, expected, "Expected URI %r to become conn "
                         "string\n%r\nbut it was\n%r" % (i, expected, output))

    def test_sqlite(self):
        self.t('sqlite:', ':memory:')
        self.t('sqlite:/tmp/foo', '/tmp/foo')
        self.t('sqlite:tmp/foo', 'tmp/foo')


    def test_postgres(self):
        def t(i, expected):
            expected_parts = expected.split(' ')
            parsed = parseURI(i)

# Path: norm/uri.py
# def parseURI(uri):
#     """
#     Parse a database uri into component parts.
#     """
#     ret = {}
#     r = urlparse(uri)
#     ret['scheme'] = r.scheme
#     if r.scheme == 'sqlite':
#         # sqlite
#         ret['file'] = uri.split(':')[1]
#     else:
#         # postgres
#         parts = r.path.lstrip('/').split('?')
#         ret['db'] = parts[0]
#         if r.username:
#             ret['user'] = r.username
#         if r.hostname:
#             ret['host'] = r.hostname
#         if r.port:
#             ret['port'] = r.port
#         if r.password:
#             ret['password'] = r.password
#         if len(parts) == 2:
#             query = parts[1]
#             for k,v in parse_qs(query).items():
#                 ret[k] = v[-1]
#     return ret
# 
# def mkConnStr(parsed_uri):
#     """
#     Turn a parsed uri into a connection string suitable for the db module
#     """
#     if parsed_uri['scheme'] == 'sqlite':
#         return parsed_uri['file'] or ':memory:'
# 
#     # postgres
#     parts = ['dbname=%s' % parsed_uri['db']]
#     for attr in ['host', 'port', 'user', 'password', 'sslmode']:
#         if attr in parsed_uri:
#             parts.append('%s=%s' % (attr, parsed_uri[attr]))
#     return ' '.join(parts)


# Path: norm/test/test_uri.py
from twisted.trial.unittest import TestCase
from norm.uri import parseURI, mkConnStr


        p = parseURI('postgres://user:password@')
        self.assertEqual(p['user'], 'user')
        self.assertEqual(p['password'], 'password')

        p = parseURI('postgres:///postgres?sslmode=require')
        self.assertEqual(p['db'], 'postgres')
        self.assertEqual(p['sslmode'], 'require')



class makeConnStrTest(TestCase):


    def t(self, i, expected):
        parsed = parseURI(i)
        output = mkConnStr(parsed)
        self.assertEqual(output, expected, "Expected URI %r to become conn "
                         "string\n%r\nbut it was\n%r" % (i, expected, output))

    def test_sqlite(self):
        self.t('sqlite:', ':memory:')
        self.t('sqlite:/tmp/foo', '/tmp/foo')
        self.t('sqlite:tmp/foo', 'tmp/foo')


    def test_postgres(self):
        def t(i, expected):
            expected_parts = expected.split(' ')
            parsed = parseURI(i)

            output = mkConnStr(parsed)