test_zblog.py :  » Database » SQLAlchemy » SQLAlchemy-0.6.0 » test » zblog » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLAlchemy 
SQLAlchemy » SQLAlchemy 0.6.0 » test » zblog » test_zblog.py
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.test import *
from test.zblog import mappers,tables
from test.zblog.user import *
from test.zblog.blog import *


class ZBlogTest(TestBase, AssertsExecutionResults):

    @classmethod
    def create_tables(cls):
        tables.metadata.drop_all(bind=testing.db)
        tables.metadata.create_all(bind=testing.db)
    
    @classmethod
    def drop_tables(cls):
        tables.metadata.drop_all(bind=testing.db)

    @classmethod
    def setup_class(cls):
        cls.create_tables()
    @classmethod
    def teardown_class(cls):
        cls.drop_tables()
    def teardown(self):
        pass
    def setup(self):
        pass


class SavePostTest(ZBlogTest):
    @classmethod
    def setup_class(cls):
        super(SavePostTest, cls).setup_class()
        
        mappers.zblog_mappers()
        global blog_id, user_id
        s = create_session(bind=testing.db)
        user = User('zbloguser', "Zblog User", "hello", group=administrator)
        blog = Blog(owner=user)
        blog.name = "this is a blog"
        s.add(user)
        s.add(blog)
        s.flush()
        blog_id = blog.id
        user_id = user.id
        s.close()

    @classmethod
    def teardown_class(cls):
        clear_mappers()
        super(SavePostTest, cls).teardown_class()

    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=testing.db)

        s.begin()
        try:
            blog = s.query(Blog).get(blog_id)
            post = Post(headline="asdf asdf", summary="asdfasfd")
            s.add(post)
            post.blog_id=blog_id
            post.blog = blog
            assert post in blog.posts
        finally:
            s.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=testing.db)

        s.begin()
        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.add(post)
            s.flush()
            s.expunge_all()

            user = s.query(User).get(user_id)
            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.expunge_all()

            assert s.query(Post).get(post.id) is not None

        finally:
            s.rollback()


www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.