libtest.py 3.96 KB
Newer Older
jvoisin's avatar
jvoisin committed
1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*
3
4
5
6
7

'''
    Unit test for the library
'''

8
9
import unittest
import test
jvoisin's avatar
jvoisin committed
10
import sys
11
import tempfile
jvoisin's avatar
jvoisin committed
12
sys.path.append('..')
13
import MAT
14

jvoisin's avatar
jvoisin committed
15

jvoisin's avatar
jvoisin committed
16
17
18
19
class TestRemovelib(test.MATTest):
    '''
        test the remove_all() method
    '''
20
21
    def test_remove(self):
        '''make sure that the lib remove all compromizing meta'''
jvoisin's avatar
jvoisin committed
22
        for _, dirty in self.file_list:
23
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
24
            current_file.remove_all()
25
            current_file2 = MAT.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
jvoisin's avatar
Bugfix  
jvoisin committed
26
            self.assertTrue(current_file2.is_clean())
27
28
29

    def test_remove_empty(self):
        '''Test removal with clean files'''
jvoisin's avatar
jvoisin committed
30
        for clean, _ in self.file_list:
31
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
32
33
34
35
            current_file.remove_all()
            self.assertTrue(current_file.is_clean())


jvoisin's avatar
jvoisin committed
36
37
38
39
class TestListlib(test.MATTest):
    '''
        test the get_meta() method
    '''
40
41
    def test_list(self):
        '''check if get_meta returns all the expected meta'''
jvoisin's avatar
jvoisin committed
42
        for _, dirty in self.file_list:
43
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
jvoisin's avatar
jvoisin committed
44
            self.assertIsNotNone(current_file.get_meta())
45
46
47

    def testlist_list_empty(self):
        '''check that a listing of a clean file return an empty dict'''
jvoisin's avatar
jvoisin committed
48
        for clean, _ in self.file_list:
49
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
jvoisin's avatar
jvoisin committed
50
            self.assertEqual(current_file.get_meta(), dict())
51
52


jvoisin's avatar
jvoisin committed
53
54
55
56
class TestisCleanlib(test.MATTest):
    '''
        test the is_clean() method
    '''
57
    def test_dirty(self):
58
        '''test is_clean on clean files'''
jvoisin's avatar
jvoisin committed
59
        for _, dirty in self.file_list:
60
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
jvoisin's avatar
jvoisin committed
61
            self.assertFalse(current_file.is_clean())
62
63
64

    def test_clean(self):
        '''test is_clean on dirty files'''
jvoisin's avatar
jvoisin committed
65
        for clean, _ in self.file_list:
66
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
jvoisin's avatar
jvoisin committed
67
            self.assertTrue(current_file.is_clean())
68

jvoisin's avatar
jvoisin committed
69

jvoisin's avatar
jvoisin committed
70
71
72
73
74
class TestFileAttributes(unittest.TestCase):
    '''
        test various stuffs about files (readable, writable, exist, ...)
    '''
    def test_not_writtable(self):
jvoisin's avatar
jvoisin committed
75
        ''' test MAT's behaviour on non-writable file'''
76
        self.assertFalse(MAT.mat.create_class_file('not_writtable', False, add2archive=True, low_pdf_quality=True))
jvoisin's avatar
jvoisin committed
77
78

    def test_not_exist(self):
jvoisin's avatar
jvoisin committed
79
        ''' test MAT's behaviour on non-existent file'''
80
        self.assertFalse(MAT.mat.create_class_file('ilikecookies', False, add2archive=True, low_pdf_quality=True))
81

jvoisin's avatar
jvoisin committed
82
83
    def test_empty(self):
        ''' test MAT's behaviour on empty file'''
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        self.assertFalse(MAT.mat.create_class_file('empty_file', False, add2archive=True, low_pdf_quality=True))

class TestSecureRemove(unittest.TestCase):
    ''' Test the secure_remove function
    '''
    def test_remove_existing(self):
        ''' test the secure removal of an existing file
        '''
        _, file_to_remove = tempfile.mkstemp()
        self.assertTrue(MAT.mat.secure_remove(file_to_remove))

    def test_remove_fail(self):
        ''' test the secure removal of an non-removable file
        '''
        self.assertRaises(MAT.exceptions.UnableToRemoveFile, MAT.mat.secure_remove, '/OMAGAD')

jvoisin's avatar
jvoisin committed
100

101
def get_tests():
jvoisin's avatar
jvoisin committed
102
103
104
105
106
107
    ''' Return every libtests'''
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestRemovelib))
    suite.addTest(unittest.makeSuite(TestListlib))
    suite.addTest(unittest.makeSuite(TestisCleanlib))
    suite.addTest(unittest.makeSuite(TestFileAttributes))
108
    suite.addTest(unittest.makeSuite(TestSecureRemove))
jvoisin's avatar
jvoisin committed
109
    return suite
110

jvoisin's avatar
jvoisin committed
111
112

if __name__ == '__main__':
113
    unittest.TextTestRunner(verbosity=test.VERBOSITY).run(get_tests())