Beispiel #1
0
    def test_fields_can_be_reused_across_risk_types(self):
        """
        A single field may be reused in different risk types.
        """
        # create two separate risk types
        real_state_risk_title = 'Real State Risk Profile'
        car_risk_title = 'Car Risk Profile'
        real_state_risk = RiskType(name=real_state_risk_title)
        car_risk = RiskType(name=car_risk_title)

        # create a single field and add it to both types
        cost_field = GenericField(name='Cost',
                                  description='Asset\'s Cost',
                                  type=FieldType.NUMBER)
        real_state_risk.fields.append(cost_field)
        car_risk.fields.append(cost_field)
        self.db.session.add_all([real_state_risk, car_risk, cost_field])
        self.db.session.commit()

        risk_profiles = RiskType.query.all()
        self.assertEqual(len(risk_profiles), 2)
        real_state_risk = RiskType.query.filter_by(
            name=real_state_risk_title).first()
        car_risk = RiskType.query.filter_by(name=car_risk_title).first()
        # each risk type should have a single field
        self.assertEqual(len(real_state_risk.fields), 1)
        self.assertEqual(len(car_risk.fields), 1)
        # each risk type's field must be the same
        self.assertEqual(real_state_risk.fields[0], car_risk.fields[0])
    def test_save(self, init_db, new_company):
        """
            Test for creating a new risk-type

            Args:
                init_db(SQLAlchemy): fixture to initialize the test database
                new_company (Company): Fixture to create a new company
        """
        params = {'name': fake.industry(), 'company_id': new_company.id}
        risk_type = RiskType(**params)
        assert risk_type == risk_type.save()
Beispiel #3
0
def new_risk_type_with_attribute1(app, new_company):
    params = {'name': fake.industry(), 'company_id': new_company.id}
    risk_type = RiskType(**params)
    risk_type = risk_type.save()

    attribute = Attribute(_key='branch',
                          label='branch',
                          is_required=False,
                          input_control='text',
                          choices='choice')
    risk_type.attributes.append(attribute)
    return risk_type.save()
def generate_risk_with_fields(db, risk_type_data):
    """
    Utility function to create the risk and fields as specified via
    `risk_type_data` at the corresponding tables on the `db`.

    The expected `risk_type_data` must have the following form:

    {
        'name': 'Name of risk',
        'description': 'Description of risk',
        'fields': [
            { 'name': 'name of field',
              'type': 'type of field',
              'description': 'field desc',
              'options': { ... } },
            ...
        ]
    }
    """
    risk = RiskType(name=risk_type_data['name'],
                    description=risk_type_data['description'])
    db.session.add(risk)
    for field_data in risk_type_data['fields']:
        field = GenericField(**field_data)
        risk.fields.append(field)
        db.session.add(field)
    db.session.commit()
    return risk
Beispiel #5
0
 def test_risk_types_can_have_multiple_custom_fields(self):
     """
     Multiple fields may be added to a single risk type.
     """
     # create risk type
     real_state_risk = RiskType(
         name='Real State Risk Profile',
         description='Risk model for house/real state insurance')
     # create generic fields
     address_field = GenericField(name='Address',
                                  description='Property\'s address',
                                  type=FieldType.TEXT)
     cost_field = GenericField(
         name='Cost',
         description='Current market price for the property',
         type=FieldType.NUMBER)
     build_date_field = GenericField(
         name='Built Date',
         description='The date the house was built',
         type=FieldType.DATE)
     # add generic fields to risk type
     real_state_risk.fields.extend(
         [address_field, cost_field, build_date_field])
     # commit
     self.db.session.add_all(
         [real_state_risk, address_field, cost_field, build_date_field])
     self.db.session.commit()
     # query risk type and assert fields are present
     saved_real_state_risk = RiskType.query.first()
     # the risk type should contain all three fields
     self.assertEqual(len(saved_real_state_risk.fields), 3)
    def test_delete(self, init_db, new_risk_type):
        """
            Test that a new risk-type gets deleted

            Args:
                init_db(SQLAlchemy): fixture to initialize the test database
                new_risk_type (RiskType): Fixture to create a new risk-type
        """
        new_risk_type.delete()
        assert RiskType.get(new_risk_type.id) is None
    def test_get(self, init_db, new_risk_type):
        """
            Test that a new risk-type gets fetched

            Args:
                init_db(SQLAlchemy): fixture to initialize the test database
                new_risk_type (RiskType): Fixture to create a new risk-type
        """

        assert RiskType.get(new_risk_type.id) == new_risk_type
