-
Notifications
You must be signed in to change notification settings - Fork 3
baseapp-profiles: change profile filter to add name and urlpath query - BA-1737 #178
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import django_filters | ||
| import swapper | ||
| from django.db.models import Q | ||
|
|
||
| Profile = swapper.load_model("baseapp_profiles", "Profile") | ||
|
|
||
|
|
||
| class ProfileFilter(django_filters.FilterSet): | ||
| q = django_filters.CharFilter(method="filter_q") | ||
|
|
||
| order_by = django_filters.OrderingFilter( | ||
| fields=( | ||
| ("created", "created"), | ||
| ("followers_count__total", "followers_count_total"), | ||
| ("following_count__total", "following_count_total"), | ||
| ) | ||
| ) | ||
|
|
||
| class Meta: | ||
| model = Profile | ||
| fields = ["q", "order_by"] | ||
|
|
||
| def filter_q(self, queryset, name, value): | ||
| return queryset.filter(Q(name__icontains=value) | Q(url_paths__path__icontains=value)) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import swapper | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from baseapp_pages.tests.factories import URLPathFactory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from django.contrib.contenttypes.models import ContentType | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from .factories import ProfileFactory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -22,6 +24,18 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SEARCH_PROFILES_BY_QUERY_PARAM = """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query AllProfiles($q: String!) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allProfiles(q: $q) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| edges { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| node { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_profile_metadata(graphql_user_client, image_djangofile): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile = ProfileFactory(image=image_djangofile) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -128,3 +142,29 @@ def test_another_user_cant_view_members(graphql_user_client): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content = response.json() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert content["data"]["profile"]["members"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_search_profiles(graphql_user_client): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile1 = ProfileFactory(name="David") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile2 = ProfileFactory(name="Daniel") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile3 = ProfileFactory(name="Mark") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile4 = ProfileFactory(name="John") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile5 = ProfileFactory(name="Donald") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| urlPath1 = URLPathFactory(path="danger.john", is_active=True, language=None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile_content_type = ContentType.objects.get_for_model(Profile) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| urlPath1.target_content_type = profile_content_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| urlPath1.target_object_id = profile4.id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| urlPath1.save() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| urlPath1.refresh_from_db() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profile4.refresh_from_db() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response = graphql_user_client(SEARCH_PROFILES_BY_QUERY_PARAM, variables={"q": "da"}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content = response.json() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profiles = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id for id in [edge["node"]["id"] for edge in content["data"]["allProfiles"]["edges"]] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert profile1.relay_id in profiles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert profile2.relay_id in profiles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert profile3.relay_id not in profiles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert profile4.relay_id in profiles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert profile5.relay_id not in profiles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+147
to
+170
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Test coverage and assertions need improvement Several issues need to be addressed:
Here's a suggested improvement: def test_search_profiles(graphql_user_client):
- profile1 = ProfileFactory(name="David")
- profile2 = ProfileFactory(name="Daniel")
- profile3 = ProfileFactory(name="Mark")
- profile4 = ProfileFactory(name="John")
- profile5 = ProfileFactory(name="Donald")
- urlPath1 = URLPathFactory(path="danger.john", is_active=True, language=None)
- profile_content_type = ContentType.objects.get_for_model(Profile)
- urlPath1.target_content_type = profile_content_type
- urlPath1.target_object_id = profile4.id
- urlPath1.save()
- urlPath1.refresh_from_db()
- profile4.refresh_from_db()
+ # Test profiles for name search
+ profile_david = ProfileFactory(name="David")
+ profile_daniel = ProfileFactory(name="Daniel")
+ profile_mark = ProfileFactory(name="Mark")
+
+ # Test profile for URL path search
+ profile_with_path = ProfileFactory(name="John")
+ URLPathFactory(
+ path="dashboard/john",
+ is_active=True,
+ target_content_type=ContentType.objects.get_for_model(Profile),
+ target_object_id=profile_with_path.id
+ )
- response = graphql_user_client(SEARCH_PROFILES_BY_QUERY_PARAM, variables={"q": "da"})
- content = response.json()
- profiles = [
- id for id in [edge["node"]["id"] for edge in content["data"]["allProfiles"]["edges"]]
- ]
- assert profile1.relay_id in profiles
- assert profile2.relay_id in profiles
- assert profile3.relay_id not in profiles
- assert profile4.relay_id in profiles
- assert profile5.relay_id not in profiles
+ def get_profile_ids(query):
+ response = graphql_user_client(SEARCH_PROFILES_BY_QUERY_PARAM, variables={"q": query})
+ return [edge["node"]["id"] for edge in response.json()["data"]["allProfiles"]["edges"]]
+
+ # Test name search
+ name_results = get_profile_ids("da")
+ assert profile_david.relay_id in name_results
+ assert profile_daniel.relay_id in name_results
+ assert profile_mark.relay_id not in name_results
+ assert profile_with_path.relay_id not in name_results
+
+ # Test URL path search
+ path_results = get_profile_ids("dashboard")
+ assert profile_with_path.relay_id in path_results
+ assert profile_david.relay_id not in path_results📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Existing test has incorrect assertion
The test
test_another_user_cant_view_membersasserts that another user CAN view members (assert content["data"]["profile"]["members"]), which contradicts the test name and likely the intended behavior. This should be fixed while we're updating the test file.📝 Committable suggestion