Exemple #1
0
    def test_mutate__objects_exists__updates(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class BatchPatchDogMutation(DjangoBatchPatchMutation):
            class Meta:
                model = Dog

        class Mutations(graphene.ObjectType):
            batch_patch_dog = BatchPatchDogMutation.Field()

        dog_1 = DogFactory.create()
        dog_2 = DogFactory.create()
        user = UserFactory.create()

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation BatchPatchDog(
                $input: [BatchPatchDogInput]! 
            ){
                batchPatchDog(input: $input){
                    dogs{
                        id
                        name
                    }
                }
            }
        """

        result = schema.execute(mutation,
                                variables={
                                    "input": [
                                        {
                                            "id":
                                            to_global_id("DogNode", dog_1.id),
                                            "name": "New name 1",
                                        },
                                        {
                                            "id":
                                            to_global_id("DogNode", dog_2.id),
                                            "name": "New name 2",
                                        },
                                    ]
                                },
                                context=Dict(user=user))
        self.assertIsNone(result.errors)

        dogs = result.data["batchPatchDog"]["dogs"]
        dog_1_result = dogs[0]
        dog_2_result = dogs[1]
        self.assertEqual("New name 1", dog_1_result["name"])
        self.assertEqual("New name 2", dog_2_result["name"])

        dog_1.refresh_from_db()
        dog_2.refresh_from_db()
        self.assertEqual("New name 1", dog_1.name)
        self.assertEqual("New name 2", dog_2.name)
    def test_field_types__specified__overrides_field_type(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class UpdateDogMutation(DjangoUpdateMutation):
            class Meta:
                model = Dog
                field_types = {"tag": graphene.Int()}

            @classmethod
            def handle_tag(self, value, *args, **kwargs):
                return f"Dog-{value}"

        class Mutations(graphene.ObjectType):
            update_dog = UpdateDogMutation.Field()

        dog = DogFactory.create()
        user = UserFactory.create()
        schema = Schema(mutation=Mutations)
        mutation = """
            mutation UpdateDog(
                $id: ID!,
                $input: UpdateDogInput! 
            ){
                updateDog(id: $id, input: $input){
                    dog{
                        id
                    }
                }
            }
        """
        # Result with a string in the tag field should fail now
        result = schema.execute(mutation,
                                variables={
                                    "id": to_global_id("DogNode", dog.id),
                                    "input": {
                                        "name": "Sparky",
                                        "tag": "not-an-int",
                                        "breed": "HUSKY",
                                        "owner":
                                        to_global_id("UserNode", user.id)
                                    }
                                },
                                context=Dict(user=user))
        self.assertEqual(len(result.errors), 1)

        result = schema.execute(mutation,
                                variables={
                                    "id": to_global_id("DogNode", dog.id),
                                    "input": {
                                        "name": "Sparky",
                                        "breed": "HUSKY",
                                        "tag": 25,
                                        "owner":
                                        to_global_id("UserNode", user.id)
                                    }
                                },
                                context=Dict(user=user))
        self.assertIsNone(result.errors)
    def test_many_to_many_extras__add_extra_by_input__adds_by_input(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class CreateCatMutation(DjangoCreateMutation):
            class Meta:
                model = Cat

        class UpdateDogMutation(DjangoUpdateMutation):
            class Meta:
                model = Dog
                many_to_many_extras = {"enemies": {"exact": {"type": "CreateCatInput"}}}

        class Mutations(graphene.ObjectType):
            create_cat = CreateCatMutation.Field()
            update_dog = UpdateDogMutation.Field()

        dog = DogFactory.create()
        user = UserFactory.create()

        # Create some enemies
        cats = CatFactory.create_batch(5)
        self.assertEqual(dog.enemies.all().count(), 0)

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation UpdateDog(
                $id: ID!,
                $input: UpdateDogInput! 
            ){
                updateDog(id: $id, input: $input){
                    dog{
                        id
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "id": to_global_id("DogNode", dog.id),
                "input": {
                    "name": "Sparky",
                    "tag": "tag",
                    "breed": "HUSKY",
                    "owner": to_global_id("UserNode", user.id),
                    "enemies": [
                        {"name": cat.name, "owner": cat.owner.id} for cat in cats
                    ],
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        dog.refresh_from_db()
        self.assertEqual(dog.enemies.all().count(), 5)
    def test_many_to_many_extras__type_auto__makes_it_possible_to_add_new_full_objects(
        self, ):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class UpdateDogMutation(DjangoUpdateMutation):
            class Meta:
                model = Dog
                many_to_many_extras = {"enemies": {"add": {"type": "auto"}}}

        class Mutations(graphene.ObjectType):
            update_dog = UpdateDogMutation.Field()

        dog = DogFactory.create()
        user = UserFactory.create()

        self.assertEqual(dog.enemies.all().count(), 0)

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation UpdateDog(
                $id: ID!,
                $input: UpdateDogInput! 
            ){
                updateDog(id: $id, input: $input){
                    dog{
                        id
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "id": to_global_id("DogNode", dog.id),
                "input": {
                    "name":
                    "Sparky",
                    "tag":
                    "tag",
                    "breed":
                    "HUSKY",
                    "owner":
                    to_global_id("UserNode", user.id),
                    "enemiesAdd": [{
                        "name": "Meowington",
                        "owner": to_global_id("UserNode", user.id),
                    }],
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        dog.refresh_from_db()
        self.assertEqual(dog.enemies.all().count(), 1)
    def test_auto_type__with_proper_setup__generates_new_auto_type(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class UpdateDogMutation(DjangoUpdateMutation):
            class Meta:
                model = Dog
                foreign_key_extras = {
                    "owner": {
                        "type": "auto",
                        "exclude_fields": ["password"]
                    }
                }

        class Mutations(graphene.ObjectType):
            update_dog = UpdateDogMutation.Field()

        dog = DogFactory.create()
        user = UserFactory.create()

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation UpdateDog(
                $id: ID!,
                $input: UpdateDogInput! 
            ){
                updateDog(id: $id, input: $input){
                    dog{
                        id
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "id": to_global_id("DogNode", dog.id),
                "input": {
                    "name": "Sparky",
                    "tag": "tag",
                    "breed": "HUSKY",
                    "owner": {
                        "username": "******",
                        "email": "*****@*****.**",
                        "firstName": "Tormod",
                        "lastName": "Haugland",
                    },
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        dog.refresh_from_db()
        self.assertEqual("*****@*****.**", dog.owner.email)
Exemple #6
0
    def test__filter_update__updates(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class FilterUpdateDogMutation(DjangoFilterUpdateMutation):
            class Meta:
                model = Dog
                filter_fields = ("name", "name__startswith")

        class Mutations(graphene.ObjectType):
            filter_update_dogs = FilterUpdateDogMutation.Field()

        dog = DogFactory.create(name="Simen")
        user = UserFactory.create()

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation FilterUpdateDog(
                $filter: FilterUpdateDogFilterInput!,
                $data: FilterUpdateDogDataInput! 
            ){
                filterUpdateDogs(filter: $filter, data: $data){
                    updatedCount
                    updatedObjects{
                        id
                        name
                        tag
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "filter": {
                    "name_Startswith": "Sim"
                },
                "data": {
                    "tag": "New tag"
                },
            },
            context=Dict(user=user),
        )

        self.assertIsNone(result.errors)
        self.assertEqual(
            1, len(result.data["filterUpdateDogs"]["updatedObjects"]))

        dog_result = result.data["filterUpdateDogs"]["updatedObjects"][0]
        self.assertEqual("New tag", dog_result["tag"])

        dog.refresh_from_db()
        self.assertEqual("New tag", dog.tag)
Exemple #7
0
    def test_many_to_many_extras__calling_exact_with_empty_list__resets_relation(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class PatchDogMutation(DjangoPatchMutation):
            class Meta:
                model = Dog
                many_to_many_extras = {"enemies": {"exact": {"type": "ID"}}}

        class Mutations(graphene.ObjectType):
            patch_dog = PatchDogMutation.Field()

        dog = DogFactory.create()
        user = UserFactory.create()

        # Create some enemies
        cats = CatFactory.create_batch(5)
        dog.enemies.set(cats)
        self.assertEqual(dog.enemies.all().count(), 5)

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation PatchDog(
                $id: ID!,
                $input: PatchDogInput! 
            ){
                patchDog(id: $id, input: $input){
                    dog{
                        id
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "id": to_global_id("DogNode", dog.id),
                "input": {
                    "name": "Sparky",
                    "tag": "tag",
                    "breed": "HUSKY",
                    "owner": to_global_id("UserNode", user.id),
                    "enemies": [],
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        dog.refresh_from_db()
        self.assertEqual(dog.enemies.all().count(), 0)
    def test_many_to_many_extras__calling_exact_with_empty_list__resets_relation(
            self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class UpdateCatMutation(DjangoUpdateMutation):
            class Meta:
                model = Cat
                many_to_many_extras = {"enemies": {"exact": {"type": "ID"}}}

        class Mutations(graphene.ObjectType):
            update_cat = UpdateCatMutation.Field()

        cat = CatFactory.create()
        user = UserFactory.create()

        # Create some enemies
        dog = DogFactory.create_batch(5)
        cat.enemies.set(dog)
        self.assertEqual(cat.enemies.all().count(), 5)

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation UpdateCat(
                $id: ID!,
                $input: UpdateCatInput! 
            ){
                updateCat(id: $id, input: $input){
                    cat{
                        id
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "id": to_global_id("CatNode", cat.id),
                "input": {
                    "name": "Garfield",
                    "owner": to_global_id("UserNode", user.id),
                    "enemies": [],
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        cat.refresh_from_db()
        self.assertEqual(cat.enemies.all().count(), 0)
Exemple #9
0
    def test_many_to_many_extras__add_extra_by_id__adds_by_id(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class PatchCatMutation(DjangoPatchMutation):
            class Meta:
                model = Cat
                many_to_many_extras = {"enemies": {"add": {"type": "ID"}}}

        class Mutations(graphene.ObjectType):
            patch_cat = PatchCatMutation.Field()

        cat = CatFactory.create()
        user = UserFactory.create()

        # Create some enemies
        dog = DogFactory.create_batch(5)
        self.assertEqual(cat.enemies.all().count(), 0)

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation PatchCat(
                $id: ID!,
                $input: PatchCatInput! 
            ){
                patchCat(id: $id, input: $input){
                    cat{
                        id
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "id": to_global_id("CatNode", cat.id),
                "input": {
                    "name": "Garfield",
                    "owner": to_global_id("UserNode", user.id),
                    "enemiesAdd": [dog.id for dog in dog],
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        cat.refresh_from_db()
        self.assertEqual(cat.enemies.all().count(), 5)
    def test_default_setup__adding_resource_by_id__adds_resource(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class UpdateCatMutation(DjangoUpdateMutation):
            class Meta:
                model = Cat

        class Mutations(graphene.ObjectType):
            update_cat = UpdateCatMutation.Field()

        cat = CatFactory.create()
        user = UserFactory.create()
        dog = DogFactory.create()

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation UpdateCat(
                $id: ID!,
                $input: UpdateCatInput! 
            ){
                updateCat(id: $id, input: $input){
                    cat{
                        id
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "id": to_global_id("CatNode", cat.id),
                "input": {
                    "name": "Garfield",
                    "owner": to_global_id("UserNode", user.id),
                    "enemies": [to_global_id("DogNode", dog.id)],
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        cat.refresh_from_db()
        self.assertEqual(cat.enemies.all().count(), 1)
    def test_custom_field__separate_from_model_fields__adds_new_field_which_can_be_handled(
            self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class CreateDogMutation(DjangoCreateMutation):
            class Meta:
                model = Dog
                custom_fields = {"bark": graphene.Boolean()}

            @classmethod
            def before_save(cls, root, info, input, obj):
                if input.get("bark"):
                    obj.bark_count += 1
                return obj

        class Mutations(graphene.ObjectType):
            create_dog = CreateDogMutation.Field()

        dog = DogFactory.create()
        user = UserFactory.create()

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation CreateDog(
                $input: CreateDogInput! 
            ){
                createDog(input: $input){
                    dog{
                        id
                        barkCount
                    }
                }
            }
        """

        self.assertEqual(0, dog.bark_count)
        result = schema.execute(
            mutation,
            variables={
                "input": {
                    "name": "Sparky",
                    "tag": "tag",
                    "breed": "HUSKY",
                    "bark": True,
                    "owner": to_global_id("UserNode", user.id)
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        self.assertEqual(1, result.data["createDog"]["dog"]["barkCount"])

        result = schema.execute(
            mutation,
            variables={
                "input": {
                    "name": "Sparky",
                    "tag": "tag-2",
                    "breed": "HUSKY",
                    "owner": to_global_id("UserNode", user.id)
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)

        self.assertEqual(0, result.data["createDog"]["dog"]["barkCount"])
    def test__reverse_one_to_one_exists__updates_specified_fields(self):
        # This registers the UserNode type
        # noinspection PyUnresolvedReferences
        from .schema import UserNode

        class UpdateDogRegistrationMutation(DjangoUpdateMutation):
            class Meta:
                model = DogRegistration
                one_to_one_extras = {"dog": {"type": "auto"}}

        class Mutations(graphene.ObjectType):
            update_dog_registration = UpdateDogRegistrationMutation.Field()

        user = UserFactory.create()
        dog = DogFactory.create(breed="HUSKY")
        dog_registration = DogRegistration.objects.create(
            dog=dog, registration_number="1234")

        schema = Schema(mutation=Mutations)
        mutation = """
            mutation UpdateDogRegistration(
                $id: ID!,
                $input: UpdateDogRegistrationInput!
            ){
                updateDogRegistration(id: $id, input: $input){
                    dogRegistration{
                        id
                        registrationNumber
                        dog{
                            id
                            breed
                        }
                    }
                }
            }
        """

        result = schema.execute(
            mutation,
            variables={
                "id": to_global_id("DogRegistrationNode", dog_registration.id),
                "input": {
                    "registrationNumber": dog_registration.registration_number,
                    "dog": {
                        "name": dog.name,
                        "breed": "LABRADOR",
                        "tag": dog.tag,
                        "owner": to_global_id("UserNode", dog.owner.id),
                    },
                },
            },
            context=Dict(user=user),
        )
        self.assertIsNone(result.errors)
        data = Dict(result.data)
        self.assertEqual("LABRADOR",
                         data.updateDogRegistration.dogRegistration.dog.breed)

        # Load from database
        dog_registration.refresh_from_db()
        dog.refresh_from_db()
        self.assertEqual(dog.breed, "LABRADOR")