# Path: template_maker/generator/models.py
# class DocumentBase(Model):
#     '''
#     Metadata about a new document, relates back to
#     a template
#     '''
# 
#     __tablename__ = 'document_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     name = Column(db.String(255))
#     # published = Column(db.Boolean, default=False)
#     template_id = ReferenceCol('template_base', ondelete='CASCADE')
# 
#     def __init__(self, created_at, updated_at, name, template_id):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.name = name
#         self.template_id = template_id
# 
# class DocumentPlaceholder(Model):
#     '''
#     A document's placeholder's value. Relates to the
#     document and the placeholder
#     '''
# 
#     __tablename__ = 'document_placeholder'
#     id = Column(db.Integer, primary_key=True)
#     document_id = ReferenceCol('document_base')
#     placeholder_id = ReferenceCol('template_placeholders', ondelete='CASCADE')
#     value = Column(db.Text)
# 
#     def __init__(self, document_id, placeholder_id):
#         self.document_id = document_id
#         self.placeholder_id = placeholder_id
# 
# Path: template_maker/generator/views.py
# def create_rivets_bindings(placeholder, section_text):
#     '''
#     Converts a placeholder into a <span> that rivets can grab onto
#     '''
#     repl_text = '<input id="{placeholder_display_name}" placeholder="{placeholder_name}"'.format(
#         placeholder_name=placeholder.display_name,
#         placeholder_display_name=placeholder.display_name
#     ) + \
#     ' name="{placeholder_name}"'.format(placeholder_name=placeholder.display_name) + \
#     ' class="' + generate_class(placeholder) + '" rv-value="template.placeholder_{idcombo}"'.format(
#         idcombo='_'.join(strip_tags(placeholder.display_name).split())
#     ) + \
#     'value="{placeholder_value}">'.format(placeholder_value=placeholder.value)
#     new_text = re.sub(re.escape(placeholder.full_name), repl_text, section_text)
#     return new_text
# 
# Path: template_maker/data/documents.py
# def get_document_placeholders(document_id):
#     '''
#     Gets all the placeholders associated with a document
#     '''
#     return db.session.query(
#         DocumentPlaceholder.id, TemplatePlaceholders.full_name, TemplatePlaceholders.type,
#         TemplatePlaceholders.display_name, DocumentPlaceholder.value
#     ).filter(DocumentPlaceholder.document_id==document_id).filter(
#         DocumentPlaceholder.placeholder_id==TemplatePlaceholders.id
#     ).all()
# 
# Path: template_maker_test/unit/util.py
# 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
# 
# def insert_new_document(template_id, name='test document'):
#     now = datetime.datetime.utcnow()
#     document = DocumentBase(
#         created_at=now,
#         updated_at=now,
#         name=name,
#         template_id=template_id
#     )
#     db.session.add(document)
#     db.session.commit()
#     return document
# 
# def create_document_with_placeholders():
#     insert_placeholder_type()
#     template = insert_new_template()
#     placeholder = insert_new_placeholder()
#     placeholder.type = 1
#     template.section_order = [placeholder.section_id]
#     document = insert_new_document(template.id)
#     set_document_placeholders(template.id, document)
#     return document
# 
# def insert_new_placeholder(section_id=None, template_id=1, full_name='[[BAR||BAZ]]', display_name='[[BAZ]]'):
#     if len(TemplateSection.query.all()) == 0 or section_id is None:
#         section = insert_new_section('foo', 'bar')
#         section_id = section.id
#     placeholder = create_a_placeholder(section_id, template_id=template_id, full_name=full_name, display_name=display_name)
#     db.session.add(placeholder)
#     db.session.commit()
#     return placeholder
# 
# def insert_placeholder_type():
#     db.session.execute('''INSERT INTO placeholder_types VALUES (1, 'unicode')''')
# 
# 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


from template_maker.generator.models import DocumentBase, DocumentPlaceholder
from template_maker.generator.views import create_rivets_bindings
from template_maker.data.documents import get_document_placeholders
from template_maker_test.unit.util import (
    insert_new_template, insert_new_document, create_document_with_placeholders,
    insert_new_placeholder, insert_placeholder_type
)
from template_maker_test.unit.test_base import BaseTestCase

class TestGenerator(BaseTestCase):
    render_templates = False
    def test_list_view(self):
        # should only show published templates
        insert_new_template(published=True)
        insert_new_template(published=False)

        response = self.client.get('/generate/')
        assert response.status_code == 200

