Newer
Older
import unittest
from os import environ
from app import app
from flask_testing import LiveServerTestCase
from json import loads
from database.database import db_session
from database.models import User
class GraphQlTests(LiveServerTestCase):
def create_app(self):
app.config["TESTING"] = True
return app
self.client = GraphQLClient(self.get_server_url()+"/graphql")
# clean up tables
users = User.query.filter_by(username="graphqluser").delete()
db_session.commit()
username = "graphqluser"
password = "randompassword"
email = "user@example.com"
querystring = '''
mutation{{
createUser(
username: "{0}",
password: "{1}",
email: "{2}"){{
user{{
username,
email}}}}}}
'''.format(username, password, email).strip()
result = loads(self.client.execute(querystring))
self.assertNotIn("error", result, "Creating user failed due to an error")
self.assertIn("data", result)
self.assertIn("createUser", result["data"])
self.assertIn("user", result["data"]["createUser"])
user = result["data"]["createUser"]["user"]
self.assertEqual(user["username"], username)
self.assertEqual(user["email"], email)
querystring = '''
{{
getUser(username: "{0}"){{
username,
email
}}
}}
'''.format(username).strip()
result = loads(self.client.execute(querystring))
self.assertIn("data", result)
self.assertIn("getUser", result["data"])
user = result["data"]["getUser"]
self.assertEqual(user["username"], username)
self.assertEqual(user["email"], email)