Skip to content

Commit 16909d3

Browse files
committed
動作確認のため一旦setup.pyでテストする形に戻し
1 parent 13f9dfe commit 16909d3

File tree

8 files changed

+174
-4
lines changed

8 files changed

+174
-4
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
recursive-include beproud/django/notify/tests/templates *
12
recursive-include beproud/django/notify/templates *
23
recursive-include beproud/django/notify/fixtures *
34
include README.rst

beproud/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
2+
try:
3+
__import__('pkg_resources').declare_namespace(__name__)
4+
except ImportError:
5+
from pkgutil import extend_path
6+
__path__ = locals()['__path__'] # make PyFlakes happy
7+
__path__ = extend_path(__path__, __name__)

beproud/django/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
2+
try:
3+
__import__('pkg_resources').declare_namespace(__name__)
4+
except ImportError:
5+
from pkgutil import extend_path
6+
__path__ = locals()['__path__'] # make PyFlakes happy
7+
__path__ = extend_path(__path__, __name__)

beproud/django/notify/tests/settings.py

-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@
5757
}
5858
BPNOTIFY_SETTINGS_STORAGE = 'beproud.django.notify.storage.db.DBStorage'
5959

60-
# The name of the class to use to run the test suite
61-
# TEST_RUNNER = 'django.test.runner.DiscoverRunner'
62-
6360
app = celery.Celery()
6461
app.config_from_object('django.conf:settings', namespace='CELERY')
6562
app.autodiscover_tasks(lambda: INSTALLED_APPS)

setup.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
#:coding=utf-8:
3+
4+
import os
5+
from setuptools import setup, find_packages
6+
7+
def read_file(filename):
8+
basepath = os.path.dirname(__file__)
9+
filepath = os.path.join(basepath, filename)
10+
with open(filepath) as f:
11+
read_text = f.read()
12+
return read_text
13+
14+
15+
setup(
16+
name='bpnotify',
17+
version='0.48',
18+
description='Notification routing for Django',
19+
author='BeProud',
20+
author_email='[email protected]',
21+
long_description=read_file('README.rst'),
22+
long_description_content_type="text/x-rst",
23+
url='https://github.com/beproud/bpnotify/',
24+
python_requires='>=3.6',
25+
classifiers=[
26+
'Development Status :: 3 - Alpha',
27+
'Environment :: Plugins',
28+
'Framework :: Django',
29+
'Intended Audience :: Developers',
30+
'License :: OSI Approved :: BSD License',
31+
'Programming Language :: Python',
32+
'Programming Language :: Python :: 3',
33+
'Programming Language :: Python :: 3.6',
34+
'Programming Language :: Python :: 3.9',
35+
'Framework :: Django',
36+
'Framework :: Django :: 2.2',
37+
'Framework :: Django :: 3.2',
38+
'Topic :: Software Development :: Libraries :: Python Modules',
39+
],
40+
include_package_data=True,
41+
packages=find_packages(),
42+
namespace_packages=['beproud', 'beproud.django'],
43+
test_suite='tests.main',
44+
install_requires=[
45+
'Django>=2.2',
46+
'django-jsonfield>=1.0.1',
47+
'Celery>=4.2',
48+
'six',
49+
],
50+
zip_safe=False,
51+
)

