~ubuntu-branches/ubuntu/quantal/pytables/quantal

« back to all changes in this revision

Viewing changes to test/test_filenode.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2005-11-27 20:25:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127202534-l8jzyd8357krw40h
Tags: 1.1.1-1ubuntu1
* Sync with Debian:
  + Use python 2.4 as default

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#
3
3
#       License: BSD
4
4
#       Created: October 2, 2004
5
 
#       Author:  Ivan Vilata i Balaguer - reverse:net.selidor@ivan
 
5
#       Author:  Ivan Vilata i Balaguer - reverse:com.carabos@ivilata
6
6
#
7
 
#       $Source: /cvsroot/pytables/pytables/test/test_filenode.py,v $
8
 
#       $Id: test_filenode.py,v 1.3.2.5 2004/11/18 19:22:15 ivilata Exp $
 
7
#       $Source: /home/ivan/_/programari/pytables/svn/cvs/pytables/pytables/test/test_filenode.py,v $
 
8
#       $Id: test_filenode.py 1009 2005-06-15 13:39:15Z faltet $
9
9
#
10
10
########################################################################
11
11
 
17
17
import warnings
18
18
 
19
19
 
20
 
__revision__ = '$Id: test_filenode.py,v 1.3.2.5 2004/11/18 19:22:15 ivilata Exp $'
 
20
__revision__ = '$Id: test_filenode.py 1009 2005-06-15 13:39:15Z faltet $'
21
21
 
22
22
 
23
23
 
377
377
                        IOError, FileNode.openNode, self.h5file.getNode('/test'), 'w')
378
378
 
379
379
 
380
 
        def test03_OpenFileNoAttrs(self):
381
 
                "Opening a node with no type attributes."
382
 
 
383
 
                node = self.h5file.getNode('/test')
384
 
                self.h5file.delAttrNode('/test', '_type')
385
 
                # Another way to get the same result is changing the value.
386
 
                ##self.h5file.setAttrNode('/test', '_type', 'foobar')
387
 
                self.assertRaises(ValueError, FileNode.openNode, node)
 
380
        # This no longer works since type and type version attributes
 
381
        # are now system attributes.  ivb(2004-12-29)
 
382
        ##def test03_OpenFileNoAttrs(self):
 
383
        ##      "Opening a node with no type attributes."
 
384
        ##
 
385
        ##      node = self.h5file.getNode('/test')
 
386
        ##      self.h5file.delNodeAttr('/test', '_type')
 
387
        ##      # Another way to get the same result is changing the value.
 
388
        ##      ##self.h5file.setNodeAttr('/test', '_type', 'foobar')
 
389
        ##      self.assertRaises(ValueError, FileNode.openNode, node)
388
390
 
389
391
 
390
392
 
752
754
                os.remove(self.h5fname)
753
755
 
754
756
 
755
 
        def test00_GetTypeAttr(self):
756
 
                "Getting the type attribute of a file node."
757
 
 
 
757
        # This no longer works since type and type version attributes
 
758
        # are now system attributes.  ivb(2004-12-29)
 
759
        ##def test00_GetTypeAttr(self):
 
760
        ##      "Getting the type attribute of a file node."
 
761
        ##
 
762
        ##      self.assertEqual(
 
763
        ##              getattr(self.fnode.attrs, '_type', None), FileNode.NodeType,
 
764
        ##              "File node has no '_type' attribute.")
 
765
 
 
766
 
 
767
        def test00_MangleTypeAttrs(self):
 
768
                "Mangling the type attributes on a file node."
 
769
 
 
770
                nodeType = getattr(self.fnode.attrs, 'NODE_TYPE', None)
758
771
                self.assertEqual(
759
 
                        getattr(self.fnode.attrs, '_type', None), FileNode.NodeType,
760
 
                        "File node has no '_type' attribute.")
761
 
 
762
 
 
763
 
        def test01_SetSystemAttr(self):
764
 
                "Setting a system attribute on a file node."
765
 
 
766
 
                self.assertRaises(
767
 
                        RuntimeError, setattr, self.fnode.attrs, 'CLASS', 'foobar')
 
772
                        nodeType, FileNode.NodeType,
 
773
                        "File node does not have a valid 'NODE_TYPE' attribute.")
 
774
 
 
775
                nodeTypeVersion = getattr(self.fnode.attrs, 'NODE_TYPE_VERSION', None)
 
776
                self.assert_(
 
777
                        nodeTypeVersion in FileNode.NodeTypeVersions,
 
778
                        "File node does not have a valid 'NODE_TYPE_VERSION' attribute.")
 
779
 
 
780
                # System attributes are now writable.  ivb(2004-12-30)
 
781
                ##self.assertRaises(
 
782
                ##      AttributeError,
 
783
                ##      setattr, self.fnode.attrs, 'NODE_TYPE', 'foobar')
 
784
                ##self.assertRaises(
 
785
                ##      AttributeError,
 
786
                ##      setattr, self.fnode.attrs, 'NODE_TYPE_VERSION', 'foobar')
 
787
 
 
788
                self.assertRaises(
 
789
                        AttributeError,
 
790
                        delattr, self.fnode.attrs, 'NODE_TYPE')
 
