django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Add ability for tests to use mock models.

Open lovelydinosaur opened this issue 15 years ago • 3 comments

At the moment none of the tests are able to create some mock Models and test stuff against them, because the tests don't have a models.py. It be good if we could just do something like add 'models.py' into the tests directory, and add 'djangorestframework.tests' as an INSTALLED_APP in the runtests.py script, or something like that. But needs someone to find out if that'd actually work, how to add them in the runtests.py script, and make sure they're not installed if you just do a regular syncdb on djangorestframework. For example, would be nice to see some tests to check serializing of nested models, but we wouldn't want those models to actually be installed in djangorestframework by default.

lovelydinosaur avatar May 12 '11 15:05 lovelydinosaur

Got this approach working on code for a work-project.

Just going to paste some stuff here as a remeninder.

use the following code when you don't want to do manage.py test appname:

from django.test.utils import setup_test_environment
setup_test_environment()

from django.db import connection
connection.creation.create_test_db()

and in the test:

class MyTest(TestCase):

  fixtures = ['test_fixtures'] # store this file inside the mockapp

  def setUp...

  def tearDown....

  *tests with db magic goes here*....

And all we need now is a very basic settings module with a sqlite3 db.

That's about it.

markotibold avatar Dec 13 '11 23:12 markotibold

It looks like this issue is actually already solved in a couple of tests, eg:

https://github.com/ekohl/django-rest-framework/blob/pep8/djangorestframework/tests/modelviews.py https://github.com/ekohl/django-rest-framework/blob/pep8/djangorestframework/tests/serializer.py

@tomchristie or do you beleaeve there's more work needed done on this issue?

markotibold avatar Dec 14 '11 12:12 markotibold

Ah yeah. Looks done. djangorestframework/tests/models.py exists, and does what this ticket was originally requesting. Thanks!

lovelydinosaur avatar Dec 14 '11 13:12 lovelydinosaur