Skip to content

Commit

Permalink
test: fixes test to use the correct max lenght for NM and SI
Browse files Browse the repository at this point in the history
  • Loading branch information
svituz committed Jan 9, 2024
1 parent 522e296 commit 37c20a2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9]
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
6 changes: 5 additions & 1 deletion hl7apy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import absolute_import

import os
import sys

try:
from collections.abc import MutableMapping
except ImportError:
from collections import MutableMapping

import importlib

try:
import cPickle as pickle
except ImportError:
import pickle


from hl7apy.exceptions import UnsupportedVersion, InvalidEncodingChars, UnknownValidationLevel
from hl7apy.consts import DEFAULT_ENCODING_CHARS, DEFAULT_ENCODING_CHARS_27, DEFAULT_VERSION, VALIDATION_LEVEL

Expand Down
20 changes: 16 additions & 4 deletions hl7apy/base_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
"""

from __future__ import absolute_import
import re

import numbers
import re
from datetime import datetime
from decimal import Decimal
from functools import cmp_to_key
Expand Down Expand Up @@ -65,6 +66,7 @@ class BaseDataType(object):
greater than the :attr:`max_length`. Only if :attr:`validation_level` is
:attr:`STRICT <VALIDATION_LEVEL.STRICT>`
"""

def __init__(self, value, max_length=None, validation_level=None):
if validation_level is None:
validation_level = get_default_validation_level()
Expand Down Expand Up @@ -113,6 +115,7 @@ class TextualDataType(BaseDataType):
:raise: :exc:`MaxLengthReached <hl7apy.exceptions.MaxLengthReached>` When the :attr:`value`'s length is
greater than :attr:`max_length`
"""

def __init__(self, value, max_length=32, highlights=None,
validation_level=None):
self.highlights = highlights
Expand All @@ -132,7 +135,7 @@ def _get_translations(self, encoding_chars):
(encoding_chars['REPETITION'], '{esc}R{esc}'.format(esc=escape_char)),)

def _get_escape_char_regex(self, escape_char):
return r'(?<!%s[HNFSTRE])%s(?![HNFSTRE]%s)' % tuple(3*[re.escape(escape_char)])
return r'(?<!%s[HNFSTRE])%s(?![HNFSTRE]%s)' % tuple(3 * [re.escape(escape_char)])

def _escape_value(self, value, encoding_chars=None):
escape_char = encoding_chars['ESCAPE']
Expand Down Expand Up @@ -196,6 +199,7 @@ class NumericDataType(BaseDataType):
:raise: :exc:`hl7apy.exceptions.MaxLengthReached` When the `value`'s length is greater than `max_length`
"""

