def test_from_string_valids(): def _valid_id_sets(): yield None for p1, t1 in VALID_SLUGS: yield p1 for p2, t2 in VALID_SLUGS: if ((t1 == 'projects') + (t2 == 'projects')) >= 1: yield f'{p1}/{p2}' for prefixes in _valid_id_sets(): for owner in (None, '*', 'x', 'x*'): for name in (None, '*', 'y', 'y*'): for revision in (None, '*', 'z', 'z*'): idstr, pid, id = _build_string(owner, name, prefixes, revision, True) if idstr: ident = Identifier.from_string(idstr) assert ident.owner == ('' if owner in (None, '*') else owner), idstr assert ident.name == ('' if name in (None, '*') else name), idstr assert ident.revision == ('' if revision in (None, '*') else revision), idstr assert ident.id == id, idstr assert ident.pid == pid, idstr
def _build_idstr(prefixes, valid=True): prefixes = prefixes.split('/') suffixes = [uuid4().hex for _ in prefixes] # Valid id pairs do not have two different ids with the same type. if valid and len(prefixes) == 2 and prefixes[0] == prefixes[1]: suffixes[0] = suffixes[1] ids = [f'{p}-{s}' for p, s in zip(prefixes, suffixes)] idstr = '/'.join(ids) if len(ids) == 1: id = ids[0] pid = id if Identifier.id_type(id, quiet=True) == 'projects' else '' elif Identifier.id_type(ids[0], quiet=True) == 'projects': pid, id = ids else: id, pid = ids return idstr, pid, id
def test_from_string_invalids(): def _invalid_id_sets(): yield INVALID_SLUG for p1, t1 in VALID_SLUGS: yield f'{INVALID_SLUG}/{p1}' yield f'{p1}/{INVALID_SLUG}' for p2, t2 in VALID_SLUGS: if ((t1 == 'projects') + (t2 == 'projects')) != 1: yield f'{p1}/{p2}' for prefixes in _invalid_id_sets(): for owner in (None, '*', 'x', 'x*'): for name in (None, '*', 'y', 'y*'): for revision in (None, '*', 'z', 'z*'): idstr, pid, id = _build_string(owner, name, prefixes, revision, False) if idstr: ident = Identifier.from_string(idstr, quiet=True) assert ident is None, idstr
def test_id_prefix_with_valid_types(): for prefix, type in VALID_SLUGS: assert Identifier.id_prefix(type) == prefix
def _assert_round_trip(ident): return Identifier.from_string(str(ident)) == ident
def _assert_valid_id(idstr, type=None): assert Identifier.id_type(idstr) == type or type is None
def _assert_invalid_id(idstr): with pytest.raises(ValueError) as excinfo: Identifier.id_type(idstr) assert 'Invalid identifier' in str(excinfo.value) assert idstr in str(excinfo.value)