Testing an object

23 views
Skip to first unread message

Neil Hunt

unread,
Aug 1, 2016, 7:43:23 AM8/1/16
to Django users
I've read and reread the tutorial and I'm working on writing a very simple banking app by copying and modifying what's in the tutorial. I've started a new project and copied what was on the first two pages of the tutorial and I've written a new models file. Make migrations works okay with the new models. I tried testing these new models using the test below and the console came up with an attribute error. Did I make a stupid mistake somewhere? Any help would be much appreciated.

Traceback (most recent call last):
  File "/home/DJapps/banking/mybank/banking/test_models.py", line 12, in test_animals_can_speak
    self.assertEqual(person.get_first_name(), 'Bob')
AttributeError: 'Person' object has no attribute 'get_first_name'


from django.test import TestCase
from banking.models import Person

class PersonTestCase(TestCase):
    def setUp(self):
        Person.objects.create(first_name="Bob", last_name="Smith", login_name="myname", password="Dog")

    def test_animals_can_speak(self):
        """Animals that can speak are correctly identified"""
        person = Person.objects.get(first_name="Bob")
        #person = mommy.make(Person)
        self.assertEqual(person.get_first_name(), 'Bob')
        #self.assertEqual(person.get_last_name(), 'Smith')
        #self.assertEqual(person.get_login_name(), 'myname')
        #self.assertEqual(person.get_password(), 'Dog')
        #self.assertEqual(person._str__, person_text)

New models...

from django.db import models

# Create your models here.
class Person(models.Model):
    first_name = models.CharField(max_length=4,default="Bob")
    last_name = models.CharField(max_length=6,default="Smith")
    login_name = models.CharField(max_length=3,default="me")
    password = models.CharField(max_length=7, default="password")
    person_text = models.CharField(max_length=10, default="Bob Smith")
   
def get_first_name(self):
    return self.first_name

def get_last_name(self):
    return self.last_name

def get_login_name(self):
    return self.login_name

def get_password(self, user_input):
    return self.password

def __str__(self):
    return self.person_text

class Account1(models.Model):
    account_number = models.IntegerField(default=12345678)
    bank_balance = models.DecimalField(max_digits=4, decimal_places=2, default=12.99)
    interest_rate = models.IntegerField(default=2)
    account_text = models.CharField(max_length=16, default="Current Account")
    person = models.ForeignKey(Person, on_delete=models.CASCADE, default="")
       

def get_account_number(self):
    return self.account_number

def get_bank_balance(self):
    return self.bank_balance

def deposit(self, amount):
    self.bank_balance = self.bank_balance + amount
    return

def get_interest_rate(self):
    return self.interest_rate

def withdraw(self, amount):
    self.bank_balance = self.bank_balance - amount
    return

def __str__(self):
    return self.account_text


ludovic coues

unread,
Aug 1, 2016, 7:58:01 AM8/1/16
to django...@googlegroups.com
Look like you forget to indent the method inside your class.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/1b946075-f532-408f-ace7-4e4361ad350b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Neil Hunt

unread,
Aug 1, 2016, 8:33:06 AM8/1/16
to Django users
That was an silly basic python mistake. Doh! I just indented the method correctly and the test ran fine. Thanks a lot, ludovic :)
Reply all
Reply to author
Forward
0 new messages