from sqlalchemy import Column, Integer, String, Text, ForeignKey
from sqlalchemy import Table
from sqlalchemy.orm import relationship

from database import Base, DeclEnum

regressionTestCategoryLinkTable = Table(
    'regression_test_category', Base.metadata,
    Column('regression_id', Integer, ForeignKey(
        'regression_test.id', onupdate='CASCADE', ondelete='RESTRICT')),
    Column('category_id', Integer, ForeignKey(
        'category.id', onupdate='CASCADE', ondelete='RESTRICT'))
)


class Category(Base):
    __tablename__ = 'category'
    __table_args__ = {'mysql_engine': 'InnoDB'}
    id = Column(Integer, primary_key=True)
    name = Column(String(64), unique=True)
    description = Column(Text(), nullable=False)
    regression_tests = relationship(
        'RegressionTest', secondary=regressionTestCategoryLinkTable,
        back_populates='categories')

    def __init__(self, name, description):
        self.name = name
        self.description = description

    def __repr__(self):
        return '<Category %r>' % self.name


class InputType(DeclEnum):
    file = "file", "File"
    stdin = "stdin", "Stdin"
    udp = "udp", "UDP"


class OutputType(DeclEnum):
    file = "file", "File"
    null = "null", "Null"
    tcp = "tcp", "TCP"
    cea708 = "cea708", "CEA-708"
    multi_program = "multiprogram", "Multi-program"
    stdout = "stdout", "Stdout"
    report = "report", "Report"


class RegressionTest(Base):
    __tablename__ = 'regression_test'
    __table_args__ = {'mysql_engine': 'InnoDB'}
    id = Column(Integer, primary_key=True)
    sample_id = Column(Integer, ForeignKey('sample.id', onupdate='CASCADE',
                                           ondelete='CASCADE'))
    sample = relationship('Sample', uselist=False, back_populates='tests')
    command = Column(Text(), nullable=False)
    input_type = Column(InputType.db_type())
    output_type = Column(OutputType.db_type())
    categories = relationship(
        'Category', secondary=regressionTestCategoryLinkTable,
        back_populates='regression_tests')

    def __init__(self, sample_id, command, input_type, output_type,
                 category_id):
        self.sample_id = sample_id
        self.command = command
        self.input_type = input_type
        self.output_type = output_type
        self.category_id = category_id

    def __repr__(self):
        return '<RegressionTest %r>' % self.id

@@ -1,28 +1,59 @@
+from sqlalchemy import Boolean%0A
 from sqlalchemy import Colum
@@ -2085,16 +2085,138 @@
 _tests')
+%0A    output_files = relationship('RegressionTestOutput',%0A                                back_populates='regression_test')
 %0A%0A    de
@@ -2561,8 +2561,992 @@
 self.id%0A
+%0A%0Aclass RegressionTestOutput(Base):%0A    __tablename__ = 'regression_test_output'%0A    __table_args__ = %7B'mysql_engine': 'InnoDB'%7D%0A    id = Column(Integer, primary_key=True)%0A    regression_id = Column(Integer, ForeignKey(%0A        'regression_test.id', onupdate='CASCADE', ondelete='RESTRICT'))%0A    regression_test = relationship('RegressionTest',%0A                                   back_populates='output_files')%0A    correct = Column(Text())%0A    correct_extension = Column(String(64), nullable=False)  # contains the .%0A    expected_filename = Column(Text())%0A    ignore = Column(Boolean())%0A%0A    def __init__(self, regression_id, correct, correct_extension,%0A                 expected_filename, ignore=False):%0A        self.sample_id = regression_id%0A        self.correct = correct%0A        self.correct_extension = correct_extension%0A        self.expected_filename = expected_filename%0A        self.ignore = ignore%0A%0A    def __repr__(self):%0A        return '%3CRegressionTestOutput %25r%3E' %25 self.id%0A

Add model for regression output tests