Skip to content

Commit

Permalink
starting with pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Jan 9, 2025
1 parent 1df26fb commit 7c1580f
Show file tree
Hide file tree
Showing 28 changed files with 447 additions and 140 deletions.
4 changes: 3 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[run]
branch = True
source = filer
source =
finder
demoapp/unittests
omit =
migrations/*
tests/*
Expand Down
21 changes: 0 additions & 21 deletions .github/workflows/frontend.yml

This file was deleted.

44 changes: 0 additions & 44 deletions .github/workflows/test.yml

This file was deleted.

51 changes: 51 additions & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Test django-filer

on:
push:
branches:
- finder
paths-ignore:
- '**.md'
- '**.rst'
- '/docs/**'
pull_request:
branches:
- develop
paths-ignore:
- '**.md'
- '**.rst'
- '/docs/**'

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12"]
django-version: ["5.2.*"]
node-version: ["18.x"]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
npm install --include=dev
python -m pip install --upgrade pip
python -m pip install https://github.com/django/django/archive/refs/heads/main.zip
python -m pip install django-cte django-entangled ffmpeg-python pillow reportlab svglib
python -m pip install beautifulsoup4 coverage Faker lxml pytest pytest-django pytest-cov
- name: Build Client
run: |
npm run compilescss
npm run esbuild
- name: Test with pytest
run: |
python -m pytest -v demoapp/unittests
1 change: 0 additions & 1 deletion client/scss/finder-admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,3 @@ ul.messagelist {
}

@import 'node_modules/react-image-crop/src/ReactCrop.scss';
@import 'node_modules/react-h5-audio-player/src/styles.scss';
Empty file added demoapp/e2etests/__init__.py
Empty file.
109 changes: 109 additions & 0 deletions demoapp/e2etests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
import pytest

from playwright.sync_api import sync_playwright

from django.conf import settings
from django.core.management import call_command
from django.urls import reverse

from finder.models.realm import RealmModel

from ..utils import create_random_image

os.environ.setdefault('DJANGO_ALLOW_ASYNC_UNSAFE', 'true')


@pytest.fixture(autouse=True, scope='session')
def create_assets():
os.makedirs(settings.BASE_DIR / 'workdir/assets', exist_ok=True)
for counter in range(10):
image = create_random_image()
image.save(settings.BASE_DIR / 'workdir/assets' / f'image_{counter:01d}.png')


@pytest.fixture(scope='session')
def django_db_setup(django_db_blocker):
database_file = settings.BASE_DIR / 'workdir/test_db.sqlite3'
settings.DATABASES['default']['NAME'] = database_file
with django_db_blocker.unblock():
call_command('migrate', verbosity=0)
yield
os.remove(database_file)


@pytest.fixture
def realm(admin_client):
if realm := RealmModel.objects.first():
return realm
response = admin_client.get(reverse('admin:finder_foldermodel_changelist'))
assert response.status_code == 302
realm = RealmModel.objects.first()
assert realm is not None
redirected = reverse('admin:finder_inodemodel_change', kwargs={'inode_id': realm.root_folder.id})
assert response.url == redirected
assert realm.root_folder.is_folder is True
assert realm.root_folder.is_trash is False
assert realm.root_folder.owner == response.wsgi_request.user
assert realm.root_folder.name == '__root__'
assert realm.root_folder.parent is None
assert realm.root_folder.is_root
assert realm.trash_folders.count() == 0
return realm


class Connector:
def __init__(self, live_server):
print(f"\nStarting end-to-end test server at {live_server}\n")
self.live_server = live_server

def __enter__(self):
def print_args(msg):
if msg.type in ['info', 'debug']:
return
for arg in msg.args:
print(arg.json_value())

self.playwright = sync_playwright().start()
self.browser = self.playwright.chromium.launch()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.browser.close()
self.playwright.stop()


@pytest.fixture(scope='session')
def connector(live_server):
with Connector(live_server) as connector:
yield connector


@pytest.fixture
def locale():
return 'en-US'


@pytest.fixture
def language():
return 'en'


def print_args(msg):
"""
Print messages from the browser console.
"""
for arg in msg.args:
print(arg.json_value())


@pytest.fixture()
def page(connector, viewname, locale, language):
context = connector.browser.new_context(locale=locale)
context.add_cookies([{'name': 'django_language', 'value': language, 'domain': 'localhost', 'path': '/'}])
page = context.new_page()
# page.on('console', print_args)
page.goto(connector.live_server.url + reverse(viewname))
# django_formset = page.locator('django-formset:defined')
# django_formset.wait_for()
return page
Binary file added demoapp/fonts/Courier.ttf
Binary file not shown.
4 changes: 4 additions & 0 deletions demoapp/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
DJANGO_SETTINGS_MODULE = demoapp.settings
django_find_project = false
addopts = --tb=native
39 changes: 19 additions & 20 deletions demoapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',
'easy_thumbnails',
'filer',
'finder',
'finder.contrib.archive',
'finder.contrib.audio',
Expand Down Expand Up @@ -75,24 +73,25 @@
WSGI_APPLICATION = 'wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'workdir/db.sqlite3',
},
'default_': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'finder',
'USER': 'finder',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': 5432,
# 'CONN_MAX_AGE': 900,
},
}
if os.getenv('USE_POSTGRES', False) in ['1', 'True', 'true']:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'finder',
'USER': 'finder',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': 5432,
# 'CONN_MAX_AGE': 900,
},
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'workdir/db.sqlite3',
},
}


DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Expand Down
Empty file added demoapp/unittests/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions demoapp/unittests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import pytest

from django.conf import settings
from django.core.management import call_command
from django.contrib.admin.sites import site as admin_site
from django.urls import reverse

from finder.models.realm import RealmModel

Check failure on line 9 in demoapp/unittests/conftest.py

View workflow job for this annotation

GitHub Actions / flake8

'finder.models.realm.RealmModel' imported but unused
from finder.models.folder import FolderModel

from ..utils import create_random_image

os.environ.setdefault('DJANGO_ALLOW_ASYNC_UNSAFE', 'true')


@pytest.fixture(autouse=True, scope='session')
def create_assets():
os.makedirs(settings.BASE_DIR / 'workdir/assets', exist_ok=True)
for counter in range(10):
image = create_random_image()
image.save(settings.BASE_DIR / 'workdir/assets' / f'image_{counter:01d}.png')


@pytest.fixture(scope='session')
def django_db_setup(django_db_blocker):
database_file = settings.BASE_DIR / 'workdir/test_db.sqlite3'
settings.DATABASES['default']['NAME'] = database_file
with django_db_blocker.unblock():
call_command('migrate', verbosity=0)
yield
os.remove(database_file)


@pytest.fixture
def realm(rf, admin_user):
folder_admin = admin_site.get_model_admin(FolderModel)
request = rf.get(reverse('admin:finder_foldermodel_changelist'))
request.user = admin_user
return folder_admin.get_realm(request)
Loading

0 comments on commit 7c1580f

Please sign in to comment.