Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_change_datatype_for_valued_subcomponent(self):
a = SubComponent('HD_1', value='value')
with self.assertRaises(OperationNotAllowed):
a.datatype = 'ST'
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.TOLERANT)
s = m.add_segment('QPD').add_field('QPD_8').add_component('CX_10').add_subcomponent('CWE_1')
s.value = 'value'
with self.assertRaises(OperationNotAllowed):
s.datatype = 'TX'
s = SubComponent('CWE_1')
s.value = cmp_str
self.assertEqual(s.to_er7(), cmp_str)
s = SubComponent('CWE_1') # more child than allowed
s.value = '1&2'
for dt in ('ST', 'ID', 'FT', 'GTS', 'IS', 'TX'):
s = SubComponent(datatype=dt) # max length reached string type
s.value = 65537*'a'
for dt in ('NM', 'SI'):
s = SubComponent(datatype=dt)
s.value = 65537*'1'
# strict
s = SubComponent('CWE_1', validation_level=VALIDATION_LEVEL.STRICT)
s.value = cmp_str
self.assertEqual(s.to_er7(), cmp_str)
# ID is missing because its max length is None
for dt in ('ST', 'FT', 'GTS', 'IS', 'TX'):
# max length reached string type
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'
def test_assign_wrong_subcomponent_to_known_position(self):
c = Component('XPN_1')
with self.assertRaises(ChildNotValid):
c.fn_1 = SubComponent('hd_1')
c1 = Component('XPN_1', validation_level=VALIDATION_LEVEL.STRICT)
with self.assertRaises(ChildNotValid):
c1.fn_1 = SubComponent('hd_1')
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.TOLERANT)
c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_10')
with self.assertRaises(ChildNotValid):
c.cwe_1 = SubComponent('hd_1')
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.STRICT)
c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_10')
with self.assertRaises(ChildNotValid):
c.cwe_1 = SubComponent('hd_1')
def test_add_unexpected_child_to_group(self):
g = Group()
m = Message('OML_O33')
f = Field()
c = Component(datatype='ST')
sub = SubComponent(datatype='ST')
self.assertRaises(ChildNotValid, g.add, m)
self.assertRaises(ChildNotValid, g.add, f)
self.assertRaises(ChildNotValid, g.add, c)
self.assertRaises(ChildNotValid, g.add, sub)
def test_override_datatype(self):
s = SubComponent('HD_1', datatype='TX', validation_level=VALIDATION_LEVEL.TOLERANT)
self.assertEqual(s.datatype, 'TX')
self.assertRaises(OperationNotAllowed, SubComponent, 'HD_1', datatype='TX',
validation_level=VALIDATION_LEVEL.STRICT)
s = SubComponent('HD_1', validation_level=VALIDATION_LEVEL.TOLERANT)
s.datatype = 'TX'
self.assertEqual(s.datatype, 'TX')
s = SubComponent('HD_1', validation_level=VALIDATION_LEVEL.STRICT)
with self.assertRaises(OperationNotAllowed):
s.datatype = 'TX'
s = SubComponent(datatype='ST', validation_level=VALIDATION_LEVEL.STRICT)
with self.assertRaises(OperationNotAllowed):
s.datatype = 'TX'
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.TOLERANT)
s = m.add_segment('QPD').add_field('QPD_8').add_component('CX_10').add_subcomponent('CWE_1')
def test_wrong_subcomponent(self):
"""
Tests that if there is an unexpected subcomponent the message in not validated
The message used has an unexpected unknown subcomponent in the cx_10
"""
msg = self._create_message(self.adt_a01)
unkn_subcomponent = SubComponent(datatype='ST', version='2.6')
msg.pid.pid_3.cx_10 = Component('CX_10', version='2.6')
msg.pid.pid_3.cx_10.add(unkn_subcomponent)
self.assertRaises(ValidationError, msg.validate, report_file=self.report_file)
self._test_report_file('ERROR')
s = SubComponent(datatype=dt)
s.value = 65537*'1'
# strict
s = SubComponent('CWE_1', validation_level=VALIDATION_LEVEL.STRICT)
s.value = cmp_str
self.assertEqual(s.to_er7(), cmp_str)
# ID is missing because its max length is None
for dt in ('ST', 'FT', 'GTS', 'IS', 'TX'):
# max length reached string type
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'
def test_add_more_subcomponents_to_base_datatype_component(self):
c = Component(datatype='ST')
c.add(SubComponent(datatype='ST'))
self.assertRaises(MaxChildLimitReached, c.add, SubComponent(datatype='ST'))
# c1 = Component(datatype='ST', validation_level=VALIDATION_LEVEL.STRICT)
# self.assertRaises(ChildNotValid, c1.add, SubComponent(datatype='ST'))
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.STRICT)
c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_2')
c.add(SubComponent(datatype='ST', validation_level=VALIDATION_LEVEL.STRICT))
self.assertRaises(MaxChildLimitReached, c.add, SubComponent(datatype='ST'))
def test_add_empty_subcomponent(self):
c1 = Component('cx_4', validation_level=VALIDATION_LEVEL.STRICT)
self.assertRaises(ChildNotValid, c1.add, SubComponent(datatype='ST'))
c2 = Component('cx_4')
c2.add(SubComponent(datatype='ST'))
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.STRICT)
c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_4')
self.assertRaises(ChildNotValid, c.add, SubComponent(datatype='ST'))
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.TOLERANT)
c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_4')
c.add(SubComponent(datatype='ST'))
def _set_encoding_chars(self, encoding_chars):
check_encoding_chars(encoding_chars)
msh_1 = Field('MSH_1', validation_level=self.validation_level, version=self.version)
msh_2 = Field('MSH_2', validation_level=self.validation_level, version=self.version)
s = SubComponent(datatype='ST', value=encoding_chars['FIELD'],
validation_level=self.validation_level, version=self.version)
c = Component(datatype='ST', validation_level=self.validation_level,
version=self.version)
c.add(s)
msh_1.st = c
if self.version >= '2.7' and 'TRUNCATION' in encoding_chars:
value = '{0}{1}{2}{3}{4}'.format(encoding_chars['COMPONENT'],
encoding_chars['REPETITION'],
encoding_chars['ESCAPE'],
encoding_chars['SUBCOMPONENT'],
encoding_chars['TRUNCATION'])
else:
value = '{0}{1}{2}{3}'.format(encoding_chars['COMPONENT'],
encoding_chars['REPETITION'],
encoding_chars['ESCAPE'],
encoding_chars['SUBCOMPONENT'])