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_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)
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)
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_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)
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_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_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.'])
def setUp(self): self.subject = DepartmentFactory.create()