libtest.py 4.01 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
45
            #FIXME assertisNotNone() : python 2.7
            self.assertTrue(current_file.get_meta())
46
47
48

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


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

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

jvoisin's avatar
jvoisin committed
70

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

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

jvoisin's avatar
jvoisin committed
83
84
    def test_empty(self):
        ''' test MAT's behaviour on empty file'''
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        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
101

102
def get_tests():
jvoisin's avatar
jvoisin committed
103
104
105
106
107
108
    ''' 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))
109
    suite.addTest(unittest.makeSuite(TestSecureRemove))
jvoisin's avatar
jvoisin committed
110
    return suite
111

jvoisin's avatar
jvoisin committed
112
113

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