def _test_units_contribution_filter(qs, user, unit_filter): result = UnitContributionFilter(qs, user=user).filter(unit_filter) for item in result: assert item in qs assert result.count() == result.distinct().count() user_subs_overwritten = [ "my_submissions_overwritten", "user_submissions_overwritten" ] if unit_filter == "suggestions": assert (result.count() == qs.filter( suggestion__state=SuggestionStates.PENDING).distinct().count()) return elif not user: assert result.count() == 0 return elif unit_filter in ["my_suggestions", "user_suggestions"]: expected = qs.filter(suggestion__state=SuggestionStates.PENDING, suggestion__user=user).distinct() elif unit_filter == "user_suggestions_accepted": expected = qs.filter(suggestion__state=SuggestionStates.ACCEPTED, suggestion__user=user).distinct() elif unit_filter == "user_suggestions_rejected": expected = qs.filter(suggestion__state=SuggestionStates.REJECTED, suggestion__user=user).distinct() elif unit_filter in ["my_submissions", "user_submissions"]: expected = qs.filter( submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES).distinct() elif unit_filter in user_subs_overwritten: expected = qs.filter(submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES) expected = expected.exclude(submitted_by=user).distinct() assert (list(expected.order_by("pk")) == list(result.order_by("pk")))
def test_units_contribution_filter(units_contributor_searches, site_users): unit_filter = units_contributor_searches user = site_users["user"] qs = Unit.objects.all() if not hasattr(UnitContributionFilter, "filter_%s" % unit_filter): with pytest.raises(FilterNotFound): UnitContributionFilter(qs, user=user).filter(unit_filter) return test_qs = [ qs, qs.none(), qs.filter(store__translation_project__project=Project.objects.first()) ] for _qs in test_qs: _test_units_contribution_filter(_qs, user, unit_filter)
def _test_units_contribution_filter(qs, user, unit_filter): result = UnitContributionFilter(qs, user=user).filter(unit_filter) for item in result: assert item in qs assert result.count() == result.distinct().count() user_subs_overwritten = [ "my_submissions_overwritten", "user_submissions_overwritten"] if unit_filter == "suggestions": assert ( result.count() == qs.filter( suggestion__state__name="pending").distinct().count()) return elif not user: assert result.count() == 0 return elif unit_filter in ["my_suggestions", "user_suggestions"]: expected = qs.filter( suggestion__state__name="pending", suggestion__user=user).distinct() elif unit_filter == "user_suggestions_accepted": expected = qs.filter( suggestion__state__name="accepted", suggestion__user=user).distinct() elif unit_filter == "user_suggestions_rejected": expected = qs.filter( suggestion__state__name="rejected", suggestion__user=user).distinct() elif unit_filter in ["my_submissions", "user_submissions"]: expected = qs.filter(change__submitted_by=user) elif unit_filter in user_subs_overwritten: # lets calc this long hand # first submissions that have been added with no suggestion user_edit_subs = Submission.objects.filter( type__in=SubmissionTypes.EDIT_TYPES).filter( suggestion__isnull=True).filter( submitter=user).values_list("unit_id", flat=True) # next the suggestions that are accepted and the user is this user user_suggestions = Suggestion.objects.filter( state__name="accepted", user=user).values_list("unit_id", flat=True) expected = qs.filter( id__in=( set(user_edit_subs) | set(user_suggestions))).exclude(change__submitted_by=user) assert ( list(expected.order_by("pk")) == list(result.order_by("pk")))
def _test_units_contribution_filter(qs, user, unit_filter): result = UnitContributionFilter(qs, user=user).filter(unit_filter) for item in result: assert item in qs assert result.count() == result.distinct().count() user_subs_overwritten = [ "my_submissions_overwritten", "user_submissions_overwritten"] if unit_filter == "suggestions": assert ( result.count() == qs.filter( suggestion__state=SuggestionStates.PENDING).distinct().count()) return elif not user: assert result.count() == 0 return elif unit_filter in ["my_suggestions", "user_suggestions"]: expected = qs.filter( suggestion__state=SuggestionStates.PENDING, suggestion__user=user).distinct() elif unit_filter == "user_suggestions_accepted": expected = qs.filter( suggestion__state=SuggestionStates.ACCEPTED, suggestion__user=user).distinct() elif unit_filter == "user_suggestions_rejected": expected = qs.filter( suggestion__state=SuggestionStates.REJECTED, suggestion__user=user).distinct() elif unit_filter in ["my_submissions", "user_submissions"]: expected = qs.filter( submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES).distinct() elif unit_filter in user_subs_overwritten: expected = qs.filter( submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES) expected = expected.exclude(submitted_by=user).distinct() assert ( list(expected.order_by("pk")) == list(result.order_by("pk")))