Skip to content

Commit 09db62c

Browse files
committed
Move scoring to 'Delta.determine_score()' #60
* This branch was created from 52-add-score-to-delta-object and thus incorporates recent commits for issue #52. * Scoring was previously determined in 'DeltaCode.determine_delta()', 'DeltaCode.update_deltas()' and 'Delta._license_diff()'. * Refactored tests, added five new tests. Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent 48490f7 commit 09db62c

2 files changed

Lines changed: 76 additions & 9 deletions

File tree

src/deltacode/__init__.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def determine_delta(self):
104104
try:
105105
delta_old_files = old_index[path]
106106
except KeyError:
107-
self.deltas['added'].append(Delta(new_file, None, 'added', 75))
107+
self.deltas['added'].append(Delta(new_file, None, 'added'))
108108
continue
109109

110110
# at this point, we have a delta_old_file.
@@ -113,10 +113,10 @@ def determine_delta(self):
113113
for f in delta_old_files:
114114
# TODO: make sure sha1 is NOT empty
115115
if new_file.sha1 == f.sha1:
116-
self.deltas['unmodified'].append(Delta(new_file, f, 'unmodified', 0))
116+
self.deltas['unmodified'].append(Delta(new_file, f, 'unmodified'))
117117
continue
118118
else:
119-
delta = Delta(new_file, f, 'modified', 50)
119+
delta = Delta(new_file, f, 'modified')
120120
self.deltas['modified'].append(delta)
121121

122122
# now time to find the added.
@@ -131,7 +131,7 @@ def determine_delta(self):
131131
# This file already classified as 'modified' or 'unmodified' so do nothing
132132
new_index[path]
133133
except KeyError:
134-
self.deltas['removed'].append(Delta(None, old_file, 'removed', 25))
134+
self.deltas['removed'].append(Delta(None, old_file, 'removed'))
135135
continue
136136

137137
# make sure everything is accounted for
@@ -165,7 +165,7 @@ def update_deltas(self, added, removed):
165165
Convert the matched 'added' and 'removed' Delta objects to a combined
166166
'moved' Delta object and delete the 'added' and 'removed' objects.
167167
"""
168-
self.deltas.get('moved').append(Delta(added.new_file, removed.old_file, 'moved', 0))
168+
self.deltas.get('moved').append(Delta(added.new_file, removed.old_file, 'moved'))
169169
self.deltas.get('added').remove(added)
170170
self.deltas.get('removed').remove(removed)
171171

@@ -243,6 +243,8 @@ def __init__(self, new_file=None, old_file=None, delta_type=None, score=0):
243243
if self.category == 'modified':
244244
self._license_diff()
245245

246+
self.determine_score()
247+
246248
def _license_diff(self, cutoff_score=50):
247249
"""
248250
Compare the license details for a pair of 'new' and 'old' File objects
@@ -255,25 +257,40 @@ def _license_diff(self, cutoff_score=50):
255257

256258
if len(self.new_file.licenses) > 0 and self.old_file.licenses == []:
257259
self.category = 'license info added'
258-
self.score = 70
259260
return
260261

261262
if self.new_file.licenses == [] and len(self.old_file.licenses) > 0:
262263
self.category = 'license info removed'
263-
self.score = 65
264264
return
265265

266266
new_keys = set(l.key for l in new_licenses if l.score >= cutoff_score)
267267
old_keys = set(l.key for l in old_licenses if l.score >= cutoff_score)
268268

269269
if new_keys != old_keys:
270270
self.category = 'license change'
271-
self.score = 60
271+
272+
def determine_score(self):
273+
"""
274+
Assign a score to each 'Delta' object by modifying the object's 'score'
275+
attribute based on the object's 'category' attribute.
276+
"""
277+
scores = {
278+
'added': 75,
279+
'license info added': 70,
280+
'license info removed': 65,
281+
'license change': 60,
282+
'modified': 50,
283+
'removed': 25,
284+
'moved': 0,
285+
'unmodified': 0
286+
}
287+
288+
self.score = scores.get(self.category, 0)
272289

