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
9
from dicom.valuerep import PersonName, PersonNameUnicode
11
default_encoding = 'iso8859'
13
class PersonNametests(unittest.TestCase):
14
def testLastFirst(self):
15
"""PN: Simple Family-name^Given-name works..............................."""
16
pn = PersonName("Family^Given")
19
self.assertEqual(got, expected, "PN: expected '%s', got '%s' for family name" % (expected, got))
23
self.assertEqual(got, expected, "PN: expected '%s', got '%s' for given name" % (expected, got))
27
self.assertEqual(got, expected, "PN: expected '%s', got '%s' for name_suffix" % (expected, got))
31
self.assertEqual(got, expected, "PN: expected '%s', got '%s' for phonetic component" % (expected, got))
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))
69
if __name__ == "__main__":
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
9
from dicom import in_py3
10
from dicom.valuerep import PersonName, PersonNameUnicode
14
from dicom.valuerep import PersonName3 as PersonNameUnicode
15
PersonName = PersonNameUnicode
18
default_encoding = 'iso8859'
21
class DecimalStringtests(unittest.TestCase):
22
"""Unit tests unique to the use of DS class derived from python Decimal"""
25
dicom.config.DS_decimal(True)
28
dicom.config.DS_decimal(False)
30
def testValidDecimalStrings(self):
31
# Ensures that decimal.Decimal doesn't cause a valid string to become
33
valid_str = '-9.81338674e-006'
34
ds = dicom.valuerep.DS(valid_str)
36
self.assertTrue(L <= 16, "DS: expected a string of length 16 but got %d" % (L,))
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)
43
self.assertTrue(L <= 16, "DS: expected a string of length 16 but got %d" % (L,))
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)
51
class PersonNametests(unittest.TestCase):
52
def testLastFirst(self):
53
"""PN: Simple Family-name^Given-name works..............................."""
54
pn = PersonName("Family^Given")
57
self.assertEqual(got, expected, "PN: expected '%s', got '%s' for family name" % (expected, got))
61
self.assertEqual(got, expected, "PN: expected '%s', got '%s' for given name" % (expected, got))
65
self.assertEqual(got, expected, "PN: expected '%s', got '%s' for name_suffix" % (expected, got))
69
self.assertEqual(got, expected, "PN: expected '%s', got '%s' for phonetic component" % (expected, got))
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))
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))
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))
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))
109
if __name__ == "__main__":