Re: [sqlalchemy] How to test functions that use sqlalchemy ORM?

2016-02-29 Thread Mike Bayer

sure see that at http://www.sqlalchemy.org/library.html#buildingtheapp.



On 02/29/2016 04:01 AM, Abhijeet Rastogi wrote:

Hi Mike,

Thanks for the reply. Do you have any slides to share from that talk?

Cheers!

On Monday, February 22, 2016 at 9:54:38 PM UTC+5:30, Mike Bayer wrote:

I created a pretty comprehensive example of two ways to do this as part
of a talk I did at Pycon some years ago.   The example case is
https://bitbucket.org/zzzeek/pycon2014_atmcraft
 and you can see a
contrast of the "mock" approach and the "run tests in a transaction"
approach at:


https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/f50cbe745a197ea7db83569283b703c418481222/atmcraft/tests/test_views_mockdb.py?at=master=file-view-default





https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/f50cbe745a197ea7db83569283b703c418481222/atmcraft/tests/test_views_transactional.py?at=master=file-view-default




The transactional version is based on the techniques at

http://docs.sqlalchemy.org/en/rel_1_0/orm/session_transaction.html#joining-a-session-into-an-external-transaction-such-as-for-test-suites

.



There are pluses and minuses to both approaches.   Real world I think
both are usually used at the same time, which has been the case for me.





On 02/22/2016 09:46 AM, Abhijeet Rastogi wrote:
 > Hi Folks,
 >
 > I've functions that do a subset of tasks on a sqlalchemy object
for a
 > bigger function. For ex,
 >
 > def delete_certificate_store(CS):
 >
 >  CS.is_archived = True
 >  # Also delete the associated CARequest object
 >  for DR in CS.CA_request:
 >  db.session.delete(DR)
 >  # Delete the Certificate Object only if this it's the only
order
 > for that Certificate
 >  if len(CS.order.certificate.get_all_unarchived_orders()) ==
1 and
 > CS in CS.order.certificate.get_all_unarchived_certificate_stores():
 >  CS.order.certificate.is_archived = True
 >
 > Now, this function modifies a database. How do we test functions
like
 > these? If I use mocks for DB related stuff, I'm not really
testing it.
 >
 > As a human, I test these functions by executing them and
verifying if
 > the intended changes happened in the DB. How's that done by testing
 > frameworks like pytest? Sorry, if this is a stupid question, I'm
new to
 > testing.
 >
 > Thanks

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
.
To post to this group, send email to sqlalchemy@googlegroups.com
.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] How to test functions that use sqlalchemy ORM?

2016-02-22 Thread Mike Bayer
I created a pretty comprehensive example of two ways to do this as part 
of a talk I did at Pycon some years ago.   The example case is 
https://bitbucket.org/zzzeek/pycon2014_atmcraft and you can see a 
contrast of the "mock" approach and the "run tests in a transaction" 
approach at:


https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/f50cbe745a197ea7db83569283b703c418481222/atmcraft/tests/test_views_mockdb.py?at=master=file-view-default

https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/f50cbe745a197ea7db83569283b703c418481222/atmcraft/tests/test_views_transactional.py?at=master=file-view-default

The transactional version is based on the techniques at 
http://docs.sqlalchemy.org/en/rel_1_0/orm/session_transaction.html#joining-a-session-into-an-external-transaction-such-as-for-test-suites.



There are pluses and minuses to both approaches.   Real world I think 
both are usually used at the same time, which has been the case for me.






On 02/22/2016 09:46 AM, Abhijeet Rastogi wrote:

Hi Folks,

I've functions that do a subset of tasks on a sqlalchemy object for a
bigger function. For ex,

def delete_certificate_store(CS):

 CS.is_archived = True
 # Also delete the associated CARequest object
 for DR in CS.CA_request:
 db.session.delete(DR)
 # Delete the Certificate Object only if this it's the only order
for that Certificate
 if len(CS.order.certificate.get_all_unarchived_orders()) == 1 and
CS in CS.order.certificate.get_all_unarchived_certificate_stores():
 CS.order.certificate.is_archived = True

Now, this function modifies a database. How do we test functions like
these? If I use mocks for DB related stuff, I'm not really testing it.

As a human, I test these functions by executing them and verifying if
the intended changes happened in the DB. How's that done by testing
frameworks like pytest? Sorry, if this is a stupid question, I'm new to
testing.

Thanks


--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
.
To post to this group, send email to sqlalchemy@googlegroups.com
.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] How to test functions that use sqlalchemy ORM?

2016-02-22 Thread Abhijeet Rastogi
Hi Folks,

I've functions that do a subset of tasks on a sqlalchemy object for a 
bigger function. For ex,

def delete_certificate_store(CS):

CS.is_archived = True
# Also delete the associated CARequest object
for DR in CS.CA_request:
db.session.delete(DR)
# Delete the Certificate Object only if this it's the only order for 
that Certificate
if len(CS.order.certificate.get_all_unarchived_orders()) == 1 and CS in 
CS.order.certificate.get_all_unarchived_certificate_stores():
CS.order.certificate.is_archived = True

Now, this function modifies a database. How do we test functions like 
these? If I use mocks for DB related stuff, I'm not really testing it. 

As a human, I test these functions by executing them and verifying if the 
intended changes happened in the DB. How's that done by testing frameworks 
like pytest? Sorry, if this is a stupid question, I'm new to testing.

Thanks


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.