def __init__(self, value=None, max_length=16,
validation_level=None):
super(NumericDataType, self).__init__(value, max_length,
Expand Down Expand Up @@ -238,6 +242,7 @@ class WD(TextualDataType):
:attr:`max_length` is 0
"""

def __init__(self, value, highlights=None,
validation_level=None):
super(WD, self).__init__(value, 199, highlights, validation_level)
Expand Down Expand Up @@ -332,6 +337,7 @@ class ST(TextualDataType):
:attr:`max_length` is 199
"""

def __init__(self, value, highlights=None,
validation_level=None):
super(ST, self).__init__(value, 199, highlights, validation_level)
Expand All @@ -344,6 +350,7 @@ class FT(TextualDataType):
:attr:`max_length` is 65536
"""

def __init__(self, value, highlights=None,
validation_level=None):
super(FT, self).__init__(value, 65536, highlights, validation_level)
Expand All @@ -356,6 +363,7 @@ class ID(TextualDataType):
:attr:`max_length` None
"""

def __init__(self, value, highlights=None,
validation_level=None):
# max_length is None bacause it depends from the HL7 table
Expand All @@ -370,6 +378,7 @@ class IS(TextualDataType):
:attr:`max_length` is 20
"""

def __init__(self, value, highlights=None,
validation_level=None):
super(IS, self).__init__(value, 20, highlights, validation_level)
Expand All @@ -383,6 +392,7 @@ class TX(TextualDataType):
:attr:`max_length` is 65536
"""

def __init__(self, value, highlights=None,
validation_level=None):
super(TX, self).__init__(value, 65536, highlights, validation_level)
Expand All @@ -395,6 +405,7 @@ class GTS(TextualDataType):
:attr:`max_length` is 199
"""

def __init__(self, value, highlights=None,
validation_level=None):
super(GTS, self).__init__(value, 199, highlights, validation_level)
Expand Down Expand Up @@ -423,14 +434,15 @@ def __init__(self, value=None,

class SI(NumericDataType):
"""
Class for NM datatype. It extends NumericDatatype and the parameters are the same of the superclass
Class for SI datatype. It extends NumericDatatype and the parameters are the same of the superclass
:attr:`max_length` is 4.
The type of ``value`` must be `int` or :class:`numbers.Integral`
:raise: :exc:`ValueError` raised when the value is not of one of the correct type
"""

def __init__(self, value=None,
validation_level=None):
if value is not None and not isinstance(value, numbers.Integral):
Expand All @@ -450,8 +462,8 @@ class TN(TextualDataType):
:raise: :exc:`ValueError` raised when the value does not match the expected format
"""
def __init__(self, value, validation_level=None):

def __init__(self, value, validation_level=None):
regexp = r'(\d\d\s)?(\(\d+\))?(\d+-?\d+)(X\d+)?(B\d+)?(C.+)?'
if not re.match(regexp, value):
raise ValueError('Invalid value for TN data')
Expand Down
24 changes: 15 additions & 9 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@ def test_assign_value_string(self):
c.value = 65537*'a'
for dt in ('NM', 'SI'):
c = Component(datatype=dt, validation_level=VALIDATION_LEVEL.TOLERANT)
c.value = 65537*'1'
c.value = 5*'1'

# strict
c = Component(datatype='ID', validation_level=VALIDATION_LEVEL.STRICT) # ID works because its length depends on HL7 table
Expand All @@ -1599,10 +1599,12 @@ def test_assign_value_string(self):
with self.assertRaises(MaxLengthReached):
c = Component(datatype=dt, validation_level=VALIDATION_LEVEL.STRICT) # max length reached string type
c.value = 65537*'a'
for dt in ('NM', 'SI'):
with self.assertRaises(MaxLengthReached):
c = Component(datatype=dt, validation_level=VALIDATION_LEVEL.STRICT)
c.value = 65537*'1'
with self.assertRaises(MaxLengthReached):
c = Component(datatype='NM', validation_level=VALIDATION_LEVEL.STRICT)
c.value = 17*'1'
with self.assertRaises(MaxLengthReached):
c = Component(datatype='SI', validation_level=VALIDATION_LEVEL.STRICT)
c.value = 5*'1'

# complex datatypes
# tolerant
Expand Down Expand Up @@ -2110,10 +2112,14 @@ def test_assign_value(self):
s = SubComponent(datatype=dt, validation_level=VALIDATION_LEVEL.STRICT)
with self.assertRaises(MaxLengthReached):
s.value = 65537*'a'
for dt in ('SI',):
s = SubComponent(datatype=dt, validation_level=VALIDATION_LEVEL.STRICT)
with self.assertRaises(MaxLengthReached):
s.value = 65537*'1'

s = SubComponent(datatype='NM', validation_level=VALIDATION_LEVEL.STRICT)
with self.assertRaises(MaxLengthReached):
s.value = 17 * '1'

s = SubComponent(datatype='SI', validation_level=VALIDATION_LEVEL.STRICT)
with self.assertRaises(MaxLengthReached):
s.value = 5*'1'

def test_add_child_to_subcomponent(self):
a = SubComponent('HD_1')
Expand Down

0 comments on commit 37c20a2

Please sign in to comment.