Skip to content

Commit 36007a0

Browse files
committed
Move name truncating functionality in a utility function and add tests
for it. This way it's easier to test those functions and also gives us more flexibility to introduce different truncating mechanism (e.g. truncate in the middle instead of at the end).
1 parent 2877ec4 commit 36007a0

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

codespeed/tests/test_views_data.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# -*- coding: utf-8 -*-
22
from django.test import TestCase
3+
from django.test import override_settings
34

45
from codespeed.models import Project, Executable, Branch, Revision
56
from codespeed.views import getbaselineexecutables
7+
from codespeed.views_data import get_sanitized_executable_name_for_timeline_view
8+
from codespeed.views_data import get_sanitized_executable_name_for_comparison_view
69

710

811
class TestGetBaselineExecutables(TestCase):
@@ -38,3 +41,25 @@ def test_get_baseline_executables(self):
3841
Revision.objects.create(commitid='3', branch=self.branch)
3942
result = getbaselineexecutables()
4043
self.assertEqual(len(result), 3)
44+
45+
46+
class UtilityFunctionsTestCase(TestCase):
47+
@override_settings(TIMELINE_EXECUTABLE_NAME_MAX_LEN=22)
48+
def test_get_sanitized_executable_name_for_timeline_view(self):
49+
executable = Executable(name='a' * 22)
50+
name = get_sanitized_executable_name_for_timeline_view(executable)
51+
self.assertEqual(name, 'a' * 22)
52+
53+
executable = Executable(name='a' * 25)
54+
name = get_sanitized_executable_name_for_timeline_view(executable)
55+
self.assertEqual(name, 'a' * 22 + '...')
56+
57+
@override_settings(COMPARISON_EXECUTABLE_NAME_MAX_LEN=20)
58+
def test_get_sanitized_executable_name_for_comparision_view(self):
59+
executable = Executable(name='b' * 20)
60+
name = get_sanitized_executable_name_for_comparison_view(executable)
61+
self.assertEqual(name, 'b' * 20)
62+
63+
executable = Executable(name='b' * 25)
64+
name = get_sanitized_executable_name_for_comparison_view(executable)
65+
self.assertEqual(name, 'b' * 20 + '...')

codespeed/views_data.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,10 @@ def getbaselineexecutables():
5858
}]
5959
executables = Executable.objects.select_related('project')
6060
revs = Revision.objects.exclude(tag="").select_related('branch__project')
61-
maxlen = getattr(settings, 'TIMELINE_EXECUTABLE_NAME_MAX_LEN', 20)
6261
for rev in revs:
6362
# Add executables that correspond to each tagged revision.
6463
for exe in [e for e in executables if e.project == rev.branch.project]:
65-
exestring = str(exe)
66-
if len(exestring) > maxlen:
67-
exestring = str(exe)[0:maxlen] + "..."
64+
exestring = get_sanitized_executable_name_for_timeline_view(exe)
6865
name = exestring + " " + rev.tag
6966
key = str(exe.id) + "+" + str(rev.id)
7067
baseline.append({
@@ -116,7 +113,6 @@ def getcomparisonexes():
116113
for proj in Project.objects.all():
117114
executables = []
118115
executablekeys = []
119-
maxlen = getattr(settings, 'COMPARISON_EXECUTABLE_NAME_MAX_LEN', 20)
120116
# add all tagged revs for any project
121117
for exe in baselines:
122118
if exe['key'] != "none" and exe['executable'].project == proj:
@@ -134,9 +130,7 @@ def getcomparisonexes():
134130
# because we already added tagged revisions
135131
if rev.tag == "":
136132
for exe in Executable.objects.filter(project=proj):
137-
exestring = str(exe)
138-
if len(exestring) > maxlen:
139-
exestring = str(exe)[0:maxlen] + "..."
133+
exestring = get_sanitized_executable_name_for_comparison_view(exe)
140134
name = exestring + " latest"
141135
if branch.name != 'default':
142136
name += " in branch '" + branch.name + "'"
@@ -260,3 +254,47 @@ def get_stats_with_defaults(res):
260254
if res.q3 is not None:
261255
q3 = res.q3
262256
return q1, q3, val_max, val_min
257+
258+
259+
def get_sanitized_executable_name_for_timeline_view(executable):
260+
"""
261+
Return sanitized executable name which is used in the sidebar in the
262+
Timeline and Changes view.
263+
264+
If the name is longer than settings.TIMELINE_EXECUTABLE_NAME_MAX_LEN,
265+
the name will be truncated to that length and "..." appended to it.
266+
267+
:param executable: Executable object.
268+
:type executable: :class:``codespeed.models.Executable``
269+
270+
:return: ``str``
271+
"""
272+
maxlen = getattr(settings, 'TIMELINE_EXECUTABLE_NAME_MAX_LEN', 20)
273+
274+
exestring = str(executable)
275+
if len(exestring) > maxlen:
276+
exestring = str(executable)[0:maxlen] + "..."
277+
278+
return exestring
279+
280+
281+
def get_sanitized_executable_name_for_comparison_view(executable):
282+
"""
283+
Return sanitized executable name which is used in the sidebar in the
284+
comparision view.
285+
286+
If the name is longer than settings.COMPARISON_EXECUTABLE_NAME_MAX_LEN,
287+
the name will be truncated to that length and "..." appended to it.
288+
289+
:param executable: Executable object.
290+
:type executable: :class:``codespeed.models.Executable``
291+
292+
:return: ``str``
293+
"""
294+
maxlen = getattr(settings, 'COMPARISON_EXECUTABLE_NAME_MAX_LEN', 22)
295+
296+
exestring = str(executable)
297+
if len(exestring) > maxlen:
298+
exestring = str(executable)[0:maxlen] + "..."
299+
300+
return exestring

0 commit comments

Comments
 (0)