Skip to content

Commit

Permalink
動作確認のため一旦setup.pyでテストする形に戻し
Browse files Browse the repository at this point in the history
  • Loading branch information
kashewnuts committed Feb 2, 2024
1 parent 13f9dfe commit 8645f64
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 4 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
recursive-include beproud/django/notify/tests/templates *
recursive-include beproud/django/notify/templates *
recursive-include beproud/django/notify/fixtures *
include README.rst
Expand Down
7 changes: 7 additions & 0 deletions beproud/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
from pkgutil import extend_path
__path__ = locals()['__path__'] # make PyFlakes happy
__path__ = extend_path(__path__, __name__)
7 changes: 7 additions & 0 deletions beproud/django/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
from pkgutil import extend_path
__path__ = locals()['__path__'] # make PyFlakes happy
__path__ = extend_path(__path__, __name__)
3 changes: 0 additions & 3 deletions beproud/django/notify/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
}
BPNOTIFY_SETTINGS_STORAGE = 'beproud.django.notify.storage.db.DBStorage'

# The name of the class to use to run the test suite
# TEST_RUNNER = 'django.test.runner.DiscoverRunner'

app = celery.Celery()
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: INSTALLED_APPS)
51 changes: 51 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
#:coding=utf-8:

import os
from setuptools import setup, find_packages

def read_file(filename):
basepath = os.path.dirname(__file__)
filepath = os.path.join(basepath, filename)
with open(filepath) as f:
read_text = f.read()
return read_text


setup(
name='bpnotify',
version='0.48',
description='Notification routing for Django',
author='BeProud',
author_email='[email protected]',
long_description=read_file('README.rst'),
long_description_content_type="text/x-rst",
url='https://github.com/beproud/bpnotify/',
python_requires='>=3.6',
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Plugins',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.9',
'Framework :: Django',
'Framework :: Django :: 2.2',
'Framework :: Django :: 3.2',
'Topic :: Software Development :: Libraries :: Python Modules',
],
include_package_data=True,
packages=find_packages(),
namespace_packages=['beproud', 'beproud.django'],
test_suite='tests.main',
install_requires=[
'Django>=2.2',
'django-jsonfield>=1.0.1',
'Celery>=4.2',
'six',
],
zip_safe=False,
)
60 changes: 60 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Django3では、標準のdjango.conf.global_settingsの定数をオーバーライドすると例外が発生する場合がある。
# https://github.com/django/django/blob/70035fb0444ae7c01613374212ca5e3c27c9782c/django/conf/__init__.py#L188
# そのため、testではdjango.conf.global_settingsを直接利用せず、このtest用settings定数を使用する。

import os

SECRET_KEY = "SECRET"
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'beproud.django.notify',
)

# kombu.exceptions.EncodeError: Object of type User is not JSON serializable エラーを抑止する
# (参考)
# https://github.com/celery/celery/issues/5922
# https://stackoverflow.com/questions/49373825/kombu-exceptions-encodeerror-user-is-not-json-serializable
CELERY_TASK_SERIALIZER = "pickle"

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}

BASE_PATH = os.path.dirname(__file__)

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_PATH, 'beproud', 'django', 'notify', 'tests', 'templates')
],
},
]

CELERY_TASK_ALWAYS_EAGER = True

BPNOTIFY_MEDIA = {
"news": {
"verbose_name": "News",
"default_types": ("new_user", "follow", "private_msg"),
"backends": (
"beproud.django.notify.backends.model.ModelBackend",
),
},
"private_messages": {
"verbose_name": "Private Message",
"default_types": ("private_msg", "notify_type_with_length_over_thirty"),
"backends": (
"beproud.django.notify.backends.model.ModelBackend",
"beproud.django.notify.backends.mail.EmailBackend",
),
},
}
BPNOTIFY_SETTINGS_STORAGE = 'beproud.django.notify.storage.db.DBStorage'

# The name of the class to use to run the test suite
TEST_RUNNER = 'django.test.runner.DiscoverRunner'
46 changes: 46 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
import sys
import django
import celery

import test_settings

BASE_PATH = os.path.dirname(__file__)


def main():
"""
Standalone django model test with a 'memory-only-django-installation'.
You can play with a django model without a complete django app installation.
http://www.djangosnippets.org/snippets/1044/
"""

# Django標準のdjango.conf.global_settingsを設定してしまうと、
# Django3では、global_settingsの全ての定数を上書きする挙動になってしまい、
# Django3の仕様で、多重上書き禁止エラーが検知され、例外が発生する。
# (例) https://github.com/django/django/blob/70035fb0444ae7c01613374212ca5e3c27c9782c/django/conf/__init__.py#L188
# そのため、自前のテスト用settingsモジュール(test_settings.py)を設定する。
os.environ["DJANGO_SETTINGS_MODULE"] = "test_settings"

app = celery.Celery()
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: test_settings.INSTALLED_APPS)

django.setup()

from django.test.utils import get_runner

# test用のsettings情報を用いて、Djangoのtest runnerクラスを取得
TestRunner = get_runner(test_settings)

# test runnerオブジェクトを生成
test_runner = TestRunner()

# test runnerにbpnotifyの単体テストのPathを渡して、bpnotifyの単体テストを実行する
failures = test_runner.run_tests(['beproud.django.notify.tests'])

sys.exit(failures)


if __name__ == '__main__':
main()
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ deps =
pytest
pytest-django
pytest-pythonpath
setuptools
six
dj32: Django>=3.2,<4.0
dj42: Django>=4.2,<5.0
celery52: celery>=5.2,<5.3
celery53: celery>=5.3,<5.4
commands=pytest -Wa {posargs}
# commands=pytest {posargs}
commands=python setup.py test

# tox-gh-actionsパッケージの設定
[gh-actions]
Expand Down

0 comments on commit 8645f64

Please sign in to comment.