Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unicode values in fields with STRICT validation #72

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions hl7apy/base_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from __future__ import absolute_import
import re
import numbers
import sys
from datetime import datetime
from decimal import Decimal
from functools import cmp_to_key
Expand All @@ -44,6 +45,10 @@
InvalidDateOffset, InvalidMicrosecondsPrecision
from hl7apy.validation import Validator

if sys.version_info.major == 2:
bytes, unicode = str, unicode
else:
bytes, unicode = bytes, str

class BaseDataType(object):
"""
Expand Down Expand Up @@ -71,8 +76,13 @@ def __init__(self, value, max_length=None, validation_level=None):
self.validation_level = validation_level
self.max_length = max_length
if Validator.is_strict(self.validation_level):
if self.max_length is not None and len('{0}'.format(value)) > self.max_length:
raise MaxLengthReached(value, self.max_length)
if self.max_length is not None:
if isinstance(value, (bytes, unicode)):
if len(value) > self.max_length:
raise MaxLengthReached(value, self.max_length)
elif len("{0}".format(value)) > self.max_length:
raise MaxLengthReached(value, self.max_length)

self.value = value

def to_er7(self, encoding_chars=None):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ def test_to_string_wd_field(self):
parsed_s = parse_segment(s, version='2.7')
self.assertEqual(parsed_s.to_er7(), 'EVN||20080115153000||AAA|AAA|20080114003000')

def test_to_string_unicode_field(self):
pid = Segment('PID', validation_level=VALIDATION_LEVEL.STRICT)
pid.pid_5.pid_5_1 = u'€'
self.assertEqual(pid.to_er7(), u'PID|||||€')


if __name__ == '__main__':
unittest.main()