Hi,

I am a newbie having difficulty understanding and getting functional 
testing working with pyramid, SQLAlchemy, pytest and webtest. I am using 
pyramid 1.10. Hoping that someone is able to advise a way forward or direct 
me to any useful resources.

I have written the fixture below that creates a SQL Alchemy session for 
each test and initialises data within a transaction, based upon 
documentation for functional testing at the wiki 
<https://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki2/tests.html>
 that 
uses unittest.
When the fixture completes the transaction aborts and closes the session. 
When the next test runs the fixture will create a new transaction and 
reinitialise the data.
   
@pytest.fixture
def session(request, testapp):

    factory = testapp.app.registry['dbsession_factory']
    engine = factory.kw['bind']

    # create all the tables in db
    Base.metadata.create_all(engine)

    log.info ("Creating root transaction for the test session")
    with transaction.manager as tx:
        from plantoeducate_data.models import get_tm_session
        session=get_tm_session(factory, transaction.manager)

        brief=DocumentTemplateModel ()
        brief.DocumentDescription='brief'
        brief.DocumentTypeID=1
        brief.DocumentFilePath='brief.ott'
        feedback=DocumentTemplateModel ()
        feedback.DocumentDescription='feedback'
        feedback.DocumentTypeID=2
        feedback.DocumentFilePath='feedback.ott'

        session.add_all([brief, feedback])
        #session.flush()

        yield session

        log.info("Rolling back root transaction")
        transaction.abort()
        session.close()
    


I have two tests that use the fixture, listed below:
def test_delete_document(self, testapp, session):
        doc=session.query(DocumentTemplateModel).first()
        import pdb; pdb.set_trace()
        # delete the document
        res = 
testapp.delete('/documents/templates/{}'.format(doc.DocumentID), status=204)



def test_filter_documents(self, testapp, session):
         res = testapp.get('/documents/templates/1', status=200)

    expectedTemplatesCount = 2
    import pdb; pdb.set_trace()

I think that *pyramid_tm* creates a session from a SQLAlchemy session 
factory for each request and hooks up the current active transaction. 

When the first test is run I can see data in the session, however the 
request session in the view does not see the data. 
When the second test runs there is no data at all in the session that was 
created by the test fixture.

How do I make the data initialised in the test visible to the view being 
tested? Is it possible to perform testing by initialising data in 
SQLAlchemy, making it visible to the request session in the view and then 
rolling back state in preparation for subsequent test? 

Kind Regards


dcs3spp

-- 
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/bf247ef1-1a48-4934-9bce-205d166a5c64%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to