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_presentation_context_transfer(self):
"""Check the presentation context generation transfer syntax"""
# str -> UID
ae = AE(scu_sop_class=['1.1'], transfer_syntax=['1.2.840.10008.1.2'])
tran_syn = ae.presentation_contexts_scu[0].TransferSyntax[0]
self.assertEqual(tran_syn, UID('1.2.840.10008.1.2'))
self.assertTrue(isinstance(tran_syn, UID))
# UID no change
ae = AE(scu_sop_class=['1.2'], transfer_syntax=[b'1.2.840.10008.1.2'])
tran_syn = ae.presentation_contexts_scu[0].TransferSyntax[0]
self.assertEqual(tran_syn, UID('1.2.840.10008.1.2'))
self.assertTrue(isinstance(tran_syn, UID))
# bytes -> UID
ae = AE(scu_sop_class=['1.3'], transfer_syntax=[UID('1.2.840.10008.1.2')])
tran_syn = ae.presentation_contexts_scu[0].TransferSyntax[0]
self.assertEqual(tran_syn, UID('1.2.840.10008.1.2'))
self.assertTrue(isinstance(tran_syn, UID))
# If not transfer syntax raise
with self.assertRaises(ValueError):
ae = AE(scu_sop_class=['1.3'], transfer_syntax=['1.2.840'])
# If not str, bytes, UID raise
with self.assertRaises(ValueError):
ae = AE(scu_sop_class=['1.3'], transfer_syntax=[123456])
# If not valid UID raise
with self.assertRaises(ValueError):
ae = AE(scu_sop_class=['1.3'], transfer_syntax=['1.2.3.4.5.'])
def test_properties(self):
""" Check property setters and getters """
item = SOPClassExtendedNegotiationSubItem()
# SOP Class UID
item.sop_class_uid = '1.1.1'
self.assertEqual(item.sop_class_uid, UID('1.1.1'))
self.assertEqual(item.sop_class_uid_length, 5)
self.assertTrue(isinstance(item.sop_class_uid, UID))
item.sop_class_uid = b'1.1.2.1'
self.assertEqual(item.sop_class_uid, UID('1.1.2.1'))
self.assertEqual(item.sop_class_uid_length, 7)
self.assertTrue(isinstance(item.sop_class_uid, UID))
item.sop_class_uid = UID('1.1.3.1.1')
self.assertEqual(item.sop_class_uid, UID('1.1.3.1.1'))
self.assertEqual(item.sop_class_uid_length, 9)
self.assertTrue(isinstance(item.sop_class_uid, UID))
self.assertEqual(item.UID, item.sop_class_uid)
with self.assertRaises(TypeError):
item.sop_class_uid = 10002
pdu = A_ASSOCIATE_RQ()
pdu.Decode(a_associate_rq_user_id_ext_neg)
item = pdu.user_information.ext_neg[0]
def test_properies(self):
""" Check property setters and getters """
uid = ImplementationClassUIDSubItem()
uid.implementation_class_uid = '1.2.826.0.1.3680043.9.3811.0.9.1'
self.assertEqual(uid.implementation_class_uid, UID('1.2.826.0.1.3680043.9.3811.0.9.1'))
uid.implementation_class_uid = b'1.2.826.0.1.3680043.9.3811.0.9.2'
self.assertEqual(uid.implementation_class_uid, UID('1.2.826.0.1.3680043.9.3811.0.9.2'))
uid.implementation_class_uid = UID('1.2.826.0.1.3680043.9.3811.0.9.3')
self.assertEqual(uid.implementation_class_uid, UID('1.2.826.0.1.3680043.9.3811.0.9.3'))
with self.assertRaises(TypeError):
uid.implementation_class_uid = 10002
def test_to_primitive(self):
""" Check converting to primitive """
pdu = A_ASSOCIATE_RQ()
pdu.Decode(a_associate_rq_com_ext_neg)
item = pdu.user_information.common_ext_neg[0]
result = item.ToParams()
check = SOPClassCommonExtendedNegotiation()
check.sop_class_uid = UID('1.2.840.10008.5.1.4.1.1.4')
check.service_class_uid = UID('1.2.840.10008.4.2')
check.related_general_sop_class_identification = [UID('1.2.840.10008.5.1.4.1.1.88.22')]
self.assertEqual(result, check)
def test_to_primitive(self):
""" Check converting to primitive """
pdu = A_ASSOCIATE_RQ()
pdu.Decode(a_associate_rq_com_ext_neg)
item = pdu.user_information.common_ext_neg[0]
result = item.ToParams()
check = SOPClassCommonExtendedNegotiation()
check.sop_class_uid = UID('1.2.840.10008.5.1.4.1.1.4')
check.service_class_uid = UID('1.2.840.10008.4.2')
check.related_general_sop_class_identification = [UID('1.2.840.10008.5.1.4.1.1.88.22')]
self.assertEqual(result, check)
primitive.sop_class_uid = UID('1.2.840.10008.5.1.4.1.1.2')
self.assertTrue(primitive.sop_class_uid == reference_uid)
# Service Class UID
primitive.service_class_uid = b'1.2.840.10008.5.1.4.1.1.2'
self.assertTrue(primitive.service_class_uid == reference_uid)
primitive.service_class_uid = '1.2.840.10008.5.1.4.1.1.2'
self.assertTrue(primitive.service_class_uid == reference_uid)
primitive.service_class_uid = UID('1.2.840.10008.5.1.4.1.1.2')
self.assertTrue(primitive.service_class_uid == reference_uid)
# Related General SOP Class Identification
ref_uid_list = [UID('1.2.840.10008.5.1.4.1.1.2'),
UID('1.2.840.10008.5.1.4.1.1.3'),
UID('1.2.840.10008.5.1.4.1.1.4')]
uid_list = []
uid_list.append(b'1.2.840.10008.5.1.4.1.1.2')
uid_list.append('1.2.840.10008.5.1.4.1.1.3')
uid_list.append(UID('1.2.840.10008.5.1.4.1.1.4'))
primitive.related_general_sop_class_identification = uid_list
self.assertTrue(primitive.related_general_sop_class_identification == ref_uid_list)
with self.assertRaises(TypeError):
primitive.related_general_sop_class_identification = 'test'
## Check exceptions
# SOP Class UID
with self.assertRaises(TypeError):
primitive.service_class_uid = '1.2.840.10008.5.1.4.1.1.2'
self.assertTrue(primitive.service_class_uid == reference_uid)
primitive.service_class_uid = UID('1.2.840.10008.5.1.4.1.1.2')
self.assertTrue(primitive.service_class_uid == reference_uid)
# Related General SOP Class Identification
ref_uid_list = [UID('1.2.840.10008.5.1.4.1.1.2'),
UID('1.2.840.10008.5.1.4.1.1.3'),
UID('1.2.840.10008.5.1.4.1.1.4')]
uid_list = []
uid_list.append(b'1.2.840.10008.5.1.4.1.1.2')
uid_list.append('1.2.840.10008.5.1.4.1.1.3')
uid_list.append(UID('1.2.840.10008.5.1.4.1.1.4'))
primitive.related_general_sop_class_identification = uid_list
self.assertTrue(primitive.related_general_sop_class_identification == ref_uid_list)
with self.assertRaises(TypeError):
primitive.related_general_sop_class_identification = 'test'
## Check exceptions
# SOP Class UID
with self.assertRaises(TypeError):
primitive.sop_class_uid = 10
with self.assertRaises(TypeError):
primitive.sop_class_uid = 45.2
with self.assertRaises(ValueError):
primitive.sop_class_uid = 'abc'
def test_to_primitive(self):
""" Check converting to primitive """
pdu = A_ASSOCIATE_RQ()
pdu.Decode(a_associate_rq)
uid = pdu.user_information.user_data[1]
result = uid.ToParams()
check = ImplementationClassUIDNotification()
check.implementation_class_uid = UID('1.2.826.0.1.3680043.9.3811.0.9.0')
self.assertEqual(result, check)
def test_abstract_syntax(self):
"""Test abstract syntax setter"""
pc = PresentationContext(1)
pc.AbstractSyntax = '1.1.1'
self.assertEqual(pc.AbstractSyntax, UID('1.1.1'))
self.assertTrue(isinstance(pc.AbstractSyntax, UID))
pc.AbstractSyntax = b'1.2.1'
self.assertEqual(pc.AbstractSyntax, UID('1.2.1'))
self.assertTrue(isinstance(pc.AbstractSyntax, UID))
pc.AbstractSyntax = UID('1.3.1')
self.assertEqual(pc.AbstractSyntax, UID('1.3.1'))
self.assertTrue(isinstance(pc.AbstractSyntax, UID))
pc.AbstractSyntax = UID('1.4.1.')
self.assertEqual(pc.AbstractSyntax, UID('1.3.1'))
self.assertTrue(isinstance(pc.AbstractSyntax, UID))
the transfer syntax will not be used for matching. If the value
corresponds to an uncompressed syntax then matches will be made
with any uncompressed transfer syntaxes.
role : str
One of 'scu' or 'scp', the required role of the context.
context_id : int or None
If not None then the ID of the presentation context to use. It will
be checked against the available parameter values.
Returns
-------
presentation.PresentationContext
An accepted presentation context.
"""
ab_syntax = UID(ab_syntax)
tr_syntax = UID(tr_syntax)
possible_contexts = []
if context_id is None:
possible_contexts = self.accepted_contexts
else:
for cx in self.accepted_contexts:
if cx.context_id == context_id:
possible_contexts = [cx]
break
for cx in possible_contexts:
if cx.abstract_syntax != ab_syntax:
continue
# Cover both False and None
if role == 'scu' and cx.as_scu is not True: