def setUp(self):
        self.engine = create_engine('sqlite:///:memory:', echo=True)
        self.Base = declarative_base(self.engine)
        self.session_handler = ScopedSessionHandler(self.engine)

        class MyModel(self.Base):
            __tablename__ = 'my_model'
            id = Column(Integer, primary_key=True)
            value = Column(String(length=63))

        self.model = MyModel
        self.Base.metadata.create_all()

        class MyManager(AlchemyManager):
            model = self.model
            fields = (
                'id',
                'value',
            )
            paginate_by = 10

        self.manager = MyManager(self.session_handler)

        class MyResource(restmixins.RetrieveList):
            manager = self.manager
            pks = ('id', )
            resource_name = 'my_resource'

        self.resource = MyResource
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:', echo=True)
        self.Base = declarative_base(self.engine)
        self.session_handler = ScopedSessionHandler(self.engine)
        self.session = Session(self.engine)

        class One(self.Base):
            __tablename__ = 'one'
            id = Column(Integer, primary_key=True)
            value = Column(String(length=50))
            manies = relationship("Many", backref='one')

        class Many(self.Base):
            __tablename__ = 'many'
            id = Column(Integer, primary_key=True)
            many_value = Column(String(length=50))
            one_id = Column(Integer, ForeignKey('one.id'))

        class DefaultManager(AlchemyManager):
            session = self.session
            model = One
            fields = ['id', 'value', 'manies.id']

        self.model = One
        self._manager = DefaultManager

        self.One = One
        self.Many = Many
        self.Base.metadata.create_all()
Beispiel #3
0
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:', echo=True)
        self.Base = declarative_base(self.engine)
        self.session_handler = ScopedSessionHandler(self.engine)

        class MyModel(self.Base):
            __tablename__ = 'my_model'
            id = Column(Integer, primary_key=True)
            value = Column(String(length=63))

        self.model = MyModel
        self.Base.metadata.create_all()

        class MyManager(AlchemyManager):
            model = self.model
            fields = ('id', 'value',)
            paginate_by = 10

        self.manager = MyManager(self.session_handler)

        class MyResource(restmixins.RetrieveList):
            manager = self.manager
            pks = ('id',)
            resource_name = 'my_resource'

        self.resource = MyResource
Beispiel #4
0
class TestEasyResource(unittest2.TestCase):
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:')
        self.session_handler = ScopedSessionHandler(self.engine)
        Base.metadata.create_all(self.engine)

    def tearDown(self):
        if self.engine:
            self.engine.dispose()
            self.engine = None

    def test_create_resource(self):
        resource = create_resource(MyModel, self.session_handler)
        req = RequestContainer(body_args=dict(value='blah'))
        resp = resource.create(req)
        self.assertEqual(resp.properties['value'], 'blah')
        id_ = resp.properties['id']
        session = self.session_handler.get_session()
        resp = session.query(MyModel).get(id_)
        self.assertIsNotNone(resp)
        self.assertEqual(resp.value, 'blah')

        retrieved = resource.retrieve(
            RequestContainer(url_params=dict(id=id_)))
        self.assertEqual(retrieved.properties['value'], 'blah')

        retrieved_list = resource.retrieve_list(RequestContainer())
        self.assertEqual(len(retrieved_list.related_resources), 1)
        relatec = retrieved_list.related_resources[0].resource
        self.assertEqual(len(relatec), 1)

        updated = resource.update(
            RequestContainer(url_params=dict(id=id_),
                             body_args=dict(value='new')))
        self.assertEqual(updated.properties['value'], 'new')
        session = self.session_handler.get_session()
        model = session.query(MyModel).get(id_)
        self.assertEqual(model.value, 'new')

        deleted = resource.delete(RequestContainer(url_params=dict(id=id_)))
        session = self.session_handler.get_session()
        model = session.query(MyModel).get(id_)
        self.assertIsNone(model)
