lundi 22 juin 2020

How to test models with foreign keys in Django v3

I have already tested the Destinations models and it pass the test with the approach:

def test_destinations_model(self):
        destination = Destinations.objects.create(
            tour_title = 'test1',
            booking_start_date='2021-05-30',
            booking_end_date= '2022-05-30',
            price=2000,
            description='test1',
            author='test1',
            image='image.png'
        )
        destination.save()
        self.assertEquals(destination.tour_title, 'test1')
        self.assertEquals(destination.booking_start_date, '2021-05-30')
        self.assertEquals(destination.booking_end_date, '2022-05-30')
        self.assertEquals(destination.price, 2000)
        self.assertEquals(destination.description, 'test1')
        self.assertEquals(destination.author, 'test1')
        self.assertEquals(destination.image, 'image.png')

However, I don't know how to test the Comment model since it is related to the Destinations one. I have done the following approach but the test is failing with the following message:

#message of failing test for test_comment_mode

self.assertEquals(destination, comment)
AssertionError: <Destinations: test1> != <Comment: Comment test1 by John>

#Failling test:
def test_comment_model(self):
        #post not needed to test as foreignKey
        destination = Destinations(
            tour_title = 'test1',
            booking_start_date='2021-05-30',
            booking_end_date= '2022-05-30',
            price=2000,
            description='test1',
            author='test1',
            image='image.png'
        )
        destination.save()
        comment = Comment(
            post=destination,
            name = 'John',
            email = 'any@email.com',
            comment = 'test1',
            created_on = timezone.now(),
            active = False,

        )
        comment.save()
        self.assertEquals(destination, comment)


class Destinations(models.Model):

    author = models.CharField(max_length=200, unique=False)
    tour_title = models.CharField(max_length=250)
    description = RichTextUploadingField()
    image = models.ImageField(upload_to='tour_images', blank=True)
    location = models.CharField(max_length=250)
    booking_start_date = models.DateField()
    booking_end_date = models.DateField()
    price = models.DecimalField(max_digits=6, decimal_places=2)

    def __str__(self):
        return self.tour_title

class Comment(models.Model):
    post = models.ForeignKey(Destinations,on_delete=models.CASCADE,related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField()
    comment = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=False)

    class Meta:
        ordering = ['created_on']

    def __str__(self):
        return 'Comment {} by {}'.format(self.comment, self.name)

Aucun commentaire:

Enregistrer un commentaire