test_GlobalID.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_GlobalID.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 GlobalID

  The EditingContext depends on the capability of GlobalIDs to be keys in
  dictionaries (for the uniquing table) --this is a strong requirement.

  I've already been bitten by a that bug: 'KeyGlobalID' 's '__eq__()' had a
  typo which causes a ValueError to be raised. However, even if this is
  obvious when directly called, this was silently swallowed by the dictionary
  when 'KeyGlobalID' objects were used as dict.'s keys (python v.2.1.1 and
  v.2.2.2)

  This led to EditingContexts unable to enforce the unicity of objects inside
  their owned graphs of objects. Hence, these apparently silly series of tests!

"""

import unittest
if __name__ == "__main__":
  import utils, sys
  utils.fixpath()
from Modeling.GlobalID import KeyGlobalID,TemporaryGlobalID

class TestTemporaryGlobalID(unittest.TestCase):
  """
  Tests for GlobalID.TemporaryGlobalID
  """
  def setUp(self):
    "Setup"
    self.g1=TemporaryGlobalID()
    self.g2=TemporaryGlobalID()
    self.g3=KeyGlobalID('Writer', {'id': 3})

  def test_01_eq(self):
    "[TemporaryGlobalID] __eq__"
    self.assertEqual(self.g1, self.g1)
    self.assertNotEqual(self.g1, self.g2)
    self.assertNotEqual(self.g1, None)
    self.assertNotEqual(self.g1, self.g3,
                        'TemporaryGlobalID and KeyGlobalID cannot be equal')
    
  def test_02_hash(self):
    "[TemporaryGlobalID] __eq__"
    self.assertNotEqual(hash(self.g1), hash(self.g2))

  def test_03_dictionaryTests(self):
    "[TemporaryGlobalID] dictionary test"
    d={}
    d[self.g1]=0
    d[self.g1]=1
    d[self.g2]=2
    self.failUnless(len(d)==2)
    self.failUnless(d[self.g1]==1)
    self.failUnless(d[self.g2]==2)
    
class TestKeyGlobalID(unittest.TestCase):
  """
  Tests for GlobalID.KeyGlobalID
  """
  def setUp(self):
    "Setup"
    self.g1=KeyGlobalID('Writer', {'id': 2})
    self.g2=KeyGlobalID('Writer', {'id': 2})
    self.g3=KeyGlobalID('Writer', {'id': 3})
    self.g4=KeyGlobalID('Writer', {'id': 3L})
    # both shouldn't be different, even if compound primary keys are NOT
    # supported for the moment.
    self.g5=KeyGlobalID('Writer', {'id': 3L, 'id2': 4})
    self.g6=KeyGlobalID('Writer', {'id2': 4L, 'id': 3})
    self.g7=KeyGlobalID('Writer', {'id': 4, 'id2': 3})
    
  def test_01_eq(self):
    "[KeyGlobalID] __eq__"
    self.assertNotEqual(self.g1, None)
    self.assertEqual(self.g1, self.g2)
    self.assertEqual(self.g3, self.g4)
    self.assertEqual(self.g5, self.g6)
    self.assertNotEqual(self.g5, self.g7)
    self.failIf(self.g1==self.g3)

  def test_02_hash(self):
    "[KeyGlobalID] __hash__"
    self.assertEqual(hash(self.g1),hash(self.g2))
    self.assertEqual(hash(self.g3), hash(self.g4))
    self.assertEqual(hash(self.g5), hash(self.g6))
    self.assertNotEqual(hash(self.g5), hash(self.g7))
    self.failIf(hash(self.g1)==hash(self.g3))
    
  def test_03_dictionaryTests(self):
    "[KeyGlobalID] dictionary test"
    d={}
    d[self.g1]=1
    d[self.g2]=2
    self.failUnless(len(d)==1)
    self.failUnless(d.get(self.g1)==2)

    d[self.g3]=3
    d[self.g4]=4
    self.failUnless(len(d)==2)
    self.failUnless(d.get(self.g3)==4)

    d[self.g5]=5
    d[self.g6]=6
    d[self.g7]=7
    self.failUnless(len(d)==4)
    self.failUnless(d.get(self.g5)==6)
    self.failUnless(d.get(self.g7)==7)
    
def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestTemporaryGlobalID, "test_"))
    suite.addTest(unittest.makeSuite(TestKeyGlobalID, "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.