Skip to content

Commit

Permalink
[db] Remove Bug model, replace with LinkReference. Closes #1029
Browse files Browse the repository at this point in the history
tracking bugs will be performed linking to their URLs instead of
keeping a reference to their ID and trying to construct the URL
on the fly.

Obsoletes #320
  • Loading branch information
atodorov committed Sep 19, 2019
1 parent 62d7d9f commit 24f252e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 70 deletions.
7 changes: 5 additions & 2 deletions tcms/core/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.decorators import permission_required

from tcms.testcases.models import TestCase, Bug
from tcms.testcases.models import TestCase
from tcms.testcases.models import TestCaseTag
from tcms.testplans.models import TestPlan, TestPlanTag
from tcms.testruns.models import TestExecution, TestRunTag
from tcms.core.helpers.comments import add_comment
from tcms.core.utils.validations import validate_bug_id
from tcms.core.contrib.linkreference.models import LinkReference


def tags(request):
Expand Down Expand Up @@ -249,7 +250,9 @@ def update_bugs_to_caseruns(request):
bug_system_id=bug_system_id,
bz_external_track=bz_external_track)
else:
bugs = Bug.objects.filter(bug_id__in=bug_ids)
# fixme: the filter condition here is wrong. We no longer have
# separate bug_ids, we have list of primary keys for LR objects
bugs = LinkReference.objects.filter(bug_id__in=bug_ids)
for run in runs:
for bug in bugs:
if bug.case_run_id == run.pk:
Expand Down
31 changes: 31 additions & 0 deletions tcms/testcases/migrations/0010_remove_bug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.db import migrations


def forward_copy_data(apps, schema_editor):
Bug = apps.get_model('testcases', 'Bug')
LinkReference = apps.get_model('linkreference', 'LinkReference')

for bug in Bug.objects.all():
LinkReference.objects.create(
execution_id=bug.case_run_id,
name="%s %s" % (bug.bug_system.name, bug.bug_id),
url=bug.bug_system.url_reg_exp % bug.bug_id,
is_defect=True,
)


class Migration(migrations.Migration):

dependencies = [
('testcases', '0009_populate_missing_text_history'),
('linkreference', '0002_update_fields'),
]

operations = [
# copy the data from the related model
migrations.RunPython(forward_copy_data),

migrations.DeleteModel(
name='Bug',
),
]
34 changes: 0 additions & 34 deletions tcms/testcases/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,40 +452,6 @@ def tracker_from_url(cls, url):
return None


class Bug(TCMSActionModel):
bug_id = models.CharField(max_length=25)
case_run = models.ForeignKey('testruns.TestExecution', default=None, blank=True, null=True,
related_name='case_run_bug', on_delete=models.CASCADE)
case = models.ForeignKey(TestCase, related_name='case_bug', on_delete=models.CASCADE)
bug_system = models.ForeignKey(BugSystem, default=1, on_delete=models.CASCADE)
summary = models.CharField(max_length=255, blank=True, null=True)
description = models.TextField(blank=True, null=True)

class Meta:
unique_together = (('bug_id', 'case_run', 'case'),
('bug_id', 'case_run'))

def unique_error_message(self, model_class, unique_check):
"""Specific to invalid bug id"""
bug_id_uniques = (('bug_id', 'case_run', 'case'),
('bug_id', 'case_run'))
if unique_check in bug_id_uniques:
return 'Bug %d exists in run %d already.' % (self.bug_id, self.case_run.pk)
return super().unique_error_message(model_class, unique_check)

def __str__(self):
return self.bug_id

def get_name(self):
if self.summary:
return self.summary

return self.bug_id

def get_full_url(self):
return self.bug_system.url_reg_exp % self.bug_id


class TestCaseEmailSettings(models.Model):
case = models.OneToOneField(TestCase, related_name='email_settings', on_delete=models.CASCADE)
notify_on_case_update = models.BooleanField(default=True)
Expand Down
6 changes: 3 additions & 3 deletions tcms/testcases/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from tcms.testcases.models import TestCase, TestCasePlan
from tcms.testcases.views import get_selected_testcases
from tcms.testruns.models import TestExecutionStatus
from tcms.tests.factories import BugFactory
from tcms.tests.factories import LinkReferenceFactory
from tcms.tests.factories import TestCaseFactory
from tcms.tests import BasePlanCase, BaseCaseRun, remove_perm_from_user
from tcms.tests import user_should_have_perm
Expand Down Expand Up @@ -66,8 +66,8 @@ def test_user_in_default_group_sees_comments(self):
)

def test_user_sees_bugs(self):
bug_1 = BugFactory()
bug_2 = BugFactory()
bug_1 = LinkReferenceFactory()
bug_2 = LinkReferenceFactory()

