Ejemplo n.º 1
0
def test_from_string_unsupported_suffix():
    """Checks *ValueError* raised when suffix is incompatible with URI type."""
    # Note that if any of the metadata, rendered, or thumbnail suffixes is
    # supplied with only the base URL, the entire URI (including the
    # perceived suffix) shall be treated as the base URL.
    with pytest.raises(ValueError, match='lalala'):
        URI.from_string(f'{_FRAME_URI}/metadata')
Ejemplo n.º 2
0
def test_from_string_type_error():
    """Checks *ValueError* raised when the actual type does match expected."""
    for uri_type in URIType:
        if uri_type != URIType.SERVICE:
            with pytest.raises(ValueError, match='Unexpected URI type'):
                URI.from_string(_BASE_URL, uri_type)
        if uri_type != URIType.STUDY:
            with pytest.raises(ValueError, match='Unexpected URI type'):
                URI.from_string(_STUDY_URI, uri_type)
        if uri_type != URIType.SERIES:
            with pytest.raises(ValueError, match='Unexpected URI type'):
                URI.from_string(_SERIES_URI, uri_type)
        if uri_type != URIType.INSTANCE:
            with pytest.raises(ValueError, match='Unexpected URI type'):
                URI.from_string(_INSTANCE_URI, uri_type)
Ejemplo n.º 3
0
def test_from_string_frame_uri(suffix):
    """Checks frame numbers are parsed correctly and behaves as expected."""
    uri = _FRAME_URI if suffix is None else f'{_FRAME_URI}/{suffix.value}'
    frame_uri = URI.from_string(uri)
    # Properties.
    assert frame_uri.base_url == _BASE_URL
    assert frame_uri.study_instance_uid == _STUDY_UID
    assert frame_uri.series_instance_uid == _SERIES_UID
    assert frame_uri.sop_instance_uid == _INSTANCE_UID
    assert frame_uri.frames == _FRAMES
    assert frame_uri.type == URIType.FRAME
    assert frame_uri.suffix == suffix
    # String representation.
    assert str(frame_uri) == uri
    assert str(frame_uri.base_uri()) == _BASE_URL
    assert str(frame_uri.study_uri()) == _STUDY_URI
    assert str(frame_uri.series_uri()) == _SERIES_URI
    assert str(frame_uri.instance_uri()) == _INSTANCE_URI
    assert str(frame_uri.frame_uri()) == _FRAME_URI
Ejemplo n.º 4
0
def test_from_string_instance_uri(suffix):
    """Checks Instance URI is parsed correctly and behaves as expected."""
    uri = _INSTANCE_URI if suffix is None else f'{_INSTANCE_URI}/{suffix.value}'
    instance_uri = URI.from_string(uri)
    # Properties.
    assert instance_uri.base_url == _BASE_URL
    assert instance_uri.study_instance_uid == _STUDY_UID
    assert instance_uri.series_instance_uid == _SERIES_UID
    assert instance_uri.sop_instance_uid == _INSTANCE_UID
    assert instance_uri.type == URIType.INSTANCE
    assert instance_uri.suffix == suffix
    # String representation.
    assert str(instance_uri) == uri
    assert str(instance_uri.base_uri()) == _BASE_URL
    assert str(instance_uri.study_uri()) == _STUDY_URI
    assert str(instance_uri.series_uri()) == _SERIES_URI

    with pytest.raises(ValueError, match='Cannot get a Frame URI'):
        instance_uri.frame_uri()
Ejemplo n.º 5
0
def test_from_string_service_uri():
    """Checks that Service URL is parsed correctly and behaves as expected."""
    service_uri = URI.from_string(_BASE_URL)
    # Properties.
    assert service_uri.base_url == _BASE_URL
    assert service_uri.study_instance_uid is None
    assert service_uri.series_instance_uid is None
    assert service_uri.sop_instance_uid is None
    assert service_uri.type == URIType.SERVICE
    assert service_uri.suffix is None
    # String representation.
    assert str(service_uri) == _BASE_URL
    assert str(service_uri.base_uri()) == _BASE_URL
    # Constructor.
    assert str(service_uri) == str(URI(_BASE_URL))

    with pytest.raises(ValueError, match='Cannot get a Study URI'):
        service_uri.study_uri()
    with pytest.raises(ValueError, match='Cannot get a Series URI'):
        service_uri.series_uri()
    with pytest.raises(ValueError, match='Cannot get an Instance URI'):
        service_uri.instance_uri()
    with pytest.raises(ValueError, match='Cannot get a Frame URI'):
        service_uri.frame_uri()
Ejemplo n.º 6
0
def test_from_string_invalid_uri_protocol(service):
    """Checks *ValueError* raised when the URI string is invalid."""
    with pytest.raises(ValueError, match=r'Only HTTP\[S\] URLs'):
        URI.from_string(f'{service}invalid_url')
Ejemplo n.º 7
0
def test_from_string_non_integer_frames(frame_uri):
    """Checks *ValueError* is raised if unexpected resource delimiter found."""
    with pytest.raises(ValueError, match='non-integral frame numbers'):
        URI.from_string(frame_uri)
Ejemplo n.º 8
0
def test_from_string_invalid_resource_delimiter(resource_url):
    """Checks *ValueError* is raised if unexpected resource delimiter found."""
    with pytest.raises(ValueError, match='Error parsing the suffix'):
        URI.from_string(resource_url)
Ejemplo n.º 9
0
def test_from_string_service_uri_protocols(base_url):
    """Checks that Service URL permits both HTTP and HTTPs protocols."""
    service_uri = URI.from_string(base_url)
    assert service_uri.base_url == base_url
Ejemplo n.º 10
0
    with pytest.raises(ValueError, match=r'Only HTTP\[S\] URLs'):
        URI.from_string(f'{service}invalid_url')


def test_from_string_unsupported_suffix():
    """Checks *ValueError* raised when suffix is incompatible with URI type."""
    # Note that if any of the metadata, rendered, or thumbnail suffixes is
    # supplied with only the base URL, the entire URI (including the
    # perceived suffix) shall be treated as the base URL.
    with pytest.raises(ValueError, match='lalala'):
        URI.from_string(f'{_FRAME_URI}/metadata')


@pytest.mark.parametrize(
    'child,parent',
    [(URI.from_string(_BASE_URL), URI.from_string(_BASE_URL)),
     (URI.from_string(_STUDY_URI), URI.from_string(_BASE_URL)),
     (URI.from_string(_SERIES_URI), URI.from_string(_STUDY_URI)),
     (URI.from_string(_INSTANCE_URI), URI.from_string(_SERIES_URI)),
     (URI.from_string(_FRAME_URI), URI.from_string(_INSTANCE_URI)),
     (URI.from_string(f'{_STUDY_URI}/{URISuffix.RENDERED.value}'),
      URI.from_string(_STUDY_URI)),
     (URI.from_string(f'{_SERIES_URI}/{URISuffix.RENDERED.value}'),
      URI.from_string(_SERIES_URI)),
     (URI.from_string(f'{_INSTANCE_URI}/{URISuffix.RENDERED.value}'),
      URI.from_string(_INSTANCE_URI)),
     (URI.from_string(f'{_FRAME_URI}/{URISuffix.RENDERED.value}'),
      URI.from_string(_FRAME_URI)),
     ])
def test_parent(child, parent):
    """Validates the expected parent URI from `parent` attribute."""