Skip to content

Commit

Permalink
[db] Rename TestCase.case_id to TestCase.id
Browse files Browse the repository at this point in the history
INTRODUCES BACKWARDS INCOMPATIBLE CHANGES:

API methods returning serialized TestCase objects rename
the 'case_id' field to 'id'. The affected methods are:

  -- TestCase.add_component()
  -- TestCase.create()
  -- TestCase.filter()
  -- TestCase.update()
  -- TestRun.get_cases()
  • Loading branch information
atodorov committed Feb 7, 2020
1 parent 2215d03 commit 1e9f887
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion tcms/rpc/api/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_components(case_id):
:rtype: list(dict)
:raises: TestCase.DoesNotExist if missing test case matching PK
"""
test_case = TestCase.objects.get(case_id=case_id)
test_case = TestCase.objects.get(pk=case_id)

component_ids = test_case.component.values_list('id', flat=True)
query = {'id__in': component_ids}
Expand Down
2 changes: 1 addition & 1 deletion tcms/rpc/api/testrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def get_cases(run_id):
extra_info = dict(((row['case'], row) for row in qs.iterator()))

for case in tcs_serializer:
info = extra_info[case['case_id']]
info = extra_info[case['id']]
case['execution_id'] = info['pk']
case['status'] = info['status__name']

Expand Down
2 changes: 1 addition & 1 deletion tcms/rpc/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ class TestCaseRPCSerializer(QuerySetBasedRPCSerializer):

values_fields_mapping = {
'arguments': ('arguments', do_nothing),
'case_id': ('case_id', do_nothing),
'id': ('id', do_nothing),
'create_date': ('create_date', datetime_to_str),
'extra_link': ('extra_link', do_nothing),
'is_automated': ('is_automated', do_nothing),
Expand Down
4 changes: 2 additions & 2 deletions tcms/rpc/tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ class MockTestPlanSerializer(QuerySetBasedRPCSerializer):


class MockTestCaseSerializer(QuerySetBasedRPCSerializer):
primary_key = 'case_id'
primary_key = 'id'

values_fields_mapping = {
'arguments': ('arguments', do_nothing),
'case_id': ('case_id', do_nothing),
'id': ('id', do_nothing),
'create_date': ('create_date', datetime_to_str),

'author': ('author_id', do_nothing),
Expand Down
6 changes: 3 additions & 3 deletions tcms/rpc/tests/test_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_update_text_and_product(self):

self.testcase.refresh_from_db()

self.assertEqual(updated['case_id'], self.testcase.pk)
self.assertEqual(updated['id'], self.testcase.pk)
self.assertEqual('This was updated', self.testcase.summary)
self.assertEqual('new TC text', self.testcase.text)
# FK for author not passed above. Make sure it didn't change!
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_passes_with_valid_data(self):

tc_from_db = TestCase.objects.get(summary=result['summary'], text=result['text'])

self.assertEqual(result['case_id'], tc_from_db.pk)
self.assertEqual(result['id'], tc_from_db.pk)
# author field is auto-configured if not passed
self.assertEqual(result['author'], tc_from_db.author.username)
self.assertEqual(self.api_user, tc_from_db.author)
Expand All @@ -178,7 +178,7 @@ def test_author_can_be_specified(self):

tc_from_db = TestCase.objects.get(summary=result['summary'], author=new_author)

self.assertEqual(result['case_id'], tc_from_db.pk)
self.assertEqual(result['id'], tc_from_db.pk)
self.assertEqual(new_author, tc_from_db.author)

def test_fails_when_mandatory_fields_not_specified(self):
Expand Down
2 changes: 1 addition & 1 deletion tcms/search/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def order_case_queryset(cases, field, asc=False):
by calling order_by on it.
"""
orderable_fields = (
'case_id', 'summary', 'author__username',
'id', 'summary', 'author__username',
'default_tester__username', 'priority',
'is_automated', 'category__name', 'case_status',
'create_date'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function drawTable(selector, data) {
}

function renderTestCaseColumn(data) {
return `<a href="/case/${data.case_id}">TC-${data.case_id}</a>: ${data.case_summary}`;
return `<a href="/case/${data.id}">TC-${data.id}</a>: ${data.case_summary}`;
}

function renderFailedExecutionsColumn(data) {
Expand Down
2 changes: 1 addition & 1 deletion tcms/templates/case/printable.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h2 id="plan_cases">{% trans "Test Cases" %}</h2>
<div class="thick-line"></div>
{% for test_case in test_cases %}
<div style="clear: left">
<h3>TC-{{ test_case.case_id }}: {{ test_case.summary }}</h3>
<h3>TC-{{ test_case.pk }}: {{ test_case.summary }}</h3>
<div class="thin-line"></div>
<div class="case-stage">
{{ test_case.text|markdown2html }}
Expand Down
6 changes: 3 additions & 3 deletions tcms/templates/plan/cases_rows.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
class="checkbox case_selector" {% if not selected_case_ids or test_case.pk in selected_case_ids %}checked{% endif %} />
</td>
<td>
<a href="{% url "testcases-get" test_case.case_id %}">{{ test_case.pk }}</a>
<a href="{% url "testcases-get" test_case.pk %}">{{ test_case.pk }}</a>
</td>
<td>
<a href="{% url "testcases-get" test_case.case_id %}">{{ test_case.summary }}</a>
<a href="{% url "testcases-get" test_case.pk %}">{{ test_case.summary }}</a>
</td>
<td class="col_author_content"><a href="{% url "tcms-profile" test_case.author.username %}">{{ test_case.author }}</a></td>
<td class="col_author_content">
Expand Down Expand Up @@ -56,7 +56,7 @@
</td>
<td align="center" >
{% if perms.testcases.change_testcase %}
<a class="editlink" href="{% url "testcases-edit" test_case.case_id %}">Edit</a>
<a class="editlink" href="{% url "testcases-edit" test_case.pk %}">Edit</a>
{% else %}
<span class="editlink grey">{% trans "Edit" %}</span>
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions tcms/templates/plan/search_case.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ <h2>{{ test_plan.name }}</h2>
<tbody>
{% for test_case in test_cases %}
<tr class="{% cycle 'odd' 'even' %}">
<td align="left"><input id="id_checkbox_case_{{ forloop.counter }}" type="checkbox" name="case" value="{{ test_case.case_id }}"></td>
<td><a href="{% url "testcases-get" test_case.case_id %}">{{ test_case.case_id }}</a></td>
<td align="left"><input id="id_checkbox_case_{{ forloop.counter }}" type="checkbox" name="case" value="{{ test_case.pk }}"></td>
<td><a href="{% url "testcases-get" test_case.pk %}">{{ test_case.pk }}</a></td>
<td valign="top" >{{ test_case.summary }}</td>
<td valign="top" ><a href="{% url "tcms-profile" test_case.author.username %}">{{ test_case.author.username }}</a></td>
<td valign="top" >
Expand Down
2 changes: 1 addition & 1 deletion tcms/templates/run/table_executions.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{% for execution, tester, assignee, priority_value, comments_count, bugs_count in executions %}
<tr class="{% cycle 'odd' 'even' %} {% ifequal execution.assignee_id user.pk %} mine {% endifequal %}">
<td>
<input type="checkbox" name="execution" value="{{ execution.pk }}" class="caserun_selector" data-case_id="{{ execution.case.pk }}" title="Select/Unselect" />
<input type="checkbox" name="execution" value="{{ execution.pk }}" class="caserun_selector" data-case_id="{{ execution.case_id }}" title="Select/Unselect" />
<input type="hidden" name="case" value="{{ execution.case.pk }}" />
<input type="hidden" name="case_text_version" value="{{ execution.case_text_version }}" />
</td>
Expand Down
26 changes: 26 additions & 0 deletions tcms/testcases/migrations/0013_remove_db_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

class Migration(migrations.Migration):

atomic = False

dependencies = [
('testcases', '0012_remove_autofield_max_length'),
]
Expand All @@ -20,4 +22,28 @@ class Migration(migrations.Migration):
name='create_date',
field=models.DateTimeField(auto_now_add=True),
),

# rename case_id to id and let Django handle this field
migrations.RenameField(
model_name='testcase',
old_name='case_id',
new_name='id',
),
migrations.RenameField(
model_name='historicaltestcase',
old_name='case_id',
new_name='id',
),
migrations.AlterField(
model_name='testcase',
name='id',
field=models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='historicaltestcase',
name='id',
field=models.IntegerField(auto_created=True, blank=True,
db_index=True, verbose_name='ID'),
),
]
8 changes: 2 additions & 6 deletions tcms/testcases/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def __str__(self):
class TestCase(TCMSActionModel):
history = KiwiHistoricalRecords()

