Configure Python to Complex Django Project - python

I need to configure pytest in my django application. But the structure of the directories is different of what I'm used to.
.
├── apps
│   ├── __pycache__
│   ├── admcore
│   ├── ...
│   ├── cauth
│   ├── webservice
│   └── webshell
├── doc
│   └── ...
├── lib
│   └── ...
├── ...
└── project
├── __init__.py
├── globalsettings.py
├── local
│   ├── __init__.py
│   ├── app.uwsgi
│   ├── manage.py
│   ├── settings.py
│   └── wsgi.py
├── mailconf.py
└── urls.py
I created pytest.ini
[pytest]
DJANGO_SETTINGS_MODULE=sgp.local.test_settings
addopts = --nomigrations --cov=. --cov-report=html
and test_settings.py files
#!/usr/bin/env python
import os
import sys
from project import globalsettings
SECRET_KEY = "12341234AFDQFDQSDFAZR123D"
ROOT_PATH = '/usr/local/sgp'
BASEURL_ROOT = ''
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
}
}
And I put the two files in project directory.
.
├── apps
│   ├── __pycache__
│   ├── admcore
│   ├── ...
│   ├── cauth
│   ├── webservice
│   └── webshell
├── doc
│   └── ...
├── lib
│   └── ...
├── ...
└── project
├── __init__.py
├── globalsettings.py
├── local
│   ├── __init__.py
│   ├── app.uwsgi
│   ├── manage.py
│   ├── pytest.ini
│   ├── settings.py
│   ├── test_settings.py
│   └── wsgi.py
├── mailconf.py
└── urls.py
But it doesn't work as I expected. I have one test in webservice app, but when I run pytest command I got:
platform darwin -- Python 3.7.4, pytest-5.2.0, py-1.8.0, pluggy-0.13.0
Django settings: project.local.test_settings (from environment variable)
rootdir: /project/local, inifile: pytest.ini
plugins: django-3.5.1, cov-2.7.1
collected 0 items
---------- coverage: platform darwin, python 3.7.4-final-0 -----------
Coverage HTML written to dir htmlcov
=========================================================================================================== no tests ran in 0.15s ============================================================================================================
I have no ideia how to configure pytest to this project.

Related

How to add authentication to a specific page in a dash application

I have a dash app with multiple pages.
.
├── adduser.py
├── app.py
├── apps/
│   ├── __init__.py
│   ├── exome.py
│   ├── familial.py
│   ├── oncomine.py
│   ├── prenatal.py
│   ├── rapidexome.py
│   └── targeted.py
├── auth.py
├── data/
├── dataprocessor.py
├── index.py
├── layouts.py
└── static
└── fileiconn.png
I just want to prompt the use for an admin password when they try to access the /apps/exome route. is there a way to do that?

Flask-ImportError: ModuleNotFoundError: No module named 'mycode'

ImportError while loading conftest '/home/rohit/flask/src/tests/conftest.py'.
conftest.py:6: in <module>
from mycode import create_app
E ModuleNotFoundError: No module named 'mycode'
when running pytest form the src folder above mycode folder, it gives ModuleNotFoundError
The directory structure is as follow
├── mycode
│   ├── auth
│   ├── auth.py
│   ├── db_insert.py
│   ├── db.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── auth.cpython-37.pyc
│   │   ├── db.cpython-37.pyc
│   │   ├── db_insert.cpython-37.pyc
│   │   └── __init__.cpython-37.pyc
│   ├── README.md
│   ├── schema.sql
│   └── templates
│   ├── auth
│   │   ├── login.html
│   │   └── register.html
│   └── base.html
└── tests
├── conftest.py
├── data.sql
├── __pycache__
│   └── conftest.cpython-37-pytest-5.4.1.pyc
└── test_factory.py
Your __init__.py file needs to go in the folder named mycode. If the above is already true then try the following.
Rename __init__.py to mycode.py
You can see here for more solutions that may solve your problem.

What's the purpose of package.egg-info folder?

I'm developing a python package foo. My project structure looks like this:
.
├── foo
│   ├── foo
│   │   ├── bar.py
│   │   ├── foo.py
│   │   ├── __init__.py
│   ├── README.md
│   └── setup.py
├── footest
│   ├── test.py
test.py only has 1 line: import foo
In order for test.py to be able to import the package foo I install it with the command pip3 install -e foo.
Now a new folder called foo.egg-info is created under foo/
.
├── foo
│   ├── foo
│   │   ├── bar.py
│   │   ├── foo.py
│   │   ├── __init__.py
│   ├── foo.egg-info
│   │   ├── dependency_links.txt
│   │   ├── PKG-INFO.txt
│   │   ├── requires.txt
│   │   ├── SOURCES.txt
│   │   ├── top_level.txt
│   ├── README.md
│   └── setup.py
├── footest
│   ├── test.py
What's the purpose of this folder? I tried deleting it and test.py still ran properly. Is is just leftover garbage, similar to the .o files when compiling C projects? If so, is there a way to automatically remove it?
The package.egg-info saves meta data about your installed package like version.
It is used when you for example uninstall it, or "pip list" to see what is installed.
you can open it and take a look.

Why is pytest not running my tests and skipping them

