def setUp(self): self.tempdir = tempfile.mkdtemp() self.fs = Filesystem(prefix=self.tempdir) self.app = Application(self.fs) self.tasks_suite = TasksSuite() self.tasks = [Task(1, "1"), Task(2, "2"), Task(3, "3")] self.tasks_suite.extend(self.tasks)
class TestTaskSuite(unittest.TestCase): def setUp(self): self.start = datetime.now() self.t1 = TasksSuite(self.start) self.t2 = TasksSuite(self.start) def test_equality(self): self.assertEqual(self.t1, self.t2) self.assertEqual(self.t1.start, self.start) self.assertEqual(self.t2.start, self.start) def test_adding_and_removing_tasks(self): tasks = [0, 1, 2] self.t1.extend(tasks) self.t2.extend(tasks) self.assertEqual(self.t1, self.t2) self.t1.append(3) self.assertNotEqual(self.t1, self.t2) # '3' is index self.t1.pop(3) self.assertEqual(self.t1, self.t2) def test_shift_forward(self): minutes = 1 self.t1.shift(minutes) self.assertNotEqual(self.t1.start, self.t2.start) self.assertEqual(self.t1.start, self.t2.start + timedelta(minutes=minutes))
class TestApplication(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() self.fs = Filesystem(prefix=self.tempdir) self.app = Application(self.fs) self.tasks_suite = TasksSuite() self.tasks = [Task(1, "1"), Task(2, "2"), Task(3, "3")] self.tasks_suite.extend(self.tasks) def tearDown(self): shutil.rmtree(self.tempdir) def test_is_tasks_suite_active(self): self.assertFalse(self.app.is_tasks_suite_active()) self.app.start_tasks_suite(self.tasks_suite) self.assertTrue(self.app.is_tasks_suite_active()) def test_starting_already_active_tasks_suite(self): self.app.start_tasks_suite(self.tasks_suite) with self.assertRaises(TasksSuiteAlreadyActive): self.app.start_tasks_suite(self.tasks_suite) def test_stop_tasks_suite(self): self.app.start_tasks_suite(self.tasks_suite) self.assertTrue(self.app.is_tasks_suite_active()) stopped_tasks_suite = self.app.stop_tasks_suite() self.assertFalse(self.app.is_tasks_suite_active()) self.assertEqual(self.tasks_suite, stopped_tasks_suite) def test_stopping_not_active_tasks_suite(self): self.assertFalse(self.app.is_tasks_suite_active()) with self.assertRaises(TasksSuiteNotActive): self.app.stop_tasks_suite() def test_show_status(self): self.app.start_tasks_suite(self.tasks_suite) output = self.app.show_status() for t in self.tasks: self.assertIn(t.description, output) def test_show_status_not_active_tests_suite(self): self.assertFalse(self.app.is_tasks_suite_active()) output = self.app.show_status() self.assertIs(output, None) def shift_tasks_suite(self, minutes): self.app.start_tasks_suite(self.tasks_suite) self.app.shift_tasks_suite_in_time(minutes) self.shifted_tasks_suite = self.app.load_tasks_suite() def test_shift_tasks_suite_in_time_forward(self): minutes = 1 self.shift_tasks_suite(minutes) self.assertLess(self.tasks_suite.start, self.shifted_tasks_suite.start) def test_shift_tasks_suite_in_time_backward(self): minutes = -1 self.shift_tasks_suite(minutes) self.assertGreater(self.tasks_suite.start, self.shifted_tasks_suite.start)
def setUp(self): self.tempdir = tempfile.mkdtemp() self.fs = Filesystem(prefix=self.tempdir) self.app = Application(self.fs) self.stream = StringIO() self.cli = CLI(self.app, self.stream) self.task = Task(30, "desc") self.tasks_suite = TasksSuite() self.tasks_suite.append(self.task)
def test_execute_shift(self): original_ts = TasksSuite() minutes = "1" shifted_ts = TasksSuite(original_ts.start + timedelta(minutes=int(minutes))) args = ["", "shift", minutes] self.app.start_tasks_suite(original_ts) self.cli.execute(args) self.cli.status() output = self.stream.getvalue() self.assertNotIn(str(original_ts), output) self.assertIn(str(shifted_ts), output)
def shift(self, minutes): original_ts = TasksSuite() shifted_ts = TasksSuite(original_ts.start + timedelta(minutes=minutes)) self.app.start_tasks_suite(original_ts) self.cli.shift(minutes) self.assertTrue(self.app.is_tasks_suite_active()) output = self.stream.getvalue() self.assertEqual(len(output), 0) # check if no error printed self.cli.status() output = self.stream.getvalue() self.assertNotIn(str(original_ts), output) self.assertIn(str(shifted_ts), output) self.assertTrue(self.app.is_tasks_suite_active())
def setUp(self): self.start = datetime.now() self.t1 = TasksSuite(self.start) self.t2 = TasksSuite(self.start)
class TestCLI(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() self.fs = Filesystem(prefix=self.tempdir) self.app = Application(self.fs) self.stream = StringIO() self.cli = CLI(self.app, self.stream) self.task = Task(30, "desc") self.tasks_suite = TasksSuite() self.tasks_suite.append(self.task) def tearDown(self): shutil.rmtree(self.tempdir) self.stream.close() def test_execute_too_few_args(self): args = [""] self.cli.execute(args) output = self.stream.getvalue() self.assertIn(CLI.msg["too_few_args"], output) def test_execute_command_not_found(self): args = ["", "invalid_command"] self.cli.execute(args) output = self.stream.getvalue() self.assertIn(CLI.msg["command_not_found"], output) def test_status(self): self.cli.status() output = self.stream.getvalue() self.assertIn(CLI.msg["no_tasks"], output) self.app.start_tasks_suite(self.tasks_suite) self.cli.status() output = self.stream.getvalue() self.assertIn(self.task.description, output) def test_execute_status(self): args = ["", "status"] self.cli.execute(args) output = self.stream.getvalue() self.assertIn(CLI.msg["no_tasks"], output) self.app.start_tasks_suite(self.tasks_suite) self.cli.execute(args) output = self.stream.getvalue() self.assertIn(self.task.description, output) def test_start(self): self.cli.start() output = self.stream.getvalue() self.assertEqual(len(output), 0) def test_already_started(self): self.app.start_tasks_suite(self.tasks_suite) self.cli.start() output = self.stream.getvalue() self.assertIn(CLI.msg["already_started"], output) def test_execute_start(self): args = ["", "start"] self.cli.execute(args) output = self.stream.getvalue() self.assertEqual(len(output), 0) self.cli.execute(args) output = self.stream.getvalue() self.assertIn(CLI.msg["already_started"], output) def test_stop(self): self.app.start_tasks_suite(self.tasks_suite) self.cli.stop() output = self.stream.getvalue() self.assertIn(CLI.msg["stopped"], output) self.assertIn(str(self.tasks_suite), output) def test_stop_not_started_tasks_suite(self): self.cli.stop() output = self.stream.getvalue() self.assertIn(CLI.msg["no_tasks"], output) def test_execute_stop(self): args = ["", "stop"] self.cli.execute(args) output = self.stream.getvalue() self.assertIn(CLI.msg["no_tasks"], output) self.cli.start() self.cli.execute(args) output = self.stream.getvalue() self.assertIn(CLI.msg["stopped"], output) def test_add(self): self.app.start_tasks_suite(self.tasks_suite) task = Task(1, "task") self.cli.add(task) self.cli.status() output = self.stream.getvalue() self.assertIn(task.description, output) def test_add_to_not_active_tasks_suite(self): task = Task(1, "task") self.cli.add(task) self.cli.status() output = self.stream.getvalue() self.assertIn(task.description, output) self.assertTrue(self.app.is_tasks_suite_active()) def test_execute_add(self): minutes = 1 description = "task" task = Task(minutes, description) args = ["", "add", str(minutes), description] self.cli.execute(args) self.cli.status() output = self.stream.getvalue() self.assertIn(task.description, output) def test_remove(self): self.app.start_tasks_suite(self.tasks_suite) self.cli.remove(1) self.cli.status() output = self.stream.getvalue() self.assertNotIn(str(self.task), output) self.assertTrue(self.app.is_tasks_suite_active()) def test_remove_on_not_active_tasks_suite(self): self.cli.remove(1) output = self.stream.getvalue() self.assertIn(CLI.msg["no_tasks"], output) self.assertFalse(self.app.is_tasks_suite_active()) def test_remove_invalid_index(self): self.app.start_tasks_suite(self.tasks_suite) invalid_index = 999 self.cli.remove(invalid_index) output = self.stream.getvalue() self.assertIn(CLI.msg["index_err"], output) self.assertTrue(self.app.is_tasks_suite_active()) invalid_index = 0 self.cli.remove(invalid_index) output = self.stream.getvalue() self.assertIn(CLI.msg["index_err"], output) self.assertTrue(self.app.is_tasks_suite_active()) def test_execute_remove(self): index = "1" ts = self.tasks_suite args = ["", "remove", index] self.app.start_tasks_suite(ts) self.cli.execute(args) self.cli.status() output = self.stream.getvalue() self.assertNotIn(str(ts), output) self.assertTrue(self.app.is_tasks_suite_active()) def test_execute_remove_on_not_active_tasks_suite(self): args = ["", "remove", "1"] self.assertFalse(self.app.is_tasks_suite_active()) self.cli.execute(args) output = self.stream.getvalue() self.assertIn(CLI.msg["no_tasks"], output) self.assertFalse(self.app.is_tasks_suite_active()) def test_execute_remove_invalid_index(self): self.app.start_tasks_suite(self.tasks_suite) invalid_index = "999" args = ["", "remove", invalid_index] self.cli.execute(args) output = self.stream.getvalue() self.assertIn(CLI.msg["index_err"], output) self.assertTrue(self.app.is_tasks_suite_active()) def test_execute_remove_invalid_index_format(self): args = ["", "remove", "xxx"] self.app.start_tasks_suite(self.tasks_suite) self.cli.execute(args) output = self.stream.getvalue() self.assertTrue(self.app.is_tasks_suite_active()) def shift(self, minutes): original_ts = TasksSuite() shifted_ts = TasksSuite(original_ts.start + timedelta(minutes=minutes)) self.app.start_tasks_suite(original_ts) self.cli.shift(minutes) self.assertTrue(self.app.is_tasks_suite_active()) output = self.stream.getvalue() self.assertEqual(len(output), 0) # check if no error printed self.cli.status() output = self.stream.getvalue() self.assertNotIn(str(original_ts), output) self.assertIn(str(shifted_ts), output) self.assertTrue(self.app.is_tasks_suite_active()) def test_shift(self): self.shift(1) def test_shift_backward(self): self.shift(-1) def test_shift_on_not_active_tasks_suite(self): self.assertFalse(self.app.is_tasks_suite_active()) self.cli.shift(1) output = self.stream.getvalue() self.assertIn(CLI.msg["no_tasks"], output) self.assertFalse(self.app.is_tasks_suite_active()) def test_shift_invalid_format(self): invalid_format_minutes = "1min" self.app.start_tasks_suite(self.tasks_suite) self.cli.shift(invalid_format_minutes) output = self.stream.getvalue() self.assertIn(CLI.msg["shift_arg_format_err"], output) self.assertTrue(self.app.is_tasks_suite_active()) def test_execute_shift(self): original_ts = TasksSuite() minutes = "1" shifted_ts = TasksSuite(original_ts.start + timedelta(minutes=int(minutes))) args = ["", "shift", minutes] self.app.start_tasks_suite(original_ts) self.cli.execute(args) self.cli.status() output = self.stream.getvalue() self.assertNotIn(str(original_ts), output) self.assertIn(str(shifted_ts), output)