Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability for tests to use mock models. #3

Closed
tomchristie opened this issue May 12, 2011 · 3 comments
Closed

Add ability for tests to use mock models. #3

tomchristie opened this issue May 12, 2011 · 3 comments

Comments

@tomchristie
Copy link
Member

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.

@markotibold
Copy link
Contributor

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.

@ghost ghost assigned markotibold Dec 13, 2011
@markotibold
Copy link
Contributor

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?

@tomchristie
Copy link
Member Author

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

tomchristie pushed a commit that referenced this issue Oct 3, 2013
tomchristie pushed a commit that referenced this issue Dec 5, 2014
treyhunner pushed a commit to treyhunner/django-rest-framework that referenced this issue Sep 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants