tests.files.test_filehandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 33
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A TestFileHandler.setUp() 0 15 2
A TestFileHandler.test_get_files() 0 15 1
A TestFileHandler.test_move_to_category() 0 12 2
A TestFileHandler.tearDown() 0 6 1
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
import os
4
import shutil
5
import unittest
6
import uuid
7
from time import sleep
8
9
from saucenao import FileHandler, Filter
10
11
12
class TestFileHandler(unittest.TestCase):
13
    """
14
    test cases for the file handler operations
15
    """
16
17
    def setUp(self):
18
        """Constructor for the unittest
19
20
        :return:
21
        """
22
        self.dir = os.path.join(os.getcwd(), str(uuid.uuid4()))
23
        os.mkdir(self.dir)
24
25
        file_name = os.path.join(self.dir, str(uuid.uuid4()))
26
        # create a file
27
        with open(file_name, "wb") as _:
28
            pass
29
30
        # wait for file operations to finish
31
        sleep(0.5)
32
33
    def tearDown(self):
34
        """Destructor for the unittest
35
36
        :return:
37
        """
38
        shutil.rmtree(self.dir)
39
40
    def test_get_files(self):
41
        """Test the get_files function of the file handler
42
43
        :return:
44
        """
45
        files = FileHandler.get_files(directory=self.dir)
46
        self.assertEqual(len(list(files)), 1)
47
48
        file_filter = Filter(assert_is_folder=True)
49
        files = FileHandler.get_files(directory=self.dir, file_filter=file_filter)
50
        self.assertEqual(len(list(files)), 0)
51
52
        file_filter = Filter(assert_is_file=True)
53
        files = FileHandler.get_files(directory=self.dir, file_filter=file_filter)
54
        self.assertEqual(len(list(files)), 1)
55
56
    def test_move_to_category(self):
57
        """Test moving the file to a category folder
58
59
        :return:
60
        """
61
        files = list(FileHandler.get_files(directory=self.dir))
62
        self.assertEqual(len(files), 1)
63
        for file in files:
64
            FileHandler.move_to_category(file, 'Example Category', self.dir)
65
66
        new_files = FileHandler.get_files(directory=self.dir)
67
        self.assertEqual(len(list(new_files)), 0)
68