Beispiel #1
0
def update_or_delete_task(id):

    service = DataService(current_app.config["app_context"])
    request_data = request.get_json()
    try:
        data_exist = service.check_exist_by_id(id)

        if request.method == "PUT":
            status = request_data.get("status")
            name = request_data.get("name")
            _id = request_data.get("id")
            if int(_id) != id:
                return json.jsonify({"error": "id is not match."}), 400
            if status == None or name == None:
                return json.jsonify({"error": "missing request"}), 400
            if not isinstance(status, int) or not isinstance(name, str):
                return json.jsonify({"error": "request type wrong"}), 400
            result = service.update_task(id=_id, status=status, name=name)
            return json.jsonify(result), 200

        if request.method == "DELETE":
            result = service.delete_task(id)
            return json.jsonify({"success": "delete success"}), 200
    except RuntimeError as exp:
        return json.jsonify({"error": str(exp)}), 400
    except Exception as e:
        return json.jsonify({"error": "server error"}), 500
Beispiel #2
0
class ServiceTestCase(unittest.TestCase):
    task_data = None

    def setUp(self):
        self.service = DataService(app_context)

    def test_0100_find_all_data_success(self):
        find_all_patch = patch.object(DataDao,
                                      'find_all',
                                      side_effect=self.mock_find_all)
        find_all_patch.start()
        return_data = self.service.find_all_data()
        assert return_data == [{
            "id": 1,
            "name": "name",
            "status": 0
        }, {
            "id": 2,
            "name": "name2",
            "status": 1
        }]
        find_all_patch.stop()

    def test_0200_check_exist_by_id_success(self):
        """
        Test case:
        return_data must be true
        """
        check_exist_by_id_patch = patch.object(
            DataDao,
            'check_exist_by_id',
            side_effect=self.mock_check_exist_by_id)
        check_exist_by_id_patch.start()
        id = 1
        return_data = self.service.check_exist_by_id(id)
        assert return_data == True
        check_exist_by_id_patch.stop()

    def test_0201_check_exist_by_id_fail(self):
        """
        Test case:
        function will raise RuntimeError
        """
        self.assertRaisesRegex(RuntimeError, "id is not found in database",
                               self.check_exist_by_id_fail)

    def check_exist_by_id_fail(self):
        check_exist_by_id_patch = patch.object(
            DataDao,
            'check_exist_by_id',
            side_effect=self.mock_check_exist_by_id)
        check_exist_by_id_patch.start()
        id = 2
        self.service.check_exist_by_id(id)
        check_exist_by_id_patch.stop()

    def test_0300_create_task_success(self):
        """
        Test case:
        create task will return data
        """
        name = "test"
        insert_data_patch = patch.object(DataDao,
                                         'insert_data',
                                         side_effect=self.mock_insert_data)
        get_by_id_patch = patch.object(DataDao,
                                       'get_by_id',
                                       side_effect=self.mock_get_by_id)
        get_by_id_patch.start()
        insert_data_patch.start()
        return_data = self.service.create_task(name)
        insert_data_patch.stop()
        get_by_id_patch.stop()
        assert return_data == {"id": 1, "name": name, "status": 0}

    def test_0400_update_task_success(self):
        """
        Test case:
        update task will return data
        """
        _id = 1
        status = 1
        name = "test2"
        update_data_patch = patch.object(DataDao,
                                         'update_data',
                                         side_effect=self.mock_update_data)
        get_by_id_patch = patch.object(DataDao,
                                       'get_by_id',
                                       side_effect=self.mock_get_by_id)
        get_by_id_patch.start()
        update_data_patch.start()
        return_data = self.service.update_task(_id, status=status, name=name)
        update_data_patch.stop()
        get_by_id_patch.stop()
        assert return_data == {"id": 1, "name": "test2", "status": 1}

    def test_0500_delete_task_success(self):
        """
        Test case:
        delete task success
        """
        _id = 1
        delete_data_patch = patch.object(DataDao,
                                         'delete_data',
                                         side_effect=self.mock_delete_data)
        delete_data_patch.start()
        return_data = self.service.delete_task(_id)
        delete_data_patch.stop()
        assert return_data == "success"

    def test_0501_delete_task_fail(self):
        """
        Test case:
        delete task fail
        """
        self.assertRaisesRegex(RuntimeError, "id is not found in database",
                               self.delete_task_fail)

    def delete_task_fail(self):
        _id = 2
        delete_data_patch = patch.object(DataDao,
                                         'delete_data',
                                         side_effect=self.mock_delete_data)
        delete_data_patch.start()
        return_data = self.service.delete_task(_id)
        delete_data_patch.stop()

    def mock_find_all(self, *args, **kwargs):
        return [{
            "id": 1,
            "name": "name",
            "status": 0
        }, {
            "id": 2,
            "name": "name2",
            "status": 1
        }]

    def mock_check_exist_by_id(self, id):
        if id == 1:
            return True
        else:
            return False

    def mock_insert_data(self, name):
        self.__class__.task_data = {"id": 1, "name": name, "status": 0}
        return 1

    def mock_get_by_id(self, id):
        if id == 1:
            return self.task_data

    def mock_update_data(self, id, **kwargs):
        self.__class__.task_data = {
            "id": id,
            "name": kwargs["name"],
            "status": kwargs["status"]
        }

    def mock_delete_data(self, id):
        if id == 1:
            return True
        else:
            return False