Newer
Older
#!/usr/bin/python3
# -*-coding:Utf-8 -*
# Copyright laurent modolo for the LBMC UMR 5239 ©.
# contributor(s) : laurent modolo (2017)
#
# laurent.modolo@ens-lyon.fr
#
# This software is a computer program whose purpose is to test the
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
import unittest
import datetime
import os
import sys
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from file_handle import Dated_file
from file_handle import Dated_file_list
class Dated_file_TestCase(unittest.TestCase):
def test_test_no_date_found(self):
'''
if there is no date and older file don't exist we should set the \
current date.
'''
datefile = Dated_file("/path/test_file.txt")
current_date = datetime.date.today()
self.assertEqual(
datefile.get_date(),
current_date.strftime("%Y_%m_%d")
)
self.assertEqual(
datefile.get_full_file_name(),
current_date.strftime("%Y_%m_%d_") + "test_file.txt"
)
def test_test_date_found(self):
'''
if there is no date and older file don't exist we should set the \
current date.
'''
datefile = Dated_file("/path/2003_10_02_test_file.txt")
self.assertEqual(
datefile.get_date(),
"2003_10_02"
)
def test_date_setting(self):
datefile = Dated_file("/path/2003_10_02_test_file.txt")
datefile.set_date("2005_11_04")
self.assertEqual(
datefile.get_date(),
"2005_11_04"
)
def test_tuncate_file_name(self):
datefile = Dated_file("/path/2003_10_02_test_file.txt")
self.assertEqual(
datefile.get_file_name(),
"test_file.txt"
)
self.assertEqual(
datefile.get_full_file_name(),
"2003_10_02_test_file.txt"
)
def test_abs_path(self):
datefile = Dated_file("/path/2003_10_02_test_file.txt")
self.assertEqual(
datefile.get_file_path(),
os.path.abspath("/path/")
)
def test_list_files_empty(self):
datefile = Dated_file("./data/examples/2004_10_02_test_file.txt")
self.assertEqual(
datefile[2],
"2004_10_02_test_file.txt"
)
self.assertEqual(
datefile[1],
"2004_12_02_test_file.txt"
)
self.assertEqual(
datefile[0],
"2006_02_08_test_file.txt"
)
def test_set_to_last_existing_file(self):
datefile = Dated_file("./data/examples/test_file.txt")
self.assertEqual(
datefile.get_date(),
"2006_02_08"
)
self.assertEqual(
datefile.get_file_name(),
"test_file.txt"
)
self.assertEqual(
datefile.get_full_file_name(),
"2006_02_08_test_file.txt"
)
def test_set_to_dated_existing_file(self):
datefile = Dated_file("./data/examples/2004_12_02_test_file.txt")
self.assertEqual(
datefile.get_date(),
"2004_12_02"
)
self.assertEqual(
datefile.get_file_name(),
"test_file.txt"
)
self.assertEqual(
datefile.get_full_file_name(),
"2004_12_02_test_file.txt"
)
def test_set_to_dated_existing_folder(self):
datefile = Dated_file("./data/examples/2004_12_02_test_folder")
self.assertEqual(
datefile.get_date(),
"2004_12_02"
)
self.assertEqual(
datefile.get_file_name(),
"test_folder"
)
self.assertEqual(
datefile.get_full_file_name(),
"2004_12_02_test_folder"
)
def test_date_existed_file(self):
with open(os.path.abspath("./data/examples/test_file2.txt"), 'w'):
datefile = Dated_file("./data/examples/test_file2.txt")
current_date = datetime.date.today()
self.assertTrue(
os.path.isfile(
"./data/examples/" +
current_date.strftime("%Y_%m_%d_") +
"test_file2.txt"))
self.assertEqual(
datefile.get_full_file_name(),
current_date.strftime("%Y_%m_%d_") + "test_file2.txt"
)
os.remove(os.path.abspath(
"./data/examples/" +
current_date.strftime("%Y_%m_%d_") +
"test_file2.txt"))
def test_date_existed_folder(self):
newpath = "./data/examples/test_folder"
if not os.path.exists(newpath):
os.makedirs(newpath)
datefile = Dated_file("./data/examples/test_folder",
date=None,
redate=True)
current_date = datetime.date.today()
self.assertTrue(
os.path.exists(
"./data/examples/" +
current_date.strftime("%Y_%m_%d_") +
"test_folder"))
self.assertEqual(
datefile.get_full_file_name(),
current_date.strftime("%Y_%m_%d_") + "test_folder"
)
os.rmdir(os.path.abspath(
"./data/examples/" +
current_date.strftime("%Y_%m_%d_") +
"test_folder"))
def test_set_date_existed_file(self):
with open(os.path.abspath("./data/examples/test_file2.txt"), 'w'):
datefile = Dated_file(
"./data/examples/test_file2.txt",
"2008_12_02")
datefile = Dated_file("./data/examples/2008_12_02_test_file2.txt")
self.assertEqual(
datefile.get_full_file_name(),
"2008_12_02_test_file2.txt"
)
os.remove(os.path.abspath(
"./data/examples/2008_12_02_test_file2.txt"))
def test_set_date_existed_folder(self):
newpath = "./data/examples/test_folder2"
if not os.path.exists(newpath):
os.makedirs(newpath)
datefile = Dated_file(
"./data/examples/test_folder2",
"2008_12_02")
datefile = Dated_file("./data/examples/2008_12_02_test_folder2")
self.assertEqual(
datefile.get_full_file_name(),
"2008_12_02_test_folder2"
)
os.rmdir(os.path.abspath(
"./data/examples/2008_12_02_test_folder2"))
class Dated_file_list_TestCase(unittest.TestCase):
def test_read_list(self):
file_list = [
"./data/examples/2004_10_02_test_file.txt",
"./data/examples/2004_12_02_test_file.txt",
"./data/examples/2006_02_08_test_file.txt"]
datefile_list = Dated_file_list(file_list)
for i in range(len(file_list)):
self.assertEqual(
datefile_list[i].get_full_file_name(),
os.path.basename(file_list[i])
)
def test_read_list_with_date(self):
file_list = [
"./data/examples/test_file.txt",
"./data/examples/test_file.txt",
"./data/examples/test_file.txt"]
date_list = ["2004_10_02", "2004_12_02", "2006_02_08"]
datefile_list = Dated_file_list(file_list, date_list)
for i in range(len(file_list)):
self.assertEqual(
datefile_list[i].get_full_file_name(),
date_list[i] + "_" + os.path.basename(file_list[i])
)
def test_read_list_with_wildcard(self):
file_list = ["./data/examples/test_file.*"]
file_list_check = [
"./data/examples/2008_04_12_test_file.csv",
"./data/examples/2006_02_08_test_file.txt"]
date_list = list()
datefile_list = Dated_file_list(file_list, date_list)
for i in range(len(file_list_check)):
self.assertEqual(
datefile_list[i].get_full_file_name(),
os.path.basename(file_list_check[i])
)
def test_read_list_with_wildcard_and_date(self):
file_list = ["./data/examples/test_file.*"]
file_list_check = [
"./data/examples/2008_04_12_test_file.csv",
"./data/examples/2004_12_02_test_file.txt"]
date_list = ["2008_04_12", "2004_12_02"]
datefile_list = Dated_file_list(file_list, date_list)
for i in range(len(file_list_check)):
self.assertEqual(
datefile_list[i].get_full_file_name(),
os.path.basename(file_list_check[i])
)
def test_read_list_redate(self):
with open(os.path.abspath(
"./data/examples/2017_04_04_test_file3.txt"), 'w'):
current_date = datetime.date.today()
file_list = [
"./data/examples/2017_04_04_test_file3.txt"]
file_list_check = [
"./data/examples/" +
current_date.strftime("%Y_%m_%d") + "_test_file3.txt"]
datefile_list = Dated_file_list(
file_list,
"2098_02_12",
False,
True)
for i in range(len(file_list_check)):
self.assertEqual(
datefile_list[i].get_full_file_name(),
os.path.basename(file_list_check[i])
)
def test_read_list_redate_file(self):
with open(os.path.abspath(
"./data/examples/2017_04_04_test_file3.txt"), 'w'):
current_date = datetime.date.today()
file_list = [
"./data/examples/2017_04_04_test_file3.txt"]
file_list_check = [
"./data/examples/" +
current_date.strftime("%Y_%m_%d") + "_test_file3.txt"]
Dated_file_list(
file_list,
check=False,
redate=True)
for i in range(len(file_list_check)):
self.assertEqual(
os.path.isfile(os.path.abspath(
"./data/examples/" +
current_date.strftime("%Y_%m_%d_") +
"test_file3.txt")),
True
)
os.remove(os.path.abspath(
"./data/examples/" +
current_date.strftime("%Y_%m_%d_") +
"test_file3.txt"))
if __name__ == '__main__':
unittest.main()