<filename>tests/unit/graphql/test_userQL.py
import pytest
from app.modules.graphql import graphql


def test_list_user(db_user):
    users = graphql.execute('''
    query {
      users {
            name
        }
    }
    ''').data['users']
    assert users == [{"name": "User1"}, {"name": "User2"}]


def test_query_user(db_user):
    user = graphql.execute(
        '''
    query {
      user(email: "<EMAIL>") {
            name
        }
    }
    '''
    ).data['user']
    assert user == {"name": "User1"}


def test_query_user_by_id(db_user):
    id = graphql.execute(
        '''
    query {
        user(email: "<EMAIL>"){
            id
        }
    }'''
    ).data['user']['id']

    user = graphql.execute(
        '''
    query {
      user(id: "%s") {
            name
        }
    }
    ''' % id
    ).data['user']

    assert user == {"name": "User1"}


def test_query_user_not_found(db_user):
    user = graphql.execute(
        '''
    query {
      user(email: "") {
            name
        }
    }
    '''
    ).data['user']
    assert user == None

    user = graphql.execute('''
    query {
      user {
            name
        }
    }
    ''').data['user']
    assert user == None


def test_query_login(db_user):
    ok = graphql.execute(
        '''
    query {
        login(email: "<EMAIL>", password: "<PASSWORD>"){
            ok
        }
    }
    '''
    ).data['login']['ok']
    assert ok == True


def test_failed_query_login(db_user):
    ok = graphql.execute(
        '''
    query {
        login(email: "<EMAIL>", password: "<PASSWORD>"){
            ok
        }
    }
    '''
    ).data['login']['ok']
    assert ok == False


def test_create_query_delete_user(db):
    ok = graphql.execute(
        '''
    mutation {
        createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){
            ok
        }
    }'''
    ).data['createUser']['ok']
    assert ok == True

    name = graphql.execute(
        '''
    query {
      user(email: "<EMAIL>") {
            name
        }
    }
    '''
    ).data["user"]["name"]

    assert name == "123"

    ok = graphql.execute(
        '''
    mutation {
        deleteUser(email: "<EMAIL>"){
            ok
        }
    }
    '''
    ).data['deleteUser']['ok']
    assert ok == True


def test_create_failed(db):
    ok = graphql.execute(
        '''
    mutation {
        createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){
            ok
        }
    }'''
    ).data['createUser']['ok']
    assert ok == True

    ok = graphql.execute(
        '''
    mutation {
        createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){
            ok
        }
    }'''
    ).data['createUser']['ok']
    assert ok == False


def test_mutate_user(db_user):
    result = graphql.execute(
        '''
    mutation {
        mutateUser(userData: {email: "<EMAIL>", name: "123"}){
            user {
                name
            }
            ok
        }
    }'''
    ).data['mutateUser']
    ok = result['ok']
    assert ok == True
    name = result['user']['name']
    assert name == '123'


def test_mutate_user_role(db_user):
    result = graphql.execute(
        '''
    mutation {
        mutateUser(userData: {email: "<EMAIL>", role:"Nurse"}){
            user {
                role
            }
            ok
        }
    }'''
    ).data['mutateUser']
    ok = result['ok']
    assert ok == True
    role = result['user']['role']
    assert role == 'Nurse'


def test_failed_mutate_user_role(db_user):
    result = graphql.execute(
        '''
    mutation {
        mutateUser(userData: {email: "<EMAIL>", role:"test"}){
            user {
                role
            }
            ok
        }
    }'''
    ).data['mutateUser']
    ok = result['ok']
    assert ok == False


def test_mutate_user_password(db_user):
    result = graphql.execute(
        '''
    mutation {
        mutateUser(userData: {email: "<EMAIL>", password:"<PASSWORD>"}){
            ok
        }
    }'''
    ).data['mutateUser']
    ok = result['ok']
    assert ok == True


def test_delete_user_by_id(db_user):
    id = graphql.execute(
        '''
    query {
        user(email: "<EMAIL>"){
            id
        }
    }'''
    ).data['user']['id']

    ok = graphql.execute(
        '''
    mutation {
        deleteUser(id: "%s"){
            ok
        }
    }''' % id
    ).data['deleteUser']['ok']

    assert ok == True

    user = graphql.execute(
        '''
    query {
        user(email: "<EMAIL>"){
            id
        }
    }'''
    ).data['user']

    assert user == None


def test_delete_empty_user(db_user):
    ok = graphql.execute(
        '''
    mutation {
      deleteUser {
            ok
        }
    }
    '''
    ).data['deleteUser']['ok']
    assert ok == False

<filename>tests/unit/graphql/test_userQL.py
import pytest
from app.modules.graphql import graphql


def test_list_user(db_user):
    users = graphql.execute('''
    query {
      users {
            name
        }
    }
    ''').data['users']
    assert users == [{"name": "User1"}, {"name": "User2"}]


def test_query_user(db_user):
    user = graphql.execute(
        '''
    query {
      user(email: "<EMAIL>") {
            name
        }
    }
    '''
    ).data['user']
    assert user == {"name": "User1"}


def test_query_user_by_id(db_user):
    id = graphql.execute(
        '''
    query {
        user(email: "<EMAIL>"){
            id
        }
    }'''
    ).data['user']['id']

    user = graphql.execute(
        '''
    query {
      user(id: "%s") {
            name
        }
    }
    ''' % id
    ).data['user']

    assert user == {"name": "User1"}


