Pand-Aid / pandaid-api

backend for Pand-Aid pandemic response app
3 stars 3 forks source link

Running `./manage.py test` command to run tests results in error #17

Open bhgrant8 opened 4 years ago

bhgrant8 commented 4 years ago

TEAM NAME: Backend PRIORITY (1-5): P2

P2: This is preventing development

Description of issue

When I attempt to run the ./manage.py test command within the Docker container to test functionality of our API, I receive an error.

Developers as well as our CI/CD Pipeline will need to run tests to be able to ensure code is functional

Error Message/Logs

root@e3067e2141e8:/code# ./manage.py test
nosetests /code/pandaid-rest-api -s --nologcapture --with-coverage --with-progressive --cover-package=pandaid-rest-api --verbosity=1
Creating test database for alias 'default'...

ERROR: users.test.test_serializers
  vi +6   pandaid-rest-api/users/test/test_serializers.py  # <module>
    from ..serializers import CreateUserSerializer
  vi +2   pandaid-rest-api/users/serializers.py  # <module>
    from .models import User
  vi +10  pandaid-rest-api/users/models.py  # <module>
    class User(AbstractUser):
  vi +112 /usr/local/lib/python3.8/site-packages/django/db/models/base.py  # __new__
    raise RuntimeError(
RuntimeError: Model class users.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

ERROR: users.test.test_views
  vi +8   pandaid-rest-api/users/test/test_views.py  # <module>
    from ..models import User
  vi +10  pandaid-rest-api/users/models.py  # <module>
    class User(AbstractUser):
  vi +112 /usr/local/lib/python3.8/site-packages/django/db/models/base.py  # __new__
    raise RuntimeError(
RuntimeError: Model class users.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
Name                                                           Stmts   Miss Branch BrPart  Cover                                                   
------------------------------------------------------------------------------------------------
pandaid-rest-api/__init__.py                                       0      0      0      0   100%
pandaid-rest-api/config/__init__.py                                2      2      0      0     0%
pandaid-rest-api/config/common.py                                 35     35      0      0     0%
pandaid-rest-api/config/local.py                                  12     12      0      0     0%
pandaid-rest-api/config/production.py                             18     18      0      0     0%
pandaid-rest-api/users/__init__.py                                 0      0      0      0   100%
pandaid-rest-api/users/admin.py                                    6      6      2      0     0%
pandaid-rest-api/users/migrations/0001_initial.py                 10     10      0      0     0%
pandaid-rest-api/users/migrations/0002_auto_20171227_2246.py       4      4      0      0     0%
pandaid-rest-api/users/migrations/__init__.py                      0      0      0      0   100%
pandaid-rest-api/users/models.py                                  15      5      2      0    59%
------------------------------------------------------------------------------------------------
TOTAL                                                            102     92      4      0     9%

2 tests, 0 failures, 2 errors in 0.1s
Destroying test database for alias 'default'...

Reproduction Steps

  1. I clone repo locally, git clone https://github.com/Pand-Aid/pandaid-api.git
  2. I ensure I am on master branch using git branch
  3. I ensure I am in sync with remote branch using git pull master (should be if you did new clone of repo as described in step 1, but good to confirm)
  4. I start the containers using: docker-compose up
  5. Once running (confirmed by accessing browsable API in my web browser), I ssh into the container using the docker-compose command: docker-compose exec web bash
  6. I then run the basic command to run django tests: ./manage.py test

Expected: Tests run without issue

Actual: I receive an error when running tests

Code Snippets

I run: ./manage.py test which runs this in the backend: nosetests /code/pandaid-rest-api -s --nologcapture --with-coverage --with-progressive --cover-package=pandaid-rest-api --verbosity=1

As setup here:

https://github.com/Pand-Aid/pandaid-api/blob/655aef3f9adf9b3619ef46240117d21ae988fd55/pandaid-rest-api/config/local.py#L9-L20

Screenshots/GIF

n/a - behavior described through logs and recreation

Priority/Impact

We do want to be able to run tests, so should be fixed within 1-2 days

bhgrant8 commented 4 years ago

The error appears to be what is described in this blog post:

https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a

Mainly being the author's description here:

Importing the model classes in the init.py file of the models package confuses the test runner when reading this file as for some reason it can’t identify to which app these models belong.

We can test a few of the theories in the guide, but removing the __init__.py from the models is an option, so it is treated as a simple module vs. a package.