Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

add unit tests, and add fuzzy/structural name duplicate detection for sqlite as well

pankaj-basnetd71b0428fa37
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 62d9098d7..82def8b3b 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -15,6 +15,7 @@
# Standard Library
import re
import uuid
+from difflib import SequenceMatcher
# Django
from django.conf import settings
@@ -24,10 +25,8 @@ from django.db import (
models,
transaction,
)
-from django.db.models import (
- Length,
- Q,
-)
+from django.db.models import Q
+from django.db.models.functions import Length
# Third Party
from actstream import action as actstream_action
@@ -51,6 +50,7 @@ from wger.exercises.models import (
from wger.exercises.views.helper import StreamVerbs
from wger.utils.cache import CacheKeyMapper
from wger.utils.constants import CC_BY_SA_4_LICENSE_ID
+from wger.utils.db import is_postgres_db
from wger.utils.url import make_absolute_url
@@ -87,6 +87,16 @@ def _check_duplicates(name, language, exclude_pk=None):
if exclude_pk:
base_qs = base_qs.exclude(pk=exclude_pk)
+ # Trigram Similarity
+ if is_postgres_db():
+ return (
+ base_qs.annotate(similarity=TrigramSimilarity('name', name))
+ .filter(similarity__gte=SIMILARITY_THRESHOLD)
+ .order_by('-similarity')
+ .values_list('name', flat=True)
+ .first()
+ )
+
norm_new = _normalise_name(name)
input_len = len(name)
@@ -95,20 +105,16 @@ def _check_duplicates(name, language, exclude_pk=None):
n_len__gte=input_len - 4, n_len__lte=input_len + 4
)
+ # SequenceMatcher
for candidate in candidates:
if _normalise_name(candidate.name) == norm_new:
return candidate.name
- # Trigram Similarity
- hit = (
- base_qs.annotate(similarity=TrigramSimilarity('name', name))
- .filter(similarity__gte=SIMILARITY_THRESHOLD)
- .order_by('-similarity')
- .values_list('name', flat=True)
- .first()
- )
-
- return hit
+ if (
+ SequenceMatcher(None, norm_new, _normalise_name(candidate.name)).ratio()
+ >= SIMILARITY_THRESHOLD
+ ):
+ return candidate.name
class ExerciseSerializer(serializers.ModelSerializer):
diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.py
index d42b962e2..9f5aab65c 100644
--- a/wger/exercises/tests/test_exercise_translation.py
+++ b/wger/exercises/tests/test_exercise_translation.py
@@ -361,3 +361,136 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTes
response = self.client.patch(self.url_detail, data={'language': 3})
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertTrue(response.data['non_field_errors'])
+
+ def test_post_rejects_structural_duplicate_name(self):
+ """
+ Test that submitting a name structurally identical (ignoring spaces/hyphens)
+ to an existing exercise in the same language is rejected.
+ """
+
+ # wger
+ from wger.exercises.models import ExerciseCategory
+
+ category = ExerciseCategory.objects.first()
+
+ exercise_a = Exercise.objects.create(
+ category=category,
+ license_id=2,
+ license_author='test',
+ )
+ Translation.objects.create(
+ exercise=exercise_a,
+ language_id=2,
+ name='Pull ups',
+ description_source='A base description for the duplicate name seed translation.',
+ )
+
+ exercise_b = Exercise.objects.create(
+ category=category,
+ license_id=2,
+ license_author='test',
+ )
+
+ payload = {
+ 'name': 'Pull-ups',
+ 'description_source': 'A new description for the pullups duplicate test.',
+ 'exercise': exercise_b.pk,
+ 'language': 2,
+ }
+
+ self.authenticate('trainer1')
+ response = self.client.post(self.url, data=payload)
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn('name', response.json())
+ self.assertIn('too similar', response.json()['name'][0])
+
+ def test_post_rejects_fuzzy_duplicate_name(self):
+ """
+ Test that submitting a name semantically similar (trigram)
+ to an existing exercise in the same language is rejected.
+ """
+ # wger
+ from wger.exercises.models import ExerciseCategory
+
+ category = ExerciseCategory.objects.first()
+
+ exercise_a = Exercise.objects.create(
+ category=category,
+ license_id=2,
+ license_author='test',
+ )
+ Translation.objects.create(
+ exercise=exercise_a,
+ language_id=2,
+ name='Bench Press',
+ description_source='A base description for the bench press seed translation.',
+ )
+
+ exercise_b = Exercise.objects.create(
+ category=category,
+ license_id=2,
+ license_author='test',
+ )
+
+ payload = {
+ 'name': 'Benchpress', # fuzzy duplicate
+ 'description_source': 'A new description for the benchpress duplicate test.',
+ 'exercise': exercise_b.pk,
+ 'language': 2,
+ }
+
+ self.authenticate('trainer1')
+ response = self.client.post(self.url, data=payload)
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn('name', response.json())
+ self.assertIn('too similar', response.json()['name'][0])
+
+ def test_patch_allows_self_edit_with_same_name(self):
+ """
+ Test that editing an existing translation without changing its name
+ (or submitting the exact same name) does not trigger a duplicate error,
+ validating the `exclude_pk` logic.
+ """
+ translation = Translation.objects.get(pk=self.pk)
+ original_name = translation.name
+
+ self.authenticate('trainer1')
+ # passing the same name back
+ response = self.client.patch(
+ self.url_detail,
+ data={'description_source': 'Updating the description only', 'name': original_name},
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+ def test_patch_rejects_conflict_with_other_name(self):
+ """
+ Test that editing an existing translation's name to collide with
+ a DIFFERENT existing translation's name is rejected.
+ """
+
+ # wger
+ from wger.exercises.models import ExerciseCategory
+
+ other_exercise = Exercise.objects.create(
+ category=ExerciseCategory.objects.first(),
+ license_id=2,
+ license_author='test',
+ )
+ Translation.objects.create(
+ exercise=other_exercise,
+ language_id=2,
+ name='Barbell Squat',
+ description_source='Base description for barbell squat test.',
+ )
+
+ self.authenticate('trainer1')
+ response = self.client.patch(
+ self.url_detail,
+ data={'name': 'Barbell-Squat'}, # Structural duplicate
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn('name', response.json())