def test_query_user_not_found(db_user):
    user = graphql.execute(
        '''
    query {
      user(email: "") {
            name
        }
    }
    '''
    ).data['user']
    assert user == None

    user = graphql.execute('''
    query {
      user {
            name
        }
    }
    ''').data['user']
    assert user == None


def test_query_login(db_user):
    ok = graphql.execute(
        '''
    query {
        login(email: "<EMAIL>", password: "<PASSWORD>"){
            ok
        }
    }
    '''
    ).data['login']['ok']
    assert ok == True


def test_failed_query_login(db_user):
    ok = graphql.execute(
        '''
    query {
        login(email: "<EMAIL>", password: "<PASSWORD>"){
            ok
        }
    }
    '''
    ).data['login']['ok']
    assert ok == False


def test_create_query_delete_user(db):
    ok = graphql.execute(
        '''
    mutation {
        createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){
            ok
        }
    }'''
    ).data['createUser']['ok']
    assert ok == True

    name = graphql.execute(
        '''
    query {
      user(email: "<EMAIL>") {
            name
        }
    }
    '''
    ).data["user"]["name"]

    assert name == "123"

    ok = graphql.execute(
        '''
    mutation {
        deleteUser(email: "<EMAIL>"){
            ok
        }
    }
    '''
    ).data['deleteUser']['ok']
    assert ok == True


def test_create_failed(db):
    ok = graphql.execute(
        '''
    mutation {
        createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){
            ok
        }
    }'''
    ).data['createUser']['ok']
    assert ok == True

    ok = graphql.execute(
        '''
    mutation {
        createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){
            ok
        }
    }'''
    ).data['createUser']['ok']
    assert ok == False


def test_mutate_user(db_user):
    result = graphql.execute(
        '''
    mutation {
        mutateUser(userData: {email: "<EMAIL>", name: "123"}){
            user {
                name
            }
            ok
        }
    }'''
    ).data['mutateUser']
    ok = result['ok']
    assert ok == True
    name = result['user']['name']
    assert name == '123'


def test_mutate_user_role(db_user):
    result = graphql.execute(
        '''
    mutation {
        mutateUser(userData: {email: "<EMAIL>", role:"Nurse"}){
            user {
                role
            }
            ok
        }
    }'''
    ).data['mutateUser']
    ok = result['ok']
    assert ok == True
    role = result['user']['role']
    assert role == 'Nurse'


def test_failed_mutate_user_role(db_user):
    result = graphql.execute(
        '''
    mutation {
        mutateUser(userData: {email: "<EMAIL>", role:"test"}){
            user {
                role
            }
            ok
        }
    }'''
    ).data['mutateUser']
    ok = result['ok']
    assert ok == False


def test_mutate_user_password(db_user):
    result = graphql.execute(
        '''
    mutation {
        mutateUser(userData: {email: "<EMAIL>", password:"<PASSWORD>"}){
            ok
        }
    }'''
    ).data['mutateUser']
    ok = result['ok']
    assert ok == True


def test_delete_user_by_id(db_user):
    id = graphql.execute(
        '''
    query {
        user(email: "<EMAIL>"){
            id
        }
    }'''
    ).data['user']['id']

    ok = graphql.execute(
        '''
    mutation {
        deleteUser(id: "%s"){
            ok
        }
    }''' % id
    ).data['deleteUser']['ok']

    assert ok == True

    user = graphql.execute(
        '''
    query {
        user(email: "<EMAIL>"){
            id
        }
    }'''
    ).data['user']

    assert user == None


def test_delete_empty_user(db_user):
    ok = graphql.execute(
        '''
    mutation {
      deleteUser {
            ok
        }
    }
    '''
    ).data['deleteUser']['ok']
    assert ok == False

query {       users {             name         }     } query {       user(email: "<EMAIL>") {             name         }     } query {         user(email: "<EMAIL>"){             id         }     } query {       user(id: "%s") {             name         }     } query {       user(email: "") {             name         }     } query {       user {             name         }     } query {         login(email: "<EMAIL>", password: "<PASSWORD>"){             ok         }     } query {         login(email: "<EMAIL>", password: "<PASSWORD>"){             ok         }     } mutation {         createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){             ok         }     } query {       user(email: "<EMAIL>") {             name         }     } mutation {         deleteUser(email: "<EMAIL>"){             ok         }     } mutation {         createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){             ok         }     } mutation {         createUser(userData: {email: "<EMAIL>", name: "123", password: "<PASSWORD>", role: "Nurse"}){             ok         }     } mutation {         mutateUser(userData: {email: "<EMAIL>", name: "123"}){             user {                 name             }             ok         }     } mutation {         mutateUser(userData: {email: "<EMAIL>", role:"Nurse"}){             user {                 role             }             ok         }     } mutation {         mutateUser(userData: {email: "<EMAIL>", role:"test"}){             user {                 role             }             ok         }     } mutation {         mutateUser(userData: {email: "<EMAIL>", password:"<PASSWORD>"}){             ok         }     } query {         user(email: "<EMAIL>"){             id         }     } mutation {         deleteUser(id: "%s"){             ok         }     } query {         user(email: "<EMAIL>"){             id         }     } mutation {       deleteUser {             ok         }     }