# Path: tkp/db/model.py
# class Rejection(Base):
#     __tablename__ = 'rejection'
# 
#     id = Column(Integer, primary_key=True)
# 
#     image_id = Column('image', ForeignKey('image.id'), index=True)
#     image = relationship('Image')
# 
#     # TO DO: Rename this column to 'rejectreason_id',
#     # (rather than just 'rejectreason') so the model attribute matches
#     # the SQL column name, avoiding the current confusing name-shadowing
#     # between the SQL columns and the model attributes. (Issue #508)
#     rejectreason_id = Column('rejectreason', ForeignKey('rejectreason.id'), index=True)
#     rejectreason = relationship('Rejectreason')
# 
#     comment = Column(String(512))
# 
# class Rejectreason(Base):
#     __tablename__ = 'rejectreason'
# 
#     id = Column(Integer,  primary_key=True)
#     description = Column(String(512))
# 
# Path: tkp/testutil/decorators.py
# def requires_database():
#     if database_disabled():
#         return unittest.skip("Database functionality disabled in configuration")
#     return lambda func: func
# 
# Path: tkp/testutil/db_subs.py
# def delete_test_database(database):
# def example_dbimage_data_dict(**kwargs):
# def generate_timespaced_dbimages_data(n_images,
#                            timedelta_between_images=datetime.timedelta(days=1),
#                            **kwargs):
# def example_extractedsource_tuple(ra=123.123, dec=10.5,  # Arbitrarily picked defaults
#                                   ra_fit_err=5. / 3600, dec_fit_err=6. / 3600,
#                                   peak=15e-3, peak_err=5e-4,
#                                   flux=15e-3, flux_err=5e-4,
#                                   sigma=15.,
#                                   beam_maj=100., beam_min=100., beam_angle=45.,
#                                   ew_sys_err=20., ns_sys_err=20.,
#                                   error_radius=10.0, fit_type=0,
#                                   chisq=5., reduced_chisq=1.5):
# def deRuiter_radius(src1, src2):
# def lightcurve_metrics(src_list):
#     def __init__(self,
#                  template_extractedsource,
#                  lightcurve,
#                  ):
#     def value_at_dtime(self, dtime, image_rms):
#     def simulate_extraction(self, db_image, extraction_type,
#                             rms_attribute='rms_min'):
# def insert_image_and_simulated_sources(dataset, image_params, mock_sources,
#                                        new_source_sigma_margin,
#                                        deruiter_radius=3.7):
# def get_newsources_for_dataset(dsid):
# def get_sources_filtered_by_final_variability(dataset_id,
#                      eta_min,
#                      v_min,
#                      # minpoints
#     ):
#         N = i + 1
# class MockSource(object):


