TypeError while running Django tests
Question: im new at Django or python, but im currently working in a project with both.
Right now im trying to get my tests to work. I wrote these simple tests about
3 months ago and im 100% sure they worked back then. Also, when I run the
server and try different searches manually I get the expected results, so I
know the view is at least correct (I know its horrible and slow, I will work
on fixing that). I have searched for this error but the only related thing I
found was that Ubuntu was my problem, but I have tried both on Ubuntu and
Windows 7. I have no idea what happened between then and now but they give me
the following error:

EDIT:I have no idea why all my indentation is being ignored :| oh well, after
some suggestions I changed a couple of things and now i get a failure like
this:

    
    
    enrique@enrique-XPS-L521X:~/Documents/Reeduq$ 
    python manage.py test Search
    Creating test database for alias 'default'...
    FF
    ======================================================================
    FAIL: test_private_courses_search (Search.tests.SearchTests)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/enrique/Documents/Reeduq/Search/tests.py", line 18, in test_private_courses_search
      self.assertEqual(response.context['found_entries'],[])
      AssertionError: [] != []
    ======================================================================
    FAIL: test_public_course_search (Search.tests.SearchTests)
    ----------------------------------------------------------------------
    Traceback (most recent call last): 
    File "/home/enrique/Documents/Reeduq/Search/tests.py", line 29, in test_public_course_search   
       self.assertEqual(response.context['found_entries'],['<Course: test>'])
       AssertionError: [<Course: test>] != ['<Course: test>']
    ----------------------------------------------------------------------
    Ran 2 tests in 0.018s
    
    FAILED (failures=2)
    Destroying test database for alias 'default'...
    

I read that this means that i dont have a `__unicode__` function or something
like that, but my Course model and User model have one each, so Im not sure
what to make of it then

This is the test code:

    
    
    from django.test import TestCase
    from django.shortcuts import render, get_object_or_404, redirect, render_to_response, redirect
    from django.core.urlresolvers import reverse
    from Search.views import search
    from Course.models import *
    from Reeduq_Home.models import *
    class SearchTests(TestCase):    
        def test_private_courses_search(self):
            """     
            a search should not return private courses      
            """     
            new_user=EndUser.objects.create(username="test", first_name="test", last_name="test", email="t@t.com", password="test", account_type="E")         
            Course.objects.create(name="test", instructor=new_user, description="test", tags="test", start_date="2014-3-9", end_date="2014-3-10", public=False)       
            response=self.client.get(reverse('Search:search', args=("test",)))
            self.assertEqual(response.status_code, 200)
            self.assertQuerysetEqual(response.context['found_entries'],[])  
    
        def test_public_course_search(self):        
            """     
            a search should return public courses       
            """
            new_user=EndUser.objects.create(username="test", first_name="test", last_name="test", email="t@t.com", password="test", account_type="E")
            Course.objects.create(name="test", instructor=new_user, description="test", tags="test, wat, wait", start_date="2014-3-9", end_date="2014-3-10", public=True)
            response=self.client.get(reverse('Search:search', args=("wat",)))
            self.assertEqual(response.status_code, 200)
            self.assertQuerysetEqual(response.context['found_entries'],['<Course: test>'])
    

This is the view code:

    
    
    def search(request, query):
       query=query.replace('_', ' ')    
       found_entries = []    
       objects = Course.objects.all()    
       for p in objects:        
           a=[x.strip() for x in p.tags.split(',')] 
           for i in a:      
               if i == query:           
                  if p.public:              
                       found_entries.append(p.id)    
       results = Course.objects.all().filter(pk__in=found_entries)    
    
        return render_to_response('search.html',
                { 'query': query, 'found_entries': results,},) 
    

Thank you for your help.


Answer: Not sure where you're getting the unicode comment.
`response.context['found_entries']` is, behind the scenes, doing
`response.context.__get__item('found_entries')`, except response.context is
None. As @AlexShkop points out, it sounds like your response isn't what you
expect, probably because you're actually getting a 302 redirect (or 401/403).
You could try @alecxe's suggestion, or use the built in client.login
capability (after creating a dummy user)

