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
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pylons-discuss/f0ddb5a3-6a10-4b2e-ba50-b854c37902db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.