#! /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)
|