Remove setUp in order to remove unneded objects.create

This commit is contained in:
martin056 2021-11-24 13:43:48 +02:00 committed by Radoslav Georgiev
parent 8a6e4e9635
commit c2c82e8573
No known key found for this signature in database
GPG Key ID: 0B7753A4DFCE646D

View File

@ -852,33 +852,39 @@ from django_styleguide.payments.models import Payment, Item
class ItemBuyTests(TestCase): class ItemBuyTests(TestCase):
def setUp(self):
self.user = User.objects.create_user(username='Test User')
self.item = Item.objects.create(
name='Test Item',
description='Test Item description',
price=10.15
)
@patch('project.payments.services.items_get_for_user') @patch('project.payments.services.items_get_for_user')
def test_buying_item_that_is_already_bought_fails(self, items_get_for_user_mock): def test_buying_item_that_is_already_bought_fails(self, items_get_for_user_mock):
""" """
Since we already have tests for `items_get_for_user`, Since we already have tests for `items_get_for_user`,
we can safely mock it here and give it a proper return value. we can safely mock it here and give it a proper return value.
""" """
items_get_for_user_mock.return_value = [self.item] user = User(username='Test User')
item = Item(
name='Test Item',
description='Test Item description',
price=10.15
)
items_get_for_user_mock.return_value = [item]
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
item_buy(user=self.user, item=self.item) item_buy(user=user, item=item)
@patch('project.payments.services.payment_charge.delay') @patch('project.payments.services.payment_charge.delay')
def test_buying_item_creates_a_payment_and_calls_charge_task( def test_buying_item_creates_a_payment_and_calls_charge_task(
self, self,
payment_charge_mock payment_charge_mock
): ):
user = User.objects.create_user(username='Test User')
item = Item.objects.create(
name='Test Item',
description='Test Item description',
price=10.15
)
self.assertEqual(0, Payment.objects.count()) self.assertEqual(0, Payment.objects.count())
payment = item_buy(user=self.user, item=self.item) payment = item_buy(user=user, item=item)
self.assertEqual(1, Payment.objects.count()) self.assertEqual(1, Payment.objects.count())
self.assertEqual(payment, Payment.objects.first()) self.assertEqual(payment, Payment.objects.first())