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)
Пример #2
0
    def test_name_must_be_unique(self):
        """
        name field must be unique
        """

        name = self.subject.name
        exception_a = 'UNIQUE constraint failed: staff_department.name'
        exception_b = 'column name is not unique'
        expected_exception_msg = '({0}|{1})'.format(exception_a, exception_b)

        with self.assertRaisesRegexp(IntegrityError, expected_exception_msg):
            DepartmentFactory.create(name=name)
    def test_read_department_fetch_all(self):
        # TODO: assert pagination
        # TODO: assert response schema

        queryset_length = 3
        DepartmentFactory.create_batch(queryset_length)

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

        self.assertEqual(response.status_code, status.HTTP_200_OK)
    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.'])
Пример #5
0
    def test_updated_at_is_equal_to_now(self, *kwargs):
        """
        updated_at field generated value must be equal to this moment
        """

        department = DepartmentFactory.create()

        self.assertEqual(department.updated_at, FROZEN_DATETIME_NOW)
Пример #6
0
    def test_updated_at_is_automatically_generated(self):
        """
        updated_at field is automatically generated at object creation
        """

        department = DepartmentFactory.create()

        self.assertIsNotNone(department.updated_at)
Пример #7
0
    def test_department_must_be_a_saved_instance(self):
        """
        department field must be a saved instance
        """

        self.subject.department = DepartmentFactory.build()
        expected_exception_msg = 'department.*This field cannot be null'

        with self.assertRaisesRegexp(ValidationError, expected_exception_msg):
            self.subject.full_clean()
    def test_delete_department_when_found(self):
        department = DepartmentFactory.create()

        view = DepartmentViewSet.as_view({'delete': 'destroy'})

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

        request = self.factory.delete(url, format='json')
        force_authenticate(request, user=self.user)
        response = view(request, pk=department.id)

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
Пример #9
0
    def test_updated_at_is_regenerated_on_updates(self):
        """
        updated_at field is automatically generated on updates to object
        """

        department = DepartmentFactory.create()
        first_updated_at = department.updated_at
        department.name = FuzzyText()
        department.save()

        self.assertNotEqual(first_updated_at, department.updated_at)
        self.assertIsNotNone(department.updated_at)  # just a double-check
    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_read_department_when_found(self):
        department = DepartmentFactory.create()

        view = DepartmentViewSet.as_view({'get': 'retrieve'})

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

        request = self.factory.get(url, format='json')
        force_authenticate(request, user=self.user)
        response = view(request, pk=department.id)
        response.render()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data.get('name'), department.name)
    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.'])
Пример #14
0
 def setUp(self):
     self.subject = DepartmentFactory.create()