test_sweeper.py 1.76 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
# Author: Darko Poljak <darko.poljak@gmail.com>
# License: GPLv3

import unittest
6
from sweeper import Sweeper
7 8 9 10 11 12 13
import os

mydir = os.path.dirname(os.path.realpath(__file__))


class TestSweeper(unittest.TestCase):
    def test_file_dups_dups(self):
14 15
        swp = Sweeper(topdirs=[os.path.join(mydir, 'testfiles_dups')])
        dups = swp.file_dups()
16 17 18 19 20 21 22
        dups_exist = False
        for h, flist in dups.items():
            if len(flist) > 1:
                dups_exist = True
        self.assertTrue(dups_exist)

    def test_file_dups_nodups(self):
23 24
        swp = Sweeper(topdirs=[os.path.join(mydir, 'testfiles_nodups')])
        dups = swp.file_dups()
25 26 27
        for h, flist in dups.items():
            self.assertTrue(len(flist) == 1)

28 29 30
    # does not actually test safe_mode, we would need to find
    # hash collision
    def test_file_dups_safe_mode(self):
31 32 33
        swp = Sweeper(topdirs=[os.path.join(mydir, 'testfiles_dups')],
                      safe_mode=True)
        dups = swp.file_dups()
34 35 36 37 38
        for h, flist in dups.items():
            if len(flist) > 1:
                dups_exist = True
        self.assertTrue(dups_exist)

Darko Poljak's avatar
Darko Poljak committed
39
    def test_iter_file_dups_dups(self):
40
        swp = Sweeper(topdirs=[os.path.join(mydir, 'testfiles_dups')])
41
        dups_exist = False
42
        for x in swp:
43
            dups_exist = True
Darko Poljak's avatar
Darko Poljak committed
44 45 46
            filepath, h, dups = x
            self.assertNotIn(filepath, dups)
            self.assertTrue(len(dups) > 0)
47 48
        self.assertTrue(dups_exist)

Darko Poljak's avatar
Darko Poljak committed
49
    def test_iter_file_dups_nodups(self):
50
        swp = Sweeper([os.path.join(mydir, 'testfiles_nodups')])
Darko Poljak's avatar
Darko Poljak committed
51
        dups_exist = False
52
        for x in swp:
Darko Poljak's avatar
Darko Poljak committed
53 54 55 56
            dups_exist = True
            break
        self.assertFalse(dups_exist)

57 58 59

if __name__ == '__main__':
    unittest.main()