portfoliome /
foil
| 1 | import os |
||
|
0 ignored issues
–
show
|
|||
| 2 | import shutil |
||
| 3 | import unittest |
||
| 4 | from tempfile import NamedTemporaryFile, mkdtemp |
||
| 5 | from uuid import uuid4 |
||
| 6 | |||
| 7 | from foil.filesys import file_exists, ensure_file_directory |
||
| 8 | |||
| 9 | |||
| 10 | class TestFileExists(unittest.TestCase): |
||
| 11 | |||
| 12 | def setUp(self): |
||
| 13 | with NamedTemporaryFile(prefix='mocke_', suffix='.txt', |
||
| 14 | delete=False) as tmp: |
||
| 15 | with open(tmp.name, 'w') as fp: |
||
| 16 | fp.write('121XZTT') |
||
| 17 | |||
| 18 | self.tmp_path = tmp.name |
||
| 19 | |||
| 20 | def test_file_exists(self): |
||
| 21 | self.assertTrue(file_exists(self.tmp_path)) |
||
| 22 | |||
| 23 | def test_not_exists(self): |
||
| 24 | file_name = str(uuid4()) + '.txt' |
||
| 25 | |||
| 26 | with self.assertRaises(FileExistsError): |
||
| 27 | file_exists(file_name) |
||
| 28 | |||
| 29 | def tearDown(self): |
||
| 30 | if os.path.exists(self.tmp_path): |
||
| 31 | os.unlink(self.tmp_path) |
||
| 32 | |||
| 33 | |||
| 34 | class TestEnsureFiles(unittest.TestCase): |
||
| 35 | |||
| 36 | def setUp(self): |
||
| 37 | self.base_path = mkdtemp() |
||
| 38 | |||
| 39 | def test_ensure_file_directory(self): |
||
| 40 | directory = str(uuid4()) |
||
| 41 | file_name = str(uuid4()) + '.txt' |
||
| 42 | directory_path = os.path.join(self.base_path, directory) |
||
| 43 | path = os.path.join(directory_path, file_name) |
||
| 44 | |||
| 45 | ensure_file_directory(path) |
||
| 46 | |||
| 47 | self.assertTrue(os.path.exists(directory_path)) |
||
| 48 | |||
| 49 | # make sure nothing happens on second call |
||
| 50 | ensure_file_directory(path) |
||
| 51 | |||
| 52 | def tearDown(self): |
||
| 53 | if os.path.exists(self.base_path): |
||
| 54 | shutil.rmtree(self.base_path) |
||
| 55 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.