def test_success_when_element_validation_on_none_data(): schema = Kanpai.Array().of( Kanpai.Object({ 'name': Kanpai.String().trim().required(), 'age': Kanpai.Number().integer() })) result = schema.validate(None) assert result.get('success') is True
def create_job(): schema = Kanpai.Object({ 'goal': Kanpai.Array({ 'position': Kanpai.Object({ 'x': Kanpai.Number().required(), 'y': Kanpai.Number().required(), 'z': Kanpai.Number().required() }).required(), 'orientation': Kanpai.Object({ 'x': Kanpai.Number().required(), 'y': Kanpai.Number().required(), 'z': Kanpai.Number().required(), 'w': Kanpai.Number().required() }).required() }).max(2).min(2).required() }) validation_result = schema.validate(request.json) if validation_result.get('success', False) is False: abort(400) # Initial status to pending status = JobStatus.PENDING.value # Initial goal status to pending goal_status = GoalStatus.PENDING.value # Initial created date to now created_date = datetime.datetime.now() job = JobRequest(status=status, created_date=created_date, updated_date=None) try: job_id = job_service.create_job(job) if job_id is None: abort(400) for goal in [Object(obj) for obj in request.json['goal']]: goal_request = GoalRequest(job_id=job_id, position_x=goal.position.x, position_y=goal.position.y, position_z=goal.position.z, orientation_x=goal.orientation.x, orientation_y=goal.orientation.y, orientation_z=goal.orientation.z, orientation_w=goal.orientation.w, status=goal_status) goal_service.create_goal(goal_request) return jsonify(message='success', result=None), 201 except Exception as e: abort(500)
def test_successful_validation(): schema = Kanpai.Object({ 'name': Kanpai.String(), 'permissions': Kanpai.Array().of(Kanpai.UUID()).required() }).required() result = schema.validate({ 'permissions': ['a7e459b9-0ec9-41f6-8c78-149ad76c943d'] }) assert result == { 'success': True, 'data': { 'permissions': [UUID('a7e459b9-0ec9-41f6-8c78-149ad76c943d')], 'name': None }, 'error': None}
def test_success_when_element_validation_success(): schema = Kanpai.Array().of( Kanpai.Object({ 'name': Kanpai.String().trim().required(), 'age': Kanpai.Number().integer() })).required() result = schema.validate([{ 'name': ' Rpose ion ', 'age': 46 }, { 'age': '98', 'name': 'Mert rt' }]) assert result.get('success') is True
def test_error_when_element_validation_error(): schema = Kanpai.Array().of( Kanpai.Object({ 'name': Kanpai.String().trim().required(), 'age': Kanpai.Number().integer() })).required() result = schema.validate([{ 'name': ' Rpose ion ', 'age': 46 }, { 'age': '98' }]) assert result.get('success') is False assert result.get('error').get(1, None) is not None
def validatePodcast(requestBody): schema = Kanpai.Object({ "audiotype": Kanpai.String(error="audio type must be a string").required( error="audiotype is required"), "metadata": Kanpai.Object({ "name": Kanpai.String( error="podcast name must be a string").max(100).required( error="name is required"), "duration": Kanpai.Number(error="duration must be an int").required( error="duration is required"), "host": Kanpai.String(error="host must be a string").max(100).required( error="host is required"), "participants": Kanpai.Array(error="participants must be a list").max(10) }).required(error="metadata is required") }).required(error="Invalid request body passed") valid_result = schema.validate(requestBody) return valid_result
def test_error_when_more_than_maximum_element(): schema = Kanpai.Array().of(Kanpai.String()).max(3) result = schema.validate(['CKP', 'RZSD', 'LOMP', 'LKUIO']) assert result.get('success') is False
def test_error_when_minimum_element_not_there(): schema = Kanpai.Array().of(Kanpai.String()).min(1) result = schema.validate([]) assert result.get('success') is False
def test_error_when_invalid_element_validator(): with pytest.raises(TypeError): Kanpai.Array().of("kanpai").required()
def test_error_when_data_is_not_array(): schema = Kanpai.Array() result = schema.validate('not an array') assert result.get('success') is False
def test_sucees_if_data_is_proper_when_required(): schema = Kanpai.Array().required() result = schema.validate([12, 45]) assert result.get('success') is True
def test_error_if_data_is_none_when_required(): schema = Kanpai.Array().required() result = schema.validate(None) assert result.get('success') is False