# Path: tests/util.py
# def runtask(task, superclasses=None):
#     '''
#     Run deps of tasks then the task, faking session management
# 
#     superclasses is a list of classes that we will be willing to run as
#     pre-reqs, other pre-reqs will be ignored.  Can be useful when testing to
#     only run metadata classes, for example.
#     '''
#     from lib.logger import get_logger
#     LOGGER = get_logger(__name__)
#     if task.complete():
#         return
#     for dep in task.deps():
#         if superclasses:
#             for klass in superclasses:
#                 if isinstance(dep, klass):
#                     runtask(dep, superclasses=superclasses)
#                     assert dep.complete() is True, 'dependency {} not complete for class {}'.format(dep, klass)
#         else:
#             runtask(dep)
#             assert dep.complete() is True, 'dependency {} not complete'.format(dep)
#     try:
#         before = time()
#         for klass, cb_dict in task._event_callbacks.items():
#             if isinstance(task, klass):
#                 start_callbacks = cb_dict.get('event.core.start', [])
#                 for scb in start_callbacks:
#                     scb(task)
#         task.run()
#         task.on_success()
#         after = time()
#         LOGGER.warn('runtask timing %s: %s', task, round(after - before, 2))
#     except Exception as exc:
#         task.on_failure(exc)
#         raise
# 
# def setup():
#     from tasks.meta import current_session, Base
#     if Base.metadata.bind.url.database != 'test':
#         raise Exception('Can only run tests on database "test"')
#     session = current_session()
#     session.rollback()
#     session.execute('DROP SCHEMA IF EXISTS observatory CASCADE')
#     session.execute('CREATE SCHEMA observatory')
#     session.commit()
#     Base.metadata.create_all()
#     session.close()
# 
# def teardown():
#     from tasks.meta import current_session, Base
#     if Base.metadata.bind.url.database != 'test':
#         raise Exception('Can only run tests on database "test"')
#     session = current_session()
#     session.rollback()
#     Base.metadata.drop_all()
#     session.execute('DROP SCHEMA IF EXISTS observatory CASCADE')
#     session.commit()
#     session.close()
# 
# class FakeTask(object):
# 
#     task_id = 'fake'
# 
# Path: tasks/meta.py
# def current_session():
#     '''
#     Returns the session relevant to the currently operating :class:`Task`, if
#     any.  Outside the context of a :class:`Task`, this can still be used for
#     manual session management.
#     '''
#     return _current_session.get()


from collections import OrderedDict
from luigi import Parameter
from nose.tools import (assert_equals, with_setup, assert_raises, assert_in,
                        assert_is_none, assert_true, assert_false)
from tests.util import runtask, setup, teardown, FakeTask
from tasks.meta import current_session
import tasks.carto
import imp
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_geom_numer_timespan').fetchone()[0], 1)

    imp.reload(tasks.carto)
    task = tasks.carto.OBSMetaToLocal(force=True)
    runtask(task)
    assert_equals(session.execute('SELECT COUNT(*) FROM observatory.obs_meta').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_numer').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_denom').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_geom').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_timespan').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_geom_numer_timespan').fetchone()[0], 0)


@with_setup(setup, teardown)
def test_obs_meta_to_local_works_twice():
    '''
    Should be possible to run OBSMetaToLocal twice in a row.
    '''
    imp.reload(tasks.carto)
    task = tasks.carto.OBSMetaToLocal()
    runtask(task)
    runtask(task)

# Path: tests/util.py
# def runtask(task, superclasses=None):
#     '''
#     Run deps of tasks then the task, faking session management
# 
#     superclasses is a list of classes that we will be willing to run as
#     pre-reqs, other pre-reqs will be ignored.  Can be useful when testing to
#     only run metadata classes, for example.
#     '''
#     from lib.logger import get_logger
#     LOGGER = get_logger(__name__)
#     if task.complete():
#         return
#     for dep in task.deps():
#         if superclasses:
#             for klass in superclasses:
#                 if isinstance(dep, klass):
#                     runtask(dep, superclasses=superclasses)
#                     assert dep.complete() is True, 'dependency {} not complete for class {}'.format(dep, klass)
#         else:
#             runtask(dep)
#             assert dep.complete() is True, 'dependency {} not complete'.format(dep)
#     try:
#         before = time()
#         for klass, cb_dict in task._event_callbacks.items():
#             if isinstance(task, klass):
#                 start_callbacks = cb_dict.get('event.core.start', [])
#                 for scb in start_callbacks:
#                     scb(task)
#         task.run()
#         task.on_success()
#         after = time()
#         LOGGER.warn('runtask timing %s: %s', task, round(after - before, 2))
#     except Exception as exc:
#         task.on_failure(exc)
#         raise
# 
# def setup():
#     from tasks.meta import current_session, Base
#     if Base.metadata.bind.url.database != 'test':
#         raise Exception('Can only run tests on database "test"')
#     session = current_session()
#     session.rollback()
#     session.execute('DROP SCHEMA IF EXISTS observatory CASCADE')
#     session.execute('CREATE SCHEMA observatory')
#     session.commit()
#     Base.metadata.create_all()
#     session.close()
# 
# def teardown():
#     from tasks.meta import current_session, Base
#     if Base.metadata.bind.url.database != 'test':
#         raise Exception('Can only run tests on database "test"')
#     session = current_session()
#     session.rollback()
#     Base.metadata.drop_all()
#     session.execute('DROP SCHEMA IF EXISTS observatory CASCADE')
#     session.commit()
#     session.close()
# 
# class FakeTask(object):
# 
#     task_id = 'fake'
# 
# Path: tasks/meta.py
# def current_session():
#     '''
#     Returns the session relevant to the currently operating :class:`Task`, if
#     any.  Outside the context of a :class:`Task`, this can still be used for
#     manual session management.
#     '''
#     return _current_session.get()


# Path: tests/test_carto.py
from collections import OrderedDict
from luigi import Parameter
from nose.tools import (assert_equals, with_setup, assert_raises, assert_in,
                        assert_is_none, assert_true, assert_false)
from tests.util import runtask, setup, teardown, FakeTask
from tasks.meta import current_session
import tasks.carto
import imp

    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_geom_numer_timespan').fetchone()[0], 1)

    imp.reload(tasks.carto)
    task = tasks.carto.OBSMetaToLocal(force=True)
    runtask(task)
    assert_equals(session.execute('SELECT COUNT(*) FROM observatory.obs_meta').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_numer').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_denom').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_geom').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_timespan').fetchone()[0], 0)
    assert_equals(session.execute(
        'SELECT COUNT(*) FROM observatory.obs_meta_geom_numer_timespan').fetchone()[0], 0)


@with_setup(setup, teardown)
def test_obs_meta_to_local_works_twice():
    '''
    Should be possible to run OBSMetaToLocal twice in a row.
    '''
    imp.reload(tasks.carto)
    task = tasks.carto.OBSMetaToLocal()
    runtask(task)
    runtask(task)

    session = current_session()