I have this model:
class Manager(models.Model):
"""
This model defines a manager. If a User has a Manager record, the User is a manager.
"""
user = models.OneToOneField('users.User', on_delete=models.PROTECT)
static_name = models.CharField(max_length=100)
def __str__(self):
return 'Manager {}'.format(self.static_name)
class Meta:
ordering = ('static_name',)
default_permissions = ['index', 'manage']
def save(self, *args, **kwargs):
"""
The save method is overwritten to perform a safety check: a patient should never be made a manager.
"""
if self.user.is_patient or self.user.has_coupled_patient:
raise ValueError('Een patiënt mag geen manager gemaakt worden.')
super(Manager, self).save(*args, **kwargs)
With these tests:
class ManagerTest(TestCase):
def setUp(self):
user1 = User.objects.create(first_name='User', last_name='One', email='user@user.com', password='test')
user2 = User.objects.create(first_name='User', last_name='with Patient', email='user2@user.com', password='test')
pharmacy = Pharmacy.objects.create(vidivici_id='2511', name='Pharmacy 1', address='Test', opening_hours='Test')
couple_request1 = UserPatientCoupleRequest.objects.create(user=user2, couple_last_name='with Patient',
couple_sex='M', couple_DOB='1980-01-01',
couple_BSN=123456789, couple_pharmacy=pharmacy)
patient1 = Patient.objects.create(vidivici_id='25110001', first_name='Patient', last_name='One', sex='M',
DOB='1980-01-01', BSN=123456789, pharmacy=pharmacy)
def test_manager_str(self):
user = User.objects.get(email='user@user.com')
manager = Manager.objects.create(user=user, static_name='Manager 1')
self.assertEqual(str(manager), 'Manager {}'.format(manager.static_name))
def test_manager_save_with_patient(self):
user_with_patient = User.objects.get(email='user2@user.com')
patient = Patient.objects.get(vidivici_id='25110001')
couple_request = UserPatientCoupleRequest.objects.get(user=user_with_patient)
UserPatient.objects.create(user=user_with_patient, patient=patient, couple_request=couple_request)
with self.assertRaises(ValueError):
Manager.objects.create(user=user_with_patient, static_name=user_with_patient.get_full_name())
The tests both pass, but coverage still tells me the __str__
and save
methods are not covered. What am I doing wrong? I'm using Django 1.11b1 on Python 3.6. I'm a beginner at testing so it's probably something obvious.
Aucun commentaire:
Enregistrer un commentaire