def test_add_new_risk_type(self):
     session = self.Session()
     risk_type = RiskType(name='Golf prize')
     session.add(risk_type)
     session.commit()
     found_risk_type = session.query(RiskType).filter_by(name='Golf prize').first() 
     assert risk_type == found_risk_type
     assert found_risk_type.id is not None
 def test_add_new_risk_type_attribute_Date(self):
     session = self.Session()
     risk_type = RiskType(name='Golf prize')
     session.add(risk_type)
     attr_type = AttributeTypeDate(name='When', risk_type=risk_type)
     session.add(attr_type)
     session.commit()
     found_risk_type = session.query(RiskType).filter_by(name='Golf prize').first()
     found_risk_type_attribute = session.query(AttributeType).filter_by(name='When').first()
     assert found_risk_type_attribute.risk_type == risk_type
     assert found_risk_type.attributes == [attr_type]
 def test_add_new_risk_type_attribute_Numeric(self):
     session = self.Session()
     risk_type = RiskType(name='Golf prize')
     session.add(risk_type)
     prize_value = AttributeTypeNumeric(name='Value', risk_type=risk_type)
     session.add(prize_value)
     session.commit()
     found_risk_type = session.query(RiskType).filter_by(name='Golf prize').first()
     found_risk_type_attribute = session.query(AttributeType).filter_by(name='Value').first()
     assert found_risk_type_attribute.risk_type == risk_type
     assert found_risk_type.attributes == [prize_value]
 def test_add_new_risk_type_attribute_Enum(self):
     session = self.Session()
     risk_type = RiskType(name='Golf prize')
     session.add(risk_type)
     allowed_values = [AttributeTypeEnumValue(value=v) for v in ['EASY', 'NORMAL', 'HARD']]
     attr_type = AttributeTypeEnum(name='level', risk_type=risk_type, allowed_values=allowed_values)
     session.add(attr_type)
     session.commit()
     found_risk_type = session.query(RiskType).filter_by(name='Golf prize').first()
     found_risk_type_attribute = session.query(AttributeType).filter_by(name='level').first()
     assert found_risk_type_attribute.risk_type == risk_type
     assert found_risk_type.attributes == [attr_type]
    def test_add_new_risk_type_attribute_instance_DateAttribute(self):
        session = self.Session()
        risk_type = RiskType(name='Golf prize')
        session.add(risk_type)
        attr_type = AttributeTypeDate(name='When', risk_type=risk_type)
        session.add(attr_type)

        risk_type_instance = RiskTypeInstance(risk_type=risk_type)
        session.add(risk_type_instance)

        risk_type_attribute_instance = AttributeInstanceDate(risk_type_instance=risk_type_instance,
                                            attribute_type=attr_type, date_value=datetime.date(2017, 12, 15))
        session.add(risk_type_attribute_instance)

        session.commit()

        found_risk_type_instance = session.query(RiskTypeInstance).filter_by(risk_type=risk_type).first()

        assert found_risk_type_instance.attributes_instances == [risk_type_attribute_instance]
    def test_add_new_risk_type_attribute_instance_TextAttribute(self):
        session = self.Session()
        risk_type = RiskType(name='Golf prize')
        session.add(risk_type)
        attr_type = AttributeTypeText(name='Player', risk_type=risk_type)
        session.add(attr_type)

        risk_type_instance = RiskTypeInstance(risk_type=risk_type)
        session.add(risk_type_instance)

        risk_type_attribute_instance = AttributeInstanceText(risk_type_instance=risk_type_instance,
                                            attribute_type=attr_type, text_value='John')
        session.add(risk_type_attribute_instance)

        session.commit()

        found_risk_type_instance = session.query(RiskTypeInstance).filter_by(risk_type=risk_type).first()

        assert found_risk_type_instance.attributes_instances == [risk_type_attribute_instance]
    def test_add_new_risk_type_attribute_instance_NumericAttribute(self):
        session = self.Session()
        risk_type = RiskType(name='Golf prize')
        session.add(risk_type)
        attr_type = AttributeTypeNumeric(name='Value', risk_type=risk_type)
        session.add(attr_type)

        risk_type_instance = RiskTypeInstance(risk_type=risk_type)
        session.add(risk_type_instance)

        risk_type_attribute_instance = AttributeInstanceNumeric(risk_type_instance=risk_type_instance,
                                            attribute_type=attr_type, numeric_value=Decimal('10.50'))
        session.add(risk_type_attribute_instance)

        session.commit()

        found_risk_type_instance = session.query(RiskTypeInstance).filter_by(risk_type=risk_type).first()

        assert found_risk_type_instance.attributes_instances == [risk_type_attribute_instance]
    def test_add_new_risk_type_attribute_instance_using_data_type(self):
        session = self.Session()
        risk_type = RiskType(name='Golf prize')
        session.add(risk_type)
        attr_type = AttributeType(name='Value', data_type=AttributeDataType.INT, risk_type=risk_type)
        session.add(attr_type)

        risk_type_instance = RiskTypeInstance(risk_type=risk_type)
        session.add(risk_type_instance)

        risk_type_attribute_instance = AttributeInstanceInt(risk_type_instance=risk_type_instance,
                                            attribute_type=attr_type, int_value=10)
        session.add(risk_type_attribute_instance)

        session.commit()

        found_risk_type_instance = session.query(RiskTypeInstance).filter_by(risk_type=risk_type).first()

        assert found_risk_type_instance.attributes_instances == [risk_type_attribute_instance]
    def test_add_new_risk_type_attribute_instance_EnumAttribute(self):
        session = self.Session()
        risk_type = RiskType(name='Golf prize')
        session.add(risk_type)

        allowed_values = [AttributeTypeEnumValue(value=v) for v in ['EASY', 'NORMAL', 'HARD']]
        attr_type = AttributeTypeEnum(name='Level', risk_type=risk_type, allowed_values=allowed_values)
        session.add(attr_type)

        risk_type_instance = RiskTypeInstance(risk_type=risk_type)
        session.add(risk_type_instance)

        risk_type_attribute_instance = AttributeInstanceEnum(risk_type_instance=risk_type_instance,
                                            attribute_type=attr_type, enum_value='EASY')
        session.add(risk_type_attribute_instance)

        session.commit()

        found_risk_type_instance = session.query(RiskTypeInstance).filter_by(risk_type=risk_type).first()

        assert found_risk_type_instance.attributes_instances == [risk_type_attribute_instance]
    def add_type(self, type_description):
        session = self._Session()
        risk_type = RiskType(name=type_description['name'],
                             description=type_description['description'])
        session.add(risk_type)

        for attribute in type_description.get('attributes', []):
            args = {
                'name': attribute['name'],
                'description': attribute.get('description'),
                'data_type': str2enum(attribute['type'])
            }

            if args['data_type'] == AttributeDataType.ENUM:
                args['allowed_values'] = [
                    AttributeTypeEnumValue(value=v)
                    for v in attribute.get('allowed_values', [])
                ]
            model_type = _data_type2model_type(args['data_type'])
            attribute_type = model_type(**args)
            risk_type.attributes.append(attribute_type)

        session.commit()
        return risk_type.id