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 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_enum_type_requires_options_dict(self): """ Enum-typed fields may not be created without specifying `options`. """ with self.assertRaises(ValueError): enum_type_field = GenericField('Enum field without options', type=FieldType.ENUM)
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_enum_type_requires_options_dict(self): """ Enum-typed fields require a dictionary as their `options`. """ with self.assertRaises(ValueError): enum_type_field = GenericField('Enum field with non-dict options', type=FieldType.ENUM, options='not a dictionary')
def test_enum_type_requires_options_dict_with_at_least_one_choice(self): """ Enum-typed fields may not be created without specifying some choices in the options field. """ with self.assertRaises(ValueError): enum_type_field = GenericField('Enum field with empty choices', type=FieldType.ENUM, options={'choices': []})
def test_enum_type_happy_path(self): """ Enum-typed fields should receive an extra `options` parameter. """ options = {'choices': ['One', 'Two', 'Three']} enum_type_field = GenericField('An enum-typed field', type=FieldType.ENUM, options=options) self.db.session.add(enum_type_field) self.db.session.commit() saved_field = GenericField.query.first() self.assertIsNotNone(saved_field) self.assertEqual(saved_field.options, options)