import mock
import pkg_resources
import unittest

class ZODBURITests(unittest.TestCase):

    def parse(self, uri):
        resolve_uri = pkg_resources.load_entry_point(
            'newt.db', 'zodburi.resolvers', 'newt')
        factory, dbkw = resolve_uri(uri)
        with mock.patch("newt.db.storage") as storage:
            factory()
            (dsn,), options = storage.call_args
            return dsn, options, dbkw

    def test_minimal(self):
        self.assertEqual(('postgresql://', {}, {}), self.parse("newt://"))

    def test_full(self):
        self.assertEqual(
            ('postgresql://jim:123@foo.bar/mydb?params1=value1&param2=value3',
             dict(keep_history=True, driver='psycopg2'),
             dict(connection_pool_size = 1,
                  connection_cache_size = 3,
                  database_name='main',
                  ),
             ),
            self.parse("newt://jim:123@foo.bar/mydb"
                       "?params1=value1"
                       "&keep_history=True"
                       "&driver=psycopg2"
                       "&connection_pool_size=1"
                       "&connection_cache_size=3"
                       "&database_name=main"
                       "&param2=value3"
                       ),
            )

        def test_bool_variations(self):
            self.assertEqual(('postgresql://', dict(keep_history=True), {}),
                              self.parse("newt://?keep_history=True"))
            self.assertEqual(('postgresql://', dict(keep_history=True), {}),
                              self.parse("newt://?keep_history=TRUE"))
            self.assertEqual(('postgresql://', dict(keep_history=True), {}),
                              self.parse("newt://?keep_history=trUe"))
            self.assertEqual(('postgresql://', dict(keep_history=True), {}),
                              self.parse("newt://?keep_history=YeS"))
            self.assertEqual(('postgresql://', dict(keep_history=True), {}),
                              self.parse("newt://?keep_history=1"))

            self.assertEqual(('postgresql://', dict(keep_history=False), {}),
                              self.parse("newt://?keep_history=False"))
            self.assertEqual(('postgresql://', dict(keep_history=False), {}),
                              self.parse("newt://?keep_history=FALSE"))
            self.assertEqual(('postgresql://', dict(keep_history=False), {}),
                              self.parse("newt://?keep_history=fAlse"))
            self.assertEqual(('postgresql://', dict(keep_history=False), {}),
                              self.parse("newt://?keep_history=nO"))
            self.assertEqual(('postgresql://', dict(keep_history=False), {}),
                              self.parse("newt://?keep_history=0"))

@@ -47,52 +47,8 @@
 st%0A%0A
-class ZODBURITests(unittest.TestCase):%0A%0A    
 def 
@@ -57,14 +57,8 @@
 rse(
-self, 
 uri)
@@ -63,20 +63,16 @@
 i):%0A    
-   
- 
 resolve_
@@ -109,20 +109,16 @@
 _point(%0A
-    
         
@@ -157,20 +157,16 @@
 'newt')%0A
-    
     fact
@@ -198,20 +198,16 @@
 ri)%0A    
- 
-   
 with moc
@@ -253,20 +253,16 @@
         
-    
 factory(
@@ -263,20 +263,16 @@
 ctory()%0A
-    
         
@@ -315,20 +315,16 @@
         
-    
 return d
@@ -342,16 +342,56 @@
 , dbkw%0A%0A
+class ZODBURITests(unittest.TestCase):%0A%0A
     def 
@@ -462,21 +462,16 @@
 %7D, %7B%7D), 
-self.
 parse(%22n
@@ -838,37 +838,32 @@
  ),%0A            
-self.
 parse(%22newt://ji
@@ -900,21 +900,16 @@
         
-     
 %22?params
@@ -936,21 +936,16 @@
         
-    
- 
 %22&keep_h
@@ -975,21 +975,16 @@
         
-     
 %22&driver
@@ -1004,37 +1004,32 @@
                 
-    
- 
 %22&connection_poo
@@ -1038,21 +1038,16 @@
 size=1%22%0A
-     
         
@@ -1083,21 +1083,16 @@
 size=3%22%0A
-     
         
@@ -1141,21 +1141,16 @@
         
-     
 %22&param2
@@ -1158,21 +1158,16 @@
 value3%22%0A
-     
         
@@ -1333,37 +1333,32 @@
                 
-self.
 parse(%22newt://?k
@@ -1476,37 +1476,32 @@
                 
-self.
 parse(%22newt://?k
@@ -1619,37 +1619,32 @@
                 
-self.
 parse(%22newt://?k
@@ -1762,37 +1762,32 @@
                 
-self.
 parse(%22newt://?k
@@ -1904,37 +1904,32 @@
                 
-self.
 parse(%22newt://?k
@@ -2046,37 +2046,32 @@
                 
-self.
 parse(%22newt://?k
@@ -2191,37 +2191,32 @@
                 
-self.
 parse(%22newt://?k
@@ -2336,37 +2336,32 @@
                 
-self.
 parse(%22newt://?k
@@ -2481,37 +2481,32 @@
                 
-self.
 parse(%22newt://?k
@@ -2631,21 +2631,16 @@
         
-self.
 parse(%22n

make parse a top-level function