test_namespaces.py :  » Database » PyTable » pytable-0.8.20a » pytable » tests » 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 » PyTable 
PyTable » pytable 0.8.20a » pytable » tests » test_namespaces.py
from pytable.schemabuilder import *
from pytable import dbschema,sqlgeneration,sqlquery,dbspecifier,specifierfromoptions
import unittest

testSpec = specifierfromoptions.specifierFromOptions()

driver,connection = testSpec.connect()
if driver.capabilities.schemaSupport:
  cats = database(
    "cats",
    namespaces = [
      namespace(
        name = "eft",
      )
    ],
    tables = [
      table(
        "eft.ownersforeignkey",
        (
          field( "owner_id", "integer", 0, "Unique owner identifier",
            constraints = (notNull(),primary()),
          ),
          field( "owner_name", "text", 0, """Owner's first name""",
            constraints = (notNull(),),
          ),
        ),
        """Table for storing owner information""",
        friendlyNameField = "owner_name",
        defaultRecords = [
          { "owner_id":1, "owner_name":"Tim" },
          { "owner_id":2, "owner_name":"John" },
          { "owner_id":3, "owner_name":"Jerry" },
          { "owner_id":4, "owner_name":"Mike" },
        ],
      ),
      table(
        "eft.dogsforeignkey",
        (
          field("dog_id", "integer", 0, "Unique dog identifier",
            constraints = (notNull(),primary()),
          ),
          field(
            "dog_name", "varchar", 255, "The dog's name",
            constraints = (notNull(),),
          ),
        ),
        """Primary table for storing critical info about cats""",
        defaultRecords = [
          {'dog_id':0, "dog_name":"Felix2"},
          {'dog_id':1, "dog_name":"Ginger2"},
          {'dog_id':2, "dog_name":"Robusta2"},
        ],
      ),
      table(
        "eft.catsforeignkey",
        (
          field("cat_id", "integer", 0, "Unique cat identifier",
            constraints = (notNull(),primary()),
          ),
          field(
            "cat_name", "varchar", 255, "The cat's name",
            constraints = (notNull(),),
          ),
          field( "declawed", "bool", 0, "Poor lil' kitty"),
          field( "owner", "integer", 0, "Owner of the kitty",
               constraints = (notNull(), foreignKey(
                 foreignTable = "eft.ownersforeignkey",
                 foreignFields = "owner_id",
              )),
          ),
          field( "chasedby", "integer", 0, "Kitty's exercise maven",
               constraints = [ foreignKey(
                 foreignTable = "eft.dogsforeignkey",
                 foreignFields = "dog_id",
              
              )],
          ),
          
        ),
        """Primary table for storing critical info about cats""",
        defaultRecords = [
          {'cat_id':0, "cat_name":"Felix", "declawed":'t', "owner":1 },
          {'cat_id':1, "cat_name":"Ginger", "declawed":'f', "owner":2 },
          {'cat_id':2, "cat_name":"Robusta", "declawed":'t', "owner":3 },
        ],
      ),
    ],
  )

  cats.resolve()

  class NamespaceTest( unittest.TestCase ):
    def setUp( self ):
      """Setup the schema in the database"""
      driver,connection = testSpec.connect()
      generator = sqlgeneration.SQLDropStatements(driver)
      sql = generator( cats, cascade=1 )
      for q in sql:
        query= sqlquery.SQLQuery(
          sql = q,
        )
        try:
          query( connection )
          connection.commit()
        except Exception, err:
          connection.rollback()
      generator = sqlgeneration.SQLCreateStatements(driver)
      query= sqlquery.SQLQuery(
        sql = generator( cats ),
      )
      query( connection )
      connection.commit()
    def testLookup( self ):
      """Do name lookups work for foreign keys?"""
      table = cats.lookupName( 'eft.catsforeignkey' )
      field = table.lookupName( 'chasedby' )
      other = cats.lookupName( field.foreign().foreignTable )
    def testInsert( self ):
      """Can we properly insert an instance of a table in a namespace?"""
      driver,connection = testSpec.connect()
      owner = cats.lookupName( "eft.ownersforeignkey" )
      instance = owner.itemClass(
        owner_id = 30,
        owner_name = 'Wanda',
      )
      instance.insertQuery( connection )
      instance.refreshQuery( connection )
      instance.deleteQuery( connection )
      connection.rollback()
    def testTableClassName( self ):
      """Is the table's class-name Python-compatible?"""
      owner = cats.lookupName( "eft.ownersforeignkey" )
      assert owner.itemClass.__name__ == 'eft_ownersforeignkey', owner.itemClass.__name__
    def testListing( self ):
      """Can we list the namespaces from the driver?"""
      driver,connection = testSpec.connect()
      result = driver.listNamespaces( connection )
      expected = ['eft']
      assert result == expected, """Don't seem to have retrieved the namespace list properly, should be %r, was %r"""%(expected, result)
    def testTableListing( self ):
      """Can we list the namespace'd tables from the driver?"""
      driver,connection = testSpec.connect()
      result = driver.listNamespaceTables( connection )
      expected = [
        ('eft','catsforeignkey'),
        ('eft','dogsforeignkey'),
        ('eft','ownersforeignkey'),
      ]
      assert result == expected, """Don't seem to have retrieved the namespace table list properly, should be %r, was %r"""%(expected, result)
  
  
if __name__ == "__main__":
  unittest.main ()
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.