def test_convert_many_of_same_type(self):
     names = ["name_1", "name_2"]
     models = [SQLAlchemySample(), SQLAlchemySample()]
     models[0].name = names[0]
     models[1].name = names[1]
     converted = convert_to_popo_models(models)     # type: List[NamedModel]
     self.assertEquals([x.name for x in converted], [x.name for x in models])
 def get_all(self) -> Sequence[MappedType]:
     query_model = self._sqlalchemy_model_type
     session = self._database_connector.create_session()
     result = session.query(query_model).all()
     session.close()
     assert isinstance(result, collections.Sequence)
     return convert_to_popo_models(result)
 def test_convert_many_of_different_type(self):
     names = ["name_1", "name_2"]
     models = [SQLAlchemySample(), SQLAlchemyLibrary()]
     models[0].name = names[0]
     models[1].name = names[1]
     converted = convert_to_popo_models(models)     # type: List[NamedModel]
     self.assertEquals([x.name for x in converted], [x.name for x in models])
     self.assertIsInstance(converted[0], Sample)
     self.assertIsInstance(converted[1], Library)
    def _get_association(self, associated_with: Union[InternalIdModel, Iterable[InternalIdModel]],
                         relationship_property_name: str) -> Sequence[_InternalIdMappedType]:
        """
        Gets the models that are associated to another model, linked to via the specified relationship property.
        :param associated_with: the model to find other models that are associated with it
        :param relationship_property_name: the property on `associated_with` in which the relationship is expressed
        :return: all models associated with the given `associated_with` model
        """
        if isinstance(associated_with, InternalIdModel):
            associated_with = [associated_with]
        if len(associated_with) == 0:
            return []

        session = self._database_connector.create_session()
        sqlalchemy_associated_with_type = get_equivalent_sqlalchemy_model_type(associated_with[0].__class__)
        assert sqlalchemy_associated_with_type is not None
        results = session.query(sqlalchemy_associated_with_type). \
            filter(sqlalchemy_associated_with_type.internal_id.
            in_([x.internal_id for x in associated_with])). \
            all()
        assert isinstance(results, collections.Sequence)

        if len(results) != len(associated_with):
            raise ValueError(
                "Not all given models to find associations with exist in the database.\nGiven: %s\nExisting: %s"
                % (associated_with, convert_to_popo_models(results)))

        associated = []
        for result in results:
            relationships = getattr(result, relationship_property_name)
            if not isinstance(relationships, list):
                relationships = [relationships]
            # Ensure only gets put in `associated` list once, even if the associate is associated with many of the given
            # `associated_with` models.
            for relationship in relationships:
                if relationship not in associated:
                    associated.append(relationship)
        session.close()

        return convert_to_popo_models(associated)
    def _get_by_property_value_sequence(self, property: Property, required_property_values: Iterable[Any]) \
            -> Sequence[MappedType]:
        query_model = self._sqlalchemy_model_type
        session = self._database_connector.create_session()

        # FIXME: It is an assumption that the Model property has the same name as SQLAlchemyModel property
        query_column = query_model.__dict__[property]   # type: Column
        results = session.query(query_model). \
            filter(query_column.in_(required_property_values)).\
            all()
        session.close()
        assert isinstance(results, collections.Sequence)
        return convert_to_popo_models(results)