I am using pytest-django to test my django application. My directory structure is like this :
.
├── Dockerfile
├── Pipfile
├── Pipfile.lock
├── README.md
├── app
│   ├── __pycache__
│   │   └── test_example.cpython-37-pytest-5.2.1.pyc
│   ├── app
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-37.pyc
│   │   │   ├── settings.cpython-37.pyc
│   │   │   └── urls.cpython-37.pyc
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── core
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-37.pyc
│   │   │   ├── admin.cpython-37.pyc
│   │   │   └── models.cpython-37.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │   ├── 0001_initial.cpython-37.pyc
│   │   │   └── __init__.cpython-37.pyc
│   │   ├── models.py
│   │   └── tests
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-37.pyc
│   │   │   ├── test_admin.cpython-37-pytest-5.2.1.pyc
│   │   │   └── test_models.cpython-37-pytest-5.2.1.pyc
│   │   ├── test_admin.py
│   │   └── test_models.py
│   ├── db.sqlite3
│   ├── manage.py
│   ├── pytest.ini
│   └── test_example.py
└── docker-compose.yml
The file : test_models.py is like below :
from django.contrib.auth import get_user_model
import pytest
#pytest.mark.django_db
class TestUserCreation:
def test_create_user_with_email_successfull(self):
"""
Test creating a new user with an email is successfull
"""
email = "test#gmail.com"
password = "testpass123"
user = get_user_model().objects.create_user(
email=email,
password=password
)
assert user.email == email
assert user.check_password(password)
def test_new_user_email_normalized(self):
"""Test the email for a new user is normalized"""
email = "test#GMAIL.COM"
user = get_user_model().objects.create_user(
email=email,
password="Testpass123"
)
assert user.email == email.lower()
def test_new_user_invalid_email(self):
"""Test creating user with no email raises error"""
with pytest.raises(ValueError):
get_user_model().objects.create_user(
email=None,
password="testpass123"
)
def test_create_new_super_user(self):
"""Test creating a super user"""
user = get_user_model().objects.create_superuser(
email='test#gmail.com',
password='testpass123',
)
assert user.is_superuser
assert user.is_staff
and the file : test_admin.py is like so :
import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
def test_something_simple():
names = ["Subhayan", "Shaayan"]
assert len(names) == 2
when i run pytest from the root of my project:
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api:master$ pytest -v
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /Users/subhayanbhattacharya/.local/share/virtualenvs/recipe-app-api-xtbpUqq8/bin/python3.7
cachedir: .pytest_cache
rootdir: /Users/subhayanbhattacharya/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api
plugins: django-3.5.1
collected 6 items
app/core/tests/test_models.py::TestUserCreation::test_create_user_with_email_successfull SKIPPED [ 16%]
app/core/tests/test_models.py::TestUserCreation::test_new_user_email_normalized SKIPPED [ 33%]
app/core/tests/test_models.py::TestUserCreation::test_new_user_invalid_email SKIPPED [ 50%]
app/core/tests/test_models.py::TestUserCreation::test_create_new_super_user SKIPPED [ 66%]
app/test_example.py::test_example PASSED [ 83%]
app/core/tests/test_admin.py::test_something_simple PASSED [100%]
========================================================================== 2 passed, 4 skipped in 0.42s ==========================================================================
But very strangely when i go inside the app folder:
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api:master$ cd app
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api/app:master$ pytest -v
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /Users/subhayanbhattacharya/.local/share/virtualenvs/recipe-app-api-xtbpUqq8/bin/python3.7
cachedir: .pytest_cache
Django settings: app.settings (from ini file)
rootdir: /Users/subhayanbhattacharya/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api/app, inifile: pytest.ini
plugins: django-3.5.1
collected 6 items
core/tests/test_models.py::TestUserCreation::test_create_user_with_email_successfull PASSED [ 16%]
core/tests/test_models.py::TestUserCreation::test_new_user_email_normalized PASSED [ 33%]
core/tests/test_models.py::TestUserCreation::test_new_user_invalid_email PASSED [ 50%]
core/tests/test_models.py::TestUserCreation::test_create_new_super_user PASSED [ 66%]
test_example.py::test_example PASSED [ 83%]
core/tests/test_admin.py::test_something_simple PASSED [100%]
=============================================================================== 6 passed in 0.81s ================================================================================
I am not sure what is wrong . Can someone please throw some more light on this . The pytest.ini file is as follows
DJANGO_SETTINGS_MODULE = app.settings(recipe-app-api)

PyCharm launching Django console for Flask app

For whatever reason, PyCharm thinks my Flask project is a Django project, and thus launches a Django console instead of a Python console in the toolbar:
The project interpreter is configured properly:
Here is my project hierarchy, if it's relevant:
.
├── LICENSE
├── README.md
├── app
│   ├── __init__.py
│   ├── email.py
│   ├── main
│   │   ├── __init__.py
│   │   ├── errors.py
│   │   ├── forms.py
│   │   └── views.py
│   ├── models.py
│   ├── static
│   │   └── favicon.ico
│   └── templates
│   ├── 404.html
│   ├── 500.html
│   ├── base.html
│   ├── index.html
│   └── mail
│   ├── new_user.html
│   └── new_user.txt
├── config.py
├── data-dev.sqlite
├── data-test.sqlite
├── manage.py
├── migrations
│   ├── README
│   ├── alembic.ini
│   ├── env.py
│   ├── script.py.mako
│   └── versions
│   ├── 38c4e85512a9_initial_migration.py
├── requirements.txt
└── tests
├── __init__.py
└── test_basics.py
Just because there is a module called manage.py doesn't mean I'm working on a Django project!
How can I fix this?
You can disable Django support for your project here:
Settings > Language & Frameworks > Django

Categories

Resources