Skip to content

Commit

Permalink
basic skeleton for test-suite
Browse files Browse the repository at this point in the history
  • Loading branch information
arneb committed Jan 29, 2021
1 parent 676383a commit be28331
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.py[co]
.tox
dist/
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Newsletter management for the Django webframework
=================================================

.. image:: https://img.shields.io/pypi/v/django-campaign.svg
:target: https://pypi.python.org/pypi/django-campaign/

Django-campaign is a newsletter campaign management app for the Django
webframework. It can manage multiple newsletters with different subscriberlists.

Expand Down
74 changes: 74 additions & 0 deletions campaign/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
from django.test import TestCase

from campaign.forms import SubscriberListForm
from campaign.models import (
BlacklistEntry, Campaign, MailTemplate, Newsletter, SubscriberList
)

User = get_user_model()


class SubscriberlistFormTestCase(TestCase):
def test_all_fields_validation_valid(self):
data = {
"name": "Test list",
"content_type": ContentType.objects.get_for_model(User).pk,
"filter_condition": '{"is_active": true}',
"email_field_name": "email"
}
form = SubscriberListForm(data)
self.assertTrue(form.is_valid())

def test_email_field_name_validation_not_valid(self):
data = {
"name": "Test list",
"content_type": ContentType.objects.get_for_model(User).pk,
"filter_condition": '{"is_active": true}',
"email_field_name": "foo"
}
form = SubscriberListForm(data)
self.assertFalse(form.is_valid())
self.assertTrue("email_field_name" in form.errors)

def test_filter_condition_validation_not_valid(self):
data = {
"name": "Test list",
"content_type": ContentType.objects.get_for_model(User).pk,
"filter_condition": '{"active": "foo"}',
"email_field_name": "email"
}
form = SubscriberListForm(data)
self.assertFalse(form.is_valid())
self.assertTrue("filter_condition" in form.errors)


class AdminTestCase(TestCase):
def setUp(self):
user = User.objects.create_superuser("test", "[email protected]", "p")
self.client.force_login(user)

def test_changelists(self):
for model in (BlacklistEntry, Campaign, MailTemplate, Newsletter, SubscriberList):
response = self.client.get(
reverse("admin:campaign_%s_changelist" % model._meta.model_name),
follow=False,
)
self.assertEqual(response.status_code, 200)
content = response.content.decode("utf-8")
self.assertTrue('<form id="changelist-form" method="post"' in content)

def test_addform(self):
for model in (BlacklistEntry, Campaign, MailTemplate, Newsletter, SubscriberList):
response = self.client.get(
reverse("admin:campaign_%s_add" % model._meta.model_name),
follow=False,
)
self.assertEqual(response.status_code, 200)
content = response.content.decode("utf-8")
self.assertTrue('id="%s_form"' % model._meta.model_name in content)
Empty file added tests/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions tests/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
import os
import sys


sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.path.pardir))


def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
98 changes: 98 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'only-for-the-test-suite'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

SITE_ID = 1

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'campaign',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'wsgi.application'


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

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = []


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
8 changes: 8 additions & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin
#from django.urls import path, include
from django.conf.urls import url, include

urlpatterns = [
url('admin/', admin.site.urls),
url('campaign/', include('campaign.urls')),
]
7 changes: 7 additions & 0 deletions tests/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')

application = get_wsgi_application()
16 changes: 16 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tox]
envlist = py27-dj11, py{35,36,37,38}-dj{11,20,21,22}

[testenv]
commands = {envpython} tests/manage.py test campaign --settings=settings
basepython =
py27: python2.7
py35: python3.5
py36: python3.6
py37: python3.7
py38: python3.8
deps =
dj11: django>=1.11,<1.11.99
dj20: django>=2,<2.0.99
dj21: django>=2.1,<2.1.99
dj22: django>=2.2,<2.2.99

0 comments on commit be28331

Please sign in to comment.