Beispiel #8
0
    def post(self):
        """
        POST method for creating risk-types.

        Payload should have the following parameters:
            name(str): name of the risk-type
        """

        request_data = request.get_json()

        request_data['companyId'] = request.decoded_token.get('sub').get(
            'company')['id']

        risk_type_schema = RiskTypeSchema()
        risk_type_data = risk_type_schema.load_object_into_schema(request_data)

        risk_type = RiskType(**risk_type_data)

        attributes_data = request_data.get('customAttributes')

        if attributes_data:
            attributes_schema = AttributeSchema(many=True, exclude=['id'])

            attributes = attributes_schema.load_object_into_schema(
                attributes_data)
            risk_type.attributes = attributes
            attributes = attributes_schema.dump(attributes).data
        else:
            raise ValidationError(
                {'message': ERROR_MESSAGES['PROVIDE_CUSTOM_ATTRIBUTES']})

        risk_type = risk_type.save()

        return {
            'status': 'success',
            'message': SUCCESS_MESSAGES['RISK_TYPE_CREATED'],
            'data': risk_type_schema.dump(risk_type).data
        }, 201
Beispiel #9
0
    def delete(self, risk_type_id):
        """
        Delete a single risk type
        """
        risk_type = RiskType.get_or_404(risk_type_id)

        risk_type.delete()

        return (
            {
                "message": SUCCESS_MESSAGES["DELETED"].format("Risk Type"),
                "status": "success",
            },
            200,
        )
Beispiel #10
0
    def get(self, risk_type_id):
        """
        Get a single risk type
        """

        risk_type = RiskType.get_or_404(risk_type_id)

        return (
            {
                "data":
                EagerLoadRiskTypeAttributesSchema().dump(risk_type).data,
                "message": SUCCESS_MESSAGES["FETCHED"].format("Risk Type"),
                "status": "success",
            },
            200,
        )
Beispiel #11
0
    def get(self):
        """
        Gets risk-type list
        """
        risk_types = RiskType.filter(
            company_id=request.decoded_token.get('sub').get('company')['id'])

        risk_type_schema = EagerLoadRiskTypeAttributesSchema(many=True)

        return (
            {
                "data": risk_type_schema.dump(risk_types).data,
                "message": SUCCESS_MESSAGES["FETCHED"].format("Risk Types"),
                "status": "success",
            },
            200,
        )
Beispiel #12
0
    def get(self, risk_type_id):
        """
        Get a single risk type risks
        """

        risk_type = RiskType.get_or_404(risk_type_id)

        risks = risk_type.risks.all()

        risk_schema = RiskSchema(many=True)

        return (
            {
                "data": risk_schema.dump(risks).data,
                "message": SUCCESS_MESSAGES["FETCHED"].format("Risks"),
                "status": "success",
            },
            200,
        )
Beispiel #13
0
    def put(self, risk_type_id):
        """
        Updates risk type and the corresponding attributes
        """

        risk_type = RiskType.get_or_404(risk_type_id)
        request_data = request.get_json()

        request_data['id'] = risk_type.id

        risk_type_schema = RiskTypeSchema()
        risk_type_data = risk_type_schema.load_object_into_schema(request_data,
                                                                  partial=True)

        attributes_data = request_data.get('customAttributes')
        attributes_schema = AttributeSchema(many=True)

        if attributes_data:
            attributes_schema_data = attributes_schema.load_object_into_schema(
                attributes_data, partial=True)

            parse_attributes_data(risk_type, attributes_schema_data,
                                  risk_type_id)

        risk_type.update(**risk_type_data)

        attributes = attributes_schema.dump(risk_type.attributes.all()).data
        risk_type_data = risk_type_schema.dump(risk_type).data

        response = jsonify({
            "status":
            'success',
            "message":
            SUCCESS_MESSAGES['UPDATED'].format('Risk type'),
            "data": {
                **risk_type_data, "customAttributes": attributes
            }
        })

        response.status_code = 200
        return response
Beispiel #14
0
def new_risk_type(app, new_company):
    params = {'name': fake.industry(), 'company_id': new_company.id}
    risk_type = RiskType(**params)
    return risk_type.save()
Beispiel #15
0
def new_multiple_risk_types(app, new_company):
    risk_types = []
    for each in range(2):
        params = {'name': fake.industry(), 'company_id': new_company.id}
        risk_type = RiskType(**params)
        risk_types.append(risk_type.save())