Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test DoesNotExist: Contact matching query does not exist

Tags:

python

django

I'm trying to run my first test. The test fails with:

DoesNotExist: Contact matching query does not exist. Lookup parameters were {'mobile': '07000000000'}

I seem to create user contact in the setup function so why is it not available?

Thanks

test.py

class BatchTestCase(TestCase):

    def setup(self):
         user = User.objects.get(username='glynjackson')
         contact = Contact.objects.get(mobile="07000000000", contact_owner=user, group=None)


    def test_get_contact(self):
        contact = Contact.objects.get(mobile='07000000000')
        self.assertEqual(contact.full_name(), 'Got Contact')

full error

ERROR: test_get_contact (sms.tests.test_sms_simulation.BatchTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/user/Documents/workspace/example/sms/tests/test_sms_simulation.py", line 18, in test_get_contact
    contact = Contact.objects.get(mobile='07000000000')
  File "/Users/user/Documents/workspace/example/django-env/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/Users/user/Documents/workspace/example/django-env/lib/python2.7/site-packages/django/db/models/query.py", line 389, in get
    (self.model._meta.object_name, kwargs))
DoesNotExist: Contact matching query does not exist. Lookup parameters were {'mobile': '07000000000'}
like image 516
GrantU Avatar asked Jul 12 '13 10:07

GrantU


2 Answers

You shall use setUp method, not setup. This method is called before running every test.

class BatchTestCase(TestCase):

    def setUp(self):
         # create test objects here

    # ...
like image 81
stalk Avatar answered Oct 23 '22 19:10

stalk


get doesn't create record in the database and tries actually to get record. It founds no such record in the database and raises error DoesNotExist.

You should use something like:

contact = Contact(mobile="07000000000", contact_owner=user, group=None)
contact.save()
like image 6
shalakhin Avatar answered Oct 23 '22 18:10

shalakhin