test_settings.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Django3では、標準のdjango.conf.global_settingsの定数をオーバーライドすると例外が発生する場合がある。
2+
# https://github.com/django/django/blob/70035fb0444ae7c01613374212ca5e3c27c9782c/django/conf/__init__.py#L188
3+
# そのため、testではdjango.conf.global_settingsを直接利用せず、このtest用settings定数を使用する。
4+
5+
import os
6+
7+
SECRET_KEY = "SECRET"
8+
INSTALLED_APPS = (
9+
'django.contrib.auth',
10+
'django.contrib.contenttypes',
11+
'beproud.django.notify',
12+
)
13+
14+
# kombu.exceptions.EncodeError: Object of type User is not JSON serializable エラーを抑止する
15+
# (参考)
16+
# https://github.com/celery/celery/issues/5922
17+
# https://stackoverflow.com/questions/49373825/kombu-exceptions-encodeerror-user-is-not-json-serializable
18+
CELERY_TASK_SERIALIZER = "pickle"
19+
20+
DATABASES = {
21+
'default': {
22+
'ENGINE': 'django.db.backends.sqlite3',
23+
'NAME': ':memory:',
24+
}
25+
}
26+
27+
BASE_PATH = os.path.dirname(__file__)
28+
29+
TEMPLATES = [
30+
{
31+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
32+
'DIRS': [
33+
os.path.join(BASE_PATH, 'beproud', 'django', 'notify', 'tests', 'templates')
34+
],
35+
},
36+
]
37+
38+
CELERY_TASK_ALWAYS_EAGER = True
39+
40+
BPNOTIFY_MEDIA = {
41+
"news": {
42+
"verbose_name": "News",
43+
"default_types": ("new_user", "follow", "private_msg"),
44+
"backends": (
45+
"beproud.django.notify.backends.model.ModelBackend",
46+
),
47+
},
48+
"private_messages": {
49+
"verbose_name": "Private Message",
50+
"default_types": ("private_msg", "notify_type_with_length_over_thirty"),
51+
"backends": (
52+
"beproud.django.notify.backends.model.ModelBackend",
53+
"beproud.django.notify.backends.mail.EmailBackend",
54+
),
55+
},
56+
}
57+
BPNOTIFY_SETTINGS_STORAGE = 'beproud.django.notify.storage.db.DBStorage'
58+
59+
# The name of the class to use to run the test suite
60+
TEST_RUNNER = 'django.test.runner.DiscoverRunner'

tests.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import sys
3+
import django
4+
import celery
5+
6+
import test_settings
7+
8+
BASE_PATH = os.path.dirname(__file__)
9+
10+
11+
def main():
12+
"""
13+
Standalone django model test with a 'memory-only-django-installation'.
14+
You can play with a django model without a complete django app installation.
15+
http://www.djangosnippets.org/snippets/1044/
16+
"""
17+
18+
# Django標準のdjango.conf.global_settingsを設定してしまうと、
19+
# Django3では、global_settingsの全ての定数を上書きする挙動になってしまい、
20+
# Django3の仕様で、多重上書き禁止エラーが検知され、例外が発生する。
21+
# (例) https://github.com/django/django/blob/70035fb0444ae7c01613374212ca5e3c27c9782c/django/conf/__init__.py#L188
22+
# そのため、自前のテスト用settingsモジュール(test_settings.py)を設定する。
23+
os.environ["DJANGO_SETTINGS_MODULE"] = "test_settings"
24+
25+
app = celery.Celery()
26+
app.config_from_object('django.conf:settings', namespace='CELERY')
27+
app.autodiscover_tasks(lambda: test_settings.INSTALLED_APPS)
28+
29+
django.setup()
30+
31+
from django.test.utils import get_runner
32+
33+
# test用のsettings情報を用いて、Djangoのtest runnerクラスを取得
34+
TestRunner = get_runner(test_settings)
35+
36+
# test runnerオブジェクトを生成
37+
test_runner = TestRunner()
38+
39+
# test runnerにbpnotifyの単体テストのPathを渡して、bpnotifyの単体テストを実行する
40+
failures = test_runner.run_tests(['beproud.django.notify.tests'])
41+
42+
sys.exit(failures)
43+
44+
45+
if __name__ == '__main__':
46+
main()

tox.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ deps =
2323
dj42: Django>=4.2,<5.0
2424
celery52: celery>=5.2,<5.3
2525
celery53: celery>=5.3,<5.4
26-
commands=pytest -Wa {posargs}
26+
# commands=pytest {posargs}
27+
commands=python setup.py test
2728

2829
# tox-gh-actionsパッケージの設定
2930
[gh-actions]

0 commit comments

Comments
 (0)