Testing with pyramid_services

140 views
Skip to first unread message

Mark Laskey

unread,
Jun 20, 2017, 4:49:11 PM6/20/17
to pylons-discuss
Hello all!

I started diving into pyramid_services at work last week and it seems like a great tool to refactor view and test code into something that looks cleaner.  We intend to use the service to separate live DB data from test data by having our tests hit up against the testing service that would hold mock data.  I've followed the documentation at https://github.com/mmerickel/pyramid_services and the service works as intended when it comes to the live DB data.  The issue lies within our test code and the DummyRequest object.  

    def __init__(self, context, request):
        self.context = context
        self.request = request
>       self.data_svc = self.request.find_service(Data)
E       AttributeError: 'DummyRequest' object has no attribute 'find_service'

Is there a way to give the DummyRequest object the find_service attribute like we do with the BaseView(see below)?  We were going to explore the pyramid.requests.Request 

Mind you, I'm not 100% sure if this is the proper way to use pyramid_services so please feel free to let me know if this is the correct way or if there are any tweaks you would recommend.



CODE:

interfaces.py

from zope.interface import Interface

class Data(Interface):
    def get_companies():
        pass

services.py (live DB data)

class DBData(object):
    def get_companies(self):
        bpm_companies = DBSession.query(BPMCompany).filter(BPMCompany.status == 1).all()

        return bpm_companies

services_test.py(test data)

class MockData(object):
    def get_companies(self):
        bpm_companies = [{'company_name': 'ABC Corp'}, {'company_name': 'DEF Corp'}, {'company_name': 'Z Corp'}]

        return bpm_companies

__init__.py

...
config.include('pyramid_services')
config.register_service(DBData(), Data)
...

base.py(BaseView)

class BaseView(object):
    ...
    def __init__(self, context, request):
        self.context = context
        self.request = request
        self.data_svc = self.request.find_service(Data)
    ...

company.py(view)

class CompanyView(BaseView):
    @view_config(route_name='company', renderer='reporting/company.mako', permission='admin')
    def index(self):
        request = self.request

        bpm_companies = self.data_svc.get_companies()

        return {'companies': bpm_companies}

company_test.py

from pyramid import testing

import pytest

from bpmreporting.views.company import CompanyView

from bpmreporting.services.services_test import MockData
from bpmreporting.services.interfaces import Data

@pytest.fixture
def config():
    config = testing.setUp()
    config.include('pyramid_mako')
    config.include('pyramid_services')
    config.register_service(MockData(), Data)
    config.add_route('company', 'company')
    yield config
    testing.tearDown()

@pytest.fixture
def dummy_request():
    return testing.DummyRequest()

@pytest.fixture
def dummy_context():
    return testing.DummyResource()

# Make sure index is returning the proper list of company names.  ie: The MockData list of dictionaries.
def test_company_index_view(config, dummy_request, dummy_context):
    vw = CompanyView(dummy_context, dummy_request)
    response = vw.index()
    print(response)
    # assert 'report_type_options' in response





Message has been deleted

Mark Laskey

unread,
Jun 20, 2017, 4:52:35 PM6/20/17
to pylons-discuss
Never finished my sentence!

We were going to explore the pyramid.requests.Request, but we aren't sure how to implement it within a test.

Michael Merickel

unread,
Jun 20, 2017, 5:01:36 PM6/20/17
to Pylons
Hey Mark!

The `request.find_service` method is a request method... these are not enabled by default as they are extensions to the individual request instance. You can use `pyramid.request.apply_request_extensions(request)` to make this happen in your setup code (after calling `config.add_request_method(...)` or `config.include(...)`). You can do this with either the DummyRequest of a real pyramid.request.Request object.

- Michael


On Tue, Jun 20, 2017 at 3:52 PM, Mark Laskey <laskeym...@gmail.com> wrote:
Never finished my sentence!

We were going to explore the pyramid.requests.Request, but we aren't sure how to implement it within a test.

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/0acff96f-272a-467d-852f-313954a8e3b9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Bert JW Regeer

unread,
Jun 20, 2017, 5:06:19 PM6/20/17
to pylons-...@googlegroups.com
This would be nice to have documented under:

http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/testing.html#creating-integration-tests

As well as an example showing the use of a real p.r.Request object for when you do need the full power of the Request object.

Bert

> On Jun 20, 2017, at 15:00 , Michael Merickel <mmer...@gmail.com> wrote:
>
> Hey Mark!
>
> The `request.find_service` method is a request method... these are not enabled by default as they are extensions to the individual request instance. You can use `pyramid.request.apply_request_extensions(request)` to make this happen in your setup code (after calling `config.add_request_method(...)` or `config.include(...)`). You can do this with either the DummyRequest of a real pyramid.request.Request object.
>
> - Michael
>
>
> On Tue, Jun 20, 2017 at 3:52 PM, Mark Laskey <laskeym...@gmail.com> wrote:
> Never finished my sentence!
>
> We were going to explore the pyramid.requests.Request, but we aren't sure how to implement it within a test.
>
> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
> To post to this group, send email to pylons-...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
> To post to this group, send email to pylons-...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAKdhhwEDZxsuf8APCEYZCkF5Vv%3DhSN1ciyme5oCJn-6atSJvNw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages