Example #1
0
    def save_with_tasks(cls, **kwargs):
        project_data = kwargs
        tasks_data = project_data["tasks"]
        project_data["tasks"] = []

        project = cls.save(**project_data)
        TaskModel.create_from_list(tasks_data, project.id)
        return project
def test_create_tasks_from_list(test_app, test_db, factory):
    project = factory.add_project("test project")
    task1_data = dict(task_name="task1", recorded_time=100)
    task2_data = dict(task_name="task2", recorded_time=200)
    tasks_data = [task1_data, task2_data]
    TaskModel.create_from_list(tasks_data, project.id)
    tasks = project.tasks.all()
    assert len(tasks) == 2
Example #3
0
    def patch(self, task_id):
        update_data = task_schema.load(request.get_json(), partial=True)

        if not TaskModel.find_by_id(task_id):
            return {"message": gettext("task_not_found").format(task_id)}, 404

        TaskModel.update_by_id(update_data, task_id)
        task = TaskModel.find_by_id(task_id)

        return task_schema.dumps(task), 200
Example #4
0
    def post(self):
        task_data = task_schema.load(request.get_json())
        task_name = task_data["task_name"]

        if TaskModel.find_by_name(task_name):
            return {
                "message": gettext("task_name_exists").format(task_name)
            }, 400

        task = TaskModel.create(**task_data)

        return task_schema.dump(task), 201
def test_add_task(test_app, test_db):
    project = Factory.add_project("test project")
    Factory.add_task("test task", project_id=project.id)
    result = TaskModel.find_by_name("test task")
    assert result.task_name == "test task"
    assert result.project_id == project.id
    assert type(result.recorded_time) == int
Example #6
0
    def delete(self, task_id):
        task = TaskModel.find_by_id(task_id)

        if not task:
            return {"message": gettext("task_not_found").format(task_id)}, 404

        task.delete_from_db()
        return task_schema.dumps(task), 200
Example #7
0
def test_create_task_already_exists(test_app, test_db, factory):
    project = factory.add_project("test project")
    task_data = factory.fake_task_data("test task", project.id)
    task = TaskModel.create(**task_data)

    client = test_app.test_client()
    resp = client.post("/api/tasks",
                       data=json.dumps(task_data),
                       content_type="application/json")
    data = json.loads(resp.data.decode())

    assert resp.status_code == 400
    assert cached_strings["task_name_exists"].format(
        task.task_name) == data["message"]
Example #8
0
def test_create_task(test_app, test_db, factory):
    test_db.session.query(TaskModel).delete()
    project = factory.add_project("test project")
    task_name = "test task"
    task_data = factory.fake_task_data(task_name, project.id)
    client = test_app.test_client()
    resp = client.post("/api/tasks",
                       data=json.dumps(task_data),
                       content_type="application/json")
    data = json.loads(resp.data.decode())
    task = TaskModel.find_by_name(task_data["task_name"])

    assert resp.status_code == 201
    assert data["task_name"] == task_name
    assert task.task_name == task_name
Example #9
0
 def add_task(task_name, project_id):
     return TaskModel.create(
         task_name=task_name,
         project_id=project_id,
         recorded_time=random.randrange(0, 10000),
     )