import sqlalchemy as sa
from sqlalchemy_utils import generates
from tests import TestCase


class GeneratesTestCase(TestCase):
    def test_generates_value_before_flush(self):
        article = self.Article()
        article.name = u'some article name'
        self.session.add(article)
        self.session.flush()
        assert article.slug == u'some-article-name'


class TestGeneratesWithBoundMethodAndClassVariableArg(GeneratesTestCase):
    def create_models(self):
        class Article(self.Base):
            __tablename__ = 'article'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            slug = sa.Column(sa.Unicode(255))

            @generates(slug)
            def _create_slug(self):
                return self.name.lower().replace(' ', '-')

        self.Article = Article


class TestGeneratesWithBoundMethodAndStringArg(GeneratesTestCase):
    def create_models(self):
        class Article(self.Base):
            __tablename__ = 'article'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            slug = sa.Column(sa.Unicode(255))

            @generates('slug')
            def _create_slug(self):
                return self.name.lower().replace(' ', '-')

        self.Article = Article


class TestGeneratesWithFunctionAndClassVariableArg(GeneratesTestCase):
    def create_models(self):
        class Article(self.Base):
            __tablename__ = 'article'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            slug = sa.Column(sa.Unicode(255))

        @generates(Article.slug)
        def _create_article_slug(self):
            return self.name.lower().replace(' ', '-')

        self.Article = Article



class TestGeneratesWithSourcePath(TestCase):
    def create_models(self):
        class Document(self.Base):
            __tablename__ = 'document'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            locale = sa.Column(sa.String(10))

        class Section(self.Base):
            __tablename__ = 'section'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            locale = sa.Column(sa.String(10))

            document_id = sa.Column(
                sa.Integer, sa.ForeignKey(Document.id)
            )

            document = sa.orm.relationship(Document)

            @generates(locale, source='document')
            def copy_locale(self, document):
                return document.locale

        class SubSection(self.Base):
            __tablename__ = 'subsection'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            locale = sa.Column(sa.String(10))

            section_id = sa.Column(
                sa.Integer, sa.ForeignKey(Section.id)
            )

            section = sa.orm.relationship(Section)

            @generates(locale, source='section.document')
            def copy_locale(self, document):
                return document.locale


        self.Document = Document
        self.Section = Section
        self.SubSection = SubSection

    def test_simple_source_paths(self):
        document = self.Document(name=u'Document 1', locale='en')
        section = self.Section(name=u'Section 1', document=document)

        self.session.add(document)
        self.session.add(section)
        self.session.commit()

        assert section.locale == 'en'

@@ -3276,16 +3276,23 @@
 _simple_
+dotted_
 source_p
@@ -3294,17 +3294,16 @@
 rce_path
-s
 (self):%0A
@@ -3538,16 +3538,16 @@
 mmit()%0A%0A
-
         
@@ -3576,8 +3576,371 @@
 == 'en'%0A
+%0A    def test_deep_dotted_source_path(self):%0A        document = self.Document(name=u'Document 1', locale='en')%0A        section = self.Section(name=u'Section 1', document=document)%0A        subsection = self.SubSection(name=u'Section 1', section=section)%0A%0A        self.session.add(subsection)%0A        self.session.commit()%0A%0A        assert subsection.locale == 'en'%0A

Add more tests for generates function