Skip to content

Commit

Permalink
Remove Test Run export functionality. Fixes #79
Browse files Browse the repository at this point in the history
The Export To XML and Export To CSV buttons have been removed.
The export functionality is only useful for integration with other
systems and we have an API layer for that. Any future work in the
export area should be done on the API side.
  • Loading branch information
atodorov committed Nov 2, 2017
1 parent 08491f3 commit d1c170b
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 187 deletions.
6 changes: 0 additions & 6 deletions tcms/static/js/testrun_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,6 @@ Nitrate.TestRuns.Details.on_load = function() {
jQ('#btn_delete').bind('click', function() {
window.location.href = jQ(this).data('param');
});
jQ('#btn_export_csv').bind('click', function() {
window.location.href = jQ(this).data('param') + '?format=csv&' + jQ('#id_form_case_runs').serialize();
});
jQ('#btn_export_xml').bind('click', function() {
window.location.href = jQ(this).data('param') + '?format=xml&' + jQ('#id_form_case_runs').serialize();
});
jQ('.js-remove-tag').bind('click', function() {
var params = jQ(this).data('params');
removeRuntag(jQ('.js-tag-ul')[0], params[0], params[1]);
Expand Down
2 changes: 0 additions & 2 deletions tcms/templates/run/get.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@
<input id="btn_delete" type="button" value="Delete" title="Delete the run" data-param="{% url 'testruns-delete' test_run.pk %}">
{% endifequal %}
{% endifequal %}
<input id="btn_export_csv" type="button" value="Export To CSV" title="Export cases in this run" data-param="{% url 'testruns-export' test_run.pk %}" >
<input id="btn_export_xml" type="button" value="Export To XML" title="Export cases in this run" data-param="{% url 'testruns-export' test_run.pk %}" >
{% endif %}
</div>
<h2 id="display_title">{{ test_run.summary }}</h2>
Expand Down
111 changes: 0 additions & 111 deletions tcms/testruns/helpers/serializer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# -*- coding: utf-8 -*-
'''
A serializer to import/export between model objects and file formats.
'''

import csv

from xml.sax.saxutils import escape


Expand All @@ -17,108 +11,3 @@ def escape_entities(text):
@rtype: str
'''
return escape(text, {'"': '&quot;'}) if text else text


# TODO: rewrite export module to export TestCaseRuns, TestPlans and other
# Nitrate objects.


class TCR2File(object):
'''
Write TestCaseRun queryset into CSV or XML.
'''
ROOT = 'testcaseruns'
HEADERS = ("Case Run ID", "Case ID",
"Category", "Status", "Summary",
"script", "Automated", "Log Link",
"Bug IDs")

def __init__(self, tcrs):
self.root = self.ROOT
self.headers = self.HEADERS

qs = tcrs.select_related('case',
'case_run_status')
self.tcrs = qs.only('case__summary',
'case__script',
'case__is_automated',
'case__category__name',
'case_run_status__name')
self.rows = []

def tcr_attrs_in_a_list(self, tcr):
if tcr.case.script is None:
case_script = ''
else:
case_script = tcr.case.script.encode('utf-8')

line = (tcr.pk, tcr.case.pk,
tcr.case.category.name.encode('utf-8'),
tcr.case_run_status.name.encode('utf-8'),
tcr.case.summary.encode('utf-8'),
case_script,
tcr.case.is_automated,
self.log_links(tcr),
self.bug_ids(tcr))
return line

def log_links(self, tcr):
'''
Wrap log links into a single cell by
joining log links.
'''
return '\n'.join(
(url.encode('utf-8')
for url in tcr.links.values_list('url', flat=True))
)

def bug_ids(self, tcr):
'''
Wrap bugs into a single cell by
joining bug IDs.
'''
bug_ids = tcr.case_run_bug.values_list('bug_id', flat=True)
return ' '.join((str(pk) for pk in bug_ids.iterator()))

def tcrs_in_rows(self):
tcr_attrs_in_a_list = self.tcr_attrs_in_a_list
for tcr in self.tcrs.iterator():
row = tcr_attrs_in_a_list(tcr)
yield row

def write_to_csv(self, fileobj):
writer = csv.writer(fileobj)
writer.writerow(self.headers)
writer.writerows(self.tcrs_in_rows())

def write_to_xml(self, output):
write_to_output = output.write
tcr_start_elem = u'<testcaserun case_run_id="%d" case_id="%d" ' \
u'category="%s" status="%s" summary="%s" ' \
u'scripts="%s" automated="%s">'

def write_loglink(link):
return write_to_output(
u'<loglink name="%s" url="%s" />' % (link.name, link.url))

def write_bug(bug):
return write_to_output(u'<bug id="%s" />' % bug.bug_id)

write_to_output(u'<%s>' % self.root)
for tcr in self.tcrs.iterator():
summary = escape_entities(tcr.case.summary)
script = escape_entities(tcr.case.script)
write_to_output(tcr_start_elem % (tcr.pk, tcr.case.pk,
tcr.case.category.name or u'',
tcr.case_run_status.name,
summary or u'',
script or u'',
str(tcr.case.is_automated)))
write_to_output(u'<loglinks>')
map(write_loglink, tcr.links.iterator())
write_to_output(u'</loglinks>')
write_to_output(u'<bugs>')
map(write_bug, tcr.case_run_bug.iterator())
write_to_output(u'</bugs>')
write_to_output(u'</testcaserun>')
write_to_output(u'</%s>' % self.root)
40 changes: 0 additions & 40 deletions tcms/testruns/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# -*- coding: utf-8 -*-

import os
import json
import http.client
from datetime import timedelta
from mock import patch
from xml.etree import ElementTree

from django.utils import formats
from django.urls import reverse
Expand Down Expand Up @@ -1023,43 +1020,6 @@ def test_delete_env_value_from_runs(self):
self.assertFalse(rel.exists())


class TestExportTestRunCases(BaseCaseRun):
"""Test export view method to export test case runs"""

@classmethod
def setUpTestData(cls):
super(TestExportTestRunCases, cls).setUpTestData()

cls.export_url = reverse('testruns-export',
args=[cls.test_run.pk])

@patch('tcms.testruns.views.time.strftime', return_value='2017-06-17')
def test_export_to_xml_file(self, strftime):
response = self.client.get(self.export_url, {'format': 'xml'})
self.assertEqual(
'attachment; filename=tcms-testcase-runs-2017-06-17.xml',
response['Content-Disposition'])

@patch('tcms.testruns.views.time.strftime', return_value='2017-06-17')
def test_export_to_csv_file(self, strftime):
response = self.client.get(self.export_url, {'format': 'csv'})
self.assertEqual(
'attachment; filename=tcms-testcase-runs-2017-06-17.csv',
response['Content-Disposition'])

def test_export_all_case_runs_to_csv_by_default(self):
response = self.client.get(self.export_url, {'format': 'csv'})
self.assertEqual(self.test_run.case_run.count(),
# Do not count header line
len(str(response.content, encoding=settings.DEFAULT_CHARSET).strip().split(os.linesep)) - 1)

def test_export_all_case_runs_to_xml_by_default(self):
response = self.client.get(self.export_url, {'format': 'xml'})
xmldoc = ElementTree.fromstring(response.content)
case_run_nodes = xmldoc.findall('testcaserun')
self.assertEqual(self.test_run.case_run.count(), len(case_run_nodes))


class TestBugActions(BaseCaseRun):
"""Test bug view method"""

Expand Down
1 change: 0 additions & 1 deletion tcms/testruns/urls/run_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@
url(r'^(?P<run_id>\d+)/cc/$', views.cc, name='testruns-cc'),
url(r'^(?P<run_id>\d+)/update/$', views.update_case_run_text,
name='testruns-update_case_run_text'),
url(r'^(?P<run_id>\d+)/export/$', views.export, name='testruns-export'),
]
27 changes: 0 additions & 27 deletions tcms/testruns/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import datetime
import itertools
import json
import time
from urllib.parse import urlencode
from functools import reduce

Expand Down Expand Up @@ -47,7 +46,6 @@
from tcms.testruns.data import TestCaseRunDataMixin
from tcms.testruns.forms import MulitpleRunsCloneForm, PlanFilterRunForm
from tcms.testruns.forms import NewRunForm, SearchRunForm, EditRunForm, RunCloneForm
from tcms.testruns.helpers.serializer import TCR2File
from tcms.testruns.models import TestRun, TestCaseRun, TestCaseRunStatus, TCMSEnvRunValueMap
from tcms.issuetracker.types import IssueTrackerType

Expand Down Expand Up @@ -1386,31 +1384,6 @@ def update_case_run_text(request, run_id):
)


@require_GET
def export(request, run_id):
timestamp_str = time.strftime('%Y-%m-%d')
case_runs = request.GET.getlist('case_run')
format = request.GET.get('format', 'csv')
# Export selected case runs
if case_runs:
tcrs = TestCaseRun.objects.filter(case_run_id__in=case_runs)
# Export all case runs
else:
tcrs = TestCaseRun.objects.filter(run=run_id)
response = HttpResponse()
writer = TCR2File(tcrs)
if format == 'csv':
writer.write_to_csv(response)
response['Content-Disposition'] = \
'attachment; filename=tcms-testcase-runs-%s.csv' % timestamp_str
else:
writer.write_to_xml(response)
response['Content-Disposition'] = \
'attachment; filename=tcms-testcase-runs-%s.xml' % timestamp_str

return response


@require_GET
def env_value(request):
"""Run environment property edit function"""
Expand Down

0 comments on commit d1c170b

Please sign in to comment.