def test_masters_index_2(self, mock_render, mock_decorators): from django.db import connection from django.db import reset_queries mock_decorators.login_required = lambda x: x mock_decorators.permission_required = lambda *args, **kwargs: lambda func: func fake = faker.Faker() organization = OrganizationFactory(cohort=self.cohort, reference=fake.random_int(min=10, max=100)) # speciality = SpecialityFactory(cohort=self.cohort) master = MasterFactory(organization=organization) master2 = MasterFactory() request_factory = RequestFactory() request = request_factory.get(reverse('internships_masters', kwargs={ 'cohort_id': self.cohort.id })) request.user = mock.Mock() from internship.views.master import internships_masters reset_queries() self.assertEqual(len(connection.queries), 0) internships_masters(request, self.cohort.id) self.assertTrue(mock_render.called) request, template, context = mock_render.call_args[0] self.assertEqual(context['cohort'], self.cohort) masters_count = context['all_masters'].count() # print("Queries", len(connection.queries)) # from pprint import pprint as pp # pp(connection.queries) from internship.models.internship_master import InternshipMaster self.assertEqual(masters_count, InternshipMaster.objects.filter(organization=organization).count()) specs = InternshipMaster.objects.filter(organization=organization)\ .distinct('speciality')\ .values_list('speciality', flat=True)\ .order_by('speciality') self.assertEqual(len(context['all_spec']), specs.count()) self.assertEqual(set(context['all_spec']), set(list(specs)))
def test_masters_index_bad_masters(self): fake = faker.Faker() master = MasterFactory(organization=OrganizationFactory(reference=fake.random_int(min=10, max=100))) url = reverse('internships_masters', kwargs={ 'cohort_id': self.cohort.id }) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'internships_masters.html') masters = response.context['all_masters'] self.assertEqual(masters.count(), 0) url = reverse('internships_masters', kwargs={ 'cohort_id': master.organization.cohort.id, }) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'internships_masters.html') masters = response.context['all_masters'] self.assertEqual(masters.count(), 1) self.assertEqual(masters.first(), master)
def test_get_by_id(self): master = MasterFactory() persisted_master = internship_master.get_by_id(master.id) self.assertEquals(master.id, persisted_master.id) nonexistent_master = internship_master.get_by_id(0) self.assertIsNone(nonexistent_master)
def test_create_user_account_for_internship_master_with_no_birth_date( self): master = MasterFactory(person=PersonFactory(birth_date=None)) url = reverse('create_accounts', kwargs={'cohort_id': self.cohort.pk}) response = self.client.post(url, data={'selected_master': [master.pk]}) messages_list = [msg for msg in response.wsgi_request._messages] self.assertEqual(messages_list[0].level_tag, "error") self.assertIn(str(master.person), messages_list[0].message)
def test_create_user_account_for_internship_master(self, mock_ldap_api_call): master = MasterFactory(person=PersonFactory(birth_date=date.today())) url = reverse('create_accounts', kwargs={'cohort_id': self.cohort.pk}) response = self.client.post(url, data={'selected_master': [master.pk]}) messages_list = [msg for msg in response.wsgi_request._messages] self.assertEqual(messages_list[0].level_tag, "success") self.assertIn(str(master.person), messages_list[0].message)
def test_search_master_by_name_unaccent(self): organization = OrganizationFactory(cohort=self.cohort) master_with_accent = MasterFactory(person__last_name='Éçàüî') MasterAllocationFactory(organization=organization, master=master_with_accent) url = reverse('internships_masters', kwargs={ 'cohort_id': self.cohort.id, }) query_string = '?name={}'.format("Éçàüî") response = self.client.get("{}{}".format(url, query_string)) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['allocations'].object_list[0].master, master_with_accent)
def test_delete_master_only_delete_allocations(self): master_test = MasterFactory( person__source=person_source_type.INTERNSHIP) # master and person exists before delete self.assertTrue( InternshipMaster.objects.filter(pk=master_test.pk).exists()) self.assertTrue( Person.objects.filter(pk=master_test.person.pk).exists()) fake = faker.Faker() organization_test = OrganizationFactory(cohort=self.cohort, reference=fake.random_int( min=10, max=100)) allocation = MasterAllocationFactory(master=master_test, organization=organization_test) url = reverse('master_delete', kwargs={ 'cohort_id': self.cohort.id, 'master_id': master_test.id }) allocations = master_allocation.find_by_master(self.cohort, master_test) self.assertIn(allocation, allocations) response = self.client.get(url) self.assertRedirects( response, reverse('internships_masters', kwargs={'cohort_id': self.cohort.id})) allocations = master_allocation.find_by_master(self.cohort, master_test) self.assertNotIn(allocation, allocations) # master and person have NOT been deleted self.assertTrue( InternshipMaster.objects.filter(pk=master_test.pk).exists()) self.assertTrue( Person.objects.filter(pk=master_test.person.pk).exists())
def setUp(self): self.master = MasterFactory()
def setUpTestData(cls): cls.master = MasterFactory()