class TestEasyResource(unittest2.TestCase):
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:')
        self.session_handler = ScopedSessionHandler(self.engine)
        Base.metadata.create_all(self.engine)

    def tearDown(self):
        if self.engine:
            self.engine.dispose()
            self.engine = None

    def test_create_resource(self):
        resource = create_resource(MyModel, self.session_handler)
        req = RequestContainer(body_args=dict(value='blah'))
        resp = resource.create(req)
        self.assertEqual(resp.properties['value'], 'blah')
        id_ = resp.properties['id']
        session = self.session_handler.get_session()
        resp = session.query(MyModel).get(id_)
        self.assertIsNotNone(resp)
        self.assertEqual(resp.value, 'blah')

        retrieved = resource.retrieve(RequestContainer(url_params=dict(id=id_)))
        self.assertEqual(retrieved.properties['value'], 'blah')

        retrieved_list = resource.retrieve_list(RequestContainer())
        self.assertEqual(len(retrieved_list.related_resources), 1)
        relatec = retrieved_list.related_resources[0].resource
        self.assertEqual(len(relatec), 1)

        updated = resource.update(RequestContainer(url_params=dict(id=id_),
                                                   body_args=dict(value='new')))
        self.assertEqual(updated.properties['value'], 'new')
        session = self.session_handler.get_session()
        model = session.query(MyModel).get(id_)
        self.assertEqual(model.value, 'new')

        deleted = resource.delete(RequestContainer(url_params=dict(id=id_)))
        session = self.session_handler.get_session()
        model = session.query(MyModel).get(id_)
        self.assertIsNone(model)
 def test_resource_construction(self):
     """Simple Resource construction"""
     with self.app.app_context():
         user = self.db.session.query(User).filter_by(username='******').first()
         man_model = ManagerModel(model_name='blah', owner=user, fields=('id',))
         self.db.session.add(man_model)
         self.db.session.commit()
         res = ResourceModel(restmixin='CRUDL', manager=man_model, owner=user)
         res.pks = ['id']
         resource_class = res.resource
         self.assertEqual(restmixins.CRUDL, res.restmixin_class)
         self.assertIsInstance(resource_class, ResourceMetaClass)
         req = RequestContainer()
         resource_instance = resource_class.create(req)
         self.assertIsInstance(resource_instance, resource_class)
         id_ = resource_instance.properties['id']
         engine = get_database_engine(user)
         session = ScopedSessionHandler(engine).get_session()
         model = session.query(self.model).get(id_)
         self.assertIsNotNone(model)
         self.assertListEqual(res.pks, ['id'])
         self.assertEqual(res._pks, '["id"]')
Beispiel #7
0
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:', echo=True)
        self.Base = declarative_base(self.engine)
        self.session_handler = ScopedSessionHandler(self.engine)
        self.session = Session(self.engine)

        class MyModel(self.Base):
            __tablename__ = 'my_model'
            id = Column(Integer, primary_key=True)
            big_integer = Column(BigInteger)
            boolean = Column(Boolean)
            date = Column(Date)
            date_time = Column(DateTime)
            enum = Column(Enum('one', 'two' 'three'))
            float = Column(Float)
            integer = Column(Integer)
            interval = Column(Interval)
            large_binary = Column(LargeBinary)
            numeric = Column(Numeric)
            pickle_type = Column(PickleType)
            small_integer = Column(SmallInteger)
            string = Column(String)
            text = Column(Text)
            time = Column(Time)
            unicode = Column(Unicode)
            unicode_text = Column(UnicodeText)

        self.model = MyModel
        self.Base.metadata.create_all()

        class ModelManager(AlchemyManager):
            model = self.model
            fields = [
                'id', 'big_integer', 'boolean', 'date', 'date_time', 'enum',
                'float', 'integer', 'interval', 'large_binary', 'numeric',
                'pickle_type', 'small_integer', 'string', 'text', 'time',
                'unicode', 'unicode_text'
            ]

        self._manager = ModelManager
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import re

from ripozo.resources.fields.common import StringField
from ripozo_sqlalchemy import AlchemyManager, ScopedSessionHandler

from common.models import engine, Post, Comment

session_handler = ScopedSessionHandler(engine)


class PostManager(AlchemyManager):
    model = Post
    fields = (
        'id',
        'username',
        'post_text',
        'comments.id',
    )
    create_fields = (
        'username',
        'post_text',
    )
    _field_validators = {
        'username':
        StringField('username', minimum=3, regex=re.compile(r'^[a-zA-Z0-9]+$'))
    }