import unittest
import tkp.quality
import tkp.db
import tkp.db.quality as dbqual
import tkp.db.database
from tkp.db.model import Rejection, Rejectreason
from sqlalchemy.exc import DatabaseError, IntegrityError
from tkp.testutil.decorators import requires_database
from tkp.testutil import db_subs
    def setUpClass(cls):
        cls.db = tkp.db.Database()

    def setUp(self):
        self.session = self.db.Session()
        self.fake_images = db_subs.generate_timespaced_dbimages_data(n_images=1)
        self.dataset = tkp.db.DataSet(data={'description':
                                                "Reject:" + self._testMethodName})
        self.image = tkp.db.Image(data=self.fake_images[0],
                                  dataset=self.dataset)

    def tearDown(self):
        self.session.rollback()
        tkp.db.rollback()
        pass

    def test_rejectrms_and_unreject(self):
        dbqual.reject(self.image.id,
                      dbqual.reject_reasons['rms'],
                      "10 times too high",
                      self.session)
        image_rejections_q = self.session.query(Rejection).filter(
            Rejection.image_id == self.image.id)
        self.assertEqual(image_rejections_q.count(), 1)
        dbqual.unreject(self.image.id, self.session)
        self.assertEqual(image_rejections_q.count(), 0)

    def test_unknownreason(self):
        with self.assertRaises(IntegrityError):
            dbqual.reject(self.image.id,

# Path: tkp/db/model.py
# class Rejection(Base):
#     __tablename__ = 'rejection'
# 
#     id = Column(Integer, primary_key=True)
# 
#     image_id = Column('image', ForeignKey('image.id'), index=True)
#     image = relationship('Image')
# 
#     # TO DO: Rename this column to 'rejectreason_id',
#     # (rather than just 'rejectreason') so the model attribute matches
#     # the SQL column name, avoiding the current confusing name-shadowing
#     # between the SQL columns and the model attributes. (Issue #508)
#     rejectreason_id = Column('rejectreason', ForeignKey('rejectreason.id'), index=True)
#     rejectreason = relationship('Rejectreason')
# 
#     comment = Column(String(512))
# 
# class Rejectreason(Base):
#     __tablename__ = 'rejectreason'
# 
#     id = Column(Integer,  primary_key=True)
#     description = Column(String(512))
# 
# Path: tkp/testutil/decorators.py
# def requires_database():
#     if database_disabled():
#         return unittest.skip("Database functionality disabled in configuration")
#     return lambda func: func
# 
# Path: tkp/testutil/db_subs.py
# def delete_test_database(database):
# def example_dbimage_data_dict(**kwargs):
# def generate_timespaced_dbimages_data(n_images,
#                            timedelta_between_images=datetime.timedelta(days=1),
#                            **kwargs):
# def example_extractedsource_tuple(ra=123.123, dec=10.5,  # Arbitrarily picked defaults
#                                   ra_fit_err=5. / 3600, dec_fit_err=6. / 3600,
#                                   peak=15e-3, peak_err=5e-4,
#                                   flux=15e-3, flux_err=5e-4,
#                                   sigma=15.,
#                                   beam_maj=100., beam_min=100., beam_angle=45.,
#                                   ew_sys_err=20., ns_sys_err=20.,
#                                   error_radius=10.0, fit_type=0,
#                                   chisq=5., reduced_chisq=1.5):
# def deRuiter_radius(src1, src2):
# def lightcurve_metrics(src_list):
#     def __init__(self,
#                  template_extractedsource,
#                  lightcurve,
#                  ):
#     def value_at_dtime(self, dtime, image_rms):
#     def simulate_extraction(self, db_image, extraction_type,
#                             rms_attribute='rms_min'):
# def insert_image_and_simulated_sources(dataset, image_params, mock_sources,
#                                        new_source_sigma_margin,
#                                        deruiter_radius=3.7):
# def get_newsources_for_dataset(dsid):
# def get_sources_filtered_by_final_variability(dataset_id,
#                      eta_min,
#                      v_min,
#                      # minpoints
#     ):
#         N = i + 1
# class MockSource(object):


# Path: tests/test_quality/test_reject.py
import unittest
import tkp.quality
import tkp.db
import tkp.db.quality as dbqual
import tkp.db.database
from tkp.db.model import Rejection, Rejectreason
from sqlalchemy.exc import DatabaseError, IntegrityError
from tkp.testutil.decorators import requires_database
from tkp.testutil import db_subs

    def setUpClass(cls):
        cls.db = tkp.db.Database()

    def setUp(self):
        self.session = self.db.Session()
        self.fake_images = db_subs.generate_timespaced_dbimages_data(n_images=1)
        self.dataset = tkp.db.DataSet(data={'description':
                                                "Reject:" + self._testMethodName})
        self.image = tkp.db.Image(data=self.fake_images[0],
                                  dataset=self.dataset)

    def tearDown(self):
        self.session.rollback()
        tkp.db.rollback()
        pass

    def test_rejectrms_and_unreject(self):
        dbqual.reject(self.image.id,
                      dbqual.reject_reasons['rms'],
                      "10 times too high",
                      self.session)
        image_rejections_q = self.session.query(Rejection).filter(
            Rejection.image_id == self.image.id)
        self.assertEqual(image_rejections_q.count(), 1)
        dbqual.unreject(self.image.id, self.session)
        self.assertEqual(image_rejections_q.count(), 0)

    def test_unknownreason(self):
        with self.assertRaises(IntegrityError):
            dbqual.reject(self.image.id,

                          Rejectreason(id=666666, description="foobar"),