Beispiel #1
0
    def repository(self, reference):
        """ Retrieve the collection

            :param reference: the entity class or entity metadata of the target repository / collection
            :rtype: tori.db.repository.Repository
        """
        key = None

        if isinstance(reference, EntityMetadata):
            key = reference.collection_name
        elif EntityMetadataHelper.hasMetadata(reference):
            is_registerable_reference = True

            metadata = EntityMetadataHelper.extract(reference)
            key      = metadata.collection_name

            self.register_class(reference)

        if not key:
            raise UnsupportedRepositoryReferenceError('Either a class with metadata or an entity metadata is supported.')

        if key not in self._repository_map:
            repository = Repository(
                session            = self,
                representing_class = reference
            )

            repository.setup_index()

            self._repository_map[key] = repository

        return self._repository_map[key]
Beispiel #2
0
    def test_negative_put_without_id(self, session):
        repository = Repository(session, Data)

        a = Data()
        a.__session__ = True

        with self.assertRaises(EntityNotRecognized):
            repository.put(a)
Beispiel #3
0
    def test_negative_put_without_session(self, session):
        repository = Repository(session, Data)

        a = Data()
        a.id = ObjectId()

        with self.assertRaises(EntityNotRecognized):
            repository.put(a)
Beispiel #4
0
    def test_negative_post(self, session):
        repository = Repository(session, Data)

        data = Data()
        data.__session__ = True

        with self.assertRaises(EntityAlreadyRecognized):
            repository.post(data)
Beispiel #5
0
    def test_negative_delete_random(self, session):
        repository = Repository(session, Data)

        a = Data()
        a.id = PseudoObjectId()
        a.__session__ = True

        with self.assertRaises(EntityNotRecognized):
            repository.put(a)
Beispiel #6
0
    def test_positive_post(self, session):
        repository = Repository(session, Data)

        data = Data()
        data.__session__ = None

        repository.post(data)

        session.persist.assert_called_with(data)
        session.flush.assert_called_with()
Beispiel #7
0
    def test_positive_delete(self, session):
        repository = Repository(session, Data)

        a = Data()
        a.id = 1
        a.__session__ = True

        repository.delete(a)

        session.delete.assert_called_with(a)
        session.flush.assert_called_with()
Beispiel #8
0
    def test_positive_put(self, session):
        repository = Repository(session, Data)

        a = Data()
        a.id = ObjectId()
        a.__session__ = True

        repository.put(a)

        session.persist.assert_called_with(a)
        session.flush.assert_called_with()