Ejemplo n.º 1
0
def test_init_fail():
    _init_fail(None, MissingParameterError('userid'))
    _init_fail('   \t    ', MissingParameterError('userid'))
    _init_fail('foo \t bar',
               IllegalParameterError('userid contains control characters'))
    _init_fail('u' * 257,
               IllegalParameterError('userid exceeds maximum length of 256'))
Ejemplo n.º 2
0
def test_sample_node_address_init_fail():
    sa = SampleAddress(uuid.UUID('1234567890abcdef1234567890abcdef'), 5)

    _sample_node_address_init_fail(None, 'f',  ValueError(
        'sample cannot be a value that evaluates to false'))
    _sample_node_address_init_fail(sa, None, MissingParameterError('node'))
    _sample_node_address_init_fail(sa, '  \t \n  ', MissingParameterError('node'))
    _sample_node_address_init_fail(sa, '3' * 257, IllegalParameterError(
        'node exceeds maximum length of 256'))
def test_get_admin_request_from_object_fail_bad_args():
    _get_admin_request_from_object_fail(None, '1', '2', ValueError('params cannot be None'))
    _get_admin_request_from_object_fail({'a': 'b'}, None, '2', MissingParameterError('as_admin'))
    _get_admin_request_from_object_fail({'a': 'b'}, '1', None, MissingParameterError('as_user'))
    _get_admin_request_from_object_fail(
        {'asa': True, 'asu': ['foo']}, 'asa', 'asu', IllegalParameterError(
            'asu must be a string if present'))
    _get_admin_request_from_object_fail(
        {'asa': True, 'asu': 'whe\tee'}, 'asa', 'asu', IllegalParameterError(
            'userid contains control characters'))
def test_get_version_from_object_fail_bad_args():
    get_version_from_object_fail(None, False, ValueError('params cannot be None'))
    get_version_from_object_fail({}, True, MissingParameterError('version'))
    get_version_from_object_fail(
        {'version': None}, True, MissingParameterError('version'))
    get_version_from_object_fail(
        {'version': 'whee'}, False, IllegalParameterError('Illegal version argument: whee'))
    get_version_from_object_fail(
        {'version': 0}, True, IllegalParameterError('Illegal version argument: 0'))
    get_version_from_object_fail(
        {'version': -3}, False, IllegalParameterError('Illegal version argument: -3'))
def test_get_id_from_object_fail_bad_args():
    _get_id_from_object_fail({'id': 6}, True, IllegalParameterError(
        'Sample ID 6 must be a UUID string'))
    _get_id_from_object_fail({
        'id': 'f5bd78c3-823e-40b2-9f93-20e78680e41'},
        False,
        IllegalParameterError(
            'Sample ID f5bd78c3-823e-40b2-9f93-20e78680e41 must be a UUID string'))
    _get_id_from_object_fail(None, True, MissingParameterError('Sample ID'))
    _get_id_from_object_fail({}, True, MissingParameterError('Sample ID'))
    _get_id_from_object_fail({'id': None}, True, MissingParameterError('Sample ID'))
def check_string(string: Optional[str],
                 name: str,
                 max_len: int = None,
                 optional: bool = False) -> Optional[str]:
    '''
    Check that a string meets a set of criteria:
    - it is not None or whitespace only (unless the optional parameter is specified)
    - it contains no control characters
    - (optional) it is less than some specified maximum length

    :param string: the string to test.
    :param name: the name of the string to be used in error messages.
    :param max_len: the maximum length of the string.
    :param optional: True if no error should be thrown if the string is None.
    :returns: the stripped string or None if the string was optional and None or whitespace only.
    :raises MissingParameterError: if the string is None or whitespace only.
    :raises IllegalParameterError: if the string is too long or contains illegal characters.
    '''
    # See the IDMapping service if character classes are needed.
    # Maybe package this stuff
    if max_len is not None and max_len < 1:
        raise ValueError('max_len must be > 0 if provided')
    if not string or not string.strip():
        if optional:
            return None
        raise MissingParameterError(name)
    string = string.strip()
    _no_control_characters(string, name)
    if max_len and len(string) > max_len:
        raise IllegalParameterError('{} exceeds maximum length of {}'.format(
            name, max_len))
    return string
