Skip to content

Commit 3655568

Browse files
committed
Add object check to Delta class constructor #27
* Also fixed five failing tests. Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent f06ba66 commit 3655568

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

src/deltacode/__init__.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from collections import OrderedDict
2929

30+
from deltacode.models import File
3031
from deltacode.models import Scan
3132
from deltacode import utils
3233

@@ -171,8 +172,8 @@ class Delta(object):
171172
"""
172173
def __init__(self, new_file=None, old_file=None, delta_type=None):
173174
# TODO: add check to ensure both are File objects
174-
self.new_file = new_file
175-
self.old_file = old_file
175+
self.new_file = new_file if new_file else File()
176+
self.old_file = old_file if old_file else File()
176177
self.category = delta_type
177178

178179
# Change the Delta object's 'category' attribute to
@@ -187,9 +188,6 @@ def _license_diff(self, cutoff_score=50):
187188
'license change' if those details differ and the cutoff score test is
188189
satisfied.
189190
"""
190-
if not self.new_file or not self.old_file:
191-
return
192-
193191
new_licenses = self.new_file.licenses or []
194192
new_keys = set(l.key for l in new_licenses if l.score >= cutoff_score)
195193

@@ -204,9 +202,6 @@ def to_dict(self):
204202
Check the 'category' attribute of the Delta object and return an
205203
OrderedDict comprising the 'category' and 'path' of the object.
206204
"""
207-
if self.new_file is None and self.old_file is None:
208-
return
209-
210205
if self.category == 'added':
211206
return OrderedDict([
212207
('category', 'added'),

tests/test_deltacode.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@ def test_Delta_license_diff_one_None(self):
493493
def test_Delta_license_diff_None_files(self):
494494
delta = deltacode.Delta(None, None, None)
495495

496-
assert delta.new_file == None
497-
assert delta.old_file == None
496+
assert type(delta.new_file) == type(models.File())
497+
assert type(delta.old_file) == type(models.File())
498498
assert delta.category == None
499499

500500
def test_DeltaCode_license_modified_low_score(self):
@@ -678,15 +678,15 @@ def test_Delta_to_dict_unmodified(self):
678678
def test_Delta_to_dict_empty(self):
679679
delta = deltacode.Delta()
680680

681-
assert delta.to_dict() == None
681+
assert delta.to_dict() == OrderedDict([('category', 'unmodified'), ('path', None)])
682682

683683
def test_Delta_create_object_removed(self):
684684
new = None
685685
old = models.File({'path': 'path/removed.txt'})
686686

687687
delta = deltacode.Delta(new, old, 'removed')
688688

689-
assert delta.new_file == None
689+
assert type(delta.new_file) == type(models.File())
690690
assert delta.old_file.path == 'path/removed.txt'
691691
assert delta.category == 'removed'
692692

@@ -697,7 +697,7 @@ def test_Delta_create_object_added(self):
697697
delta = deltacode.Delta(new, old, 'added')
698698

699699
assert delta.new_file.path == 'path/added.txt'
700-
assert delta.old_file == None
700+
assert type(delta.old_file) == type(models.File())
701701
assert delta.category == 'added'
702702

703703
def test_Delta_create_object_modified(self):
@@ -727,6 +727,6 @@ def test_Delta_create_object_unmodified(self):
727727
def test_Delta_create_object_empty(self):
728728
delta = deltacode.Delta()
729729

730-
assert delta.new_file == None
731-
assert delta.old_file == None
730+
assert type(delta.new_file) == type(models.File())
731+
assert type(delta.old_file) == type(models.File())
732732
assert delta.category == None

0 commit comments

Comments
 (0)