Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Test Time Test our Writer model

Katelyn Polahar
Katelyn Polahar
2,691 Points

Can someone please help me determine why my WriterModelTestCase is not working properly?

I tried to format it similarly to the tests.py file from the previous video, which I added at the bottom, commented out.

but my code Isn't working, and I'm not sure why. I also tried to figure out if maybe I needed to do something to access the email within what I think is returned as a formatted string? But I'm not sure how to do that with Python.

articles/tests.py
from django.test import TestCase
from .models import Writer

class WriterModelTestCase(TestCase):
    '''Tests for the Writer model'''
    def test_writer_creation(self):
        writer = Writer.objects.create(
                                        name="Max",
                                        email="max@somefakedomain.not",
                                        bio="Max writes Steven Universe fan fictions"
                                      )
        self.assertIn(writer.name, writer.mailto)

# Code from video - tests.py - learning_site
#
# class CourseModelTests(TestCase):
#   def test_course_creation(self):
#       course = Course.objects.create(
#           title="Python Regular Expressions",
#           description="Learn to write regular expressions in Python"
#           )
#       now = timezone.now()
#       self.assertLess(course.created_at, now)
articles/models.py
from django.db import models


class Article(models.Model):
    headline = models.CharField(max_length=255)
    publish_date = models.DateTimeField()
    content = models.TextField()
    writer = models.ForeignKey('Writer')

    def __str__(self):
        return self.headline


class Writer(models.Model):
    name = models.CharField(max_length=255)
    email = models.EmailField()
    bio = models.TextField()

    def __str__(self):
        return self.name

    def mailto(self):
        return '{} <{}>'.format(self.name, self.email)
Katelyn Polahar
Katelyn Polahar
2,691 Points

Ok. This is the first time I've ever used this part of Treehouse. So, comments are not the same as answers, and I should be more careful which I click in the future. I think.

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

mailto is a method/function. What to methods/functions always have at the end when you want to use them?

Katelyn Polahar
Katelyn Polahar
2,691 Points

Well, usually it would be mailto(), but I also thought that now used now(), but when you called it inside of...oh, crap...it does use the parentheses, but I lost track of them, because the function now() got called and stored to a variable now, which got called inside of assertLess, which was why I thought mailto wasn't supposed to have them, because I spaced. But I am sure that within this Django Basics course, there were a couple of times where functions were called without parentheses. Is there a rule of thumb for that?

Thanks for your time!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

There are times and places where you don't use parentheses, yes, but the functions weren't actually being called at that time.

Here's a common example:

created_at = models.DateTimeField(default=datetime.datetime.now)

So there's that now() function but there's no parentheses, huh? What you've actually done is just provide a reference to the function instead of the result of the function. When Django actually instantiates the field on a model instance, it'll check to see if the default is a function and, if it is, it'll call it and use the result as the default value for that instance. That is, of course, assuming you didn't provide a value to use instead.

Test assertions, though, aren't "lazily evaluated" like default values are, so you have to be sure and actually call the functions you need.

Katelyn Polahar
Katelyn Polahar
2,691 Points

That actually made a lot of sense, thanks!

Now, however, it says that it doesn't see that I've called self.assertIn, when I have. Like, it's right there.

from django.test import TestCase
from .models import Writer

class WriterModelTestCase(TestCase):
    '''Tests for the Writer model'''
    def test_writer_creation(self):
        writer = Writer.objects.create(
                                        name="Max",
                                        email="max@somefakedomain.not",
                                        bio="Max writes Steven Universe fan fictions"
                                      )
        mail = writer.mailto()
        self.assertIn(writer.name, mail)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Well, you found half of a bug in the challenge and you also made half a mistake.

I fixed my bug. Now you can fix yours. Double check the challenge prompt for what you're supposed to be testing in the assertIn.

Katelyn Polahar
Katelyn Polahar
2,691 Points

NOTE: I am now looking at that article on formatting your comments so that when I try to insert code snippets it doesn't paragraphetize them.

Katelyn Polahar
Katelyn Polahar
2,691 Points

OMG I swear when I originally tried to answer this earlier, I had email...thanks, Kenneth! I am loving your tutorials, and I appreciate your help!