No fixtures found when testing an app on Django - django

I have finally decided to create unit test for all my code. That is I'm trying to create fixtures based on my models to test my function against them.
I have create a json fixture using dumpdata command and placed it under fixtures directory on my app. The following is the code on my test:
import unittest
from mysite.myapp.models import Post
class RatingTestCase(unittest.TestCase):
fixtures = [
'posts.json',
]
def test_me(self):
p = Post.objects.all()
self.assertTrue(p)
I run my test using the following command on my Arch Linux machine:
python2.7 manage.py test myapp
It creates a sqlite database and installs all the tables and indeces, however at the end it says no fixtures found and it says my test failed since it didn't find any data.
I'm running the latest revision of development version of Django and noticed that based on the documentation I am supposed to import unittest using:
from django.utils import unittest
However, when I do this, it complains that unittest cannot be imported. That's why I import
unittest directly from my python path which worked.
I've tried to mock django model objects before, but I think it's better to test Django apps using fixtures than using mock libraries. Any idea how to load the fixtures?
Thanks in advance.
EDIT: If I change my fixture name to initial_data.json, it will load it every time I run my test. However, I still need to have multiple fixture names for running different tests.
EDIT: I got it working by importing TestCase from the following:
from django.test import TestCase

There are a couple things that could help:
You can run the test with --verbosity=2 to see which directories django looks in for fixtures.
django TestCase is what django recommends to use, opposed to unittest.
Where is your fixture directory/file located? If it's not in your app you have to specify a fixture directory in your settings file.

Have you checked if the path where the fixture is present is available in:
settings.FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)

I had the name problem. The code below would not work even though I had the file in my fixture directory under the app folder.
fixtures = ['initial_data2.json']
I then changed the import statement to below an it just worked.
from django.test import TestCase

I had the same problem, and the reason it didnt work for me was that I had no extension on my initial-data-file.
I did:
manage.py dumpdata > initial_data
manage.py loaddata initial_data
Which doesnt work, since there's no extension on initial_data, however, this does work:
mv initial_data initial_data.json
manage.py loaddata --verbosity=2 initial_data.json

Just to confirm the import statement for TestCase that breaks finding any fixtures
but works in all other ways, so just throws no errors since it attempts no fixture loading is
from django.utils.unittest import TestCase
... DONT USE IT ... as suggested use
from django.test import TestCase

Related

Unable to import models from the local python file

I have installed flask and flask-sqlalchemy in my base environment of the conda. I defined the models in separate file (model.py), when I am trying to import it in my app.py file it's throwing the following error
When you write from model import User, you cause python to import from app import db, i.e. import app into app, which is not allowed in python
Please, take a look at this answer, I wrote it pretty straightforward how to solve this problem with flask-sqlalchemy:
https://stackoverflow.com/a/62991785/10468419

Can't use django management commands because of Import Errors when haystack tries to import multiligualmodel

I'm using django-haystack for searching on my site.
I'm also using django multilingual model for I18n.
I import MultilingualModel in search_indexes.py
I ca run all manangement commands as long as I don't have haystack in the INSTALLED_APPS.
When haystack is in the INSTALLED_APPS and try to run syncdb or migrate (and several other management commands) I'm always getting:
django.core.exceptions.ImproperlyConfigured: ImportError haystack: cannot import name MultilingualModel
This is likely related to the hacks done in haystack.autodiscover(). This behavior is documented here: http://docs.haystacksearch.org/dev/debugging.html#import-errors-on-start-up-mentioning-handle-registrations There is a long discussion in this ticket: https://github.com/toastdriven/django-haystack/issues/84
The long and short if it is that moving haystack.autodiscover() into your urls.py can sometimes resolve this issue. Setting HAYSTACK_ENABLE_REGISTRATIONS = False when running syncdb or migrate has resolved this for me using this snippet in my settings.py:
# FIXME: This is a complete hack to get around circular imports in
# django-haystack and other apps such as django-endless-pagination
SKIP_COMMANDS = ['syncdb', 'migrate', 'schemamigration', 'datamigration']
if any([command in sys.argv for command in SKIP_COMMANDS]):
HAYSTACK_ENABLE_REGISTRATIONS = False
search_indexes.py doesn't get processed unless haystack is in INSTALLED_APPS. The problem is with the import of MultilingualModel in general. Either it's not truly installed in your environment (attempt to import it from a vanilla python shell), or you have the import wrong (it's actually in another module, for example).
Once you can successfully import MultilingualModel from a python shell, you won't have any problems.

Configuration error in django

I am using django with mod_python (I know it is deprecated -- I am just doing this for an exercise), and am getting the following error --
"Could not import project.views. Error was: No module named app.models"
I am getting the 'No module named app.models" in several other places as well. My syncdb is working fine, and if I go into the manage.py shell I can import the models fine. Why is this occurring and what do I need to change? Thank you.
You should use absolute imports everywhere. If your project is structured like so:
/project/settings.py
/project/app/models.py
/project/app/views.py
In INSTALLED_APPS you would use project.app. In app you'd import your models into views: import project.app.models, etc. Alternately you can try adjusting your PYTHONPATH so your imports work. When you run ./manage.py you are in your project folder, and Python automatically adds it to the PYTHONPATH. This doesn't happen automatically in most deployment scenarios (mod_python or other wise).

Import all modules in django

In django is there way to import all modules
ex: from project.models import *
ex: from project1.models import *
Can this be done with one statement
If you just want to do this when testing things in the shell, look into the shell_plus command provided by the django-extensions project.
This is a really neat extension, which starts a shell and automatically loads all the models in your project when you do ./manage.py shell_plus from the command line.

How do I run tests for all my Django apps only?

Right now, if I want to run tests from all my apps, I go:
python manage.py test app1 app2 app3
If I run:
python manage.py test
The test of all apps in INSTALLED_APPS are run, including the django ones. Is there a simple command to run the tests of all the apps that I have created?
Sadly there is no such command. Django has no way of telling which apps are "yours" versus which are someone else's.
What I would suggest is writing a new management command, call it mytest. Then create a new setting MY_INSTALLED_APPS. The mytest command will just run the test for every app in MY_INSTALLED_APPS. You'll want the mytest command to subclass django.core.management.base.AppCommand. django.core.management.call_command will also be helpful.
The only problem with this method is that you will have to constantly maintain the MY_INSTALLED_APPS setting to make sure it is correct.
You could create an management/commands/testmyapps.py for one of your app which has:
from django.core.management.base import BaseCommand, CommandError
import django.core.management.commands.test
from django.core import management
from django.conf import settings
class Command(django.core.management.commands.test.Command):
args = ''
help = 'Test all of MY_INSTALLED_APPS'
def handle(self, *args, **options):
super(Command, self).handle(*(settings.MY_INSTALLED_APPS + args), **options)
This works better in Django 1.6+: when you run python manage.py test, only your tests will run (assuming you have the default settings for TEST_RUNNER)
Use nose django test runner, it takes care of this.
Reference:
Django nose to run only project tests