Graphene: [Question] testing, errors and debuging

Created on 22 Nov 2017  路  2Comments  路  Source: graphql-python/graphene

Hi there; I'm using graphene for a graphql application and I got to an unexpected scenario while doing tests; I haven't been able to find out if it's a bug or something I'm doing wrong.

Scenario:
mutations.py

class CreatePerson(relay.ClientIDMutation):
    class Input:
        name = graphene.String()

    ok = graphene.Boolean() 
    data = graphene.Field(Person)
    errors = graphene.List(graphene.String)

    @classmethod
    def mutate_and_get_payload(cls, root, info, **input):
        name = input.get('name')

        ok, data, errors = myservice.create_person(name=name)
        return CreatePerson(ok=ok, data=data, errors=errors)

myservice.py

def create_person(name):
    already_created = myrepository.get_person_by_name(name)
    if already_created:
        return False, {}, ['ERROR_NOT_UNIQUE']

    person = Person(name=name)
    data = myrepository.create_person(person)
    return True, data, []

tests/mutations/test_person.py

def test_create_person_ko():                                         
    # given                                                           
    person = factories.create_person()                              
    client = Client(schema)                                           
    variables = { "input": {                                          
                    "name": person.name #  repeated name so it should fail
                  }                                                   
                }                                                     
    result = client.execute('''                                       
        mutation CreatePerson($input: CreatePersonInput!) {         
            createPerson(input: $input) {                            
                ok                                                    
            }                                                         
        }                                                             
    ''', variable_values=variables)                                   

    # then                                                            
    assert result == {"data": {"createPerson": {"ok": False}}}       

The first issue is that this test is unexpectedly green, although the result of my mutation should include a list with the errors. And related to this: I tried to debug the code, so I put an import pdb; pdb.set_trace() in the mutation (before the return) but it didn't stop there; then I wrote a simple print("sth") and again, this wasn't displayed in the shell.

My questions (I see them related, but I can open different issues if they are not):

  • is this grafene.test.Client mocking everything and ignoring print/pdb?
  • is it expected in this code not to receive the error list?

Sorry for the textwall, I hope at least it's clear. Thanks for your time!
Thank you for your time!

Most helpful comment

is it expected in this code not to receive the error list?

The query is not asking for the errors, so I guess that's why they are not in the response...

    result = client.execute('''
        mutation CreatePerson($input: CreatePersonInput!) {
            createPerson(input: $input) {
                ok
                errors  # Ask for the errors too
            }
        }
    ''', variable_values=variables)

Can you try adding errors to the query and see if they appear in result?

All 2 comments

is it expected in this code not to receive the error list?

The query is not asking for the errors, so I guess that's why they are not in the response...

    result = client.execute('''
        mutation CreatePerson($input: CreatePersonInput!) {
            createPerson(input: $input) {
                ok
                errors  # Ask for the errors too
            }
        }
    ''', variable_values=variables)

Can you try adding errors to the query and see if they appear in result?

Holy cow! That was the problem!! I was just not seeing this.
Thank you for your help!!!

Was this page helpful?
0 / 5 - 0 ratings