Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_expanded logic for nested fields is incorrect #48

Open
mitchelljkotler opened this issue Apr 3, 2020 · 0 comments
Open

is_expanded logic for nested fields is incorrect #48

mitchelljkotler opened this issue Apr 3, 2020 · 0 comments

Comments

@mitchelljkotler
Copy link

  1. If I have nested fields such that a.b and b are both legal expands, is_epxanded will not distinguish between b at the top level or nested
  2. is_expanded will return true for all keys if ~all is in expanded, but ~all only expands top level expands

Proposed fix:

def is_expanded(expand, key):
    """Determine if the given key is expanded"""
    expand_fields = []

    # first split on commas to get each expand
    for full in expand.split(","):
        # than split on dots to get each component that is expanded
        parts = full.split(".")
        for i in range(len(parts)):
            # add each prefix, as each prefix is epxanded, ie
            # a.b.c will add a, a.b and a.b.c to the expand_fields list
            # we do this to differentiate a.b from b
            expand_fields.append(".".join(parts[: i + 1]))

    # ~all only expands top level fields
    if "." not in key and "~all" in expand_fields:
        return True

    return key in expand_fields

Test:

import pytest

from .utils import is_expanded

data = [
    ("a", "a", True),
    ("a", "b", False),
    ("a,b,c", "a", True),
    ("a,b,c", "b", True),
    ("a,b,c", "c", True),
    ("a,b,c", "d", False),
    ("a.b.c", "a", True),
    ("a.b.c", "a.b", True),
    ("a.b.c", "a.b.c", True),
    ("a.b.c", "b", False),
    ("a.b.c", "c", False),
    ("a.b.c", "d", False),
    ("a.b.c,d", "a", True),
    ("a.b.c,d", "d", True),
    ("~all", "a", True),
    ("~all", "a.b", False),
]


class TestIsExpanded:
    @pytest.mark.parametrize("expand,key,ret", data)
    def test_expanded(self, expand, key, ret):
        assert is_expanded(expand, key) is ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant