# Path: tkp/testutil/alchemy.py
# def gen_band(dataset, central=150**6, low=None, high=None):
#     if not low:
#         low = central * .9
#     if not high:
#         high = central * 1.1
#     return tkp.db.model.Frequencyband(dataset=dataset, freq_low=low, freq_central=central,
#                                       freq_high=high)
# 
# def gen_dataset(description='A test dataset without a description'):
#     return tkp.db.model.Dataset(process_start_ts=datetime.now(),
#                                 description=description)
# 
# def gen_skyregion(dataset):
#     return tkp.db.model.Skyregion(dataset=dataset, centre_ra=1, centre_decl=1,
#                                   xtr_radius=1, x=1, y=1, z=1)
# 
# def gen_lightcurve(band, dataset, skyregion, datapoints=10):
#     """
#     returns: a list of created SQLAlchemy objects
#     """
#     start = datetime.fromtimestamp(0)
#     ten_sec = timedelta(seconds=10)
#     xtrsrcs = []
#     images = []
#     assocs = []
#     for i in range(datapoints):
#         taustart_ts = start + ten_sec * i
#         image = gen_image(band, dataset, skyregion, taustart_ts)
# 
#         if i == 5:
#             image.int = 10
#         images.append(image)
#         xtrsrcs.append(gen_extractedsource(image))
# 
#     # now we can make runningcatalog, we use first xtrsrc as trigger src
#     runningcatalog = gen_runningcatalog(xtrsrcs[0], dataset)
#     assocskyrgn = gen_assocskyrgn(runningcatalog, skyregion)
# 
#     # create the associations. Can't do this directly since the
#     # association table has non nullable columns
#     for xtrsrc in xtrsrcs:
#         assocs.append(gen_assocxtrsource(runningcatalog, xtrsrc))
# 
#     newsource = gen_newsource(runningcatalog, xtrsrcs[5], images[4])
# 
#     # just return all db objects we created
#     return [dataset, band, skyregion, runningcatalog, assocskyrgn, newsource] + \
#            images + xtrsrcs + assocs
# 
# Path: tkp/testutil/decorators.py
# def database_disabled():
#     """Return ``True`` if the database is disabled for test purposes."""
#     return os.environ.get("TKP_DISABLEDB", False)
# 
# Path: tkp/steps/varmetric.py
# def execute_store_varmetric(dataset_id, session=None):
#     """
#     Executes the storing varmetric function. Will create a database session
#     if none is supplied.
# 
#     args:
#         dataset_id: the ID of the dataset for which you want to store the
#                     varmetrics
#         session: An optional SQLAlchemy session
#     """
#     if not session:
#         database = Database()
#         session = database.Session()
# 
#     dataset = Dataset(id=dataset_id)
#     delete_ = del_duplicate_varmetric(session=session, dataset=dataset)
#     session.execute(delete_)
#     insert_ = store_varmetric(session, dataset=dataset)
#     session.execute(insert_)
#     session.commit()


import unittest
import logging
import tkp.db.model
import tkp.db
from tkp.testutil.alchemy import gen_band, gen_dataset, gen_skyregion,\
    gen_lightcurve
from tkp.testutil.decorators import database_disabled
from tkp.steps.varmetric import execute_store_varmetric






logging.basicConfig(level=logging.INFO)
logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING)