# Path: template_maker/generator/models.py
# class DocumentBase(Model):
#     '''
#     Metadata about a new document, relates back to
#     a template
#     '''
# 
#     __tablename__ = 'document_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     name = Column(db.String(255))
#     # published = Column(db.Boolean, default=False)
#     template_id = ReferenceCol('template_base', ondelete='CASCADE')
# 
#     def __init__(self, created_at, updated_at, name, template_id):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.name = name
#         self.template_id = template_id
# 
# class DocumentPlaceholder(Model):
#     '''
#     A document's placeholder's value. Relates to the
#     document and the placeholder
#     '''
# 
#     __tablename__ = 'document_placeholder'
#     id = Column(db.Integer, primary_key=True)
#     document_id = ReferenceCol('document_base')
#     placeholder_id = ReferenceCol('template_placeholders', ondelete='CASCADE')
#     value = Column(db.Text)
# 
#     def __init__(self, document_id, placeholder_id):
#         self.document_id = document_id
#         self.placeholder_id = placeholder_id
# 
# Path: template_maker/generator/views.py
# def create_rivets_bindings(placeholder, section_text):
#     '''
#     Converts a placeholder into a <span> that rivets can grab onto
#     '''
#     repl_text = '<input id="{placeholder_display_name}" placeholder="{placeholder_name}"'.format(
#         placeholder_name=placeholder.display_name,
#         placeholder_display_name=placeholder.display_name
#     ) + \
#     ' name="{placeholder_name}"'.format(placeholder_name=placeholder.display_name) + \
#     ' class="' + generate_class(placeholder) + '" rv-value="template.placeholder_{idcombo}"'.format(
#         idcombo='_'.join(strip_tags(placeholder.display_name).split())
#     ) + \
#     'value="{placeholder_value}">'.format(placeholder_value=placeholder.value)
#     new_text = re.sub(re.escape(placeholder.full_name), repl_text, section_text)
#     return new_text
# 
# Path: template_maker/data/documents.py
# def get_document_placeholders(document_id):
#     '''
#     Gets all the placeholders associated with a document
#     '''
#     return db.session.query(
#         DocumentPlaceholder.id, TemplatePlaceholders.full_name, TemplatePlaceholders.type,
#         TemplatePlaceholders.display_name, DocumentPlaceholder.value
#     ).filter(DocumentPlaceholder.document_id==document_id).filter(
#         DocumentPlaceholder.placeholder_id==TemplatePlaceholders.id
#     ).all()
# 
# Path: template_maker_test/unit/util.py
# 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
# 
# def insert_new_document(template_id, name='test document'):
#     now = datetime.datetime.utcnow()
#     document = DocumentBase(
#         created_at=now,
#         updated_at=now,
#         name=name,
#         template_id=template_id
#     )
#     db.session.add(document)
#     db.session.commit()
#     return document
# 
# def create_document_with_placeholders():
#     insert_placeholder_type()
#     template = insert_new_template()
#     placeholder = insert_new_placeholder()
#     placeholder.type = 1
#     template.section_order = [placeholder.section_id]
#     document = insert_new_document(template.id)
#     set_document_placeholders(template.id, document)
#     return document
# 
# def insert_new_placeholder(section_id=None, template_id=1, full_name='[[BAR||BAZ]]', display_name='[[BAZ]]'):
#     if len(TemplateSection.query.all()) == 0 or section_id is None:
#         section = insert_new_section('foo', 'bar')
#         section_id = section.id
#     placeholder = create_a_placeholder(section_id, template_id=template_id, full_name=full_name, display_name=display_name)
#     db.session.add(placeholder)
#     db.session.commit()
#     return placeholder
# 
# def insert_placeholder_type():
#     db.session.execute('''INSERT INTO placeholder_types VALUES (1, 'unicode')''')
# 
# 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_test/unit/generator/test_generator_views.py
from template_maker.generator.models import DocumentBase, DocumentPlaceholder
from template_maker.generator.views import create_rivets_bindings
from template_maker.data.documents import get_document_placeholders
from template_maker_test.unit.util import (
    insert_new_template, insert_new_document, create_document_with_placeholders,
    insert_new_placeholder, insert_placeholder_type
)
from template_maker_test.unit.test_base import BaseTestCase


class TestGenerator(BaseTestCase):
    render_templates = False
    def test_list_view(self):
        # should only show published templates
        insert_new_template(published=True)
        insert_new_template(published=False)

        response = self.client.get('/generate/')
        assert response.status_code == 200

        self.assertEquals(len(self.get_context_variable('templates')), 1)