Skip to content

Commit

Permalink
[EDS-616] Support "lastname, firstname" searches (#124)
Browse files Browse the repository at this point in the history
* [EDS-616] Support "lastname, firstname"-formatted name searches

* [Bot] Update version to 2.1.2

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Thomas Thorogood and github-actions[bot] authored Nov 12, 2021
1 parent 81773da commit 163ac2f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions husky_directory/models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def validate_query(cls, v: str, values: Dict[str, Any]) -> str:
if values.get("method") == "name":
if len(v) < 2:
raise ValueError("Name query string must contain at least 2 characters")
# Allow users to input "last, first" for names.
if "," in v:
tokens = reversed(v.split(","))
v = " ".join(tokens).strip()
elif len(v) < 3:
raise ValueError("Query string must contain at least 3 characters")
return v
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "uw-husky-directory"
version = "2.1.1"
version = "2.1.2"
description = "An updated version of the UW Directory"
authors = ["Thomas Thorogood <[email protected]>"]
license = "MIT"
Expand Down
17 changes: 15 additions & 2 deletions tests/models/test_search_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ def test_population_default(self, render_population, expected):
form_input = SearchDirectoryFormInput(render_population=render_population)
assert PopulationType(form_input.render_population) == PopulationType(expected)

@pytest.mark.parametrize(
"name, expected",
[
("lovelace, ada", "ada lovelace"),
("ada lovelace", "ada lovelace"),
# Please note that "Ada" is already Ada Lovelace's middle name;
# she did not have a middle initial of "M" in real life.
("lovelace, ada m", "ada m lovelace"),
],
)
def test_sanitized_name(self, name, expected):
assert SearchDirectoryFormInput(method="name", query=name).query == expected


@pytest.mark.parametrize(
"query_value, expected_value",
Expand All @@ -79,9 +92,9 @@ def test_population_default(self, render_population, expected):
# automatically stripped by pydantic
("\tfoo\t", "foo"),
(" foo bar ", "foo bar"),
# Ensure tab characters are converted to spacees
# Ensure tab characters are converted to spaces
("foo\tbar", "foo bar"),
# Ensure multiple spaces are condensed to a single spacee
# Ensure multiple spaces are condensed to a single space
("foo bar", "foo bar"),
# Ensure all the things
(" foo\t \t bar \t", "foo bar"),
Expand Down

0 comments on commit 163ac2f

Please sign in to comment.