3

In my django application, i haven't use model for database. I have traditional way of doing it, that is by mysql server and client. I have used MySQLdb for database access. Also in my django views, almost all code are written in normal python code, that is i have not used the advantage of django built in view classes and functions.

def showhome(request):

  if request.user.is_anonymous():       
      return HttpResponseRedirect("/")

  db = MySQLdb.connect(host=dbHost,port=dbPort,user=dbUser,passwd=dbPasswd,db=dbName)

  c = db.cursor()                                                                       
  username=request.user                                                                       
  c.execute("Select firstname from USER where USERNAME=%s,(username,))                     
  name=c.fetchone()
  c.close()

  firstname=name[0]                                                                         
  c=db.cursor()
  c.execute("Select vehicle,status from PROPERTY where firstname=%s,(firstname,))
  deviceDetails=c.fetchall()
  c.close()


  paginator = Paginator(deviceDetails, 10)                                                

  page = request.GET.get('page')
  try:
      List = paginator.page(page)   
  except PageNotAnInteger:
      List = paginator.page(1)      
  except EmptyPage:
      List = paginator.page(paginator.num_pages)       

  current=List.number              
  last_page=paginator.num_pages
  page_limit=10          
  n=current%page_limit

  if n==1:                                                                                        
      start=current                                                                       
  else:
      start=(current-n+1) if n!=0 else current-page_limit+1                               
  end=start+page_limit                                                                    
  if end>last_page:                                                                       
      end=last_page+1
  page_range=range(start,end)                                                             


  context={}
  context['Data']=List
  context['page_range']=page_range
  db.close()
  return render(request,"home/show.html",context)

What type of testing i have to do in tests.py file blackbox or whitebox? If it is whitebox which type i have to follow for testing the above function unittest or integration test? I think unittest is enough for the above function, isn't it?

  • This question isn't very clear; blackbox/whitebox tests are statements about your knowledge of the code, not the type of testing to be done. – Kevin McKenzie Jan 15 '18 at 17:04
1

Terminology may be not that transparent and some might say that the database - is a dependency, authentication backend - is a dependency (hence, no testing in isolation that is in the "unit test" definition) and think of this kind of tests as "integration"/"functional"/"system".

On the other hand, Django and tools around it provide ways for you to use a special test database which you can load with fixtures or generate the data for your models using factories (like factory-boy).

And, you can also do both the black box (API testing - by triggering the url route configured for your view and analyzing the response) and the white box testing (which in this case would be your Django view tests).

  • I have done only unit test for this function. Writing unittest will cover the functionality also, am i right?. Here white box testing means unit testing since here we have only one function? – vishnu m c Nov 16 '17 at 8:48
  • @vishnumc terminology should not be the focus here overall. Monitor code coverage and make sure you've covered the corner cases and important/possible code paths in the view. And yes, these tests are white box since you know the code under test and use this knowledge to test it. Thanks. – alecxe Nov 16 '17 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.