from testbase import AssertMixin
import testbase
import unittest

db = testbase.db
from sqlalchemy import *

from zblog import mappers, tables
from zblog.user import *
from zblog.blog import *

class ZBlogTest(AssertMixin):

    def create_tables(self):
        tables.metadata.create_all(connectable=db)
    def drop_tables(self):
        tables.metadata.drop_all(connectable=db)
        
    def setUpAll(self):
        self.create_tables()
    def tearDownAll(self):
        self.drop_tables()
    def tearDown(self):
        pass
    def setUp(self):
        pass


class SavePostTest(ZBlogTest):
    def setUpAll(self):
        super(SavePostTest, self).setUpAll()
        mappers.zblog_mappers()
        global blog_id, user_id
        s = create_session(bind_to=db)
        user = User('zbloguser', "Zblog User", "hello", group=administrator)
        blog = Blog(owner=user)
        blog.name = "this is a blog"
        s.save(user)
        s.save(blog)
        s.flush()
        blog_id = blog.id
        user_id = user.id
        s.close()

    def tearDownAll(self):
        clear_mappers()
        super(SavePostTest, self).tearDownAll()
        
    def testattach(self):
        """test that a transient/pending instance has proper bi-directional behavior.
        
        this requires that lazy loaders do not fire off for a transient/pending instance."""
        s = create_session(bind_to=db)

        trans = s.create_transaction()
        try:
            blog = s.query(Blog).get(blog_id)
            post = Post(headline="asdf asdf", summary="asdfasfd")
            s.save(post)
            post.blog_id=blog_id
            post.blog = blog
            assert post in blog.posts
        finally:
            trans.rollback()
            
    def testoptimisticorphans(self):
        """test that instances in the session with un-loaded parents will not 
        get marked as "orphans" and then deleted """
        s = create_session(bind_to=db)
        
        trans = s.create_transaction()
        try:
            blog = s.query(Blog).get(blog_id)
            post = Post(headline="asdf asdf", summary="asdfasfd")
            post.blog = blog
            user = s.query(User).get(user_id)
            post.user = user
            s.save(post)
            s.flush()
            s.clear()

            blog = s.query(Blog).get(blog_id)
            post = blog.posts[0]
            comment = Comment(subject="some subject", body="some body")
            comment.post = post
            comment.user = user
            s.flush()
            s.clear()
            
            assert s.query(Post).get(post.id) is not None
            
        finally:
            trans.rollback()
        
            
if __name__ == "__main__":
    testbase.main()

        