Beispiel #9
0
class TestPagination(unittest2.TestCase):
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:', echo=True)
        self.Base = declarative_base(self.engine)
        self.session_handler = ScopedSessionHandler(self.engine)

        class MyModel(self.Base):
            __tablename__ = 'my_model'
            id = Column(Integer, primary_key=True)
            value = Column(String(length=63))

        self.model = MyModel
        self.Base.metadata.create_all()

        class MyManager(AlchemyManager):
            model = self.model
            fields = ('id', 'value',)
            paginate_by = 10

        self.manager = MyManager(self.session_handler)

        class MyResource(restmixins.RetrieveList):
            manager = self.manager
            pks = ('id',)
            resource_name = 'my_resource'

        self.resource = MyResource

    def tearDown(self):
        self.engine.dispose()

    def create_models(self, count=5):
        session = self.session_handler.get_session()
        for i in range(count):
            m = self.model(value=''.join(random.choice(string.ascii_letters) for _ in range(63)))
            session.add(m)
        session.commit()
        session.close()

    def get_linked_resource(self, resource, name):
        for linked in resource.linked_resources:
            if linked.name == name:
                return linked.resource
        return None

    def test_empty(self):
        """
        Tests for when no models have been created
        """
        req = RequestContainer()
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.linked_resources), 0)
        self.assertEqual(len(resource.properties['my_resource']), 0)

    def test_not_enough_for_pagination(self):
        """
        Tests that no pagination links are available when
        there are less than or equal the paginate_by count
        of records.
        """
        self.create_models(count=10)
        req = RequestContainer()
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.linked_resources), 0)
        self.assertEqual(len(resource.properties['my_resource']), 10)

    def test_next_pagination(self):
        """
        Tests basic forward pagination end on
        a multiple of the paginate_by
        """
        self.create_models(count=100)
        req = RequestContainer()
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.properties['my_resource']), 10)
        count = 1
        while self.get_linked_resource(resource, 'next'):
            next_link = self.get_linked_resource(resource, 'next')
            req = RequestContainer(query_args=next_link.get_query_arg_dict())
            resource = self.resource.retrieve_list(req)
            self.assertEqual(len(resource.properties['my_resource']), 10)
            count += 1
        self.assertEqual(10, count)

    def test_next_pagination_partial_end(self):
        """
        Tests forward pagination that ends with a
        partial page (i.e. less than the paginate_by)
        """
        self.create_models(count=101)
        req = RequestContainer()
        resource = self.resource.retrieve_list(req)
        count = 1
        while self.get_linked_resource(resource, 'next'):
            self.assertEqual(len(resource.properties['my_resource']), 10)
            next_link = self.get_linked_resource(resource, 'next')
            req = RequestContainer(query_args=next_link.get_query_arg_dict())
            resource = self.resource.retrieve_list(req)
            count += 1
        self.assertEqual(len(resource.properties['my_resource']), 1)
        self.assertEqual(11, count)

    def test_prev_pagintation(self):
        """
        Tests prev pagination with the total count
        of models as a multiple of the paginate_by
        """
        self.create_models(count=100)
        req = RequestContainer(query_args=dict(page=10))
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.properties['my_resource']), 10)
        count = 1
        while self.get_linked_resource(resource, 'previous'):
            previous = self.get_linked_resource(resource, 'previous')
            req = RequestContainer(query_args=previous.get_query_arg_dict())
            resource = self.resource.retrieve_list(req)
            self.assertEqual(len(resource.properties['my_resource']), 10)
            count += 1
        self.assertEqual(10, count)

    def test_prev_pagination_uneven_total(self):
        """
        Tests previous pagination with the total count
        of models not as a multiple of the paginate_by.
        """
        self.create_models(count=101)
        req = RequestContainer(query_args=dict(page=11))
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.properties['my_resource']), 1)
        count = 1
        while self.get_linked_resource(resource, 'previous'):
            previous = self.get_linked_resource(resource, 'previous')
            req = RequestContainer(query_args=previous.get_query_arg_dict())
            resource = self.resource.retrieve_list(req)
            self.assertEqual(len(resource.properties['my_resource']), 10)
            count += 1
        self.assertEqual(11, count)
 def manager(self):
     session_handler = ScopedSessionHandler(self.engine)
     return PersonManager(session_handler)
Beispiel #11
0
 def setUp(self):
     self.engine = create_engine('sqlite:///:memory:')
     self.session_handler = ScopedSessionHandler(self.engine)
     Base.metadata.create_all(self.engine)