self.execution_1.add_bug(bug_1.bug_id, bug_1.bug_system.pk)
self.execution_1.add_bug(bug_2.bug_id, bug_2.bug_system.pk)
Expand Down
24 changes: 10 additions & 14 deletions tcms/testruns/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from django.contrib.contenttypes.models import ContentType
from django_comments.models import Comment

from tcms.testcases.models import Bug
from tcms.testruns.models import TestExecution
from tcms.testruns.models import TestExecutionStatus
from tcms.core.contrib.linkreference.models import LinkReference


def get_run_bug_ids(run_id):
Expand All @@ -19,12 +19,10 @@ def get_run_bug_ids(run_id):
:return: list of pairs of bug ID and bug link.
:rtype: list
"""
return Bug.objects.values(
'bug_id',
'bug_system',
'bug_system__tracker_type',
'bug_system__url_reg_exp'
).distinct().filter(case_run__run=run_id)
return LinkReference.objects.filter(
execution__run=run_id,
is_defect=True,
).distinct()


class TestExecutionDataMixin:
Expand Down Expand Up @@ -65,14 +63,12 @@ def get_execution_bugs(run_pk):
:rtype: dict
"""

bugs = Bug.objects.filter(
case_run__run=run_pk
).values(
'case_run',
'bug_id',
'bug_system__url_reg_exp'
).order_by('case_run')
bugs = LinkReference.objects.filter(
execution__run=run_pk,
is_defect=True,
).order_by('execution')

# fixme: everything below needs updating to work with LR
rows = []
for row in bugs:
row['bug_url'] = row['bug_system__url_reg_exp'] % row['bug_id']
Expand Down
13 changes: 7 additions & 6 deletions tcms/testruns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from tcms.core.models import TCMSActionModel
from tcms.core.history import KiwiHistoricalRecords
from tcms.core.contrib.linkreference.models import LinkReference
from tcms.testcases.models import Bug
from tcms.xmlrpc.serializer import TestExecutionXMLRPCSerializer
from tcms.xmlrpc.serializer import TestRunXMLRPCSerializer
from tcms.xmlrpc.utils import distinct_filter
Expand Down Expand Up @@ -142,9 +141,10 @@ def get_bug_count(self):
"""
# note fom Django docs: A count() call performs a SELECT COUNT(*)
# behind the scenes !!!
return Bug.objects.filter(
case_run__run=self.pk
).values('bug_id').distinct().count()
return LinkReference.objects.filter(
execution__run=self.pk,
is_defect=True,
).distinct().count()

def get_percentage(self, count):
case_run_count = self.total_num_caseruns
Expand Down Expand Up @@ -359,8 +359,9 @@ def remove_bug(self, bug_id, run_id=None):
self.case.remove_bug(bug_id=bug_id, run_id=run_id)

def get_bugs(self):
return Bug.objects.filter(
case_run__case_run_id=self.case_run_id)
return LinkReference.objects.filter(
execution=self.pk,
is_defect=True)

def get_bugs_count(self):
return self.get_bugs().count()
Expand Down
1 change: 1 addition & 0 deletions tcms/testruns/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ def get_context_data(self, **kwargs):

execution_bugs = []
bug_system_types = {}
# fixme: make this loop work with LinkReference objects
for _bug in get_run_bug_ids(run.pk):
# format the bug URLs based on DB settings
execution_bugs.append((
Expand Down
17 changes: 6 additions & 11 deletions tcms/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from tcms.management.models import Priority
from tcms.testcases.models import TestCaseStatus
from tcms.testcases.models import BugSystem
from tcms.testruns.models import TestExecutionStatus


Expand Down Expand Up @@ -294,19 +293,15 @@ class Meta:
build = factory.SubFactory(BuildFactory)


class BugFactory(DjangoModelFactory):
class LinkReferenceFactory(DjangoModelFactory):

class Meta:
model = 'testcases.Bug'
model = 'linkreference.LinkReference'

bug_id = factory.Sequence(lambda n: n)
summary = factory.LazyAttribute(lambda obj: 'Summary of bug %s' % obj.bug_id)
description = ''
bug_system = factory.LazyFunction(
lambda: BugSystem.objects.first() # pylint: disable=unnecessary-lambda
)
case_run = factory.SubFactory(TestExecutionFactory)
case = factory.SubFactory(TestCaseFactory)
execution = factory.SubFactory(TestExecutionFactory)
name = factory.Sequence(lambda n: "Bug %d" % n)
url = factory.Sequence(lambda n: "https://example.com/link/%d/" % n)
is_defect = True


class TestRunTagFactory(DjangoModelFactory):
Expand Down

0 comments on commit 24f252e

Please sign in to comment.