Ejemplo n.º 7
0
def test_sample_build_fail():
    # not testing every permutation of failing check_string here, just one test to make sure
    # it's there

    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    u = UserID('user')
    sn = SampleNode('foo')
    tn = SampleNode('bar', SubSampleType.TECHNICAL_REPLICATE, 'foo')
    sn2 = SampleNode('baz')
    dup = SampleNode('foo')
    d = dt(8)

    _sample_build_fail(
        [sn], 'a' * 257, IllegalParameterError('name exceeds maximum length of 256'))
    _sample_build_fail([], None, MissingParameterError('At least one node per sample is required'))
    _sample_build_fail(
        [tn, sn], 'a', IllegalParameterError('The first node in a sample must be a BioReplicate'))
    _sample_build_fail([sn, tn, sn2], 'a', IllegalParameterError(
                       'BioReplicates must be the first nodes in the list of sample nodes.'))
    _sample_build_fail([sn, sn2, dup], 'a', IllegalParameterError(
                       'Duplicate sample node name: foo'))
    _sample_build_fail([sn2, tn], 'a', IllegalParameterError(
                        'Parent foo of node bar does not appear in node list prior to node.'))

    _sample_with_id_build_fail(None, u, [sn], d, None, None,
                               ValueError('id_ cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, None, [sn], d, None, None,
                               ValueError('user cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, u, [sn], None, None, None, ValueError(
                               'savetime cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, u, [sn], datetime.datetime.now(), None, None, ValueError(
                               'savetime cannot be a naive datetime'))
    _sample_with_id_build_fail(id_, u, [sn], d, None, 0, ValueError('version must be > 0'))
def test_get_user_from_object_fail_bad_args():
    _get_user_from_object_fail(None, 'us', ValueError('params cannot be None'))
    _get_user_from_object_fail({'us': 'foo'}, None, MissingParameterError('key'))
    _get_user_from_object_fail({'us': []}, 'us', IllegalParameterError(
        'us must be a string if present'))
    _get_user_from_object_fail({'us': 'baz\tbaat'}, 'us', IllegalParameterError(
        # probably not worth the trouble to change the key name, we'll see
        'userid contains control characters'))
def test_get_data_unit_id_from_object_fail_bad_args():
    _get_data_unit_id_from_object_fail(None, ValueError('params cannot be None'))
    _get_data_unit_id_from_object_fail({}, MissingParameterError('upa'))
    _get_data_unit_id_from_object_fail({'upa': '1/0/1'}, IllegalParameterError(
        '1/0/1 is not a valid UPA'))
    _get_data_unit_id_from_object_fail({'upa': 82}, IllegalParameterError(
        'upa key is not a string as required'))
    _get_data_unit_id_from_object_fail({'upa': '1/1/1', 'dataid': []}, IllegalParameterError(
        'dataid key is not a string as required'))
    _get_data_unit_id_from_object_fail({'upa': '1/1/1', 'dataid': 'f\t/b'}, IllegalParameterError(
        'dataid contains control characters'))
def test_create_data_link_params_fail_bad_args():
    id_ = '706fe9e1-70ef-4feb-bbd9-32295104a119'
    _create_data_link_params_fail(None, ValueError('params cannot be None'))
    _create_data_link_params_fail({}, MissingParameterError('Sample ID'))
    _create_data_link_params_fail({'id': 6}, IllegalParameterError(
        'Sample ID 6 must be a UUID string'))
    _create_data_link_params_fail({'id': id_[:-1]}, IllegalParameterError(
        'Sample ID 706fe9e1-70ef-4feb-bbd9-32295104a11 must be a UUID string'))
    _create_data_link_params_fail({'id': id_}, MissingParameterError('version'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 'ver'},
        IllegalParameterError('Illegal version argument: ver'))
    _create_data_link_params_fail(
        {'id': id_, 'version': -1},
        IllegalParameterError('Illegal version argument: -1'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 1},
        MissingParameterError('node'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 1, 'node': {'a': 'b'}},
        IllegalParameterError('node key is not a string as required'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 1, 'node': 'foo\tbar'},
        IllegalParameterError('node contains control characters'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 1, 'node': 'm'},
        MissingParameterError('upa'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 1, 'node': 'm', 'upa': 3.4},
        IllegalParameterError('upa key is not a string as required'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 1, 'node': 'm', 'upa': '1/0/1'},
        IllegalParameterError('1/0/1 is not a valid UPA'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 1, 'node': 'm', 'upa': '1/1/1', 'dataid': 6},
        IllegalParameterError('dataid key is not a string as required'))
    _create_data_link_params_fail(
        {'id': id_, 'version': 1, 'node': 'm', 'upa': '1/1/1', 'dataid': 'yay\nyo'},
        IllegalParameterError('dataid contains control characters'))
Ejemplo n.º 11
0
 def __init__(
     self,
     nodes: List[SampleNode],
     name: Optional[str] = None,
 ):
     '''
     Create the the sample.
     :param nodes: The tree nodes in the sample. BIOLOGICAL_REPLICATES must come first in
         the list, and parents must come before children in the list.
     :param name: The name of the sample. Cannot contain control characters or be longer than
         255 characters.
     :raise MissingParameterError: if no nodes are provided.
     :raises IllegalParameterError: if the name is too long or contains illegal characters,
         the first node in the list is not a BIOLOGICAL_REPLICATE, all the BIOLOGICAL_REPLICATES
         are not at the start of this list, node names are not unique, or parent nodes
         do not appear in the list prior to their children.
     '''
     self.name = _check_string(name,
                               'name',
                               max_len=_MAX_SAMPLE_NAME_LEN,
                               optional=True)
     if not nodes:
         raise MissingParameterError(
             'At least one node per sample is required')
     if len(nodes) > _MAX_SAMPLE_NODES:
         raise IllegalParameterError(
             f'At most {_MAX_SAMPLE_NODES} nodes are allowed per sample')
     if nodes[0].type != SubSampleType.BIOLOGICAL_REPLICATE:
         raise IllegalParameterError(
             f'The first node in a sample must be a {SubSampleType.BIOLOGICAL_REPLICATE.value}'
         )
     no_more_bio = False
     seen_names: _Set[str] = set()
     for n in nodes:
         if no_more_bio and n.type == SubSampleType.BIOLOGICAL_REPLICATE:
             raise IllegalParameterError(
                 f'{SubSampleType.BIOLOGICAL_REPLICATE.value}s must be the first '
                 + 'nodes in the list of sample nodes.')
         if n.type != SubSampleType.BIOLOGICAL_REPLICATE:
             no_more_bio = True
         if n.name in seen_names:
             raise IllegalParameterError(
                 f'Duplicate sample node name: {n.name}')
         if n.parent and n.parent not in seen_names:
             raise IllegalParameterError(
                 f'Parent {n.parent} of node {n.name} does not ' +
                 'appear in node list prior to node.')
         seen_names.add(n.name)
     self.nodes = tuple(nodes)  # make hashable
def test_get_sample_address_from_object_fail_bad_args():
    get_sample_address_from_object_fail(None, False, MissingParameterError('Sample ID'))
    get_sample_address_from_object_fail({}, False, MissingParameterError('Sample ID'))
    get_sample_address_from_object_fail({'id': None}, False, MissingParameterError('Sample ID'))
    get_sample_address_from_object_fail({'id': 6}, False, IllegalParameterError(
        'Sample ID 6 must be a UUID string'))
    get_sample_address_from_object_fail(
        {'id': 'f5bd78c3-823e-40b2-9f93-20e78680e41'}, False,
        IllegalParameterError(
            'Sample ID f5bd78c3-823e-40b2-9f93-20e78680e41 must be a UUID string'))
    id_ = 'f5bd78c3-823e-40b2-9f93-20e78680e41e'
    get_sample_address_from_object_fail(
        {'id': id_}, True, MissingParameterError('version'))
    get_sample_address_from_object_fail(
        {'id': id_, 'version': [1]}, False, IllegalParameterError('Illegal version argument: [1]'))

    get_version_from_object_fail(
        {'id': id_, 'version': 'whee'},
        True,
        IllegalParameterError('Illegal version argument: whee'))
    get_version_from_object_fail(
        {'id': id_, 'version': 0}, True, IllegalParameterError('Illegal version argument: 0'))
    get_version_from_object_fail(
        {'id': id_, 'version': -3}, True, IllegalParameterError('Illegal version argument: -3'))
Ejemplo n.º 13
0
def test_sample_node_build_fail():
    # not testing every permutation of failing check_string here, just one test to make sure
    # it's there
    _sample_node_build_fail('', SubSampleType.BIOLOGICAL_REPLICATE, None,
                            MissingParameterError('subsample name'))
    _sample_node_build_fail('a' * 257, SubSampleType.BIOLOGICAL_REPLICATE, None,
                            IllegalParameterError('subsample name exceeds maximum length of 256'))
    _sample_node_build_fail('a', None, None,
                            ValueError('type cannot be a value that evaluates to false'))
    _sample_node_build_fail('a', SubSampleType.TECHNICAL_REPLICATE, 'b' * 257,
                            IllegalParameterError('parent exceeds maximum length of 256'))
    _sample_node_build_fail(
        'a', SubSampleType.BIOLOGICAL_REPLICATE, 'badparent', IllegalParameterError(
            'Node a is of type BioReplicate and therefore cannot have a parent'))
    _sample_node_build_fail(
        'a', SubSampleType.TECHNICAL_REPLICATE, None, IllegalParameterError(
            'Node a is of type TechReplicate and therefore must have a parent'))
    _sample_node_build_fail(
        'a', SubSampleType.SUB_SAMPLE, None, IllegalParameterError(
            'Node a is of type SubSample and therefore must have a parent'))
def test_get_upa_from_object_fail_bad_args():
    _get_upa_from_object_fail(None, ValueError('params cannot be None'))
    _get_upa_from_object_fail({}, MissingParameterError('upa'))
    _get_upa_from_object_fail({'upa': '1/0/1'}, IllegalParameterError('1/0/1 is not a valid UPA'))
    _get_upa_from_object_fail({'upa': 82}, IllegalParameterError(
        'upa key is not a string as required'))
Ejemplo n.º 15
0
def test_check_string_optional_false():
    for string in [None, '   \t   ']:
        with raises(Exception) as got:
            check_string(string, 'var name')
        assert_exception_correct(got.value, MissingParameterError('var name'))