python - Testing django methods -


developing simple chat system via django - i've implemented methods sending/receiving messages.

for example ( isn't implementation example 1 chat application ) :

def post(request):    time.sleep(2)   if not request.is_ajax():     httpresponse (" not ajax request ")   if request.method == 'post':     if request.post['message']:         message = request.post['message']         to_user = request.post['to_user']         chatmessage.objects.create(sender = request.user, receiver = user.objects.get(username = to_user), message = message,  session = session.objects.get(session_key = request.session.session_key))  return httpresponse (" not post request ") 

now have method written - need test see if message added. @ point have not written javascript i.e. intervals refresh , wait messages ect. should go straight writing js or test method first , see if works correctly write js it? sounds idiotic question i'm finding hard understand how i'd go testing method...

in opinion doesn't matter. find many developers test-before, , many test-after. implement in whatever order prefer. when in haste easier write working prototype without tests first (especially if interfaces aren't finalized , still change lot during development, in case don't waste time adjusting lot of tests every time have change interfaces.). don't lazy write tests @ least when have finalized interfaces.

what matters have defined interfaces. backend perspective have interface implementation (the view in case) , 1 or more users interface: both frontend , test users of backend implementation there more. after having interface doesn't matter 1 implement first. mocking 1 side of interface can implement other side without having original version of mocked side. example mocking/faking server responses javascript code (with pre-baked possibly constant data) can write frontend without backend code. can write backend first, or tests backend... decide.

in teams have specialized developers (frontend/backend) can agree on interface , both on frontend , backend side can "mock" (fake) other side of interface: frontend guys write code emulates server responses fake data, , backend guys write tests emulate client fake requests. way frontend , backend team doesn't have wait each other finish code , both frontend , backend testable alone. of course later recommended add end-to-end (e2e) testing tests whole stack connected together.

again, matters having defined interfaces , not code written around interfaces. in crappy systems problem have code without interfaces... if system architecturally built , interfaces defined quite lot of crappy code written around interfaces can manageable.

in case of django views have defined interface develop backend first along tests. in case django test super simple: create django test client (https://docs.djangoproject.com/en/1.8/topics/testing/tools/#test-client), post fake requests simulate client , check whether db contains expected objects result.

some additional advices:

  • decorate view @require_post
  • i think shouldn't use request.is_ajax() deny responding client. request.is_ajax() used find out kind of response needed client. if post request sent form of html page want generate html page response. if request sent using ajax want respond processable data (json, xml, etc...) instead of html.

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -