from django.conf import settings
from django.contrib.auth.models import User
from django.utils import unittest

from mock import Mock
from mysql_pool.base import DatabaseWrapper, db_pool

from sqlalchemy import event

# Alright I'm tired and don't have the time to mock this out fully. I just
# wanted to write some integration tests to prove that this does what we
# want it to and nothing more. This means some hacks in your settings and
# mysql to make it behave the way we want.
#
# This is the wait timeout you've set on mysql, so it will kill any
# connections that are older than this
mysql_timeout = 5
#
# These are the names of the two databases connections you've set in
# settings.py so we can be sure that the two databases are using different
# pools and don't stomp on each other.
#
# These two pools can point to the same database, but should have a different
# signature. For example:
# DATABASES = {
#    'default': {
#        'NAME': 'zamboni',
#        'ENGINE': 'mysql_pool',
#        'USER': 'root',
#        'PASSWORD': '',
#        'HOST': '',
#        'OPTIONS':  {'init_command': 'SET storage_engine=InnoDB'},
#        'TEST_CHARSET': 'utf8',
#        'TEST_COLLATION': 'utf8_general_ci',
#    },
#    'test_mysql_pool': {
#        'NAME': 'zamboni',
#        'ENGINE': 'mysql_pool',
#        'USER': 'root',
#        'PASSWORD': '',
#        'HOST': '',
#        'OPTIONS':  {},
#        'TEST_CHARSET': 'utf8',
#        'TEST_COLLATION': 'utf8_general_ci',
#    },
#}
# If I was, smarter this would all be done in test setup.
#
databases = ['default', 'test_mysql_pool']
#
# Got these setup? Great, let's go.

class TestPool(unittest.TestCase):

    def setUp(self):
        # Make sure we start with nothing in the pool.
        db_pool.close()
        self.wrapper = DatabaseWrapper(Mock())

    def test_serialize(self):
        _keys = []
        for db in databases:
            _settings = self.wrapper._serialize(settings.DATABASES[db])
            _keys.append(_settings['sa_pool_key'])
        assert len(set(_keys)) == len(databases)

    def test_can_query(self):
        len(User.objects.all())
        assert len(db_pool.pools) == 1

    def test_multiple_queries(self):
        for x in range(3):
            len(User.objects.all())
        assert len(db_pool.pools) == 1

    def test_multiple_pool(self):
        for db in databases:
            User.objects.using(db).all()
        assert len(db_pool.pools) == 2



@@ -70,16 +70,52 @@
 rt User%0A
+from django.core.cache import cache%0A
 from dja
@@ -217,16 +217,54 @@
 b_pool%0A%0A
+from sqlalchemy.pool import QueuePool%0A
 from sql
@@ -1816,16 +1816,38 @@
 e pool.%0A
+        cache.clear()%0A
         
@@ -1908,16 +1908,454 @@
 (Mock())
+%0A        self.events = %5B%5D%0A        self.connect = lambda *args: self.events.append('connect')%0A        self.checkin = lambda *args: self.events.append('checkin')%0A        self.checkout = lambda *args: self.events.append('checkout')%0A        event.listen(QueuePool, 'connect', self.connect)%0A        event.listen(QueuePool, 'checkin', self.checkin)%0A        event.listen(QueuePool, 'checkout', self.checkout)%0A        self.qs = User.objects.all()
 %0A%0A    de
@@ -2641,34 +2641,23 @@
     len(
-U
 se
-r.objects.all()
+lf.qs
 )%0A      
@@ -2770,34 +2770,23 @@
     len(
-U
 se
-r.objects.all()
+lf.qs
 )%0A      
@@ -2894,16 +2894,20 @@
         
+len(
 User.obj
@@ -2926,16 +2926,17 @@
 b).all()
+)
 %0A       
@@ -2969,8 +2969,183 @@
  == 
-2%0A%0A
+len(databases)%0A%0A    def test_connect(self):%0A        len(self.qs)%0A        assert len(db_pool.pools) == 1%0A        assert set(%5B'checkin', 'checkout', 'connect'%5D) == set(self.events)
 %0A

add in event test