Skip to content

Commit 0fe96e4

Browse files
committed
add test application and improve testing
1 parent d5e7138 commit 0fe96e4

File tree

12 files changed

+371
-16
lines changed

12 files changed

+371
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ before_script:
1111
- pip install coverage coveralls
1212
- pip install -q "$DJANGO_VERSION"
1313
script:
14-
- coverage run test.py
14+
- coverage run manage.py test
1515
after_script:
1616
- coveralls
1717
matrix:

manage.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_app.settings")
7+
8+
from django.core.management import execute_from_command_line
9+
10+
execute_from_command_line(sys.argv)

test.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

test_app/__init__.py

Whitespace-only changes.

test_app/admin.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from django.contrib import admin
4+
from .models import Continent, Country, Location, Publication, Book, Writer
5+
6+
class ContinentAdmin(admin.ModelAdmin):
7+
list_display = ('name',)
8+
9+
10+
class CountryAdmin(admin.ModelAdmin):
11+
list_display = ('name', 'continent')
12+
13+
14+
class LocationAdmin(admin.ModelAdmin):
15+
list_display = ('continent', 'country', 'city', 'street')
16+
17+
18+
class PublicationAdmin(admin.ModelAdmin):
19+
list_display = ('name',)
20+
21+
22+
class BookAdmin(admin.ModelAdmin):
23+
list_display = ('name',)
24+
25+
26+
class WriterAdmin(admin.ModelAdmin):
27+
list_display = ('name',)
28+
29+
admin.site.register(Continent, ContinentAdmin)
30+
admin.site.register(Country, CountryAdmin)
31+
admin.site.register(Location, LocationAdmin)
32+
admin.site.register(Publication, PublicationAdmin)
33+
admin.site.register(Book, BookAdmin)
34+
admin.site.register(Writer, WriterAdmin)

test_app/fixtures/data.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[
2+
{
3+
"fields": {
4+
"name": "Europe"
5+
},
6+
"model": "test_app.continent",
7+
"pk": 1
8+
},
9+
{
10+
"fields": {
11+
"name": "America"
12+
},
13+
"model": "test_app.continent",
14+
"pk": 2
15+
},
16+
{
17+
"fields": {
18+
"continent": 1,
19+
"name": "Czech republic"
20+
},
21+
"model": "test_app.country",
22+
"pk": 1
23+
},
24+
{
25+
"fields": {
26+
"continent": 2,
27+
"name": "New York"
28+
},
29+
"model": "test_app.country",
30+
"pk": 2
31+
},
32+
{
33+
"fields": {
34+
"continent": 1,
35+
"name": "Germany"
36+
},
37+
"model": "test_app.country",
38+
"pk": 3
39+
},
40+
{
41+
"fields": {
42+
"continent": 1,
43+
"name": "Great Britain"
44+
},
45+
"model": "test_app.country",
46+
"pk": 4
47+
},
48+
{
49+
"fields": {
50+
"city": "Praha",
51+
"continent": 1,
52+
"country": 1,
53+
"street": "Bo\u0159ivojova"
54+
},
55+
"model": "test_app.location",
56+
"pk": 1
57+
},
58+
{
59+
"fields": {
60+
"date_joined": "2015-11-12T18:16:03.886Z",
61+
"email": "[email protected]",
62+
"first_name": "",
63+
"groups": [],
64+
"is_active": true,
65+
"is_staff": true,
66+
"is_superuser": true,
67+
"last_login": "2016-01-16T17:16:11.609Z",
68+
"last_name": "",
69+
"password": "pbkdf2_sha256$24000$Wx6h5GFwF85f$ghtDkGbfcm6jirxju4ewNgr0p8oLEO11yxAlmvCQMgk=",
70+
"user_permissions": [],
71+
"username": "admin"
72+
},
73+
"model": "auth.user",
74+
"pk": 1
75+
}
76+
]

test_app/migrations/0001_initial.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9.1 on 2016-01-16 17:14
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
import smart_selects.db_fields
8+
9+
10+
class Migration(migrations.Migration):
11+
12+
initial = True
13+
14+
dependencies = [
15+
]
16+
17+
operations = [
18+
migrations.CreateModel(
19+
name='Book',
20+
fields=[
21+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
22+
('name', models.CharField(max_length=255)),
23+
],
24+
),
25+
migrations.CreateModel(
26+
name='Continent',
27+
fields=[
28+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
29+
('name', models.CharField(max_length=255)),
30+
],
31+
),
32+
migrations.CreateModel(
33+
name='Country',
34+
fields=[
35+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
36+
('name', models.CharField(max_length=255)),
37+
('continent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.Continent')),
38+
],
39+
),
40+
migrations.CreateModel(
41+
name='Location',
42+
fields=[
43+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
44+
('city', models.CharField(max_length=50)),
45+
('street', models.CharField(max_length=100)),
46+
('continent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.Continent')),
47+
('country', smart_selects.db_fields.ChainedForeignKey(auto_choose=True, chained_field=b'continent', chained_model_field=b'continent', on_delete=django.db.models.deletion.CASCADE, to='test_app.Country')),
48+
],
49+
),
50+
migrations.CreateModel(
51+
name='Publication',
52+
fields=[
53+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
54+
('name', models.CharField(max_length=255)),
55+
],
56+
),
57+
migrations.CreateModel(
58+
name='Writer',
59+
fields=[
60+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
61+
('name', models.CharField(max_length=255)),
62+
('publications', models.ManyToManyField(blank=True, to='test_app.Publication')),
63+
],
64+
),
65+
migrations.AddField(
66+
model_name='book',
67+
name='publication',
68+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.Publication'),
69+
),
70+
migrations.AddField(
71+
model_name='book',
72+
name='writer',
73+
field=smart_selects.db_fields.ChainedManyToManyField(chained_field=b'publication', chained_model_field=b'publications', to='test_app.Writer'),
74+
),
75+
]

