# Copyright (c) 2014 by Ecreall under licence AGPL terms 
# avalaible on http://www.gnu.org/licenses/agpl.html 

# licence: AGPL
# author: Amen Souissi

import unittest

from pyramid import testing
from pyramid_robot.layer import Layer
from webtest import http
from substanced.db import root_factory


class BaseFunctionalTests(object):

    def setUp(self):
        import tempfile
        import os.path
        self.tmpdir = tempfile.mkdtemp()
        dbpath = os.path.join( self.tmpdir, 'test.db')
        uri = 'file://' + dbpath
        settings = {'zodbconn.uri': uri,
                    'substanced.secret': 'sosecret',
                    'substanced.initial_login': 'admin',
                    'substanced.initial_password': 'admin',
                    'novaideo.secret' : 'seekri1',
                    'novaideo.admin_email': 'admin@example.com',
                    'pyramid.includes': [
                        'substanced',
                        'pyramid_chameleon',
                        'pyramid_layout',
                        'pyramid_mailer.testing', # have to be after substanced to override the mailer
                        'pyramid_tm',
                        'dace',
                        'pontus',
                        'daceui'
        ]}

        testing.setUp()
        from novaideo import main
        self.app = app = main({}, **settings)
        self.db = app.registry._zodb_databases['']
        self.request = request = testing.DummyRequest()
        self.config = testing.setUp(registry=app.registry, request=request)
        self.registry = self.config.registry
        self.root = root_factory(request)
        request.root = self.root


    def tearDown(self):
        from dace.processinstance import event
        with event.callbacks_lock:
            for dc_or_stream in event.callbacks.values():
                if hasattr(dc_or_stream, 'close'):
                    dc_or_stream.close()
                else:
                    dc_or_stream.stop()

            event.callbacks = {}

        from dace.objectofcollaboration.system import CRAWLERS
        for crawler in CRAWLERS:
            crawler.stop()

        import shutil
        testing.tearDown()
        self.db.close()
        shutil.rmtree(self.tmpdir)


class FunctionalTests(BaseFunctionalTests, unittest.TestCase):

    def setUp(self):
        super(FunctionalTests, self).setUp()


class RobotLayer(BaseFunctionalTests, Layer):

    defaultBases = ()

    def setUp(self):
        super(RobotLayer, self).setUp()
        self.server = http.StopableWSGIServer.create(self.app, port=8080)

    def tearDown(self):
        super(RobotLayer, self).tearDown()
        self.server.shutdown()


ROBOT_LAYER = RobotLayer()

@@ -162,17 +162,16 @@
 nittest%0A
-%0A
 from pyr
@@ -293,16 +293,57 @@
 actory%0A%0A
+from dace.subscribers import stop_ioloop%0A
 %0Aclass B
@@ -510,17 +510,16 @@
 th.join(
- 
 self.tmp
@@ -562,24 +562,60 @@
 //' + dbpath
+ + '?blobstorage_dir=' + self.tmpdir
 %0A        set
@@ -865,16 +865,80 @@
 ekri1',%0A
+                    'substanced.uploads_tempdir' : self.tmpdir,%0A
         
@@ -1858,452 +1858,21 @@
     
-from dace.processinstance import event%0A        with event.callbacks_lock:%0A            for dc_or_stream in event.callbacks.values():%0A                if hasattr(dc_or_stream, 'close'):%0A                    dc_or_stream.close()%0A                else:%0A                    dc_or_stream.stop()%0A%0A            event.callbacks = %7B%7D%0A%0A        from dace.objectofcollaboration.system import CRAWLERS%0A        for crawler in CRAWLERS:%0A            crawler.stop()%0A
+stop_ioloop()
 %0A   

fix testing.py with recent changes in dace