case_id = models.AutoField(primary_key=True)
create_date = models.DateTimeField(auto_now_add=True)
is_automated = models.BooleanField(default=False)
script = models.TextField(blank=True, null=True)
Expand Down Expand Up @@ -238,16 +237,13 @@ class TestCasePlan(models.Model):
case = models.ForeignKey(TestCase, on_delete=models.CASCADE)
sortkey = models.IntegerField(null=True, blank=True)

# TODO: create FOREIGN KEY constraint on plan_id and case_id individually
# in database.

class Meta:
unique_together = ('plan', 'case')


class TestCaseComponent(models.Model):
case = models.ForeignKey(TestCase, on_delete=models.CASCADE) # case_id
component = models.ForeignKey('management.Component', on_delete=models.CASCADE) # component_id
case = models.ForeignKey(TestCase, on_delete=models.CASCADE)
component = models.ForeignKey('management.Component', on_delete=models.CASCADE)


class TestCaseTag(models.Model):
Expand Down
4 changes: 2 additions & 2 deletions tcms/testcases/static/testcases/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ $(document).ready(function() {
dataTableJsonRPC('TestCase.filter', params, callback, pre_process_data);
},
columns: [
{ data: "case_id" },
{ data: "id" },
{
data: null,
render: function (data, type, full, meta) {
return '<a href="/case/'+ data.case_id + '/" target="_parent">' + escapeHTML(data.summary) + '</a>';
return '<a href="/case/'+ data.id + '/" target="_parent">' + escapeHTML(data.summary) + '</a>';
}
},
{ data: "create_date"},
Expand Down
4 changes: 2 additions & 2 deletions tcms/testcases/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ def printable(request, # pylint: disable=missing-permission-required
test_plan = None

tcs = TestCase.objects.filter(**case_filter).values(
'case_id', 'summary', 'text'
).order_by('case_id')
'pk', 'summary', 'text'
).order_by('pk')

context_data = {
'test_plan': test_plan,
Expand Down
4 changes: 2 additions & 2 deletions tcms/testplans/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def post(self, request, pk):
for case_id in request.POST.getlist('case'):
case_ids.append(int(case_id))

cases = TestCase.objects.filter(case_id__in=case_ids).only('pk')
cases = TestCase.objects.filter(pk__in=case_ids).only('pk')
for case in cases:
plan.add_case(case)

Expand Down Expand Up @@ -417,7 +417,7 @@ def post(self, request, pk):
'default_tester__email', 'case_status__name',
'priority__value'
).exclude(
case_id__in=plan.case.values_list('case_id', flat=True))
pk__in=plan.case.values_list('id', flat=True))

context = {
'test_plan': plan,
Expand Down
2 changes: 1 addition & 1 deletion tcms/testruns/templates/testruns/mutable.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<td>
<input type="hidden" name="case" value="{{ test_case.pk }}">
<a href="{% url 'testcases-get' test_case.pk %}">
TC-{{ test_case.case_id }}: {{ test_case.summary }}
TC-{{ test_case.pk }}: {{ test_case.summary }}
</a>
</td>
<td>
Expand Down
6 changes: 3 additions & 3 deletions tcms/testruns/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def post(self, request):
test_cases = get_selected_testcases(request)
test_plan = TestPlan.objects.get(pk=plan_id)

# note: ordered by case_id for test_show_create_new_run_page()
# note: ordered by pk for test_show_create_new_run_page()
tcs_values = test_cases.select_related('author',
'case_status',
'category',
'priority').order_by('case_id')
'priority').order_by('pk')

if request.POST.get('POSTING_TO_CREATE'):
form = NewRunForm(request.POST)
Expand Down Expand Up @@ -436,7 +436,7 @@ def post(self, request, pk):
test_plan = test_run.plan
test_cases = test_run.plan.case.filter(case_status__name='CONFIRMED').select_related(
'default_tester').only('default_tester_id').filter(
case_id__in=test_cases_ids)
pk__in=test_cases_ids)

if request.POST.get('_use_plan_sortkey'):
test_case_pks = (test_case.pk for test_case in test_cases)
Expand Down

0 comments on commit 1e9f887

Please sign in to comment.