791
                self.assertRaises(
 
792
                        AttributeError,
 
793
                        delattr, self.fnode.attrs, 'NODE_TYPE_VERSION')
 
794
 
 
795
 
 
796
        # System attributes are now writable.  ivb(2004-12-30)
 
797
        ##def test01_SetSystemAttr(self):
 
798
        ##      "Setting a system attribute on a file node."
 
799
        ##
 
800
        ##      self.assertRaises(
 
801
        ##              AttributeError, setattr, self.fnode.attrs, 'CLASS', 'foobar')
768
802
 
769
803
 
770
804
        def test02_SetGetDelUserAttr(self):
853
887
 
854
888
 
855
889
 
 
890
class OldVersionTestCaseMixin:
 
891
        """Mix-in class for old version compatibility test cases.
 
892
 
 
893
        It provides some basic tests for file operations and attribute handling.
 
894
        Sub-classes must provide the 'oldversion' attribute
 
895
        and the 'oldh5fname' attribute.
 
896
        """
 
897
 
 
898
        def setUp(self):
 
899
                """setUp() -> None
 
900
 
 
901
                This method sets the following instance attributes:
 
902
                  * 'h5fname', the name of the temporary HDF5 file
 
903
                  * 'h5file', the writable, temporary HDF5 file with a '/test' node
 
904
                  * 'fnode', the writable file node in '/test'
 
905
                """
 
906
 
 
907
                self.h5fname = tempfile.mktemp(suffix = '.h5')
 
908
 
 
909
                oldh5f = tables.openFile(self.oldh5fname)
 
910
                oldh5f.copyFile(self.h5fname)
 
911
                oldh5f.close()
 
912
 
 
913
                self.h5file = tables.openFile(
 
914
                        self.h5fname, 'r+',
 
915
                        title = "Test for file node old version compatibility")
 
916
                self.fnode = FileNode.openNode(self.h5file.root.test, 'a+')
 
917
 
 
918
 
 
919
        def tearDown(self):
 
920
                """tearDown() -> None
 
921
 
 
922
                Closes 'fnode' and 'h5file'; removes 'h5fname'.
 
923
                """
 
924
 
 
925
                self.fnode.close()
 
926
                self.fnode = None
 
927
                self.h5file.close()
 
928
                self.h5file = None
 
929
                os.remove(self.h5fname)
 
930
 
 
931
 
 
932
        def test00_Read(self):
 
933
                "Reading an old version file node."
 
934
 
 
935
                self.fnode.lineSeparator = '\n'
 
936
 
 
937
                line = self.fnode.readline()
 
938
                self.assertEqual(line, 'This is only\n')
 
939
 
 
940
                line = self.fnode.readline()
 
941
                self.assertEqual(line, 'a test file\n')
 
942
 
 
943
                line = self.fnode.readline()
 
944
                self.assertEqual(line, 'for FileNode version %d\n' % self.oldversion)
 
945
 
 
946
                line = self.fnode.readline()
 
947
                self.assertEqual(line, '')
 
948
 
 
949
                self.fnode.seek(0)
 
950
                line = self.fnode.readline()
 
951
                self.assertEqual(line, 'This is only\n')
 
952
 
 
953
 
 
954
        def test01_Write(self):
 
955
                "Writing an old version file node."
 
956
 
 
957
                self.fnode.lineSeparator = '\n'
 
958
 
 
959
                self.fnode.write('foobar\n')
 
960
                self.fnode.seek(-7, 2)
 
961
                line = self.fnode.readline()
 
962
                self.assertEqual(line, 'foobar\n')
 
963
 
 
964
 
 
965
        def test02_Attributes(self):
 
966
                "Accessing attributes in an old version file node."
 
967
 
 
968
                self.fnode.attrs.userAttr = 'foobar'
 
969
                self.assertEqual(
 
970
                        getattr(self.fnode.attrs, 'userAttr', None), 'foobar',
 
971
                        "User attribute was not correctly set.")
 
972
 
 
973
                self.fnode.attrs.userAttr = 'bazquux'
 
974
                self.assertEqual(
 
975
                        getattr(self.fnode.attrs, 'userAttr', None), 'bazquux',
 
976
                        "User attribute was not correctly changed.")
 
977
 
 
978
                del self.fnode.attrs.userAttr
 
979
                self.assertEqual(
 
980
                        getattr(self.fnode.attrs, 'userAttr', None), None,
 
981
                        "User attribute was not deleted.")
 
982
 
 
983
 
 
984
 
 
985
class Version1TestCase(OldVersionTestCaseMixin, unittest.TestCase):
 
986
        "Basic test for version 1 format compatibility."
 
987
 
 
988
        oldversion = 1
 
989
        oldh5fname = 'test_filenode_v1.h5'
 
990
 
 
991
 
 
992
 
856
993
#----------------------------------------------------------------------
857
994
 
858
995
def suite():
873
1010
        theSuite.addTest(unittest.makeSuite(LineSeparatorTestCase))
874
1011
        theSuite.addTest(unittest.makeSuite(AttrsTestCase))
875
1012
        theSuite.addTest(unittest.makeSuite(ClosedH5FileTestCase))
 
1013
        theSuite.addTest(unittest.makeSuite(Version1TestCase))
876
1014
 
877
1015
        return theSuite
878
1016