So this seems to be an issue talked about here and there on StackOverflow with no real solution. So I have a bunch of tests that all pass when run individual. They even pass when run as a full test suite, EXCEPT when I add in my TestCase ExploreFeedTest
. Now ExploreFeedTest
passes when run by itself and it actually doesn't fail when run in the full test suite as in running python manage.py test
, it causes another test HomeTest
to fail, which passes on it's own and passes when ExploreFeedTest
is commented out from the init.py
under the test
folder. I hear this is an issue with Django not cleaning up data properly? All my TestCase
classes are from django.test.TestCase
, because apparently if you don't use that class Django doesn't teardown the data properly, so I don't really know how to solve this. I'm also running Django 3.2.9, which is supposedly the latest. Anyone have a solution for this?
ExploreFeedTest.py
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
class ExploreFeedTest(TestCase):
Folder setup
Here are some others having similar issue:
why would a django test fail only when the full test suite is run? Inconsistent Django test results depending upon how the test is called in Django 1.5.1 running on Python 2.7.4
here's a small snippet of the failing test I am getting
.............................FE.E.E.EFE.E.E.EFEFE.E.E.E.E.E.E.E.E.E.E..............................
======================================================================
ERROR: test_all_posts_contains_post_by_user_followees_and_follow_goal (cheers.test.PostTests.HomeTest.HomeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\cheers\lib\site-packages\django\db\backends\utils.py", line 82, in _execute
return self.cursor.execute(sql)
psycopg2.errors.ForeignKeyViolation: insert or update on table "cheers_post" violates foreign key constraint "cheers_post_join_goal_id_da1e6957_fk_cheers_joingoal_uuid"
DETAIL: Key (join_goal_id)=(0947b806-f9f8-4e2c-98df-6072eaa61533) is not present in table "cheers_joingoal".
All the E
I'm getting in the tests are essentially because of this. Keep in mind these all pass just fine when I comment out ExploreFeedTest
and ExploreFeedTest
passes successfully when ran alone. Also as you can see it's HomeTest
failing.
Here's an example of one of the tests that's failing due to this error as well
class HomeTest(TestCase):
@classmethod
# Generates Test DB data to persist throughout all tests
def setUpTestData(cls) -> None:
cls.generate_count = 4
cls.access_token = get_test_user_access_token()
cls.num_posts = 0
cls.user = create_test_user_in_DB()
GoalCategory.objects.create(name='health', emoji_url='url')
cls.goal = GoalFactory()
cls.join_goal = JoinGoal.objects.create(joiner=cls.user, goal=cls.goal)
# Posts by the current user
for i in range(cls.generate_count):
cls.num_posts += 1
PostFactory()
cls.followee = User.objects.create(uuid=uuid.uuid4(), username='Test')
followee_join_goal = JoinGoal.objects.create(joiner=cls.followee, goal=cls.goal)
FollowUser.objects.create(followee=cls.followee, follower=cls.user)
# Posts by user the current user is following
for i in range(cls.generate_count):
cls.num_posts += 1
Post.objects.create(creator=cls.followee, join_goal=followee_join_goal,
type=PostType.UPDATE, body='test')
random_user = User.objects.create(uuid=uuid.uuid4(), username='Random')
cls.followed_join_goal = JoinGoal.objects.create(joiner=random_user, goal=cls.goal)
FollowGoal.objects.create(follower=cls.user, join_goal=cls.followed_join_goal)
# Posts of goal current user is following
for i in range(cls.generate_count):
cls.num_posts += 1
Post.objects.create(creator=random_user, join_goal=cls.followed_join_goal,
type=PostType.UPDATE, body='test')
cls.count = int(cls.num_posts / 2) - 1
cls.post_list = list(Post.objects.all().order_by('uuid'))
cls.mid_idx, cls.mid_post = get_mid_idx_and_post_from_post_list(cls.post_list)
def test_all_posts_contains_post_by_user_followees_and_follow_goal(self):
response = self.client.get(reverse('get_initial_home_feed',
kwargs={'count': self.num_posts}),
**{'HTTP_AUTHORIZATION': f'bearer {self.access_token}'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), self.num_posts)
posts_by_user = list(Post.objects.filter(creator=self.user).values_list('uuid', flat=True))
posts_by_followees = list(Post.objects.filter(creator=self.followee).values_list('uuid', flat=True))
posts_of_followed_join_goal = list(
Post.objects.filter(join_goal=self.followed_join_goal).values_list('uuid', flat=True))
uuids_of_posts = [uuid.UUID(data['uuid']) for data in list(response.data)]
self.assertTrue(all(uuid in uuids_of_posts for uuid in posts_by_user))
self.assertTrue(all(uuid in uuids_of_posts for uuid in posts_by_followees))
self.assertTrue(all(uuid in uuids_of_posts for uuid in posts_of_followed_join_goal))
from Django - Full test suite failing when adding a TestCase, but full test suite passes when it is commented out. All TestCase pass when run individually
No comments:
Post a Comment