def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.user.save() self.rental = Rental(address='test street', no_of_tenants=2) self.rental.save() self.login = self.client.login(username='******', password='******')
def setUp(self): """ Create necessary instances before tests """ self.rental = Rental(address='11 Test Street') self.maint_request = MaintRequest(id=1, title='Test request', property_ref=self.rental)
def setUp(self): """ Create necessary instances before tests """ self.rental = Rental(address='11 Test Street') self.maint_request = MaintRequest(property_ref=self.rental) self.chat_message = ChatMessage(maint_request=self.maint_request, message='A test message')
def test_payment_str(self): """ Test a payment string rep is maint request title """ rental = Rental(address='Test address') maint_request = MaintRequest(property_ref=rental, title='test request') payment = Payment(maint_request=maint_request, amount=10) self.assertTrue(str(payment), maint_request.title)
def test_url_is_resolved(self): """ test url uses correct view """ rental = Rental(address='11 Test Street') maint_request = MaintRequest(property_ref=rental, title='test') payment = Payment(id=1, maint_request=maint_request, amount=50) url = reverse('checkout', args=[payment.id]) self.assertEqual(resolve(url).func, checkout)
def test_payment_form_valid_data(self): """ Test form with valid data """ rental = Rental(address='Test address') maint_request = MaintRequest(property_ref=rental, title='test request') form = PaymentForm(property_ref=rental, data={ 'maint_request': maint_request, 'amount': '10' }) self.assertTrue(form.is_valid)
class TestViews(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.user.save() self.rental = Rental(address='test street', no_of_tenants=2) self.rental.save() self.login = self.client.login(username='******', password='******') def test_home_view(self): url = reverse('maint-home') response = self.client.get(url) # Check user is logged in self.assertEqual(str(response.context['user']), 'my_username') # check that a rental instance is not in context # if current user doesn't have a rental assigned self.assertEqual(response.status_code, 200) self.assertNotIn('userRental', response.context) # test rental is in context when assigned self.user.profile.rental = self.rental self.user.profile.save() response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue('userRental' in response.context) self.assertTemplateUsed(response, 'maintenance/home.html') def test_maintenance_list_view(self): url = reverse('maint-list') response = self.client.get(url) # test when user has no rental self.assertEqual(response.status_code, 200) self.assertNotIn('user_rental', response.context) # test user with rental self.user.profile.rental = self.rental self.user.profile.save() response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue('requests' in response.context) def test_maintenance_create_view(self): url = reverse('maint-create') # test POST request response = self.client.post( url, { 'request_create_form': 'True', 'title': 'A test request', 'details': 'test request', 'priority': 'high', 'image': 'test_image.png' }) req = MaintRequest.objects.filter(title='A test request') self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/maintenance/request/1/') # test GET request response = self.client.get(url, {'request_create_form': 'True'}) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'maintenance/maint_create.html') def test_maintenance_detail_view(self): maint_request = MaintRequest(title='Detail test request', status='low') maint_request.save() url = reverse('maint-detail', args=[maint_request.id]) # test assign cost redirect response = self.client.post(url, { 'assign_cost_form': 'True', 'cost': 100, 'paid_by': 'Tenant' }) self.assertEqual(response.status_code, 302) self.assertRedirects(response, reverse('maint-detail', args=[maint_request.id])) def test_maintenance_delete_view(self): maint_request = MaintRequest(title='Detail test request') maint_request.save() url = reverse('maint-delete', args=[maint_request.id]) # test redirect after maint request delete response = self.client.get(url) self.assertRedirects(response, reverse('maint-list'))