Django: How to create a model dynamically just for

2019-01-16 02:25发布

I have a Django app that requires a settings attribute in the form of:

RELATED_MODELS = ('appname1.modelname1.attribute1',
                  'appname1.modelname2.attribute2', 
                  'appname2.modelname3.attribute3', ...)

Then hooks their post_save signal to update some other fixed model depending on the attributeN defined.

I would like to test this behaviour and tests should work even if this app is the only one in the project (except for its own dependencies, no other wrapper app need to be installed). How can I create and attach/register/activate mock models just for the test database? (or is it possible at all?)

Solutions that allow me to use test fixtures would be great.

11条回答
男人必须洒脱
2楼-- · 2019-01-16 02:33

Quoting from a related answer:

If you want models defined for testing only then you should check out Django ticket #7835 in particular comment #24 part of which is given below:

Apparently you can simply define models directly in your tests.py. Syncdb never imports tests.py, so those models won't get synced to the normal db, but they will get synced to the test database, and can be used in tests.

查看更多
唯我独甜
3楼-- · 2019-01-16 02:34

I chose a slightly different, albeit more coupled, approach to dynamically creating models just for testing.

I keep all my tests in a tests subdirectory that lives in my files app. The models.py file in the tests subdirectory contains my test-only models. The coupled part comes in here, where I need to add the following to my settings.py file:

# check if we are testing right now
TESTING = 'test' in sys.argv

if TESTING:
    # add test packages that have models
    INSTALLED_APPS += ['files.tests',]

I also set db_table in my test model, because otherwise Django would have created the table with the name tests_<model_name>, which may have caused a conflict with other test models in another app. Here's my my test model:

class Recipe(models.Model):

    '''Test-only model to test out thumbnail registration.'''

    dish_image = models.ImageField(upload_to='recipes/')

    class Meta:
        db_table = 'files_tests_recipe'
查看更多
贼婆χ
4楼-- · 2019-01-16 02:38

I've figured out a way for test-only models for django 1.7+.

The basic idea is, make your tests an app, and add your tests to INSTALLED_APPS.

Here's an example:

$ ls common
__init__.py   admin.py      apps.py       fixtures      models.py     pagination.py tests         validators.py views.py

$ ls common/tests
__init__.py        apps.py            models.py          serializers.py     test_filter.py     test_pagination.py test_validators.py views.py

And I have different settings for different purposes(ref: splitting up the settings file), namely:

  • settings/default.py: base settings file
  • settings/production.py: for production
  • settings/development.py: for development
  • settings/testing.py: for testing.

And in settings/testing.py, you can modify INSTALLED_APPS:

settings/testing.py:

from default import *

DEBUG = True

INSTALLED_APPS += ['common', 'common.tests']

And make sure that you have set a proper label for your tests app, namely,

common/tests/apps.py

from django.apps import AppConfig


class CommonTestsConfig(AppConfig):
    name = 'common.tests'
    label = 'common_tests'

common/tests/__init__.py, set up proper AppConfig(ref: Django Applications).

default_app_config = 'common.tests.apps.CommonTestsConfig'

Then, generate db migration by

python manage.py makemigrations --settings=<your_project_name>.settings.testing tests

Finally, you can run your test with param --settings=<your_project_name>.settings.testing.

If you use py.test, you can even drop a pytest.ini file along with django's manage.py.

py.test

[pytest]
DJANGO_SETTINGS_MODULE=kungfu.settings.testing
查看更多
Deceive 欺骗
5楼-- · 2019-01-16 02:41

Here's the pattern that I'm using to do this.

I've written this method that I use on a subclassed version of TestCase. It goes as follows:

@classmethod
def create_models_from_app(cls, app_name):
    """
    Manually create Models (used only for testing) from the specified string app name.
    Models are loaded from the module "<app_name>.models"
    """
    from django.db import connection, DatabaseError
    from django.db.models.loading import load_app

    app = load_app(app_name)
    from django.core.management import sql
    from django.core.management.color import no_style
    sql = sql.sql_create(app, no_style(), connection)
    cursor = connection.cursor()
    for statement in sql:
        try:
            cursor.execute(statement)
        except DatabaseError, excn:
            logger.debug(excn.message)
            pass

Then, I create a special test-specific models.py file in something like myapp/tests/models.py that's not included in INSTALLED_APPS.

In my setUp method, I call create_models_from_app('myapp.tests') and it creates the proper tables.

The only "gotcha" with this approach is that you don't really want to create the models ever time setUp runs, which is why I catch DatabaseError. I guess the call to this method could go at the top of the test file and that would work a little better.

查看更多
看我几分像从前
6楼-- · 2019-01-16 02:43

I shared my solution that I use in my projects. Maybe it helps someone.

pip install django-fake-model

Two simple steps to create fake model:

1) Define model in any file (I usualy define model in test file near a test case)

from django_fake_model import models as f


class MyFakeModel(f.FakeModel):

    name = models.CharField(max_length=100)

2) Add decorator @MyFakeModel.fake_me to your TestCase or to test function.

class MyTest(TestCase):

    @MyFakeModel.fake_me
    def test_create_model(self):
        MyFakeModel.objects.create(name='123')
        model = MyFakeModel.objects.get(name='123')
        self.assertEqual(model.name, '123')

This decorator creates table in your database before each test and remove the table after test.

Also you may create/delete table manually: MyFakeModel.create_table() / MyFakeModel.delete_table()

查看更多
聊天终结者
7楼-- · 2019-01-16 02:44

This solution works only for earlier versions of django (before 1.7). You can check your version easily:

import django
django.VERSION < (1, 7)

Original response:

It's quite strange but form me works very simple pattern:

  1. add tests.py to app which you are going to test,
  2. in this file just define testing models,
  3. below put your testing code (doctest or TestCase definition),

Below I've put some code which defines Article model which is needed only for tests (it exists in someapp/tests.py and I can test it just with: ./manage.py test someapp ):

class Article(models.Model):
    title = models.CharField(max_length=128)
    description = models.TextField()
    document = DocumentTextField(template=lambda i: i.description)

    def __unicode__(self):
        return self.title

__test__ = {"doctest": """
#smuggling model for tests
>>> from .tests import Article

#testing data
>>> by_two = Article.objects.create(title="divisible by two", description="two four six eight")
>>> by_three = Article.objects.create(title="divisible by three", description="three six nine")
>>> by_four = Article.objects.create(title="divisible by four", description="four four eight")

>>> Article.objects.all().search(document='four')
[<Article: divisible by two>, <Article: divisible by four>]
>>> Article.objects.all().search(document='three')
[<Article: divisible by three>]
"""}

Unit tests also working with such model definition.

查看更多
登录 后发表回答