54. You just installed Django's auth system, which
means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'gilson'): admin
E-mail address: contato@gilsondev.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
62. Creating test database for alias 'default'...
F
===========================================================
===========
FAIL: test_get_talk_form (src.talks.tests.TalkUrlTest)
Retorna status 200 da lista de palestras
--------------------------------------------------------------------...
File
"/home/gilson/Projects/engipolls/src/talks/tests.py", line
10, in test_get_talk_form
self.assertEquals(response.status_code, 200)
AssertionError: 404 != 200
---------------------------------------------------------------------
67. $ python manage.py test talks
Creating test database for alias 'default'...
E
===========================================================
===========
ERROR: test_get_talk_form (src.talks.tests.TalkUrlTest)
Retorna status 200 da lista de palestras
--------------------------------------------------------------------...
ViewDoesNotExist: Could not import src.talks.views.home.
View does not exist in module src.talks.views.
--------------------------------------------------------------------Ran 1 test in 0.026s
73. $ python manage.py tests talks
Creating test database for alias 'default'...
.
--------------------------------------------------------------------Ran 1 test in 0.014s
75. # -*- coding: utf8 -*-
talks/tests.py
from django.test import TestCase
from django.core.urlresolvers import reverse
class TalkUrlTest(TestCase):
def test_get_talk_form(self):
'''Retorna status 200 da lista de
palestras'''
response =
self.client.get(reverse('home'))
self.assertEquals(response.status_code,
76. $ python manage.py test talks
Creating test database for alias 'default'...
.
--------------------------------------------------------------------Ran 1 test in 0.014s
77. # -*- coding: utf8 -*-
talks/tests.py
from django.test import TestCase
from django.core.urlresolvers import reverse
class TalkUrlTest(TestCase):
def setUp(self):
self.resp = self.client.get(reverse('home'))
def test_get_talk_form(self):
'''Retorna status 200 da lista de
palestras'''
self.assertEquals(self.resp.status_code,
200)
78. $ python manage.py test talks
Creating test database for alias 'default'...
.
--------------------------------------------------------------------Ran 1 test in 0.014s
79. talks/tests.py
# -*- coding: utf8 -*from django.test import TestCase
from django.core.urlresolvers import reverse
class TalkUrlTest(TestCase):
# ...
def test_template(self):
'''Renderiza template para enviar na resposta'''
self.assertTemplateUsed(self.resp,
'talks/talks_list.html')
80. .F
===========================================================
===========
FAIL: test_template (src.talks.tests.TalkUrlTest)
Usa o template talks/talks_list.html
--------------------------------------------------------------------...
File
"/home/gilson/Projects/engipolls/local/lib/python2.7/sitepackages/django/test/testcases.py", line 741, in
assertTemplateUsed
self.fail(msg_prefix + "No templates used to render the
response")
AssertionError: No templates used to render the response
-----------------------------------------------------------
81. # -*- coding: utf8 -*from django.http import HttpResponse
from django.template import loader, Context
def home(request):
t =
loader.get_template('talks/talks_list.html')
c = Context()
content = t.render(c)
return HttpResponse(content)
82. Creating test database for alias 'default'...
EE
===========================================================
===========
ERROR: test_get_talk_form (src.talks.tests.TalkUrlTest)
Retorna status 200 da lista de palestras
---------------------------------------------------------------------...
TemplateDoesNotExist: talks/talks_list.html
===========================================================
===========
ERROR: test_template (src.talks.tests.TalkUrlTest)
Renderiza template para enviar na resposta
--------------------------------------------------------------------...
TemplateDoesNotExist: talks/talks_list.html
-----------------------------------------------------------
85. app_directories.Loader
# crie a pasta templates e subpasta talks na
aplicação
$ mkdir -p src/talks/templates/talks
$ touch
src/talks/templates/talks/talks_list.html
86. $ python manage.py test talks
Creating test database for alias 'default'...
..
--------------------------------------------------------------------Ran 2 tests in 0.025s
89. Exemplo de Model
class Talk(models.Model):
name = models.CharField(max_length=100)
resume = models.TextField()
at = models.DateTimeField(auto_now_add=True)
talker = models.TextField(max_length=80)
92. import datetime
# …
from .models import Talk
class TalkModelTest(TestCase):
def setUp(self):
self.talk = Talk.objects.create(
name=u”O que é Python?”,
resume=u”Palestra sobre Python”,
at=datetime.datetime.now(),
talker=”Guido Van Rossum”
)
def test_create(self):
'''Registra a palestra corretamente'''
self.assertEquals(self.talk.pk, 1)
93. $ python manage.py test talks
Traceback (most recent call last):
...
File
"/home/gilson/Projects/engipolls/src/talks/tests.py", line
7, in <module>
from .models import Talk
ImportError: cannot import name Talk
94. talks/models.py
class Talk(models.Model):
name = models.CharField(max_length=100)
resume = models.TextField()
at = models.DateTimeField(auto_now_add=True)
talker = models.TextField(max_length=80)
95. $ python manage.py test talks
Creating test database for alias 'default'...
...
--------------------------------------------------------------------Ran 3 tests in 0.027s
96. # -*- coding: utf8 -*import datetime
# …
from .models import Talk
class TalkModelTest(TestCase):
# ...
def test_unicode(self):
'''É usado para a representação do objeto'''
self.assertEquals(
u'Palestra: O que é Python?',
unicode(self.talk))
97. Creating test database for alias 'default'...
.F..
===========================================================
===========
FAIL: test_unicode (src.talks.tests.TalkModelTest)
É usado para retornar a representação do objeto
--------------------------------------------------------------------...
AssertionError: u'Palestra: O que xe9 Python?' != u'Talk
object'
- Palestra: O que xe9 Python?
+ Talk object
--------------------------------------------------------------------Ran 4 tests in 0.047s
98. talks/models.py
class Talk(models.Model):
name = models.CharField(max_length=100)
resume = models.TextField()
at = models.DateTimeField(auto_now_add=True)
talker = models.TextField(max_length=80)
def __unicode__(self):
return “Palestra: %s” % (self.name,)
99. $ python manage.py test talks
Creating test database for alias 'default'...
....
--------------------------------------------------------------------Ran 4 tests in 0.030s
104. talks/tests.py
class TalkModelTest(TestCase):
def setUp(self):
self.talk = Talk.objects.create(
name=u”O que é Python?”,
resume=u”Palestra sobre Python”,
at=datetime.datetime.now(),
talker=”Guido Van Rossum”,
type_talk='P'
)
def test_create(self):
'''Registra a palestra corretamente'''
self.assertEquals(self.talk.pk, 1)
105. =============================================================
=========
ERROR: test_create (src.talks.tests.TalkModelTest)
Registra a palestra corretamente
--------------------------------------------------------------------...
TypeError: 'type_talk' is an invalid keyword argument for
this function
=============================================================
=========
ERROR: test_unicode (src.talks.tests.TalkModelTest)
É usado para retornar a representação do objeto
--------------------------------------------------------------------...
TypeError: 'type_talk' is an invalid keyword argument for
107. $ python manage.py test talks
Creating test database for alias 'default'...
....
--------------------------------------------------------------------Ran 4 tests in 0.033s
122. $ python manage.py runserver
Validating models...
0 errors found
Django version 1.4.1, using settings
'src.settings'
Development server is running at
http://127.0.0.1:8000/
Quit the server with CONTROL-C.
144. $ python manage.py test talks
Creating test database for alias 'default'...
....
--------------------------------------------------------------------Ran 4 tests in 0.033s
154. talks/forms.py
# -*- coding: utf8 -*from django import forms
class TalkForm(forms.Form):
TALK_CHOICES = (
('P', _(u'Palestra')),
('M', _(u'Minicurso')),
)
name = forms.CharField(label=u'Nome da Sessão')
resume = forms.CharField(label=u'Resumo', widget=forms.Textarea)
at = forms.DateTimeField(label=u'Data da Sessão')
talker = forms.CharField(label=u'Palestrante')
type_talk = forms.ChoiceField(label=u'Tipo de Sessão', choices=TALK_CHOICES)
155. talks/forms.py
# -*- coding: utf8 -*from django import forms
from .models import Talk
class TalkForm(forms.Form):
TALK_CHOICES = (
('P', _(u'Palestra')),
('M', _(u'Minicurso')),
)
name = forms.CharField(label=u'Nome da Sessão')
resume = forms.CharField(label=u'Resumo', widget=forms.Textarea)
at = forms.DateTimeField(label=u'Data da Sessão')
class Meta:
model = Talk
162. talks/forms.py
# -*- coding: utf8 -*from django import forms
from .models import Poll
class PollForm(forms.Form):
class Meta:
model = Poll
exclude = ('talk',)
163.
164. talks/views.py
# …
from django.shortcuts import get_object_or_404
from .models import Talk
def poll_form(request, talk_id):
talk = get_object_or_404(Talk, pk=talk_id)
if request.method == 'POST':
form = PollForm(request.POST)
if form.is_valid():
poll = form.save(commit=False)
poll.talk = talk
poll.save()
return render(request, 'talks/polls_form.html', {
'form': PollForm(),
})