# Path: template_maker_test/unit/test_base.py
# class BaseTestCase(TestCase):
#     '''
#     A base test case that boots our app
#     '''
#     def create_app(self):
#         return _create_app(TestingConfig)
# 
#     def setUp(self):
#         db.create_all()
# 
#     def tearDown(self):
#         db.session.remove()
#         db.drop_all()
#         db.get_engine(self.app).dispose()
# 
#     def assert_flashes(self, expected_message, expected_category='message'):
#         '''
#         Helper to test if we have flashes.
#         Taken from: http://blog.paulopoiati.com/2013/02/22/testing-flash-messages-in-flask/
#         '''
#         with self.client.session_transaction() as session:
#             try:
#                 category, message = session['_flashes'][0]
#             except KeyError:
#                 raise AssertionError('nothing flashed')
#             assert expected_message in message
#             assert expected_category == category
# 
# Path: template_maker/app.py
# def create_app(config_object=DevConfig):
# def register_blueprints(app):
# def register_extensions(app):
# 
# Path: template_maker/builder/models.py
# class TemplateBase(Model):
#     '''
#     Basic metadata about a template
# 
#     Fields:
#     - created_at: when the template was created
#     - updated_at: when the template metadata was last updated
#     - title: the displayed title or name of the template
#     - description: a freetext description of the template
#     '''
# 
#     __tablename__ = 'template_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_text = db.relationship('TemplateSection', cascade='all,delete', lazy='dynamic')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     published = Column(db.Boolean, default=False)
#     section_order = Column(ARRAY(db.Integer))
#     # created_by = ReferenceCol('users')
# 
#     def __init__(self, created_at, updated_at, title, description):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.title = title
#         self.description = description
# 
# class TemplateSection(Model):
#     '''
#     TemplateSection is a superclass of section information and maps to section types
# 
#     Fields:
#     - title: The name of the section
#     - description: The description of the section
#     - position: Where in the template the section lies
#     - template_id: Foreign Key relationship back to the TemplateBase
#     - section_type: The type of section to be used. This uses "joined
#     table inheritance": http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html
#     '''
# 
#     __tablename__ = 'template_section'
#     id = Column(db.Integer, primary_key=True)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_id = ReferenceCol('template_base')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     section_type = Column('section_type', db.String(50))
#     __mapper_args__ = {'polymorphic_on': section_type}
# 
#     def __init__(self, template_id=None, title=None, description=None):
#         self.template_id = template_id
#         self.title = title
#         self.description = description
# 
# Path: template_maker_test/unit/util.py
# def create_a_template(title='test'):
#     now = datetime.datetime.utcnow()
#     return TemplateBase(created_at=now, updated_at=now, title=title, description='test')
# 
# def create_a_text_section(title, text):
#     section = TextSection(template_id=1, title=title, description=None)
#     section.text = text
#     return section
# 
# def insert_new_template(title='test', published=False):
#     template = create_a_template(title)
#     template.published = published
#     db.session.add(template)
#     db.session.commit()
#     return template


from werkzeug.datastructures import ImmutableMultiDict
from template_maker_test.unit.test_base import BaseTestCase
from template_maker.app import db
from template_maker.builder.models import (
    TemplateBase, TemplateSection
)
from template_maker_test.unit.util import (
    create_a_template, create_a_text_section, insert_new_template
)
        # it should get the proper template
        response = self.client.get('/build/new')
        assert response.status_code == 200
        self.assert_template_used('builder/new.html')

        post = self.client.post('/build/new', data=dict(
            title='title',
            description = 'description'
        ))
        # it should successfully add to the db and redirect us
        # to the right section page
        self.assertEquals(len(TemplateBase.query.all()), 1)
        assert post.status_code == 302
        assert post.location == 'http://localhost/build/1/section/'

    def test_publish_template(self):
        template = create_a_template()
        db.session.add(template)
        db.session.commit()

        # assert that the right template is used
        self.client.get('/build/1/publish')
        self.assert_template_used('builder/preview.html')

        # assert that the post sets the publish flag to true
        post = self.client.post('/build/1/publish')
        template = TemplateBase.query.get(1)
        self.assertTrue(template.published)
        assert post.location == 'http://localhost/build/'


