|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 |
|
| 3 | +import arrow |
3 | 4 | import os
|
| 5 | +import shutil |
4 | 6 | import unittest
|
5 | 7 |
|
6 | 8 | from classifier import classifier as clf
|
| 9 | +from six.moves import getcwd |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class ClassifierTest(unittest.TestCase):
|
10 | 13 |
|
11 | 14 | __location = os.path.realpath(
|
12 |
| - os.path.join(os.getcwd(), os.path.dirname(__file__))) |
| 15 | + os.path.join(getcwd(), os.path.dirname(__file__), '.unittest')) |
13 | 16 |
|
14 |
| - __tmp_file_name = '.classifier_test' |
| 17 | + __tmp_files = [u'test_file', u'test_file_中文'] |
| 18 | + __tmp_dirs = [u'test_dir', u'test_dir_中文'] |
15 | 19 |
|
16 |
| - def test_moveto(self): |
| 20 | + def setUp(self): |
| 21 | + if not os.path.exists(self.__location): |
| 22 | + os.mkdir(self.__location) |
17 | 23 | os.chdir(self.__location)
|
18 |
| - tmp_file = open(self.__tmp_file_name, 'w') |
| 24 | + for file_ in self.__tmp_files: |
| 25 | + open(file_, 'w').close() |
| 26 | + for dir_ in self.__tmp_dirs: |
| 27 | + if not os.path.exists(dir_): |
| 28 | + os.mkdir(dir_) |
| 29 | + super(ClassifierTest, self).setUp() |
| 30 | + |
| 31 | + def tearDown(self): |
| 32 | + shutil.rmtree(self.__location) |
| 33 | + super(ClassifierTest, self).tearDown() |
19 | 34 |
|
20 |
| - parent_dir = os.path.abspath(os.path.join(self.__location, os.pardir)) |
21 |
| - clf.moveto(self.__tmp_file_name, self.__location, parent_dir) |
| 35 | + def test_moveto(self): |
| 36 | + target_dir = os.path.abspath(os.path.join(self.__location, 'moveto')) |
| 37 | + for file_ in self.__tmp_files: |
| 38 | + clf.moveto(file_, self.__location, target_dir) |
| 39 | + |
| 40 | + for file_ in self.__tmp_files: |
| 41 | + final_file_path = os.path.join(target_dir, file_) |
| 42 | + self.assertTrue(os.path.exists(final_file_path)) |
| 43 | + |
| 44 | + def test_classify_bydate(self): |
| 45 | + date_format = 'DD-MM-YYYY' |
| 46 | + target_files = [] |
| 47 | + for file_ in self.__tmp_files: |
| 48 | + target_dir = arrow.get(os.path.getctime(file_)).format(date_format) |
| 49 | + final_file_path = os.path.join(target_dir, file_) |
| 50 | + target_files.append(final_file_path) |
| 51 | + clf.classify_by_date(date_format, self.__location) |
| 52 | + for file_ in target_files: |
| 53 | + self.assertTrue(os.path.exists(final_file_path)) |
| 54 | + for dir_ in self.__tmp_dirs: |
| 55 | + self.assertTrue(os.path.exists(dir_)) |
22 | 56 |
|
23 |
| - final_file_path = os.path.join(parent_dir, self.__tmp_file_name) |
24 |
| - self.assertTrue(os.path.exists(final_file_path)) |
25 |
| - os.remove(final_file_path) |
26 | 57 |
|
27 | 58 | if __name__ == '__main__':
|
28 | 59 | unittest.main()
|
0 commit comments