~ubuntu-branches/ubuntu/vivid/pydicom/vivid

« back to all changes in this revision

Viewing changes to dicom/test/test_valuerep.py

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2014-03-19 21:40:46 UTC
  • mfrom: (1.2.3)
  • Revision ID: package-import@ubuntu.com-20140319214046-ivq1iz1kwosi8kj3
Tags: 0.9.8-1
* Fresh upstream release
* Adjusting X-Python-Version due to
  "pydicom > 0.9.7 requires python 2.6 or later"

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# test_valuerep.py
2
 
"""Test suite for valuerep.py"""
3
 
# Copyright (c) 2008 Darcy Mason
4
 
# This file is part of pydicom, released under a modified MIT license.
5
 
#    See the file license.txt included with this distribution, also
6
 
#    available at http://pydicom.googlecode.com
7
 
 
8
 
import unittest
9
 
from dicom.valuerep import PersonName, PersonNameUnicode
10
 
 
11
 
default_encoding = 'iso8859'
12
 
 
13
 
class PersonNametests(unittest.TestCase):
14
 
    def testLastFirst(self):
15
 
        """PN: Simple Family-name^Given-name works..............................."""
16
 
        pn = PersonName("Family^Given")
17
 
        expected = "Family"
18
 
        got = pn.family_name
19
 
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for family name" % (expected, got))
20
 
        
21
 
        expected = 'Given'
22
 
        got = pn.given_name
23
 
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for given name" % (expected, got))
24
 
        
25
 
        expected = ''
26
 
        got = pn.name_suffix
27
 
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for name_suffix" % (expected, got))
28
 
        
29
 
        expected = ''
30
 
        got = pn.phonetic
31
 
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for phonetic component" % (expected, got))
32
 
        
33
 
    def testThreeComponent(self):
34
 
        """PN: 3component (single-byte, ideographic, phonetic characters) works.."""
35
 
        # Example name from PS3.5-2008 section I.2 p. 108
36
 
        pn = PersonName("""Hong^Gildong=\033$)C\373\363^\033$)C\321\316\324\327=\033$)C\310\253^\033$)C\261\346\265\277""")
37
 
        expected = ("Hong", "Gildong")
38
 
        got = (pn.family_name, pn.given_name)
39
 
        self.assertEqual(got, expected, "PN: Expected single_byte name '%s', got '%s'" % (expected, got))
40
 
    def testFormatting(self):
41
 
        """PN: Formatting works.................................................."""
42
 
        pn = PersonName("Family^Given")
43
 
        expected = "Family, Given"
44
 
        got = pn.family_comma_given()
45
 
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for formatted Family, Given" % (expected, got))
46
 
    def testUnicodeKr(self):
47
 
        """PN: 3component in unicode works (Korean).............................."""
48
 
        # Example name from PS3.5-2008 section I.2 p. 101
49
 
        from sys import version_info
50
 
        if version_info >= (2,4):
51
 
            pn = PersonNameUnicode(
52
 
               """Hong^Gildong=\033$)C\373\363^\033$)C\321\316\324\327=\033$)C\310\253^\033$)C\261\346\265\277""",
53
 
               [default_encoding,'euc_kr'])
54
 
            expected = ("Hong", "Gildong")
55
 
            got = (pn.family_name, pn.given_name)
56
 
            self.assertEqual(got, expected, "PN: Expected single_byte name '%s', got '%s'" % (expected, got))
57
 
    def testUnicodeJp(self):
58
 
        """PN: 3component in unicode works (Japanese)............................"""
59
 
        # Example name from PS3.5-2008 section H  p. 98
60
 
        from sys import version_info
61
 
        if version_info >= (2,4):
62
 
            pn = PersonNameUnicode(
63
 
               """Yamada^Tarou=\033$B;3ED\033(B^\033$BB@O:\033(B=\033$B$d$^$@\033(B^\033$B$?$m$&\033(B""",
64
 
               [default_encoding,'iso2022_jp'])
65
 
            expected = ("Yamada", "Tarou")
66
 
            got = (pn.family_name, pn.given_name)
67
 
            self.assertEqual(got, expected, "PN: Expected single_byte name '%s', got '%s'" % (expected, got))
68
 
        
69
 
if __name__ == "__main__":
70
 
    unittest.main()
 
1
# test_valuerep.py
 
2
"""Test suite for valuerep.py"""
 
3
# Copyright (c) 2008-2012 Darcy Mason
 
4
# This file is part of pydicom, released under a modified MIT license.
 
5
#    See the file license.txt included with this distribution, also
 
6
#    available at http://pydicom.googlecode.com
 
7
 
 
8
import unittest
 
9
from dicom import in_py3
 
10
from dicom.valuerep import PersonName, PersonNameUnicode
 
11
import dicom.config
 
12
 
 
13
if in_py3:
 
14
    from dicom.valuerep import PersonName3 as PersonNameUnicode
 
15
    PersonName = PersonNameUnicode
 