# Path: template_maker_test/unit/test_base.py
# class BaseTestCase(TestCase):
#     '''
#     A base test case that boots our app
#     '''
#     def create_app(self):
#         return _create_app(TestingConfig)
# 
#     def setUp(self):
#         db.create_all()
# 
#     def tearDown(self):
#         db.session.remove()
#         db.drop_all()
#         db.get_engine(self.app).dispose()
# 
#     def assert_flashes(self, expected_message, expected_category='message'):
#         '''
#         Helper to test if we have flashes.
#         Taken from: http://blog.paulopoiati.com/2013/02/22/testing-flash-messages-in-flask/
#         '''
#         with self.client.session_transaction() as session:
#             try:
#                 category, message = session['_flashes'][0]
#             except KeyError:
#                 raise AssertionError('nothing flashed')
#             assert expected_message in message
#             assert expected_category == category
# 
# Path: template_maker/app.py
# def create_app(config_object=DevConfig):
# def register_blueprints(app):
# def register_extensions(app):
# 
# Path: template_maker/builder/models.py
# class TemplateBase(Model):
#     '''
#     Basic metadata about a template
# 
#     Fields:
#     - created_at: when the template was created
#     - updated_at: when the template metadata was last updated
#     - title: the displayed title or name of the template
#     - description: a freetext description of the template
#     '''
# 
#     __tablename__ = 'template_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_text = db.relationship('TemplateSection', cascade='all,delete', lazy='dynamic')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     published = Column(db.Boolean, default=False)
#     section_order = Column(ARRAY(db.Integer))
#     # created_by = ReferenceCol('users')
# 
#     def __init__(self, created_at, updated_at, title, description):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.title = title
#         self.description = description
# 
# class TemplateSection(Model):
#     '''
#     TemplateSection is a superclass of section information and maps to section types
# 
#     Fields:
#     - title: The name of the section
#     - description: The description of the section
#     - position: Where in the template the section lies
#     - template_id: Foreign Key relationship back to the TemplateBase
#     - section_type: The type of section to be used. This uses "joined
#     table inheritance": http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html
#     '''
# 
#     __tablename__ = 'template_section'
#     id = Column(db.Integer, primary_key=True)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_id = ReferenceCol('template_base')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     section_type = Column('section_type', db.String(50))
#     __mapper_args__ = {'polymorphic_on': section_type}
# 
#     def __init__(self, template_id=None, title=None, description=None):
#         self.template_id = template_id
#         self.title = title
#         self.description = description
# 
# Path: template_maker_test/unit/util.py
# def create_a_template(title='test'):
#     now = datetime.datetime.utcnow()
#     return TemplateBase(created_at=now, updated_at=now, title=title, description='test')
# 
# def create_a_text_section(title, text):
#     section = TextSection(template_id=1, title=title, description=None)
#     section.text = text
#     return section
# 
# def insert_new_template(title='test', published=False):
#     template = create_a_template(title)
#     template.published = published
#     db.session.add(template)
#     db.session.commit()
#     return template


# Path: template_maker_test/unit/builder/test_builder_views.py
from werkzeug.datastructures import ImmutableMultiDict
from template_maker_test.unit.test_base import BaseTestCase
from template_maker.app import db
from template_maker.builder.models import (
    TemplateBase, TemplateSection
)
from template_maker_test.unit.util import (
    create_a_template, create_a_text_section, insert_new_template
)

        # it should get the proper template
        response = self.client.get('/build/new')
        assert response.status_code == 200
        self.assert_template_used('builder/new.html')

        post = self.client.post('/build/new', data=dict(
            title='title',
            description = 'description'
        ))
        # it should successfully add to the db and redirect us
        # to the right section page
        self.assertEquals(len(TemplateBase.query.all()), 1)
        assert post.status_code == 302
        assert post.location == 'http://localhost/build/1/section/'

    def test_publish_template(self):
        template = create_a_template()
        db.session.add(template)
        db.session.commit()

        # assert that the right template is used
        self.client.get('/build/1/publish')
        self.assert_template_used('builder/preview.html')

        # assert that the post sets the publish flag to true
        post = self.client.post('/build/1/publish')
        template = TemplateBase.query.get(1)
        self.assertTrue(template.published)
        assert post.location == 'http://localhost/build/'


class TestEditTemplate(BaseTestCase):