273290
def to_dict(self):
274291
"""
275292
Check the 'category' attribute of the Delta object and return an
276-
OrderedDict comprising the 'category' and 'path' of the object.
293+
OrderedDict comprising the 'category', 'score' and 'path' of the object.
277294
"""
278295
delta = OrderedDict([
279296
('category', self.category),

tests/test_deltacode.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,7 @@ def test_Delta_create_object_removed(self):
14831483
assert type(delta.new_file) == type(models.File())
14841484
assert delta.old_file.path == 'path/removed.txt'
14851485
assert delta.category == 'removed'
1486+
assert delta.score == 25
14861487

14871488
def test_Delta_create_object_added(self):
14881489
new = models.File({'path': 'path/added.txt'})
@@ -1493,6 +1494,7 @@ def test_Delta_create_object_added(self):
14931494
assert delta.new_file.path == 'path/added.txt'
14941495
assert type(delta.old_file) == type(models.File())
14951496
assert delta.category == 'added'
1497+
assert delta.score == 75
14961498

14971499
def test_Delta_create_object_modified(self):
14981500
new = models.File({'path': 'path/modified.txt', 'sha1': 'a'})
@@ -1505,6 +1507,7 @@ def test_Delta_create_object_modified(self):
15051507
assert delta.old_file.path == 'path/modified.txt'
15061508
assert delta.old_file.sha1 == 'b'
15071509
assert delta.category == 'modified'
1510+
assert delta.score == 50
15081511

15091512
def test_Delta_create_object_unmodified(self):
15101513
new = models.File({'path': 'path/unmodified.txt', 'sha1': 'a'})
@@ -1517,6 +1520,7 @@ def test_Delta_create_object_unmodified(self):
15171520
assert delta.old_file.path == 'path/unmodified.txt'
15181521
assert delta.old_file.sha1 == 'a'
15191522
assert delta.category == 'unmodified'
1523+
assert delta.score == 0
15201524

15211525
def test_Delta_create_object_moved(self):
15221526
new = models.File({'path': 'path_new/moved.txt', 'sha1': 'a'})
@@ -1529,10 +1533,56 @@ def test_Delta_create_object_moved(self):
15291533
assert delta.old_file.path == 'path_old/moved.txt'
15301534
assert delta.old_file.sha1 == 'a'
15311535
assert delta.category == 'moved'
1536+
assert delta.score == 0
15321537

15331538
def test_Delta_create_object_empty(self):
15341539
delta = deltacode.Delta()
15351540

15361541
assert type(delta.new_file) == type(models.File())
15371542
assert type(delta.old_file) == type(models.File())
15381543
assert delta.category == ''
1544+
1545+
def test_Delta_determine_score_new_no_license_info(self):
1546+
new_file = models.File({'path': 'new/path.txt'})
1547+
old_file = models.File({'path': 'old/path.txt', 'licenses': [{'key': 'mit', 'score': 50.0}]})
1548+
1549+
result = deltacode.Delta(new_file, old_file, 'modified')
1550+
1551+
assert result.category == 'license info removed'
1552+
assert result.score == 65
1553+
1554+
def test_Delta_determine_score_new_no_license_info_below_cutoff_score(self):
1555+
new_file = models.File({'path': 'new/path.txt'})
1556+
old_file = models.File({'path': 'old/path.txt', 'licenses': [{'key': 'mit', 'score': 49.0}]})
1557+
1558+
result = deltacode.Delta(new_file, old_file, 'modified')
1559+
1560+
assert result.category == 'license info removed'
1561+
assert result.score == 65
1562+
1563+
def test_Delta_determine_score_old_no_license_info(self):
1564+
new_file = models.File({'path': 'new/path.txt', 'licenses': [{'key': 'mit', 'score': 50.0}]})
1565+
old_file = models.File({'path': 'old/path.txt'})
1566+
1567+
result = deltacode.Delta(new_file, old_file, 'modified')
1568+
1569+
assert result.category == 'license info added'
1570+
assert result.score == 70
1571+
1572+
def test_Delta_determine_score_old_no_license_info_below_cutoff_score(self):
1573+
new_file = models.File({'path': 'new/path.txt', 'licenses': [{'key': 'mit', 'score': 49.0}]})
1574+
old_file = models.File({'path': 'old/path.txt'})
1575+
1576+
result = deltacode.Delta(new_file, old_file, 'modified')
1577+
1578+
assert result.category == 'license info added'
1579+
assert result.score == 70
1580+
1581+
def test_Delta_determine_score_single_diff_multiple_keys(self):
1582+
new_file = models.File({'path': 'new/path.txt', 'licenses': [{'key': 'gpl-2.0', 'score': 100.0}, {'key': 'mit', 'score':30.0}]})
1583+
old_file = models.File({'path': 'old/path.txt', 'licenses': [{'key': 'mit', 'score': 50.0}]})
1584+
1585+
result = deltacode.Delta(new_file, old_file, 'modified')
1586+
1587+
assert result.category == 'license change'
1588+
assert result.score == 60

0 commit comments

Comments
 (0)