class TestPagination(unittest2.TestCase):
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:', echo=True)
        self.Base = declarative_base(self.engine)
        self.session_handler = ScopedSessionHandler(self.engine)

        class MyModel(self.Base):
            __tablename__ = 'my_model'
            id = Column(Integer, primary_key=True)
            value = Column(String(length=63))

        self.model = MyModel
        self.Base.metadata.create_all()

        class MyManager(AlchemyManager):
            model = self.model
            fields = (
                'id',
                'value',
            )
            paginate_by = 10

        self.manager = MyManager(self.session_handler)

        class MyResource(restmixins.RetrieveList):
            manager = self.manager
            pks = ('id', )
            resource_name = 'my_resource'

        self.resource = MyResource

    def tearDown(self):
        self.engine.dispose()

    def create_models(self, count=5):
        session = self.session_handler.get_session()
        for i in range(count):
            m = self.model(value=''.join(
                random.choice(string.ascii_letters) for _ in range(63)))
            session.add(m)
        session.commit()
        session.close()

    def get_linked_resource(self, resource, name):
        for linked in resource.linked_resources:
            if linked.name == name:
                return linked.resource
        return None

    def test_empty(self):
        """
        Tests for when no models have been created
        """
        req = RequestContainer()
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.linked_resources), 0)
        self.assertEqual(len(resource.properties['my_resource']), 0)

    def test_not_enough_for_pagination(self):
        """
        Tests that no pagination links are available when
        there are less than or equal the paginate_by count
        of records.
        """
        self.create_models(count=10)
        req = RequestContainer()
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.linked_resources), 0)
        self.assertEqual(len(resource.properties['my_resource']), 10)

    def test_next_pagination(self):
        """
        Tests basic forward pagination end on
        a multiple of the paginate_by
        """
        self.create_models(count=100)
        req = RequestContainer()
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.properties['my_resource']), 10)
        count = 1
        while self.get_linked_resource(resource, 'next'):
            next_link = self.get_linked_resource(resource, 'next')
            req = RequestContainer(query_args=next_link.get_query_arg_dict())
            resource = self.resource.retrieve_list(req)
            self.assertEqual(len(resource.properties['my_resource']), 10)
            count += 1
        self.assertEqual(10, count)

    def test_next_pagination_partial_end(self):
        """
        Tests forward pagination that ends with a
        partial page (i.e. less than the paginate_by)
        """
        self.create_models(count=101)
        req = RequestContainer()
        resource = self.resource.retrieve_list(req)
        count = 1
        while self.get_linked_resource(resource, 'next'):
            self.assertEqual(len(resource.properties['my_resource']), 10)
            next_link = self.get_linked_resource(resource, 'next')
            req = RequestContainer(query_args=next_link.get_query_arg_dict())
            resource = self.resource.retrieve_list(req)
            count += 1
        self.assertEqual(len(resource.properties['my_resource']), 1)
        self.assertEqual(11, count)

    def test_prev_pagintation(self):
        """
        Tests prev pagination with the total count
        of models as a multiple of the paginate_by
        """
        self.create_models(count=100)
        req = RequestContainer(query_args=dict(page=10))
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.properties['my_resource']), 10)
        count = 1
        while self.get_linked_resource(resource, 'previous'):
            previous = self.get_linked_resource(resource, 'previous')
            req = RequestContainer(query_args=previous.get_query_arg_dict())
            resource = self.resource.retrieve_list(req)
            self.assertEqual(len(resource.properties['my_resource']), 10)
            count += 1
        self.assertEqual(10, count)

    def test_prev_pagination_uneven_total(self):
        """
        Tests previous pagination with the total count
        of models not as a multiple of the paginate_by.
        """
        self.create_models(count=101)
        req = RequestContainer(query_args=dict(page=11))
        resource = self.resource.retrieve_list(req)
        self.assertEqual(len(resource.properties['my_resource']), 1)
        count = 1
        while self.get_linked_resource(resource, 'previous'):
            previous = self.get_linked_resource(resource, 'previous')
            req = RequestContainer(query_args=previous.get_query_arg_dict())
            resource = self.resource.retrieve_list(req)
            self.assertEqual(len(resource.properties['my_resource']), 10)
            count += 1
        self.assertEqual(11, count)
Beispiel #13
0
 def manager(self):
     session_handler = ScopedSessionHandler(self.engine)
     self.session_handler = session_handler
     self.session = self.session_handler.get_session()
     return self._manager(self.session_handler)
Beispiel #14
0
 def setUp(self):
     self.engine = create_engine('sqlite:///:memory:')
     self.session_handler = ScopedSessionHandler(self.engine)
     Base.metadata.create_all(self.engine)