-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
156 lines (114 loc) · 3.64 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import re
import shutil
import unittest
from time import sleep, time
class scriptTest(unittest.TestCase):
def setUp(self):
self.regex = '(.*.txt)'
def createDirectorySource(self):
"""
This method creates a folder named 'testes'
"""
os.mkdir('testes')
def createDirectoryTarget(self):
"""
This method creates a folder named 'testes2'
"""
os.mkdir('testes2')
def createFiles(self):
"""
This method creates 2 files .txt and 1 .rar
"""
open('testes/1.txt', 'a').close()
sleep(0.01)
open('testes/2.rar', 'a').close()
sleep(0.01)
open('testes/3.txt', 'a').close()
self.files = os.listdir('testes/')
def cleanSource(self):
"""
This method deletes 'testes' folder (source folder)
"""
shutil.rmtree('testes')
def cleanTarget(self):
"""
This method deletes 'testes2' folder (target folder)
"""
shutil.rmtree('testes2')
def test_numberOfMatches(self):
"""
This method tests the number of files matches
"""
self.createDirectorySource()
self.createFiles()
match = 0
for file in self.files:
m = re.search(self.regex, file)
if m != None:
match += 1
self.cleanSource()
self.assertEqual(match, 2)
def test_verifySource(self):
"""
This method tests if the source folder was created
"""
self.createDirectorySource()
response = os.listdir()
self.cleanSource()
self.assertEqual('testes' in response, True)
def test_verifyMoveFiles(self):
"""
This method tests if the files were moved
"""
target = 'testes2/'
self.createDirectorySource()
self.createDirectoryTarget()
self.createFiles()
for file in self.files:
m = re.search(self.regex, file)
if m != None:
command = "%s%s" %('testes/', file)
shutil.move(command, target)
after = os.listdir('testes/')
moved = os.listdir('testes2/')
self.assertEqual(len(after), len(self.files)-2)
self.assertEqual(len(moved), 2)
self.cleanSource()
self.cleanTarget()
def test_theMostRecent(self):
"""
This method tests if the file is the most recent
"""
self.createDirectorySource()
self.createDirectoryTarget()
self.createFiles()
time_newest = 0
for file in self.files:
time_file = os.path.getmtime('%s%s' %('testes/', file))
if time_file > time_newest \
and re.match(self.regex, file):
time_newest = time_file
name_file = file
self.cleanSource()
self.cleanTarget()
self.assertCountEqual(name_file, '3.txt')
def test_theOldest(self):
"""
This method tests if the file is the oldest
"""
self.createDirectorySource()
self.createDirectoryTarget()
self.createFiles()
time_oldest = time()
for file in self.files:
time_file = os.path.getmtime('%s%s' %('testes/', file))
if time_file < time_oldest \
and re.match(self.regex, file):
time_oldest = time_file
name_file = file
self.cleanSource()
self.cleanTarget()
self.assertCountEqual(name_file, '1.txt')
if __name__ == '__main__':
unittest.main()