Skip to content

Commit

Permalink
Fix name checking
Browse files Browse the repository at this point in the history
Fix the check for projects with too-similar names when
there is more than one existing project with the same
ultranormalized name.

Signed-off-by: Facundo Tuesca <[email protected]>
  • Loading branch information
facutuesca committed Jan 14, 2025
1 parent cf21da1 commit 1e208d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 16 additions & 4 deletions tests/unit/packaging/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,11 +1016,11 @@ def test_check_project_name_stdlib(self, name):

def test_check_project_name_already_exists(self, db_session):
service = ProjectService(session=db_session)
ProjectFactory.create(name="foo")
project = ProjectFactory.create(name="foo")

assert isinstance(
service.check_project_name("foo"), ProjectNameUnavailableExisting
)
unavailable_error = service.check_project_name("foo")
assert isinstance(unavailable_error, ProjectNameUnavailableExisting)
assert unavailable_error.existing_project == project
assert isinstance(
service.check_project_name("Foo"),
ProjectNameUnavailableExisting,
Expand All @@ -1045,6 +1045,18 @@ def test_check_project_name_too_similar(self, db_session):
service.check_project_name("foo"), ProjectNameUnavailableSimilar
)

def test_check_project_name_too_similar_multiple_existing(self, db_session):
service = ProjectService(session=db_session)
project1 = ProjectFactory.create(name="f00")
project2 = ProjectFactory.create(name="f0o")

unavailable_error = service.check_project_name("foo")
assert isinstance(unavailable_error, ProjectNameUnavailableSimilar)
assert (
unavailable_error.similar_project == project1
or unavailable_error.similar_project == project2
)

def test_check_project_name_ok(self, db_session):
service = ProjectService(session=db_session)

Expand Down
4 changes: 2 additions & 2 deletions warehouse/packaging/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def check_project_name(self, name: str) -> ProjectNameUnavailableError | None:
if (
existing_project := self.db.query(Project)
.where(Project.normalized_name == func.normalize_pep426_name(name))
.scalar()
.first()
):
return ProjectNameUnavailableExisting(existing_project)

Expand All @@ -475,7 +475,7 @@ def check_project_name(self, name: str) -> ProjectNameUnavailableError | None:
.where(
func.ultranormalize_name(Project.name) == func.ultranormalize_name(name)
)
.scalar()
.first()
):
return ProjectNameUnavailableSimilar(similar_project)

Expand Down

0 comments on commit 1e208d0

Please sign in to comment.