|
| 1 | +from datetime import date, timedelta, time, datetime |
| 2 | + |
| 3 | +from django.core.exceptions import ValidationError |
| 4 | +from django.test import TestCase |
| 5 | + |
| 6 | +from appointment.models import Service, Appointment, AppointmentRequest |
| 7 | +from appointment.utils import Utility |
| 8 | + |
| 9 | + |
| 10 | +class AppointmentModelTestCase(TestCase): |
| 11 | + def setUp(self): |
| 12 | + self.user_model = Utility.get_user_model() |
| 13 | + self.user = self.user_model.objects.create_user(first_name="Tester", |
| 14 | + |
| 15 | + username="test_user", password="Kfdqi3!?n") |
| 16 | + self.service = Service.objects.create(name="Test Service", duration=timedelta(hours=1), price=100) |
| 17 | + self.ar = AppointmentRequest.objects.create(date=date.today(), start_time=time(9, 0), end_time=time(10, 0), |
| 18 | + service=self.service) |
| 19 | + self.appointment = Appointment.objects.create(client=self.user, appointment_request=self.ar, |
| 20 | + phone="1234567890", address="Some City, Some State") |
| 21 | + |
| 22 | + # Test appointment creation |
| 23 | + def test_appointment_creation(self): |
| 24 | + appointment = Appointment.objects.get(appointment_request=self.ar) |
| 25 | + self.assertIsNotNone(appointment) |
| 26 | + self.assertEqual(appointment.client, self.user) |
| 27 | + self.assertEqual(appointment.phone, "1234567890") |
| 28 | + self.assertEqual(appointment.address, "Some City, Some State") |
| 29 | + |
| 30 | + # Test str representation |
| 31 | + def test_str_representation(self): |
| 32 | + expected_str = f"{self.user} - {self.ar.start_time.strftime('%Y-%m-%d %H:%M')} to " \ |
| 33 | + f"{self.ar.end_time.strftime('%Y-%m-%d %H:%M')}" |
| 34 | + self.assertEqual(str(self.appointment), expected_str) |
| 35 | + |
| 36 | + # Test start time |
| 37 | + def test_get_start_time(self): |
| 38 | + expected_start_time = datetime.combine(self.ar.get_date(), |
| 39 | + self.ar.get_start_time()) |
| 40 | + self.assertEqual(self.appointment.get_start_time(), expected_start_time) |
| 41 | + |
| 42 | + # Test end time |
| 43 | + def test_get_end_time(self): |
| 44 | + expected_end_time = datetime.combine(self.ar.get_date(), |
| 45 | + self.ar.get_end_time()) |
| 46 | + self.assertEqual(self.appointment.get_end_time(), expected_end_time) |
| 47 | + |
| 48 | + # Test service name retrieval |
| 49 | + def test_get_service_name(self): |
| 50 | + self.assertEqual(self.appointment.get_service_name(), "Test Service") |
| 51 | + |
| 52 | + # Test service price retrieval |
| 53 | + def test_get_service_price(self): |
| 54 | + self.assertEqual(self.appointment.get_service_price(), 100) |
| 55 | + |
| 56 | + # Test phone retrieval |
| 57 | + def test_get_phone(self): |
| 58 | + self.assertEqual(self.appointment.get_phone(), "1234567890") |
| 59 | + |
| 60 | + # Test address retrieval |
| 61 | + def test_get_address(self): |
| 62 | + self.assertEqual(self.appointment.get_address(), "Some City, Some State") |
| 63 | + |
| 64 | + # Test reminder retrieval |
| 65 | + def test_get_want_reminder(self): |
| 66 | + self.assertFalse(self.appointment.get_want_reminder()) |
| 67 | + |
| 68 | + # Test additional info retrieval |
| 69 | + def test_get_additional_info(self): |
| 70 | + self.assertIsNone(self.appointment.get_additional_info()) |
| 71 | + |
| 72 | + # Test paid status retrieval |
| 73 | + def test_is_paid(self): |
| 74 | + self.assertFalse(self.appointment.is_paid()) |
| 75 | + |
| 76 | + # Test appointment amount to pay |
| 77 | + def test_get_appointment_amount_to_pay(self): |
| 78 | + self.assertEqual(self.appointment.get_appointment_amount_to_pay(), 100) |
| 79 | + |
| 80 | + # Test appointment currency retrieval |
| 81 | + def test_get_appointment_currency(self): |
| 82 | + self.assertEqual(self.appointment.get_appointment_currency(), "USD") |
| 83 | + |
| 84 | + # Test appointment ID request retrieval |
| 85 | + def test_get_appointment_id_request(self): |
| 86 | + self.assertIsNotNone(self.appointment.get_appointment_id_request()) |
| 87 | + |
| 88 | + # Test created at retrieval |
| 89 | + def test_get_created_at(self): |
| 90 | + self.assertIsNotNone(self.appointment.get_created_at()) |
| 91 | + |
| 92 | + # Test updated at retrieval |
| 93 | + def test_get_updated_at(self): |
| 94 | + self.assertIsNotNone(self.appointment.get_updated_at()) |
| 95 | + |
| 96 | + # Test paid status setting |
| 97 | + def test_set_appointment_paid_status(self): |
| 98 | + self.appointment.set_appointment_paid_status(True) |
| 99 | + self.assertTrue(self.appointment.is_paid()) |
| 100 | + self.appointment.set_appointment_paid_status(False) |
| 101 | + self.assertFalse(self.appointment.is_paid()) |
| 102 | + |
| 103 | + # Test invalid phone number |
| 104 | + def test_invalid_phone(self): |
| 105 | + self.appointment.phone = "1234" # Invalid phone number |
| 106 | + with self.assertRaises(ValidationError): |
| 107 | + self.appointment.full_clean() |
| 108 | + |
| 109 | + # Test service down payment retrieval |
| 110 | + def test_get_service_down_payment(self): |
| 111 | + self.assertEqual(self.appointment.get_service_down_payment(), self.service.get_down_payment()) |
| 112 | + |
| 113 | + # Test service description retrieval |
| 114 | + def test_get_service_description(self): |
| 115 | + self.assertEqual(self.appointment.get_service_description(), self.service.get_description()) |
| 116 | + |
| 117 | + # Test appointment date retrieval |
| 118 | + def test_get_appointment_date(self): |
| 119 | + self.assertEqual(self.appointment.get_appointment_date(), self.ar.get_date()) |
| 120 | + |
| 121 | + # Test save function with down payment type |
| 122 | + def test_save_with_down_payment(self): |
| 123 | + self.ar.payment_type = 'down' |
| 124 | + self.ar.save() |
| 125 | + self.appointment.save() |
| 126 | + self.assertEqual(self.appointment.get_service_down_payment(), self.service.get_down_payment()) |
0 commit comments