Skip to content

Commit

Permalink
change scheme strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl5766 committed Jul 23, 2024
1 parent 54d16fb commit fc1b3d7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
20 changes: 11 additions & 9 deletions snakebids/tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@
valid_entities: tuple[str, ...] = tuple(BidsConfig.load("bids").entities.keys())

path_characters = st.characters(blacklist_characters=["/", "\x00"], codec="UTF-8")
# see StackOverflow
# https://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid#1547940
scheme_characters_str = (
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~?#[]@!$&'()*+,;="
)
scheme_characters = st.sampled_from(scheme_characters_str)


def nothing() -> Any:
return st.nothing() # type: ignore


def schemes() -> st.SearchStrategy[str | None]: # generate the prefix part of a url
def schemes() -> st.SearchStrategy[str | None]:
# Generate the prefix part of a url.
#
# we only sample from a fixed set of schemes. If schemes are randomly generated
# then something like file:// could be a problem because it may be either an
# abs or relative path and will fail the os.path.isabs() code
#
# Note for its current status we don't guarantee the following schemes to work
# but only requiring them to be retained correctly i.e. not from 'gs://' to
# 'gs:/' when fed into generate_inputs() and path expansion
fixed = st.sampled_from(["C://", "c:\\", "gs://", "s3://"])
random = st.text(scheme_characters, min_size=1, max_size=5)
return st.one_of(st.none(), random, fixed)
return st.one_of(st.none(), fixed)


def paths(
Expand Down
25 changes: 19 additions & 6 deletions snakebids/tests/test_generate_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1915,35 +1915,48 @@ def scheme_bids_component_strategy(draw, **kwargs):

class TestParseBidsPath:
@given(
component=scheme_bids_component_strategy(max_values=1, restrict_patterns=True)
component=sb_st.bids_components(max_values=1, restrict_patterns=True),
scheme=sb_st.schemes(),
)
def test_splits_wildcards_from_path(self, component: BidsComponent):
def test_splits_wildcards_from_path(
self, component: BidsComponent, scheme: str | None
):
path = component.expand()[0]
if scheme is not None:
path = f"{scheme}{path}"
entities = [BidsEntity.normalize(e).entity for e in component.zip_lists]
tpl_path, matches = _parse_bids_path(path, entities)
assert tpl_path.format(**matches) == path

@given(
component=scheme_bids_component_strategy(max_values=1, restrict_patterns=True)
component=sb_st.bids_components(max_values=1, restrict_patterns=True),
scheme=sb_st.schemes(),
)
def test_one_match_found_for_each_entity(self, component: BidsComponent):
def test_one_match_found_for_each_entity(
self, component: BidsComponent, scheme: str | None
):
path = component.expand()[0]
if scheme is not None:
path = f"{scheme}{path}"
entities = [BidsEntity.normalize(e).entity for e in component.zip_lists]
_, matches = _parse_bids_path(path, entities)
assert set(matches.items()) == {
(key, val[0]) for key, val in component.zip_lists.items()
}

@given(
component=scheme_bids_component_strategy(
component=sb_st.bids_components(
max_values=1, restrict_patterns=True, extra_entities=False
),
scheme=sb_st.schemes(),
entity=sb_st.bids_entity(),
)
def test_missing_match_leads_to_error(
self, component: BidsComponent, entity: BidsEntity
self, component: BidsComponent, scheme: str | None, entity: BidsEntity
):
path = component.expand()[0]
if scheme is not None:
path = f"{scheme}{path}"
entities = [BidsEntity.normalize(e).entity for e in component.zip_lists]
assume(entity.entity not in entities)
with pytest.raises(BidsParseError) as err:
Expand Down

0 comments on commit fc1b3d7

Please sign in to comment.