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 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
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_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()
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 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
def new_risk_type(app, new_company): params = {'name': fake.industry(), 'company_id': new_company.id} risk_type = RiskType(**params) return risk_type.save()
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())