1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2026-01-22 14:41:39 +00:00

Apply flake8 checks to tests

This commit is contained in:
Virgil Dupras
2016-05-29 15:02:39 -04:00
parent 9ed4b7abf0
commit 130581db53
13 changed files with 798 additions and 809 deletions

View File

@@ -1,9 +1,7 @@
# Created By: Virgil Dupras
# Created On: 2006/02/23
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# Copyright 2016 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html
import io
@@ -22,21 +20,21 @@ class TestCaseResultsEmpty:
def setup_method(self, method):
self.app = DupeGuru()
self.results = self.app.results
def test_apply_invalid_filter(self):
# If the applied filter is an invalid regexp, just ignore the filter.
self.results.apply_filter('[') # invalid
self.test_stat_line() # make sure that the stats line isn't saying we applied a '[' filter
def test_stat_line(self):
eq_("0 / 0 (0.00 B / 0.00 B) duplicates marked.",self.results.stat_line)
eq_("0 / 0 (0.00 B / 0.00 B) duplicates marked.", self.results.stat_line)
def test_groups(self):
eq_(0,len(self.results.groups))
eq_(0, len(self.results.groups))
def test_get_group_of_duplicate(self):
assert self.results.get_group_of_duplicate('foo') is None
def test_save_to_xml(self):
f = io.BytesIO()
self.results.save_to_xml(f)
@@ -44,15 +42,15 @@ class TestCaseResultsEmpty:
doc = ET.parse(f)
root = doc.getroot()
eq_('results', root.tag)
def test_is_modified(self):
assert not self.results.is_modified
def test_is_modified_after_setting_empty_group(self):
# Don't consider results as modified if they're empty
self.results.groups = []
assert not self.results.is_modified
def test_save_to_same_name_as_folder(self, tmpdir):
# Issue #149
# When saving to a filename that already exists, the file is overwritten. However, when
@@ -64,108 +62,108 @@ class TestCaseResultsEmpty:
folderpath.mkdir()
self.results.save_to_xml(str(folderpath)) # no crash
assert tmpdir.join('[000] foo').check()
class TestCaseResultsWithSomeGroups:
def setup_method(self, method):
self.app = DupeGuru()
self.results = self.app.results
self.objects,self.matches,self.groups = GetTestGroups()
self.objects, self.matches, self.groups = GetTestGroups()
self.results.groups = self.groups
def test_stat_line(self):
eq_("0 / 3 (0.00 B / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("0 / 3 (0.00 B / 1.01 KB) duplicates marked.", self.results.stat_line)
def test_groups(self):
eq_(2,len(self.results.groups))
eq_(2, len(self.results.groups))
def test_get_group_of_duplicate(self):
for o in self.objects:
g = self.results.get_group_of_duplicate(o)
assert isinstance(g, engine.Group)
assert o in g
assert self.results.get_group_of_duplicate(self.groups[0]) is None
def test_remove_duplicates(self):
g1,g2 = self.results.groups
g1, g2 = self.results.groups
self.results.remove_duplicates([g1.dupes[0]])
eq_(2,len(g1))
eq_(2, len(g1))
assert g1 in self.results.groups
self.results.remove_duplicates([g1.ref])
eq_(2,len(g1))
eq_(2, len(g1))
assert g1 in self.results.groups
self.results.remove_duplicates([g1.dupes[0]])
eq_(0,len(g1))
eq_(0, len(g1))
assert g1 not in self.results.groups
self.results.remove_duplicates([g2.dupes[0]])
eq_(0,len(g2))
eq_(0, len(g2))
assert g2 not in self.results.groups
eq_(0,len(self.results.groups))
eq_(0, len(self.results.groups))
def test_remove_duplicates_with_ref_files(self):
g1,g2 = self.results.groups
g1, g2 = self.results.groups
self.objects[0].is_ref = True
self.objects[1].is_ref = True
self.results.remove_duplicates([self.objects[2]])
eq_(0,len(g1))
eq_(0, len(g1))
assert g1 not in self.results.groups
def test_make_ref(self):
g = self.results.groups[0]
d = g.dupes[0]
self.results.make_ref(d)
assert d is g.ref
def test_sort_groups(self):
self.results.make_ref(self.objects[1]) #We want to make the 1024 sized object to go ref.
g1,g2 = self.groups
g1, g2 = self.groups
self.results.sort_groups('size')
assert self.results.groups[0] is g2
assert self.results.groups[1] is g1
self.results.sort_groups('size', False)
assert self.results.groups[0] is g1
assert self.results.groups[1] is g2
def test_set_groups_when_sorted(self):
self.results.make_ref(self.objects[1]) #We want to make the 1024 sized object to go ref.
self.results.sort_groups('size')
objects,matches,groups = GetTestGroups()
g1,g2 = groups
objects, matches, groups = GetTestGroups()
g1, g2 = groups
g1.switch_ref(objects[1])
self.results.groups = groups
assert self.results.groups[0] is g2
assert self.results.groups[1] is g1
def test_get_dupe_list(self):
eq_([self.objects[1],self.objects[2],self.objects[4]],self.results.dupes)
eq_([self.objects[1], self.objects[2], self.objects[4]], self.results.dupes)
def test_dupe_list_is_cached(self):
assert self.results.dupes is self.results.dupes
def test_dupe_list_cache_is_invalidated_when_needed(self):
o1,o2,o3,o4,o5 = self.objects
eq_([o2,o3,o5],self.results.dupes)
o1, o2, o3, o4, o5 = self.objects
eq_([o2, o3, o5], self.results.dupes)
self.results.make_ref(o2)
eq_([o1,o3,o5],self.results.dupes)
objects,matches,groups = GetTestGroups()
o1,o2,o3,o4,o5 = objects
eq_([o1, o3, o5], self.results.dupes)
objects, matches, groups = GetTestGroups()
o1, o2, o3, o4, o5 = objects
self.results.groups = groups
eq_([o2,o3,o5],self.results.dupes)
eq_([o2, o3, o5], self.results.dupes)
def test_dupe_list_sort(self):
o1,o2,o3,o4,o5 = self.objects
o1, o2, o3, o4, o5 = self.objects
o1.size = 5
o2.size = 4
o3.size = 3
o4.size = 2
o5.size = 1
self.results.sort_dupes('size')
eq_([o5,o3,o2],self.results.dupes)
eq_([o5, o3, o2], self.results.dupes)
self.results.sort_dupes('size', False)
eq_([o2,o3,o5],self.results.dupes)
eq_([o2, o3, o5], self.results.dupes)
def test_dupe_list_remember_sort(self):
o1,o2,o3,o4,o5 = self.objects
o1, o2, o3, o4, o5 = self.objects
o1.size = 5
o2.size = 4
o3.size = 3
@@ -173,40 +171,40 @@ class TestCaseResultsWithSomeGroups:
o5.size = 1
self.results.sort_dupes('size')
self.results.make_ref(o2)
eq_([o5,o3,o1],self.results.dupes)
eq_([o5, o3, o1], self.results.dupes)
def test_dupe_list_sort_delta_values(self):
o1,o2,o3,o4,o5 = self.objects
o1, o2, o3, o4, o5 = self.objects
o1.size = 10
o2.size = 2 #-8
o3.size = 3 #-7
o4.size = 20
o5.size = 1 #-19
self.results.sort_dupes('size', delta=True)
eq_([o5,o2,o3],self.results.dupes)
eq_([o5, o2, o3], self.results.dupes)
def test_sort_empty_list(self):
#There was an infinite loop when sorting an empty list.
app = DupeGuru()
r = app.results
r.sort_dupes('name')
eq_([],r.dupes)
eq_([], r.dupes)
def test_dupe_list_update_on_remove_duplicates(self):
o1,o2,o3,o4,o5 = self.objects
eq_(3,len(self.results.dupes))
o1, o2, o3, o4, o5 = self.objects
eq_(3, len(self.results.dupes))
self.results.remove_duplicates([o2])
eq_(2,len(self.results.dupes))
eq_(2, len(self.results.dupes))
def test_is_modified(self):
# Changing the groups sets the modified flag
assert self.results.is_modified
def test_is_modified_after_save_and_load(self):
# Saving/Loading a file sets the modified flag back to False
def get_file(path):
return [f for f in self.objects if str(f.path) == path][0]
f = io.BytesIO()
self.results.save_to_xml(f)
assert not self.results.is_modified
@@ -214,13 +212,13 @@ class TestCaseResultsWithSomeGroups:
f.seek(0)
self.results.load_from_xml(f, get_file)
assert not self.results.is_modified
def test_is_modified_after_removing_all_results(self):
# Removing all results sets the is_modified flag to false.
self.results.mark_all()
self.results.perform_on_marked(lambda x:None, True)
self.results.perform_on_marked(lambda x: None, True)
assert not self.results.is_modified
def test_group_of_duplicate_after_removal(self):
# removing a duplicate also removes it from the dupe:group map.
dupe = self.results.groups[1].dupes[0]
@@ -229,7 +227,7 @@ class TestCaseResultsWithSomeGroups:
assert self.results.get_group_of_duplicate(dupe) is None
# also remove group ref
assert self.results.get_group_of_duplicate(ref) is None
def test_dupe_list_sort_delta_values_nonnumeric(self):
# When sorting dupes in delta mode on a non-numeric column, our first sort criteria is if
# the string is the same as its ref.
@@ -239,7 +237,7 @@ class TestCaseResultsWithSomeGroups:
g2r.name = g2d1.name = "aaa"
self.results.sort_dupes('name', delta=True)
eq_("aaa", self.results.dupes[2].name)
def test_dupe_list_sort_delta_values_nonnumeric_case_insensitive(self):
# Non-numeric delta sorting comparison is case insensitive
g1r, g1d1, g1d2, g2r, g2d1 = self.objects
@@ -252,92 +250,92 @@ class TestCaseResultsWithSavedResults:
def setup_method(self, method):
self.app = DupeGuru()
self.results = self.app.results
self.objects,self.matches,self.groups = GetTestGroups()
self.objects, self.matches, self.groups = GetTestGroups()
self.results.groups = self.groups
self.f = io.BytesIO()
self.results.save_to_xml(self.f)
self.f.seek(0)
def test_is_modified(self):
# Saving a file sets the modified flag back to False
assert not self.results.is_modified
def test_is_modified_after_load(self):
# Loading a file sets the modified flag back to False
def get_file(path):
return [f for f in self.objects if str(f.path) == path][0]
self.results.groups = self.groups # sets the flag back
self.results.load_from_xml(self.f, get_file)
assert not self.results.is_modified
def test_is_modified_after_remove(self):
# Removing dupes sets the modified flag
self.results.remove_duplicates([self.results.groups[0].dupes[0]])
assert self.results.is_modified
def test_is_modified_after_make_ref(self):
# Making a dupe ref sets the modified flag
self.results.make_ref(self.results.groups[0].dupes[0])
assert self.results.is_modified
class TestCaseResultsMarkings:
def setup_method(self, method):
self.app = DupeGuru()
self.results = self.app.results
self.objects,self.matches,self.groups = GetTestGroups()
self.objects, self.matches, self.groups = GetTestGroups()
self.results.groups = self.groups
def test_stat_line(self):
eq_("0 / 3 (0.00 B / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("0 / 3 (0.00 B / 1.01 KB) duplicates marked.", self.results.stat_line)
self.results.mark(self.objects[1])
eq_("1 / 3 (1.00 KB / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("1 / 3 (1.00 KB / 1.01 KB) duplicates marked.", self.results.stat_line)
self.results.mark_invert()
eq_("2 / 3 (2.00 B / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("2 / 3 (2.00 B / 1.01 KB) duplicates marked.", self.results.stat_line)
self.results.mark_invert()
self.results.unmark(self.objects[1])
self.results.mark(self.objects[2])
self.results.mark(self.objects[4])
eq_("2 / 3 (2.00 B / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("2 / 3 (2.00 B / 1.01 KB) duplicates marked.", self.results.stat_line)
self.results.mark(self.objects[0]) #this is a ref, it can't be counted
eq_("2 / 3 (2.00 B / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("2 / 3 (2.00 B / 1.01 KB) duplicates marked.", self.results.stat_line)
self.results.groups = self.groups
eq_("0 / 3 (0.00 B / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("0 / 3 (0.00 B / 1.01 KB) duplicates marked.", self.results.stat_line)
def test_with_ref_duplicate(self):
self.objects[1].is_ref = True
self.results.groups = self.groups
assert not self.results.mark(self.objects[1])
self.results.mark(self.objects[2])
eq_("1 / 2 (1.00 B / 2.00 B) duplicates marked.",self.results.stat_line)
eq_("1 / 2 (1.00 B / 2.00 B) duplicates marked.", self.results.stat_line)
def test_perform_on_marked(self):
def log_object(o):
log.append(o)
return True
log = []
self.results.mark_all()
self.results.perform_on_marked(log_object,False)
self.results.perform_on_marked(log_object, False)
assert self.objects[1] in log
assert self.objects[2] in log
assert self.objects[4] in log
eq_(3,len(log))
eq_(3, len(log))
log = []
self.results.mark_none()
self.results.mark(self.objects[4])
self.results.perform_on_marked(log_object,True)
eq_(1,len(log))
self.results.perform_on_marked(log_object, True)
eq_(1, len(log))
assert self.objects[4] in log
eq_(1,len(self.results.groups))
eq_(1, len(self.results.groups))
def test_perform_on_marked_with_problems(self):
def log_object(o):
log.append(o)
if o is self.objects[1]:
raise EnvironmentError('foobar')
log = []
self.results.mark_all()
assert self.results.is_marked(self.objects[1])
@@ -352,55 +350,55 @@ class TestCaseResultsMarkings:
dupe, msg = self.results.problems[0]
assert dupe is self.objects[1]
eq_(msg, 'foobar')
def test_perform_on_marked_with_ref(self):
def log_object(o):
log.append(o)
return True
log = []
self.objects[0].is_ref = True
self.objects[1].is_ref = True
self.results.mark_all()
self.results.perform_on_marked(log_object,True)
self.results.perform_on_marked(log_object, True)
assert self.objects[1] not in log
assert self.objects[2] in log
assert self.objects[4] in log
eq_(2,len(log))
eq_(0,len(self.results.groups))
eq_(2, len(log))
eq_(0, len(self.results.groups))
def test_perform_on_marked_remove_objects_only_at_the_end(self):
def check_groups(o):
eq_(3,len(g1))
eq_(2,len(g2))
eq_(3, len(g1))
eq_(2, len(g2))
return True
g1,g2 = self.results.groups
g1, g2 = self.results.groups
self.results.mark_all()
self.results.perform_on_marked(check_groups,True)
eq_(0,len(g1))
eq_(0,len(g2))
eq_(0,len(self.results.groups))
self.results.perform_on_marked(check_groups, True)
eq_(0, len(g1))
eq_(0, len(g2))
eq_(0, len(self.results.groups))
def test_remove_duplicates(self):
g1 = self.results.groups[0]
self.results.mark(g1.dupes[0])
eq_("1 / 3 (1.00 KB / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("1 / 3 (1.00 KB / 1.01 KB) duplicates marked.", self.results.stat_line)
self.results.remove_duplicates([g1.dupes[1]])
eq_("1 / 2 (1.00 KB / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("1 / 2 (1.00 KB / 1.01 KB) duplicates marked.", self.results.stat_line)
self.results.remove_duplicates([g1.dupes[0]])
eq_("0 / 1 (0.00 B / 1.00 B) duplicates marked.",self.results.stat_line)
eq_("0 / 1 (0.00 B / 1.00 B) duplicates marked.", self.results.stat_line)
def test_make_ref(self):
g = self.results.groups[0]
d = g.dupes[0]
self.results.mark(d)
eq_("1 / 3 (1.00 KB / 1.01 KB) duplicates marked.",self.results.stat_line)
eq_("1 / 3 (1.00 KB / 1.01 KB) duplicates marked.", self.results.stat_line)
self.results.make_ref(d)
eq_("0 / 3 (0.00 B / 3.00 B) duplicates marked.",self.results.stat_line)
eq_("0 / 3 (0.00 B / 3.00 B) duplicates marked.", self.results.stat_line)
self.results.make_ref(d)
eq_("0 / 3 (0.00 B / 3.00 B) duplicates marked.",self.results.stat_line)
eq_("0 / 3 (0.00 B / 3.00 B) duplicates marked.", self.results.stat_line)
def test_SaveXML(self):
self.results.mark(self.objects[1])
self.results.mark_invert()
@@ -417,11 +415,11 @@ class TestCaseResultsMarkings:
d1, d2 = g2.getiterator('file')
eq_('n', d1.get('marked'))
eq_('y', d2.get('marked'))
def test_LoadXML(self):
def get_file(path):
return [f for f in self.objects if str(f.path) == path][0]
self.objects[4].name = 'ibabtu 2' #we can't have 2 files with the same path
self.results.mark(self.objects[1])
self.results.mark_invert()
@@ -430,13 +428,13 @@ class TestCaseResultsMarkings:
f.seek(0)
app = DupeGuru()
r = Results(app)
r.load_from_xml(f,get_file)
r.load_from_xml(f, get_file)
assert not r.is_marked(self.objects[0])
assert not r.is_marked(self.objects[1])
assert r.is_marked(self.objects[2])
assert not r.is_marked(self.objects[3])
assert r.is_marked(self.objects[4])
class TestCaseResultsXML:
def setup_method(self, method):
@@ -444,13 +442,13 @@ class TestCaseResultsXML:
self.results = self.app.results
self.objects, self.matches, self.groups = GetTestGroups()
self.results.groups = self.groups
def get_file(self, path): # use this as a callback for load_from_xml
return [o for o in self.objects if o.path == path][0]
def test_save_to_xml(self):
self.objects[0].is_ref = True
self.objects[0].words = [['foo','bar']]
self.objects[0].words = [['foo', 'bar']]
f = io.BytesIO()
self.results.save_to_xml(f)
f.seek(0)
@@ -460,34 +458,34 @@ class TestCaseResultsXML:
eq_(2, len(root))
eq_(2, len([c for c in root if c.tag == 'group']))
g1, g2 = root
eq_(6,len(g1))
eq_(3,len([c for c in g1 if c.tag == 'file']))
eq_(3,len([c for c in g1 if c.tag == 'match']))
eq_(6, len(g1))
eq_(3, len([c for c in g1 if c.tag == 'file']))
eq_(3, len([c for c in g1 if c.tag == 'match']))
d1, d2, d3 = [c for c in g1 if c.tag == 'file']
eq_(op.join('basepath','foo bar'),d1.get('path'))
eq_(op.join('basepath','bar bleh'),d2.get('path'))
eq_(op.join('basepath','foo bleh'),d3.get('path'))
eq_('y',d1.get('is_ref'))
eq_('n',d2.get('is_ref'))
eq_('n',d3.get('is_ref'))
eq_('foo,bar',d1.get('words'))
eq_('bar,bleh',d2.get('words'))
eq_('foo,bleh',d3.get('words'))
eq_(3,len(g2))
eq_(2,len([c for c in g2 if c.tag == 'file']))
eq_(1,len([c for c in g2 if c.tag == 'match']))
eq_(op.join('basepath', 'foo bar'), d1.get('path'))
eq_(op.join('basepath', 'bar bleh'), d2.get('path'))
eq_(op.join('basepath', 'foo bleh'), d3.get('path'))
eq_('y', d1.get('is_ref'))
eq_('n', d2.get('is_ref'))
eq_('n', d3.get('is_ref'))
eq_('foo,bar', d1.get('words'))
eq_('bar,bleh', d2.get('words'))
eq_('foo,bleh', d3.get('words'))
eq_(3, len(g2))
eq_(2, len([c for c in g2 if c.tag == 'file']))
eq_(1, len([c for c in g2 if c.tag == 'match']))
d1, d2 = [c for c in g2 if c.tag == 'file']
eq_(op.join('basepath','ibabtu'),d1.get('path'))
eq_(op.join('basepath','ibabtu'),d2.get('path'))
eq_('n',d1.get('is_ref'))
eq_('n',d2.get('is_ref'))
eq_('ibabtu',d1.get('words'))
eq_('ibabtu',d2.get('words'))
eq_(op.join('basepath', 'ibabtu'), d1.get('path'))
eq_(op.join('basepath', 'ibabtu'), d2.get('path'))
eq_('n', d1.get('is_ref'))
eq_('n', d2.get('is_ref'))
eq_('ibabtu', d1.get('words'))
eq_('ibabtu', d2.get('words'))
def test_LoadXML(self):
def get_file(path):
return [f for f in self.objects if str(f.path) == path][0]
self.objects[0].is_ref = True
self.objects[4].name = 'ibabtu 2' #we can't have 2 files with the same path
f = io.BytesIO()
@@ -495,76 +493,76 @@ class TestCaseResultsXML:
f.seek(0)
app = DupeGuru()
r = Results(app)
r.load_from_xml(f,get_file)
eq_(2,len(r.groups))
g1,g2 = r.groups
eq_(3,len(g1))
r.load_from_xml(f, get_file)
eq_(2, len(r.groups))
g1, g2 = r.groups
eq_(3, len(g1))
assert g1[0].is_ref
assert not g1[1].is_ref
assert not g1[2].is_ref
assert g1[0] is self.objects[0]
assert g1[1] is self.objects[1]
assert g1[2] is self.objects[2]
eq_(['foo','bar'],g1[0].words)
eq_(['bar','bleh'],g1[1].words)
eq_(['foo','bleh'],g1[2].words)
eq_(2,len(g2))
eq_(['foo', 'bar'], g1[0].words)
eq_(['bar', 'bleh'], g1[1].words)
eq_(['foo', 'bleh'], g1[2].words)
eq_(2, len(g2))
assert not g2[0].is_ref
assert not g2[1].is_ref
assert g2[0] is self.objects[3]
assert g2[1] is self.objects[4]
eq_(['ibabtu'],g2[0].words)
eq_(['ibabtu'],g2[1].words)
eq_(['ibabtu'], g2[0].words)
eq_(['ibabtu'], g2[1].words)
def test_LoadXML_with_filename(self, tmpdir):
def get_file(path):
return [f for f in self.objects if str(f.path) == path][0]
filename = str(tmpdir.join('dupeguru_results.xml'))
self.objects[4].name = 'ibabtu 2' #we can't have 2 files with the same path
self.results.save_to_xml(filename)
app = DupeGuru()
r = Results(app)
r.load_from_xml(filename,get_file)
eq_(2,len(r.groups))
r.load_from_xml(filename, get_file)
eq_(2, len(r.groups))
def test_LoadXML_with_some_files_that_dont_exist_anymore(self):
def get_file(path):
if path.endswith('ibabtu 2'):
return None
return [f for f in self.objects if str(f.path) == path][0]
self.objects[4].name = 'ibabtu 2' #we can't have 2 files with the same path
f = io.BytesIO()
self.results.save_to_xml(f)
f.seek(0)
app = DupeGuru()
r = Results(app)
r.load_from_xml(f,get_file)
eq_(1,len(r.groups))
eq_(3,len(r.groups[0]))
r.load_from_xml(f, get_file)
eq_(1, len(r.groups))
eq_(3, len(r.groups[0]))
def test_LoadXML_missing_attributes_and_bogus_elements(self):
def get_file(path):
return [f for f in self.objects if str(f.path) == path][0]
root = ET.Element('foobar') #The root element shouldn't matter, really.
group_node = ET.SubElement(root, 'group')
dupe_node = ET.SubElement(group_node, 'file') #Perfectly correct file
dupe_node.set('path', op.join('basepath','foo bar'))
dupe_node.set('path', op.join('basepath', 'foo bar'))
dupe_node.set('is_ref', 'y')
dupe_node.set('words', 'foo,bar')
dupe_node.set('words', 'foo, bar')
dupe_node = ET.SubElement(group_node, 'file') #is_ref missing, default to 'n'
dupe_node.set('path',op.join('basepath','foo bleh'))
dupe_node.set('words','foo,bleh')
dupe_node.set('path', op.join('basepath', 'foo bleh'))
dupe_node.set('words', 'foo, bleh')
dupe_node = ET.SubElement(group_node, 'file') #words are missing, valid.
dupe_node.set('path',op.join('basepath','bar bleh'))
dupe_node.set('path', op.join('basepath', 'bar bleh'))
dupe_node = ET.SubElement(group_node, 'file') #path is missing, invalid.
dupe_node.set('words','foo,bleh')
dupe_node.set('words', 'foo, bleh')
dupe_node = ET.SubElement(group_node, 'foobar') #Invalid element name
dupe_node.set('path',op.join('basepath','bar bleh'))
dupe_node.set('is_ref','y')
dupe_node.set('words','bar,bleh')
dupe_node.set('path', op.join('basepath', 'bar bleh'))
dupe_node.set('is_ref', 'y')
dupe_node.set('words', 'bar, bleh')
match_node = ET.SubElement(group_node, 'match') # match pointing to a bad index
match_node.set('first', '42')
match_node.set('second', '45')
@@ -582,21 +580,21 @@ class TestCaseResultsXML:
app = DupeGuru()
r = Results(app)
r.load_from_xml(f, get_file)
eq_(1,len(r.groups))
eq_(3,len(r.groups[0]))
eq_(1, len(r.groups))
eq_(3, len(r.groups[0]))
def test_xml_non_ascii(self):
def get_file(path):
if path == op.join('basepath','\xe9foo bar'):
if path == op.join('basepath', '\xe9foo bar'):
return objects[0]
if path == op.join('basepath','bar bleh'):
if path == op.join('basepath', 'bar bleh'):
return objects[1]
objects = [NamedObject("\xe9foo bar",True),NamedObject("bar bleh",True)]
objects = [NamedObject("\xe9foo bar", True), NamedObject("bar bleh", True)]
matches = engine.getmatches(objects) #we should have 5 matches
groups = engine.get_groups(matches) #We should have 2 groups
for g in groups:
g.prioritize(lambda x:objects.index(x)) #We want the dupes to be in the same order as the list is
g.prioritize(lambda x: objects.index(x)) #We want the dupes to be in the same order as the list is
app = DupeGuru()
results = Results(app)
results.groups = groups
@@ -605,20 +603,20 @@ class TestCaseResultsXML:
f.seek(0)
app = DupeGuru()
r = Results(app)
r.load_from_xml(f,get_file)
r.load_from_xml(f, get_file)
g = r.groups[0]
eq_("\xe9foo bar",g[0].name)
eq_(['efoo','bar'],g[0].words)
eq_("\xe9foo bar", g[0].name)
eq_(['efoo', 'bar'], g[0].words)
def test_load_invalid_xml(self):
f = io.BytesIO()
f.write(b'<this is invalid')
f.seek(0)
app = DupeGuru()
r = Results(app)
r.load_from_xml(f,None)
eq_(0,len(r.groups))
r.load_from_xml(f, None)
eq_(0, len(r.groups))
def test_load_non_existant_xml(self):
app = DupeGuru()
r = Results(app)
@@ -626,8 +624,8 @@ class TestCaseResultsXML:
r.load_from_xml('does_not_exist.xml', None)
except IOError:
self.fail()
eq_(0,len(r.groups))
eq_(0, len(r.groups))
def test_remember_match_percentage(self):
group = self.groups[0]
d1, d2, d3 = group
@@ -652,7 +650,7 @@ class TestCaseResultsXML:
group.switch_ref(d2)
match = group.get_match_of(d3) #d2 - d3
eq_(46, match[2])
def test_save_and_load(self):
# previously, when reloading matches, they wouldn't be reloaded as namedtuples
f = io.BytesIO()
@@ -660,17 +658,17 @@ class TestCaseResultsXML:
f.seek(0)
self.results.load_from_xml(f, self.get_file)
first(self.results.groups[0].matches).percentage
def test_apply_filter_works_on_paths(self):
# apply_filter() searches on the whole path, not just on the filename.
self.results.apply_filter('basepath')
eq_(len(self.results.groups), 2)
def test_save_xml_with_invalid_characters(self):
# Don't crash when saving files that have invalid xml characters in their path
self.objects[0].name = 'foo\x19'
self.results.save_to_xml(io.BytesIO()) # don't crash
class TestCaseResultsFilter:
def setup_method(self, method):
@@ -679,40 +677,40 @@ class TestCaseResultsFilter:
self.objects, self.matches, self.groups = GetTestGroups()
self.results.groups = self.groups
self.results.apply_filter(r'foo')
def test_groups(self):
eq_(1, len(self.results.groups))
assert self.results.groups[0] is self.groups[0]
def test_dupes(self):
# There are 2 objects matching. The first one is ref. Only the 3rd one is supposed to be in dupes.
eq_(1, len(self.results.dupes))
assert self.results.dupes[0] is self.objects[2]
def test_cancel_filter(self):
self.results.apply_filter(None)
eq_(3, len(self.results.dupes))
eq_(2, len(self.results.groups))
def test_dupes_reconstructed_filtered(self):
# make_ref resets self.__dupes to None. When it's reconstructed, we want it filtered
dupe = self.results.dupes[0] #3rd object
self.results.make_ref(dupe)
eq_(1, len(self.results.dupes))
assert self.results.dupes[0] is self.objects[0]
def test_include_ref_dupes_in_filter(self):
# When only the ref of a group match the filter, include it in the group
self.results.apply_filter(None)
self.results.apply_filter(r'foo bar')
eq_(1, len(self.results.groups))
eq_(0, len(self.results.dupes))
def test_filters_build_on_one_another(self):
self.results.apply_filter(r'bar')
eq_(1, len(self.results.groups))
eq_(0, len(self.results.dupes))
def test_stat_line(self):
expected = '0 / 1 (0.00 B / 1.00 B) duplicates marked. filter: foo'
eq_(expected, self.results.stat_line)
@@ -722,7 +720,7 @@ class TestCaseResultsFilter:
self.results.apply_filter(None)
expected = '0 / 3 (0.00 B / 1.01 KB) duplicates marked.'
eq_(expected, self.results.stat_line)
def test_mark_count_is_filtered_as_well(self):
self.results.apply_filter(None)
# We don't want to perform mark_all() because we want the mark list to contain objects
@@ -731,18 +729,18 @@ class TestCaseResultsFilter:
self.results.apply_filter(r'foo')
expected = '1 / 1 (1.00 B / 1.00 B) duplicates marked. filter: foo'
eq_(expected, self.results.stat_line)
def test_mark_all_only_affects_filtered_items(self):
# When performing actions like mark_all() and mark_none in a filtered environment, only mark
# items that are actually in the filter.
self.results.mark_all()
self.results.apply_filter(None)
eq_(self.results.mark_count, 1)
def test_sort_groups(self):
self.results.apply_filter(None)
self.results.make_ref(self.objects[1]) # to have the 1024 b obkect as ref
g1,g2 = self.groups
g1, g2 = self.groups
self.results.apply_filter('a') # Matches both group
self.results.sort_groups('size')
assert self.results.groups[0] is g2
@@ -754,43 +752,43 @@ class TestCaseResultsFilter:
self.results.apply_filter('a')
assert self.results.groups[1] is g2
assert self.results.groups[0] is g1
def test_set_group(self):
#We want the new group to be filtered
self.objects, self.matches, self.groups = GetTestGroups()
self.results.groups = self.groups
eq_(1, len(self.results.groups))
assert self.results.groups[0] is self.groups[0]
def test_load_cancels_filter(self, tmpdir):
def get_file(path):
return [f for f in self.objects if str(f.path) == path][0]
filename = str(tmpdir.join('dupeguru_results.xml'))
self.objects[4].name = 'ibabtu 2' #we can't have 2 files with the same path
self.results.save_to_xml(filename)
app = DupeGuru()
r = Results(app)
r.apply_filter('foo')
r.load_from_xml(filename,get_file)
eq_(2,len(r.groups))
r.load_from_xml(filename, get_file)
eq_(2, len(r.groups))
def test_remove_dupe(self):
self.results.remove_duplicates([self.results.dupes[0]])
self.results.apply_filter(None)
eq_(2,len(self.results.groups))
eq_(2,len(self.results.dupes))
eq_(2, len(self.results.groups))
eq_(2, len(self.results.dupes))
self.results.apply_filter('ibabtu')
self.results.remove_duplicates([self.results.dupes[0]])
self.results.apply_filter(None)
eq_(1,len(self.results.groups))
eq_(1,len(self.results.dupes))
eq_(1, len(self.results.groups))
eq_(1, len(self.results.dupes))
def test_filter_is_case_insensitive(self):
self.results.apply_filter(None)
self.results.apply_filter('FOO')
eq_(1, len(self.results.dupes))
def test_make_ref_on_filtered_out_doesnt_mess_stats(self):
# When filtered, a group containing filtered out dupes will display them as being reference.
# When calling make_ref on such a dupe, the total size and dupecount stats gets messed up
@@ -804,7 +802,7 @@ class TestCaseResultsFilter:
self.results.apply_filter(None) # Now let's make sure our unfiltered results aren't fucked up
expected = '0 / 3 (0.00 B / 3.00 B) duplicates marked.'
eq_(expected, self.results.stat_line)
class TestCaseResultsRefFile:
def setup_method(self, method):
@@ -814,8 +812,8 @@ class TestCaseResultsRefFile:
self.objects[0].is_ref = True
self.objects[1].is_ref = True
self.results.groups = self.groups
def test_stat_line(self):
expected = '0 / 2 (0.00 B / 2.00 B) duplicates marked.'
eq_(expected, self.results.stat_line)