16
 
 
17
 
 
18
default_encoding = 'iso8859'
 
19
 
 
20
 
 
21
class DecimalStringtests(unittest.TestCase):
 
22
    """Unit tests unique to the use of DS class derived from python Decimal"""
 
23
 
 
24
    def setUp(self):
 
25
        dicom.config.DS_decimal(True)
 
26
 
 
27
    def tearDown(self):
 
28
        dicom.config.DS_decimal(False)
 
29
 
 
30
    def testValidDecimalStrings(self):
 
31
        # Ensures that decimal.Decimal doesn't cause a valid string to become
 
32
        # invalid
 
33
        valid_str = '-9.81338674e-006'
 
34
        ds = dicom.valuerep.DS(valid_str)
 
35
        L = len(str(ds))
 
36
        self.assertTrue(L <= 16, "DS: expected a string of length 16 but got %d" % (L,))
 
37
 
 
38
        # Now the input string is too long but decimal.Decimal can convert it
 
39
        # to a valid 16-character string
 
40
        long_str = '-0.000000981338674'
 
41
        ds = dicom.valuerep.DS(long_str)
 
42
        L = len(str(ds))
 
43
        self.assertTrue(L <= 16, "DS: expected a string of length 16 but got %d" % (L,))
 
44
 
 
45
    def testInvalidDecimalStrings(self):
 
46
        # Now the input string truly is invalid
 
47
        invalid_string = '-9.813386743e-006'
 
48
        self.assertRaises(OverflowError, dicom.valuerep.DS, invalid_string)
 
49
 
 
50
 
 
51
class PersonNametests(unittest.TestCase):
 
52
    def testLastFirst(self):
 
53
        """PN: Simple Family-name^Given-name works..............................."""
 
54
        pn = PersonName("Family^Given")
 
55
        expected = "Family"
 
56
        got = pn.family_name
 
57
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for family name" % (expected, got))
 
58
 
 
59
        expected = 'Given'
 
60
        got = pn.given_name
 
61
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for given name" % (expected, got))
 
62
 
 
63
        expected = ''
 
64
        got = pn.name_suffix
 
65
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for name_suffix" % (expected, got))
 
66
 
 
67
        expected = ''
 
68
        got = pn.phonetic
 
69
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for phonetic component" % (expected, got))
 
70
 
 
71
    def testThreeComponent(self):
 
72
        """PN: 3component (single-byte, ideographic, phonetic characters) works.."""
 
73
        # Example name from PS3.5-2008 section I.2 p. 108
 
74
        pn = PersonName("""Hong^Gildong=\033$)C\373\363^\033$)C\321\316\324\327=\033$)C\310\253^\033$)C\261\346\265\277""")
 
75
        expected = ("Hong", "Gildong")
 
76
        got = (pn.family_name, pn.given_name)
 
77
        self.assertEqual(got, expected, "PN: Expected single_byte name '%s', got '%s'" % (expected, got))
 
78
 
 
79
    def testFormatting(self):
 
80
        """PN: Formatting works.................................................."""
 
81
        pn = PersonName("Family^Given")
 
82
        expected = "Family, Given"
 
83
        got = pn.family_comma_given()
 
84
        self.assertEqual(got, expected, "PN: expected '%s', got '%s' for formatted Family, Given" % (expected, got))
 
85
 
 
86
    def testUnicodeKr(self):
 
87
        """PN: 3component in unicode works (Korean).............................."""
 
88
        # Example name from PS3.5-2008 section I.2 p. 101
 
89
        from sys import version_info
 
90
        pn = PersonNameUnicode(
 
91
            """Hong^Gildong=\033$)C\373\363^\033$)C\321\316\324\327=\033$)C\310\253^\033$)C\261\346\265\277""",
 
92
            [default_encoding, 'euc_kr'])
 
93
        expected = ("Hong", "Gildong")
 
94
        got = (pn.family_name, pn.given_name)
 
95
        self.assertEqual(got, expected, "PN: Expected single_byte name '{0!s}', got '{1!s}'".format(expected, got))
 
96
 
 
97
    def testUnicodeJp(self):
 
98
        """PN: 3component in unicode works (Japanese)............................"""
 
99
        # Example name from PS3.5-2008 section H  p. 98
 
100
        from sys import version_info
 
101
        pn = PersonNameUnicode(
 
102
            """Yamada^Tarou=\033$B;3ED\033(B^\033$BB@O:\033(B=\033$B$d$^$@\033(B^\033$B$?$m$&\033(B""",
 
103
            [default_encoding, 'iso2022_jp'])
 
104
        expected = ("Yamada", "Tarou")
 
105
        got = (pn.family_name, pn.given_name)
 
106
        self.assertEqual(got, expected, "PN: Expected single_byte name '{0!s}', got '{1!s}'".format(expected, got))
 
107
 
 
108
 
 
109
if __name__ == "__main__":
 
110
    unittest.main()