def update_class(dataset_id):
    """Update class in a dataset.

    If one of the fields is not specified, it will not be updated.

    **Example request**:

    .. sourcecode:: json

        {
            "name": "Very happy",
            "new_name": "Recordings that represent ultimate happiness."
        }

    :reqheader Content-Type: *application/json*
    :<json string name: *Required.* Current name of the class.
    :<json string new_name: *Optional.* New name of the class. Must be unique within a dataset.
    :<json string description: *Optional.* Description of the class.

    :resheader Content-Type: *application/json*
    """
    ds = get_check_dataset(dataset_id, write=True)
    class_data = request.get_json()

    try:
        dataset_validator.validate_class_update(class_data)
    except dataset_validator.ValidationException as e:
        raise api_exceptions.APIBadRequest(str(e))

    try:
        db.dataset.update_class(ds["id"], class_data["name"], class_data)
    except db.exceptions.NoDataFoundException as e:
        # NoDataFoundException is raised if the class name doesn't exist in this dataset.
        # We treat this as a bad request, because it's based on data in the request body,
        # and not the url
        raise api_exceptions.APIBadRequest(str(e))

    return jsonify(
        success=True,
        message="Class updated."
    )
Example #2
0
def update_class(dataset_id):
    """Update class in a dataset.

    If one of the fields is not specified, it will not be updated.

    **Example request**:

    .. sourcecode:: json

        {
            "name": "Very happy",
            "new_name": "Recordings that represent ultimate happiness."
        }

    :reqheader Content-Type: *application/json*
    :<json string name: *Required.* Current name of the class.
    :<json string new_name: *Optional.* New name of the class. Must be unique within a dataset.
    :<json string description: *Optional.* Description of the class.

    :resheader Content-Type: *application/json*
    """
    ds = get_check_dataset(dataset_id, write=True)
    class_data = request.get_json()

    try:
        dataset_validator.validate_class_update(class_data)
    except dataset_validator.ValidationException as e:
        raise api_exceptions.APIBadRequest(e.error)

    try:
        db.dataset.update_class(ds["id"], class_data["name"], class_data)
    except db.exceptions.NoDataFoundException as e:
        # NoDataFoundException is raised if the class name doesn't exist in this dataset.
        # We treat this as a bad request, because it's based on data in the request body,
        # and not the url
        raise api_exceptions.APIBadRequest(str(e))

    return jsonify(success=True, message="Class updated.")
    def test_class_update(self):
        # requires at least name field
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({})

        # just name field is OK
        dataset_validator.validate_class_update({"name": "a name"})

        # all fields is OK
        dataset_validator.validate_class_update({
            "name": "a name",
            "new_name": "new name",
            "description": "a desc"
        })

        # Extra field
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({"anotherfield": "value"})

        # new name must be string
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({
                "name": "a name",
                "new_name": 1
            })
        # new name limits
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({
                "name": "a name",
                "new_name": ""
            })

        # description must be string
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({
                "name": "a name",
                "description": 1
            })
    def test_class_update(self):
        # requires at least name field
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({})

        # just name field is OK
        dataset_validator.validate_class_update({"name": "a name"})

        # all fields is OK
        dataset_validator.validate_class_update({"name": "a name", "new_name": "new name", "description": "a desc"})

        # Extra field
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({"anotherfield": "value"})

        # new name must be string
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({"name": "a name", "new_name": 1})
        # new name limits
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({"name": "a name", "new_name": ""})

        # description must be string
        with self.assertRaises(dataset_validator.ValidationException):
            dataset_validator.validate_class_update({"name": "a name", "description": 1})