test_app/migrations/__init__.py

Whitespace-only changes.

test_app/models.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
from django.db import models
3+
from smart_selects.db_fields import ChainedForeignKey, ChainedManyToManyField
4+
5+
class Continent(models.Model):
6+
name = models.CharField(max_length=255)
7+
8+
def __str__(self):
9+
return "%s" % self.name
10+
11+
class Country(models.Model):
12+
continent = models.ForeignKey(Continent)
13+
name = models.CharField(max_length=255)
14+
15+
def __str__(self):
16+
return "%s" % self.name
17+
18+
class Location(models.Model):
19+
continent = models.ForeignKey(Continent)
20+
country = ChainedForeignKey(
21+
Country,
22+
chained_field="continent",
23+
chained_model_field="continent",
24+
show_all=False,
25+
auto_choose=True
26+
)
27+
#area = ChainedForeignKey(Area, chained_field="country", chained_model_field="country")
28+
city = models.CharField(max_length=50)
29+
street = models.CharField(max_length=100)
30+
31+
32+
class Publication(models.Model):
33+
name = models.CharField(max_length=255)
34+
35+
class Writer(models.Model):
36+
name = models.CharField(max_length=255)
37+
publications = models.ManyToManyField('Publication', blank=True)
38+
39+
class Book(models.Model):
40+
publication = models.ForeignKey(Publication)
41+
writer = ChainedManyToManyField(
42+
Writer,
43+
chained_field="publication",
44+
chained_model_field="publications",
45+
)
46+
name = models.CharField(max_length=255)

test_app/settings.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Django settings for test_app project.
3+
4+
For more information on this file, see
5+
https://docs.djangoproject.com/en/1.7/topics/settings/
6+
7+
For the full list of settings and their values, see
8+
https://docs.djangoproject.com/en/1.7/ref/settings/
9+
"""
10+
11+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
12+
import os
13+
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
14+
15+
16+
# Quick-start development settings - unsuitable for production
17+
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
18+
19+
# SECURITY WARNING: keep the secret key used in production secret!
20+
SECRET_KEY = 'f8fkupu8pa%%u$wgk6c!os39el41v7i7^u*8xs3@~]$asffw'
21+
22+
# SECURITY WARNING: don't run with debug turned on in production!
23+
DEBUG = True
24+
25+
TEMPLATES = [
26+
{
27+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
28+
'DIRS': [],
29+
'APP_DIRS': True,
30+
'OPTIONS': {
31+
'context_processors': {
32+
'django.contrib.auth.context_processors.auth',
33+
}
34+
},
35+
},
36+
]
37+
38+
ALLOWED_HOSTS = []
39+
40+
41+
# Application definition
42+
43+
INSTALLED_APPS = (
44+
'django.contrib.admin',
45+
'django.contrib.auth',
46+
'django.contrib.contenttypes',
47+
'django.contrib.sessions',
48+
'django.contrib.messages',
49+
'django.contrib.staticfiles',
50+
'smart_selects',
51+
'test_app',
52+
)
53+
54+
MIDDLEWARE_CLASSES = (
55+
'django.contrib.sessions.middleware.SessionMiddleware',
56+
'django.middleware.common.CommonMiddleware',
57+
'django.middleware.csrf.CsrfViewMiddleware',
58+
'django.contrib.auth.middleware.AuthenticationMiddleware',
59+
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
60+
'django.contrib.messages.middleware.MessageMiddleware',
61+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
62+
)
63+
64+
ROOT_URLCONF = 'test_app.urls'
65+
66+
67+
# Database
68+
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
69+
70+
DATABASES = {
71+
'default': {
72+
'ENGINE': 'django.db.backends.sqlite3',
73+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
74+
}
75+
}
76+
77+
# Internationalization
78+
# https://docs.djangoproject.com/en/1.7/topics/i18n/
79+
80+
LANGUAGE_CODE = 'en-us'
81+
82+
TIME_ZONE = 'UTC'
83+
84+
USE_I18N = True
85+
86+
USE_L10N = True
87+
88+
USE_TZ = True
89+
90+
91+
# Static files (CSS, JavaScript, Images)
92+
# https://docs.djangoproject.com/en/1.7/howto/static-files/
93+
94+
STATIC_URL = '/static/'

test_app/tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.core.urlresolvers import reverse
2+
from django.test import TestCase
3+
from .models import Country
4+
5+
class ViewTests(TestCase):
6+
fixtures = [ 'data', ]
7+
8+
def setUp(self):
9+
self.assertTrue(self.client.login(username='admin', password='admin'))
10+
11+
def test_location_add_get(self):
12+
response = self.client.get(reverse('admin:test_app_location_add'), follow=True)
13+
self.assertContains(response, 'Europe')
14+
self.assertContains(response, 'America')
15+
16+
def test_location_add_post(self):
17+
post_data = {
18+
'continent': '1',
19+
'country': '2',
20+
'city': 'New York',
21+
'street': 'Wallstreet',
22+
}
23+
response = self.client.post(reverse('admin:test_app_location_add'), post_data, follow=True)
24+
country = Country.objects.get(pk=2)
25+
location = country.location_set.first()
26+
self.assertEquals(location.city, 'New York')
27+
self.assertEquals(location.street, 'Wallstreet')

0 commit comments

Comments
 (0)