test_SchemaGeneration.py :  » Database » Modeling-Framework » Modeling-0.9 » Modeling » 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 » Modeling Framework 
Modeling Framework » Modeling 0.9 » Modeling » tests » test_SchemaGeneration.py
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------

"""
Tests for SchemaGeneration

  Based on the PostgresAdaptorLayer

  CVS information

    $Id: test_SchemaGeneration.py 932 2004-07-20 06:21:57Z sbigaret $
"""
__version__='$Revision: 932 $'[11:-2]

import unittest, string

if __name__ == "__main__":
  import utils, sys
  utils.fixpath()

from Modeling.SchemaGeneration import SchemaGeneration,\
     CreateDatabaseKey, DropDatabaseKey, CreateTablesKey, DropTablesKey, \
     CreatePrimaryKeySupportKey, DropPrimaryKeySupportKey,\
     ForeignKeyConstraintsKey, PrimaryKeyConstraintsKey

from Modeling.Adaptor import Adaptor

# load the model
# this is not done in setUp() since that method is triggered for each tests
# (this would slow down the tests dramatically)
from Modeling import ModelSet
from Modeling.Model import ModelError
if 'GlobalSetUp':
  from os import getcwd,path
  xmlmodelPath=path.join(getcwd(),'xmlmodels/model_simpleModel1.xml')
  # we have no special needs to use the ModelSet.defaultModelSet() here,
  # hence we declare our own modelSet ; this also avoids entityName collision
  # with other models declared/used elsewhere (yes, the entities' names in
  # test-models are not very significative...)
  modelSet=ModelSet.ModelSet()
  modelSet.addModelFromXML({'file': xmlmodelPath})
  model=modelSet.modelNamed('simpleModel1')
  try:
    import psycopg
  except ImportError:
    # fake psycopg, we do not really need it except for successfully
    # importing the PostgresqlAdaptor
    import sys
    sys.modules['psycopg']=1
  pgAdaptor=Adaptor.adaptorWithModel(model)

