|
1 | | -from django.test import TestCase |
| 1 | +import pytest |
| 2 | +from model_bakery import baker |
| 3 | +from datetime import datetime |
| 4 | +from backend.apps.interactions.models import Like, Follow |
| 5 | +from django.db import IntegrityError |
2 | 6 |
|
3 | 7 | # Create your tests here. |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.django_db(transaction=True) # Lets Django clean up database after error to continue assertions, no TransactionManagementError |
| 11 | +def test_like_creation_and_duplicate(user, post): |
| 12 | + """ |
| 13 | + Check is like is created correctly, |
| 14 | + error when duplicates |
| 15 | + """ |
| 16 | + like = baker.make("interactions.Like", user=user, post=post) |
| 17 | + |
| 18 | + assert Like.objects.count() == 1 # Manager doesn't exist for instance |
| 19 | + assert like.post == post |
| 20 | + assert like.user == user |
| 21 | + assert isinstance(like.created_at, datetime) |
| 22 | + |
| 23 | + with pytest.raises(IntegrityError): |
| 24 | + baker.make("interactions.Like", user=user, post=post) # We want this error, values must be unique |
| 25 | + |
| 26 | + assert Like.objects.count() == 1 # To make sure there is still only one object |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.django_db |
| 30 | +def test_comment_creation(user, post): |
| 31 | + """ |
| 32 | + Checks if comment is created correctly |
| 33 | + """ |
| 34 | + |
| 35 | + comment = baker.make("interactions.Comment", user=user, post=post) |
| 36 | + |
| 37 | + assert comment.user == user |
| 38 | + assert comment.post == post |
| 39 | + assert comment.body is not None |
| 40 | + assert isinstance(comment.created_at, datetime) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.django_db(transaction=True) |
| 44 | +def test_follow_creation(user): |
| 45 | + """ |
| 46 | + Checks if follow is created correctly |
| 47 | + Error if follow duplicates |
| 48 | + Error if users tries to follow himself |
| 49 | + """ |
| 50 | + user2 = baker.make("auth.User") |
| 51 | + follow = baker.make("interactions.Follow", follower=user, following=user2) |
| 52 | + assert Follow.objects.count() == 1 |
| 53 | + assert follow.follower == user |
| 54 | + assert follow.following == user2 |
| 55 | + assert isinstance(follow.created_at, datetime) |
| 56 | + |
| 57 | + # Calling error, duplicate |
| 58 | + with pytest.raises(IntegrityError): |
| 59 | + baker.make("interactions.Follow", follower=user, following=user2) |
| 60 | + |
| 61 | + # Calling error, self follow |
| 62 | + with pytest.raises(IntegrityError): |
| 63 | + baker.make("interactions.Follow", follower=user, following=user) |
| 64 | + |
| 65 | + assert Follow.objects.count() == 1 |
0 commit comments