class TestApi(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # Can't use a regular skip here, due to a Nose bug:
        # https://github.com/nose-devs/nose/issues/946
        if database_disabled():
            raise unittest.SkipTest("Database functionality disabled "
                                    "in configuration.")
        cls.db = tkp.db.Database()
        cls.db.connect()

    def setUp(self):
        self.session = self.db.Session()

        self.dataset = gen_dataset('test varmetric step')
        band = gen_band(dataset=self.dataset, central=150**6)
        skyregion = gen_skyregion(self.dataset)

# Path: tkp/testutil/alchemy.py
# def gen_band(dataset, central=150**6, low=None, high=None):
#     if not low:
#         low = central * .9
#     if not high:
#         high = central * 1.1
#     return tkp.db.model.Frequencyband(dataset=dataset, freq_low=low, freq_central=central,
#                                       freq_high=high)
# 
# def gen_dataset(description='A test dataset without a description'):
#     return tkp.db.model.Dataset(process_start_ts=datetime.now(),
#                                 description=description)
# 
# def gen_skyregion(dataset):
#     return tkp.db.model.Skyregion(dataset=dataset, centre_ra=1, centre_decl=1,
#                                   xtr_radius=1, x=1, y=1, z=1)
# 
# def gen_lightcurve(band, dataset, skyregion, datapoints=10):
#     """
#     returns: a list of created SQLAlchemy objects
#     """
#     start = datetime.fromtimestamp(0)
#     ten_sec = timedelta(seconds=10)
#     xtrsrcs = []
#     images = []
#     assocs = []
#     for i in range(datapoints):
#         taustart_ts = start + ten_sec * i
#         image = gen_image(band, dataset, skyregion, taustart_ts)
# 
#         if i == 5:
#             image.int = 10
#         images.append(image)
#         xtrsrcs.append(gen_extractedsource(image))
# 
#     # now we can make runningcatalog, we use first xtrsrc as trigger src
#     runningcatalog = gen_runningcatalog(xtrsrcs[0], dataset)
#     assocskyrgn = gen_assocskyrgn(runningcatalog, skyregion)
# 
#     # create the associations. Can't do this directly since the
#     # association table has non nullable columns
#     for xtrsrc in xtrsrcs:
#         assocs.append(gen_assocxtrsource(runningcatalog, xtrsrc))
# 
#     newsource = gen_newsource(runningcatalog, xtrsrcs[5], images[4])
# 
#     # just return all db objects we created
#     return [dataset, band, skyregion, runningcatalog, assocskyrgn, newsource] + \
#            images + xtrsrcs + assocs
# 
# Path: tkp/testutil/decorators.py
# def database_disabled():
#     """Return ``True`` if the database is disabled for test purposes."""
#     return os.environ.get("TKP_DISABLEDB", False)
# 
# Path: tkp/steps/varmetric.py
# def execute_store_varmetric(dataset_id, session=None):
#     """
#     Executes the storing varmetric function. Will create a database session
#     if none is supplied.
# 
#     args:
#         dataset_id: the ID of the dataset for which you want to store the
#                     varmetrics
#         session: An optional SQLAlchemy session
#     """
#     if not session:
#         database = Database()
#         session = database.Session()
# 
#     dataset = Dataset(id=dataset_id)
#     delete_ = del_duplicate_varmetric(session=session, dataset=dataset)
#     session.execute(delete_)
#     insert_ = store_varmetric(session, dataset=dataset)
#     session.execute(insert_)
#     session.commit()


# Path: tests/test_steps/test_varmetric.py
import unittest
import logging
import tkp.db.model
import tkp.db
from tkp.testutil.alchemy import gen_band, gen_dataset, gen_skyregion,\
    gen_lightcurve
from tkp.testutil.decorators import database_disabled
from tkp.steps.varmetric import execute_store_varmetric







logging.basicConfig(level=logging.INFO)
logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING)


class TestApi(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # Can't use a regular skip here, due to a Nose bug:
        # https://github.com/nose-devs/nose/issues/946
        if database_disabled():
            raise unittest.SkipTest("Database functionality disabled "
                                    "in configuration.")
        cls.db = tkp.db.Database()
        cls.db.connect()

    def setUp(self):
        self.session = self.db.Session()

        self.dataset = gen_dataset('test varmetric step')
        band = gen_band(dataset=self.dataset, central=150**6)
        skyregion = gen_skyregion(self.dataset)

        lightcurve = gen_lightcurve(band, self.dataset, skyregion)