class TestSchemaGeneration(unittest.TestCase):
  "Tests the Schema Generation -- based on the postgresql adaptor layer"
  def setUp(self):
    global model, pgAdaptor
    self.schemaGeneration = pgAdaptor.schemaGenerationFactory()
    
  def test_01_createTableStatementForEntityGroup(self):
    "[SchemaGeneration] createTableStatementForEntityGroup"
    find=string.find
    ref_A_statements=('CREATE TABLE A (',
                      'PK_A INTEGER NOT NULL',
                      'FK_B_id INTEGER',
                      'A1 VARCHAR(20) NOT NULL')
    ref_B_statements=('CREATE TABLE B (',
                      '  B1 VARCHAR(20) NOT NULL',
                      '  PK_B INTEGER NOT NULL')
    generatedAstatement=self.schemaGeneration.createTableStatementsForEntityGroup([model.entityNamed('A')])[0].statement()
    generatedBstatement=self.schemaGeneration.createTableStatementsForEntityGroup([model.entityNamed('B')])[0].statement()
    for statement in ref_A_statements:
      self.failUnless(find(generatedAstatement, statement)!=-1,
                      'Statement %s was not found'%statement)
    for statement in ref_B_statements:
      self.failUnless(find(generatedBstatement, statement)!=-1,
                      'Statement %s was not found'%statement)
  
  def test_02_primaryKeySupportStatementsForEntityGroup(self):
    "[SchemaGeneration] primaryKeySupportStatementsForEntityGroup"
    find=string.find
    ref_A_statements=('CREATE SEQUENCE PK_SEQ_A START 1',)
    ref_B_statements=('CREATE SEQUENCE PK_SEQ_B START 1',)

    generatedAstatement=self.schemaGeneration.primaryKeySupportStatementsForEntityGroup([model.entityNamed('A')])[0].statement()
    generatedBstatement=self.schemaGeneration.primaryKeySupportStatementsForEntityGroup([model.entityNamed('B')])[0].statement()

    for statement in ref_A_statements:
      self.failUnless(find(generatedAstatement, statement)!=-1,
                      'Statement %s was not found'%statement)
    for statement in ref_B_statements:
      self.failUnless(find(generatedBstatement, statement)!=-1,
                      'Statement %s was not found'%statement)
  
  def test_03_primaryKeyConstraintStatementsForEntityGroup(self):
    "[SchemaGeneration] primaryKeyConstraintStatementsForEntityGroup"
    find=string.find
    ref_A_statements=('ALTER TABLE A ADD PRIMARY KEY (PK_A)',)
    ref_B_statements=('ALTER TABLE B ADD PRIMARY KEY (PK_B)',)

    generatedAstatement=self.schemaGeneration.primaryKeyConstraintStatementsForEntityGroup([model.entityNamed('A')])[0].statement()
    generatedBstatement=self.schemaGeneration.primaryKeyConstraintStatementsForEntityGroup([model.entityNamed('B')])[0].statement()

    for statement in ref_A_statements:
      self.failUnless(find(generatedAstatement, statement)!=-1,
                      'Statement %s was not found'%statement)
    for statement in ref_B_statements:
      self.failUnless(find(generatedBstatement, statement)!=-1,
                      'Statement %s was not found'%statement)
  
  def test_04_foreignKeyConstraintStatementsForRelationship(self):
    "[SchemaGeneration] foreignKeyConstraintStatementsForRelationship"
    find=string.find
    ref_FK_statements=('ALTER TABLE A ADD CONSTRAINT toB FOREIGN KEY (FK_B_id) REFERENCES B(PK_B)',)

    _rel=model.entityNamed('A').relationshipNamed('toB')
    generatedFKstatement=self.schemaGeneration.foreignKeyConstraintStatementsForRelationship(_rel)[0].statement()

    for statement in ref_FK_statements:
      self.failUnless(find(generatedFKstatement, statement)!=-1,
                      'Statement %s was not found'%statement)

  def test_04b_foreignKeyConstraintStatementsForRelationship(self):
    "[SchemaGeneration] fk constraints / bug #861048"
    model.entityNamed('B').addSubEntity(model.entityNamed('A'))
    _rel=model.entityNamed('A').relationshipNamed('toB')
    generatedFKstatement=self.schemaGeneration.foreignKeyConstraintStatementsForRelationship(_rel)

    try:
      self.failIf(generatedFKstatement)
    finally:
      model.entityNamed('B').removeSubEntity(model.entityNamed('A'))
  
  def test_05_tableEntityGroupsForEntities(self):
    "[SchemaGeneration] tableEntityGroupsForEntities"
    self.failUnless(len(self.schemaGeneration.tableEntityGroupsForEntities(model.entities()))==2)
    
  def _test_98_bothSides(self):
    xmlmodelPath=path.join(getcwd(),'xmlmodels/model_testBothSidesRels.xml')
    modelSet=ModelSet.ModelSet()
    modelSet.addModelFromXML({'file': xmlmodelPath})
    model=modelSet.modelNamed('testBothSidesRels')
    pgAdaptor=Adaptor.adaptorWithModel(model)
    schemaGeneration = pgAdaptor.schemaGenerationFactory()
    options={
      DropDatabaseKey            : 0,
      CreateDatabaseKey          : 0,
      DropTablesKey              : 1,
      CreateTablesKey            : 1,
      CreatePrimaryKeySupportKey : 1,
      DropPrimaryKeySupportKey   : 1,
      ForeignKeyConstraintsKey   : 1,
      PrimaryKeyConstraintsKey   : 1,
      }
    print '--'
    print schemaGeneration.schemaCreationScriptForEntities(model.entities(), options)
  def _test_99_misc(self):
    options={
      DropDatabaseKey            : 0,
      CreateDatabaseKey          : 0,
      DropTablesKey              : 1,
      CreateTablesKey            : 1,
      CreatePrimaryKeySupportKey : 1,
      DropPrimaryKeySupportKey   : 1,
      ForeignKeyConstraintsKey   : 1,
      PrimaryKeyConstraintsKey   : 1,
      }
    print '--'
    print self.schemaGeneration.schemaCreationScriptForEntities(model.entities(), options)



def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestSchemaGeneration, "test_"))
  return suite


if __name__ == "__main__":
    errs = utils.run_suite(test_suite())
    sys.exit(errs and 1 or 0)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.