test_Relationship.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_Relationship.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 module Relationship"""

from __future__ import nested_scopes

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

from Modeling import ModelSet,Relationship
import StoreEmployees

# load the model
# this is not done in setUp() since that method is triggered for each tests
# (this would slow down the tests dramatically)
if 'GlobalSetUp':
  modelSet=ModelSet.ModelSet()
  from os import getcwd,path
  xmlmodelPath=path.join(getcwd(),'xmlmodels/model_testBothSidesRels.xml')
  modelSet.addModelFromXML({'file': xmlmodelPath})
  model=modelSet.modelNamed('testBothSidesRels')

class TestRelationship(unittest.TestCase):
  "Tests the relationships"

  def test_00_equality_n_inequality(self):
    "[Relationship] __eq__ and __ne__"
    # we make sure that '==' and '!=' operators behaves as expected
    # There was a bug in FlattenedRelationship.__eq__: it was always returning
    # 0 whatever the argument was. This was not detected by python v2.1 and
    # v2.2 both of which were defaulting to identity comparison for '!='
    # when __ne__ was undefined (despite the fact that __eq__ was defined).
    # python2.3 operator '!=' takes '__eq__' into account and that revealed
    # both the bug and the fact that py2.1 and 2.2 rely on __cmp__ instead of
    # __eq__    
    rel_ab=model.entityNamed('A').relationshipNamed('toB')
    rel_ac=model.entityNamed('A').relationshipNamed('toCs')

    import copy
    if sys.version_info < (2,3):
      # simulates copy, because it fails if ZODB is installed (because of
      # extension class)
      rel_abb=Relationship.SimpleRelationship('toB')
      for k in rel_ab.__dict__.keys():
        setattr(rel_abb, k, getattr(rel_ab, k))
    else:
      rel_abb = copy.copy(rel_ab)

    self.assertNotEqual(id(rel_ab), id(rel_abb))
    self.failUnless(rel_ab==rel_ab, "rel==rel fails")
    self.failUnless(rel_ab==rel_abb, "rel==copy(rel) fails")
    self.failUnless(not rel_ab!=rel_ab, "rel!=rel fails")
    self.failUnless(not rel_ab!=rel_abb, "rel!=copy(rel) fails")
    self.failUnless(rel_ab!=rel_ac, "not rel1!=rel2 when rel1!=rel2 !!!")    
    self.failUnless(not rel_ab==rel_ac, "rel1==rel2 when rel1!=rel2 !!!")    
    
  def test_01_flattenedRelationship(self):
    "[Relationship] flattened"
    f = model.entityNamed('F')
    rel_af = model.entityNamed('A').relationshipNamed('toFs')
    rel_a_af = model.entityNamed('A').relationshipNamed('toA_Fs')
    rel_af_f = model.entityNamed('A_F').relationshipNamed('toF')
    self.assertEqual(rel_af.componentRelationships(), (rel_a_af, rel_af_f))
    self.assertEqual(rel_af.destinationEntity(), f)

  def test_02_equality(self):
    "[Relationship] equality"
    rel_ab=model.entityNamed('A').relationshipNamed('toB')
    rel_ac=model.entityNamed('A').relationshipNamed('toCs')
    self.assertEqual(rel_ab, rel_ab, "rel==rel fails")
    self.assertNotEqual(rel_ab, rel_ac, "rel1==rel2 when rel1!=rel2 !!!")

#  def test_hasInverseRelationship(self):
#    "Tests hasInverseRelationship"
#    self.failUnless(ab.hasInverseRelationship(), "for ab")
#    self.failUnless(ac.hasInverseRelationship(), "for ac")
#    self.failUnless(ba.hasInverseRelationship(), "for ba")
#    self.failUnless(ca.hasInverseRelationship(), "for ca")
#    self.failIf(bc.hasInverseRelationship(), "for bc")

  def test_03_inverseRelationship(self):
    "[Relationship] inverseRelationship"
    rel_ab =model.entityNamed('A').relationshipNamed('toB')
    rel_a1b=model.entityNamed('A1').relationshipNamed('toB')
    rel_ac =model.entityNamed('A').relationshipNamed('toCs')
    rel_ba =model.entityNamed('B').relationshipNamed('toA')
    rel_ba1=model.entityNamed('B').relationshipNamed('toA1')
    rel_ca =model.entityNamed('C').relationshipNamed('toA')
    rel_da =model.entityNamed('D').relationshipNamed('toAs')
    rel_af = model.entityNamed('A').relationshipNamed('toFs')
    rel_fa = model.entityNamed('F').relationshipNamed('toAs')

    # a->b and b->a are not detected as reciprocical in the model
    self.assertEqual(rel_ab.inverseRelationship(), None, "for ab")
    self.assertEqual(rel_ba.inverseRelationship(), None, "for ba")

    # all the remaining rels. should be detected as reciprocical
    self.assertEqual(rel_a1b.inverseRelationship(), rel_ba1, "for a1b")
    self.assertEqual(rel_ac.inverseRelationship(), rel_ca, "for ac")
    self.assertEqual(rel_ba1.inverseRelationship(), rel_a1b, "for ba1")
    self.assertEqual(rel_ca.inverseRelationship(), rel_ac, "for ca")
    self.assertEqual(rel_da.inverseRelationship(), None, "for da")
    # .. flattened
    self.assertEqual(rel_fa.inverseRelationship(), rel_af, "for fa")
    self.assertEqual(rel_af.inverseRelationship(), rel_fa, "for af")

  def test_04_setName(self):
    "[Relationship] setName should only accept ascii chars"
    rel_ab=model.entityNamed('A').relationshipNamed('toB')
    self.assertRaises(ValueError, rel_ab.setName, ("lephant",))
    self.assertRaises(ValueError, rel_ab.setName, ("Elphant",))
    try:
      rel_ab.setName('Elephant')
    except:
      self.fail()

  def test_05_validateValue(self):
    "[Relationship] validateValue"
    class A:
      def entityName(self):
        return "A"
      def isFault(self):
        return 0
    d = model.entityNamed('D')
    rel_da = d.relationshipNamed('toAs')
    rel_da.validateValue((A(), A(), A()))
    rel_da.validateValue([A(), A(), A()])



class TestRelationship_n_Inheritance(unittest.TestCase):
  "Tests the relationships in inheritance "
  model=ModelSet.defaultModelSet().modelNamed('StoreEmployees')
  employee=model.entityNamed('Employee')
  executive=model.entityNamed('Executive')
  salesclerk=model.entityNamed('SalesClerk')
  address=model.entityNamed('Address')
  holidays=model.entityNamed('Holidays')
  
  def test_01_inverseRelationship(self):
    "[Relationship/Inheritance] inverseRelationship"
    ## Add PersonalAddress, stupid subentity for Address
    from Modeling.Entity import externalNameForInternalName
    paddress=self.address.clone('PersonalAddress', model)
    self.address.addSubEntity(paddress)
    paddress.setClassName('AuthorBooks.PersonalAddress') # does not exist
    paddress.setExternalName(externalNameForInternalName('PersonalAddress'))

    ###
    self.perso_address=model.entityNamed('PersonalAddress')

    emp_toAddr=self.employee.relationshipNamed('toAddresses')
    ex_toAddr=self.executive.relationshipNamed('toAddresses')
    sc_toAddr=self.salesclerk.relationshipNamed('toAddresses')

    addr_toEmp=self.address.relationshipNamed('toEmployee')
    paddr_toEmp=self.perso_address.relationshipNamed('toEmployee')
    
    emp_toAddr_inverse=emp_toAddr.inverseRelationship()
    self.failUnless(emp_toAddr_inverse)
    self.assertEqual(ex_toAddr.inverseRelationship(), emp_toAddr_inverse)
    self.assertEqual(sc_toAddr.inverseRelationship(), emp_toAddr_inverse)

    addr_toEmp_inverse=addr_toEmp.inverseRelationship()
    paddr_toEmp_inverse=paddr_toEmp.inverseRelationship()
    self.assertEqual(addr_toEmp_inverse, emp_toAddr)
    self.assertEqual(paddr_toEmp_inverse, emp_toAddr)
    
  def test_02_anyInverseRelationship(self):
    "[Relationship/Inheritance] anyInverseRelationship"
    emp_toAddr=self.employee.relationshipNamed('toAddresses')
    ex_toAddr=self.executive.relationshipNamed('toAddresses')
    sc_toAddr=self.salesclerk.relationshipNamed('toAddresses')

    emp_holidays=self.employee.relationshipNamed('holidays')
    ex_holidays=self.executive.relationshipNamed('holidays')
    sc_holidays=self.salesclerk.relationshipNamed('holidays')

    # relationship 'toAddresses' has inverse defined in the model
    self.assertEqual(emp_toAddr.anyInverseRelationship(),
                     emp_toAddr.inverseRelationship())
    self.assertEqual(ex_toAddr.anyInverseRelationship(),
                     ex_toAddr.inverseRelationship())
    self.assertEqual(sc_toAddr.anyInverseRelationship(),
                     sc_toAddr.inverseRelationship())
    # relationships 'toHolidays' has no inverse defined in the model
    self.assertEqual(emp_holidays.inverseRelationship(), None)

    def check_inverse(inverse):
      self.failIf(inverse is None)
      self.failIf(inverse in self.holidays.relationships())
      self.assertEqual(inverse.entity(), self.holidays)
      self.failUnless(inverse.isToOne())
      self.assertEqual(inverse.entity(), self.holidays)
      self.assertEqual(inverse.destinationEntity(), self.employee)
      self.assertEqual(inverse.inverseRelationship(), emp_holidays)

    any_inv_emp_holidays=emp_holidays.anyInverseRelationship()
    check_inverse(any_inv_emp_holidays)

    any_inv_ex_holidays=ex_holidays.anyInverseRelationship()
    check_inverse(any_inv_ex_holidays)

    any_inv_sc_holidays=sc_holidays.anyInverseRelationship()
    check_inverse(any_inv_sc_holidays)


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestRelationship, "test_"))
    suite.addTest(unittest.makeSuite(TestRelationship_n_Inheritance, "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.