def test_save_load(self): task1 = Task("task1") start = datetime(2019, 1, 10, 11, 11) end = datetime(2019, 1, 10, 11, 22) task1.start(dt=start) task1.end(dt=end) task2 = Task("task2") task3 = Task("task3") wok1 = Wok() job1 = Job("job1") wok1.jobs.append(job1) wok1.current_job_idx = 0 job2 = Job("job2") wok1.jobs.append(job2) job1.add_task(task1) job1.add_task(task2) job2.add_task(task3) path = Path.cwd() / ".wok_test" wok1.save(dir=path) wok2 = Wok() wok2.load(dir=path) self.assertEqual(wok2.jobs[wok2.current_job_idx].name, "job1") self.assertEqual(len(wok2.jobs), 2) out = (path / "job1" / "task1").read_text() self.assertEqual( out, "2019-01-10T11:11:00.000000->2019-01-10T11:22:00.000000")
def load(self, dir: Path = default_dir) -> Tuple[bool, str]: """Load the WoK from the dir folder :param dir: Default value = default_dir :return: True if success + message :rtype: boolean, string """ if not Wok.check_dir(dir): return False, "Could not load (see previous error)" # Now dir exists and is a directory current_job_name = None try: current_job_file = [ x for x in dir.iterdir() if not x.is_dir() and x.name == "current_job" ][0] current_job_name = current_job_file.read_text().strip() except IndexError: pass for job_file in [x for x in dir.iterdir() if x.is_dir()]: job = Job(job_file.name) for task_file in job_file.iterdir(): task = Task(task_file.name) task.load(task_file.read_text().split("\n")) job.add_task(task) self.jobs.append(job) if job.name == current_job_name: self.current_job_idx = len(self.jobs) - 1 return True, "Loaded successfully"
def add_task(self, name: str) -> ApiRtype: if not WokApi.__validate_name(name): return False, f"Task name {name} is invalid" job = self.get_current_job() if job is None: return False, "No current job to add a task to" if any([task.name == name for task in job.tasks]): return False, f"Task with name '{name}' already exists in job '{job.name}'" task = Task(name) job.add_task(task) return True, f"Task '{name}' created"
def setUp(self): self.task = Task("tested_task")
def setUp(self): self.job = Job("tested_job") self.task = Task("task")