def test_patch_department_when_found(self):
        department_a = DepartmentFactory.create()
        department_b = DepartmentFactory.stub()
        data = DepartmentSerializer(department_b).data

        view = DepartmentViewSet.as_view({'patch': 'partial_update'})
        url = reverse('department-list', args=[department_a.id])
        request = self.factory.patch(url, data, format='json')
        force_authenticate(request, user=self.user)
        response = view(request, pk=department_a.id)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
    def test_create_department_with_valid_data(self):
        department = DepartmentFactory.stub()
        data = DepartmentSerializer(department).data

        view = DepartmentViewSet.as_view({'post': 'create'})
        url = reverse('department-list')
        request = self.factory.post(url, data, format='json')
        force_authenticate(request, user=self.user)
        response = view(request)
        response.render()

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertIsNotNone(response.data.get('id'))
    def test_patch_department_when_not_found(self):
        department = DepartmentFactory.stub()
        data = DepartmentSerializer(department).data

        view = DepartmentViewSet.as_view({'patch': 'partial_update'})

        url = reverse('department-detail', args=[self.random_id])

        request = self.factory.patch(url, data, format='json')
        force_authenticate(request, user=self.user)
        response = view(request, pk=self.random_id)

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
    def test_create_department_with_long_name(self):
        department = DepartmentFactory.stub(with_long_name=True)
        data = DepartmentSerializer(department).data

        view = DepartmentViewSet.as_view({'post': 'create'})
        url = reverse('department-list')
        request = self.factory.post(url, data, format='json')
        force_authenticate(request, user=self.user)
        response = view(request)
        response.render()

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        self.assertEqual(
            response.data.get('name'),
            ['Ensure this field has no more than 250 characters.'])
    def test_create_department_with_duplicated_name(self):
        department_a = DepartmentFactory.create()
        department_b = DepartmentFactory.stub()
        department_b.name = department_a.name
        data = DepartmentSerializer(department_b).data

        view = DepartmentViewSet.as_view({'post': 'create'})
        url = reverse('department-list')
        request = self.factory.post(url, data, format='json')
        force_authenticate(request, user=self.user)
        response = view(request)
        response.render()

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        self.assertEqual(response.data.get('name'),
                         ['department with this name already exists.'])