示例#1
0
    def test_update_many_to_many_nested_with_through_success(self):
        role = RoleSerializer(data={"permission": "high", "name": "admin"})
        role.is_valid(raise_exception=True)
        role.save()
        employee = EmployeeSerializer(data={
            "user": {
                "username": "******"
            },
            "status": "Some status"
        })
        employee.is_valid(raise_exception=True)
        employee.save()

        data = copy(role.data)

        data.update({
            "employees": [{
                "employee_id": employee.instance.id,
                "role_id": role.instance.id,
                "name": "Some Name"
            }]
        })

        employee_role = RoleSerializer(instance=role.instance, data=data)
        employee_role.is_valid(raise_exception=True)
        employee_role.save()

        self.assertIsNotNone(employee_role.instance)

        data = copy(employee_role.data)
        self.assertEqual(len(data.get('employees')), 1)
示例#2
0
 def test_create_direct_nested_success(self):
     employee = EmployeeSerializer(
         data={"user": {"username": "******"},
               "status": "Some status"}
     )
     employee.is_valid(raise_exception=True)
     employee.save()
     self.assertIsNotNone(employee.instance)
     self.assertIsNotNone(employee.instance.user)
示例#3
0
    def test_create_direct_nested_fail(self):
        employee = EmployeeSerializer(
            data={"user": {"username": "******",
                           "is_active": False},
                  "status": "Some status"}
        )
        employee.is_valid(raise_exception=True)
        with self.assertRaises(ValidationError) as verror:
            employee.save()

        error = verror.exception
        self.assertIn("user", error.detail)
        self.assertEqual(
            error.detail["user"],
            {'is_active': [ErrorDetail(string="User should be active", code='invalid')]}
        )
示例#4
0
    def test_update_direct_nested_empty_success(self):
        employee = EmployeeSerializer(data={
            "user": {
                "username": "******"
            },
            "status": "Some status"
        })
        employee.is_valid(raise_exception=True)
        employee.save()
        self.assertIsNotNone(employee.instance)
        self.assertIsNotNone(employee.instance.user)

        data = copy(employee.data)
        self.assertIsNotNone(data.get('user'))
        self.assertEqual(data['user'].get('username'), "Some name")

        data["user"] = None

        updated_employee = EmployeeSerializer(instance=employee.instance,
                                              data=data)
        updated_employee.is_valid(raise_exception=True)
        updated_employee.save()
        updated_employee.instance.refresh_from_db()
        self.assertIsNotNone(updated_employee.instance)
        self.assertIsNone(updated_employee.instance.user)