|
| 1 | +from command import MoveFileCommand |
| 2 | +import unittest, os, shutil, subprocess |
| 3 | + |
| 4 | + |
| 5 | +class StrategyTest(unittest.TestCase): |
| 6 | + |
| 7 | + def __get_directories(self): |
| 8 | + """ |
| 9 | + Get the directories relevant for tests: |
| 10 | + - self.file_dir: the directory of this script |
| 11 | + - self.test_dir: the root directory for tests |
| 12 | + """ |
| 13 | + self.file_dir = os.path.dirname(os.path.realpath(__file__)) |
| 14 | + self.test_dir = os.path.join(self.file_dir, 'test_command') |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + """ |
| 18 | + Creates a temporary directory and file: |
| 19 | + ./test_command |
| 20 | + /foo.txt |
| 21 | + """ |
| 22 | + os.mkdir('test_command') |
| 23 | + open('test_command/foo.txt', 'w').close() |
| 24 | + self.__get_directories() |
| 25 | + |
| 26 | + def test_sequential_execution(self): |
| 27 | + self.command_stack = [] |
| 28 | + self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'foo.txt'), os.path.join(self.test_dir, 'bar.txt'))) |
| 29 | + self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'bar.txt'), os.path.join(self.test_dir, 'baz.txt'))) |
| 30 | + self.command_stack[0].execute() |
| 31 | + output_after_first_command = os.listdir(self.test_dir) |
| 32 | + self.assertEqual(output_after_first_command[0], 'bar.txt') |
| 33 | + self.command_stack[1].execute() |
| 34 | + output_after_second_command = os.listdir(self.test_dir) |
| 35 | + self.assertEqual(output_after_second_command[0], 'baz.txt') |
| 36 | + |
| 37 | + def tearDown(self): |
| 38 | + """ |
| 39 | + Removes the temporary directory and its content: |
| 40 | + ./test_command |
| 41 | + ... |
| 42 | + """ |
| 43 | + shutil.rmtree('test_command') |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + unittest.main() |
0 commit comments