-
-
Notifications
You must be signed in to change notification settings - Fork 297
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
Make testcase filtering on the API side aware of the duration fields #2530
base: master
Are you sure you want to change the base?
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 |
---|---|---|
@@ -1,14 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from datetime import timedelta | ||
|
||
from django.db.models import Case, F, Q, When | ||
from django.forms import EmailField, ValidationError | ||
from django.forms.models import model_to_dict | ||
from django.utils.dateparse import parse_duration | ||
from modernrpc.core import REQUEST_KEY, rpc_method | ||
|
||
from tcms.core import helpers | ||
from tcms.core.utils import form_errors_to_list | ||
from tcms.management.models import Component, Tag | ||
from tcms.rpc import utils | ||
from tcms.rpc.api.forms.testcase import NewForm, UpdateForm | ||
from tcms.rpc.api.utils import stringify_values | ||
from tcms.rpc.decorators import permissions_required | ||
from tcms.testcases.models import TestCase, TestCasePlan | ||
|
||
|
@@ -279,8 +284,37 @@ def filter(query=None): # pylint: disable=redefined-builtin | |
if query is None: | ||
query = {} | ||
|
||
return list( | ||
TestCase.objects.filter(**query) | ||
for key, val in query.items(): | ||
if not key.startswith( | ||
("setup_duration", "testing_duration", "expected_duration") | ||
): | ||
continue | ||
|
||
try: | ||
duration = parse_duration(val) | ||
except TypeError: | ||
# val isn't a string or byte-like object | ||
# item is probably something like 'setup_duration__isnull=True' | ||
continue | ||
|
||
if duration is None: | ||
continue | ||
|
||
query[key] = duration | ||
|
||
qs = ( | ||
TestCase.objects.annotate( | ||
expected_duration=Case( | ||
When( | ||
Q(setup_duration__isnull=True) & Q(testing_duration__isnull=True), | ||
then=timedelta(0), | ||
), | ||
When(Q(setup_duration__isnull=True), then="testing_duration"), | ||
When(Q(testing_duration__isnull=True), then="setup_duration"), | ||
default=F("setup_duration") + F("testing_duration"), | ||
) | ||
) | ||
.filter(**query) | ||
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. It looks like there may be some confusion around this item. Being able to filter by the duration field is the last checklist item in #1923. That should not need special casing for setup_duration and testing_duration because these are model fields. The only special case is expected_duration. OTOH the item "API method TestCase.filter returns the value of all 3 fields - tests are updated to match" refers only to making sure that the API method TestCase.filter includes the new fields in its results. That should be much easier and require a lot less changes. 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. Oh oh. I was over thinking it. I thought the current checklist item also meant that filtering with respect to the duration fields should be possible on the API side. I will create a new PR to implement just the presence of the duration fields in the result. I'll keep this one alive (closed) so one can refer to it if we ever need to add filtering with respect to the duration fields on the API side. 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. It just occured to me that I can shorten the entire Coalesce("setup_duration", timedelta(0)) + Coalesce("testing_duration", timedelta(0)) |
||
.values( | ||
"id", | ||
"create_date", | ||
|
@@ -304,9 +338,19 @@ def filter(query=None): # pylint: disable=redefined-builtin | |
"default_tester__username", | ||
"reviewer", | ||
"reviewer__username", | ||
"setup_duration", | ||
"testing_duration", | ||
"expected_duration", | ||
) | ||
.distinct() | ||
) | ||
return [ | ||
stringify_values( | ||
testcase_dict, | ||
keys=["setup_duration", "testing_duration", "expected_duration"], | ||
) | ||
for testcase_dict in qs.iterator() | ||
] | ||
|
||
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. Here I loop through the list of dicts (representing testcases) and make sure that if the value of the duration keys are not null, they are converted from duration objects to strings as xmlrpc cannot marshal duration objects. |
||
|
||
@permissions_required("testcases.view_testcase") | ||
|
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.
I added this
for
loop because the values of the duration keys in the incoming dictquery
will always be strings as xmlrpc can't marshal them if they aredatetime.duration
objects. The loop parses these strings (if they are present) intodatetime.duration
objects -- so thatQuerySet.filter()
won't choke on them.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.
Since we don't need to filter by duration fields on the API side, then I'll take this out.
Although if someone ever mistakenly sends in a lookup based on a duration field, it will choke.