Fixed test execution
authorLaurent Wouters <lwouters@cenotelie.fr>
Thu, 21 Jun 2018 17:39:47 +0200
changeset 1 3fc6832162c2
parent 0 0144c9b36d54
child 2 9bf18ded95ce
Fixed test execution
cubicweb_graphql/hooks.py
cubicweb_graphql/querier.py
test/test_graphql.py
--- a/cubicweb_graphql/hooks.py	Thu Jun 21 16:32:14 2018 +0200
+++ b/cubicweb_graphql/hooks.py	Thu Jun 21 17:39:47 2018 +0200
@@ -19,6 +19,8 @@
 
 from cubicweb.server import hook
 
+from querier import GraphQLQuerier
+
 
 class GraphQLStartupHook(hook.Hook):
     """
@@ -29,5 +31,4 @@
     events = ('server_startup',)
 
     def __call__(self):
-        repo = self.repo
-        pass
+        GraphQLQuerier.register(self.repo)
--- a/cubicweb_graphql/querier.py	Thu Jun 21 16:32:14 2018 +0200
+++ b/cubicweb_graphql/querier.py	Thu Jun 21 17:39:47 2018 +0200
@@ -28,6 +28,32 @@
     Main entry point for the execution of GraphQL queries
     """
 
+    @staticmethod
+    def register(repository):
+        """
+        Register a repository into this querier
+        :param repository: The repository to register
+        :return: Nothing
+        """
+        repository.graphql = _GraphQLQuerierPart(repository, repository.schema)
+
+    @staticmethod
+    def execute(connection, query, **kwargs):
+        """
+        Execute a GraphQL query
+        :param connection: The current connection
+        :param query: The query to execute
+        :param kwargs: The mapping of values for the named variables in the query
+        :return: The GraphQL execution result
+        """
+        return connection.repo.graphql.execute(connection, query, **kwargs)
+
+
+class _GraphQLQuerierPart(object):
+    """
+    Part of a GraphQL querier for a specific repository
+    """
+
     def __init__(self, repository, yams_schema):
         """
         Initialize this structure
--- a/test/test_graphql.py	Thu Jun 21 16:32:14 2018 +0200
+++ b/test/test_graphql.py	Thu Jun 21 17:39:47 2018 +0200
@@ -17,6 +17,7 @@
 """Unit tests for GraphQL"""
 from cubicweb import Unauthorized, QueryError
 from cubicweb.devtools.testlib import CubicWebTC
+from cubicweb_graphql.querier import GraphQLQuerier
 
 
 class GraphQLBaseTC(CubicWebTC):
@@ -46,7 +47,7 @@
         :param kwargs: The arguments
         :return: The query's result
         """
-        result = connection.execute_graphql(query, **kwargs)
+        result = GraphQLQuerier.execute(connection, query, **kwargs)
         self.assertIsNotNone(result.data)
         self.assertIsNone(result.errors)
         return result
@@ -59,7 +60,7 @@
         :param kwargs: The arguments
         :return: The query's result
         """
-        result = connection.execute_graphql(query, **kwargs)
+        result = GraphQLQuerier.execute(connection, query, **kwargs)
         self.assertIsNotNone(result.errors)
         self.assertTrue(len(result.errors) >= 1)
         return result
@@ -300,7 +301,7 @@
         query = "{ Affaire { sujet } }"
         with self.admin_access.repo_cnx() as cnx:
             # Reading is OK for the manager
-            result = cnx.execute_graphql(query)
+            result = GraphQLQuerier.execute(cnx, query)
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["Affaire"]), 2)
@@ -312,12 +313,12 @@
         query = "query getAffaire($affaire_eid: ID) { Affaire(eid: $affaire_eid) { sujet } }"
         with self.admin_access.repo_cnx() as cnx:
             # Reading is OK for the manager
-            result = cnx.execute_graphql(query, affaire_eid=self.affaire1)
+            result = GraphQLQuerier.execute(cnx, query, affaire_eid=self.affaire1)
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["Affaire"]), 1)
             self.assertEquals(result.data["Affaire"][0]["sujet"], "affaire1")
-            result = cnx.execute_graphql(query, affaire_eid=self.affaire2)
+            result = GraphQLQuerier.execute(cnx, query, affaire_eid=self.affaire2)
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["Affaire"]), 1)
@@ -330,7 +331,7 @@
         query = "query getAffaire($eid1: ID, $eid2: ID) { affaire1: Affaire(eid: $eid1) { sujet }, affaire2: Affaire(eid: $eid2) { sujet } }"
         with self.admin_access.repo_cnx() as cnx:
             # Reading is OK for the manager
-            result = cnx.execute_graphql(query, eid1=self.affaire1, eid2=self.affaire2)
+            result = GraphQLQuerier.execute(cnx, query, eid1=self.affaire1, eid2=self.affaire2)
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(result.data["affaire1"][0]["sujet"], "affaire1")
@@ -348,7 +349,7 @@
             cnx.execute("INSERT Societe X: X nom 'societe2'")
             cnx.execute("SET P travaille S WHERE P nom 'personne1', S nom 'societe1'")
             cnx.execute("SET P travaille S WHERE P nom 'personne1', S nom 'societe2'")
-            result = cnx.execute_graphql(query, pn="personne1", sn="societe1")
+            result = GraphQLQuerier.execute(cnx, query, pn="personne1", sn="societe1")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["Personne"]), 1)
@@ -368,7 +369,7 @@
             cnx.execute("INSERT Societe X: X nom 'societe2'")
             cnx.execute("SET P travaille S WHERE P nom 'personne1', S nom 'societe1'")
             cnx.execute("SET P travaille S WHERE P nom 'personne2', S nom 'societe2'")
-            result = cnx.execute_graphql(query, nom="societe1")
+            result = GraphQLQuerier.execute(cnx, query, nom="societe1")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["Personne"]), 1)
@@ -388,7 +389,7 @@
             cnx.execute("INSERT Card X: X title 'fiche2', X content 'fiche2'")
             cnx.execute("SET P fiche C WHERE P nom 'personne1' AND C title 'fiche1'")
             cnx.execute("SET P fiche C WHERE P nom 'personne2' AND C title 'fiche2'")
-            result = cnx.execute_graphql(query, title="fiche1")
+            result = GraphQLQuerier.execute(cnx, query, title="fiche1")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["Personne"]), 2)
@@ -403,7 +404,7 @@
         """
         query = "mutation myMutation($sujet: String) { createAffaire(sujet: $sujet) {  eid, sujet  } }"
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql(query, sujet=u"cool")
+            result = GraphQLQuerier.execute(cnx, query, sujet=u"cool")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["createAffaire"]), 2)
@@ -415,12 +416,12 @@
         Test insertion of a new entity with a reference to a existing one looked-up based on its attributes
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) { eid } }", nom=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) { eid } }", nom=u"societe1")
             societe1 = result.data["createSociete"]["eid"]
-            result = cnx.execute_graphql("mutation myMutation($sujet: String, $societe: String) { "
-                                         "createAffaire(sujet: $sujet, concerne_as_Societe: {nom: $societe}) { "
-                                         "eid, sujet, concerne { ... on Societe { eid, nom } } } }",
-                                         sujet=u"affaire1", societe=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($sujet: String, $societe: String) { "
+                                                 "createAffaire(sujet: $sujet, concerne_as_Societe: {nom: $societe}) { "
+                                                 "eid, sujet, concerne { ... on Societe { eid, nom } } } }",
+                                            sujet=u"affaire1", societe=u"societe1")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["createAffaire"]), 3)
@@ -434,12 +435,12 @@
         Test simple update of an entity with primitive fields
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
-                                         nom=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
+                                            nom=u"societe1")
             societe1 = result.data["createSociete"]["eid"]
-            result = cnx.execute_graphql("mutation myMutation($nom_old: String, $nom_new: String) { updateSociete(old: {nom: $nom_old}, new: {nom: $nom_new}) { eid, nom } }",
-                                         nom_old=u"societe1",
-                                         nom_new=u"societe2")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom_old: String, $nom_new: String) { updateSociete(old: {nom: $nom_old}, new: {nom: $nom_new}) { eid, nom } }",
+                                            nom_old=u"societe1",
+                                            nom_new=u"societe2")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["updateSociete"]), 1)
@@ -452,19 +453,19 @@
         Test update of an entity with a new value for an object field
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
-                                         nom=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
+                                            nom=u"societe1")
             societe1 = result.data["createSociete"]["eid"]
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
-                                         nom=u"societe2")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
+                                            nom=u"societe2")
             societe2 = result.data["createSociete"]["eid"]
 
-            result = cnx.execute_graphql("mutation myMutation($sujet: String, $societe: String) { updateAffaire("
-                                         "old: {sujet: $sujet}"
-                                         ", new: {concerne_as_Societe: {nom: $societe}}) {"
-                                         "eid, sujet, concerne { ... on Societe {eid, nom} } } }",
-                                         sujet=u"affaire1",
-                                         societe=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($sujet: String, $societe: String) { updateAffaire("
+                                                 "old: {sujet: $sujet}"
+                                                 ", new: {concerne_as_Societe: {nom: $societe}}) {"
+                                                 "eid, sujet, concerne { ... on Societe {eid, nom} } } }",
+                                            sujet=u"affaire1",
+                                            societe=u"societe1")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["updateAffaire"]), 1)
@@ -475,12 +476,12 @@
             self.assertEquals(result.data["updateAffaire"][0]["concerne"][0]["eid"], societe1)
             self.assertEquals(result.data["updateAffaire"][0]["concerne"][0]["nom"], u"societe1")
 
-            result = cnx.execute_graphql("mutation myMutation($sujet: String, $societe: String) { updateAffaire("
-                                         "old: {sujet: $sujet}"
-                                         ", new: {concerne_as_Societe: {nom: $societe}}) {"
-                                         "eid, sujet, concerne { ... on Societe {eid, nom} } } }",
-                                         sujet=u"affaire2",
-                                         societe=u"societe2")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($sujet: String, $societe: String) { updateAffaire("
+                                                 "old: {sujet: $sujet}"
+                                                 ", new: {concerne_as_Societe: {nom: $societe}}) {"
+                                                 "eid, sujet, concerne { ... on Societe {eid, nom} } } }",
+                                            sujet=u"affaire2",
+                                            societe=u"societe2")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["updateAffaire"]), 1)
@@ -496,15 +497,15 @@
         Test delete a single entity on a simple query
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
-                                         nom=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
+                                            nom=u"societe1")
             societe1 = result.data["createSociete"]["eid"]
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
-                                         nom=u"societe2")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
+                                            nom=u"societe2")
             societe2 = result.data["createSociete"]["eid"]
 
-            result = cnx.execute_graphql("mutation myMutation($societe: String) { deleteSociete(nom: $societe) }",
-                                         societe=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($societe: String) { deleteSociete(nom: $societe) }",
+                                            societe=u"societe1")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["deleteSociete"]), 1)
@@ -515,14 +516,14 @@
         Test delete a multiple entities on a simple query
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
-                                         nom=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
+                                            nom=u"societe1")
             societe1 = result.data["createSociete"]["eid"]
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
-                                         nom=u"societe2")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
+                                            nom=u"societe2")
             societe2 = result.data["createSociete"]["eid"]
 
-            result = cnx.execute_graphql("mutation myMutation { deleteSociete }")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation { deleteSociete }")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["deleteSociete"]), 2)
@@ -534,19 +535,19 @@
         Test delete relations
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
-                                         nom=u"societe1")
+            result = GraphQLQuerier.execute(cnx, "mutation myMutation($nom: String) { createSociete(nom: $nom) {  eid  } }",
+                                            nom=u"societe1")
             societe1 = result.data["createSociete"]["eid"]
-            result = cnx.execute_graphql("mutation m($s: ID, $a: ID) { updateAffaire(old: {eid: $a}, new: {concerne_as_Societe: {eid: $s}}) {eid, concerne {... on Societe {eid}}} }", s=societe1, a=self.affaire1)
+            result = GraphQLQuerier.execute(cnx, "mutation m($s: ID, $a: ID) { updateAffaire(old: {eid: $a}, new: {concerne_as_Societe: {eid: $s}}) {eid, concerne {... on Societe {eid}}} }", s=societe1, a=self.affaire1)
             self.assertEquals(result.data["updateAffaire"][0]["eid"], str(self.affaire1))
             self.assertEquals(result.data["updateAffaire"][0]["concerne"][0]["eid"], str(societe1))
-            result = cnx.execute_graphql("{ Affaire { eid, concerne { ... on Societe { eid } } } }")
+            result = GraphQLQuerier.execute(cnx, "{ Affaire { eid, concerne { ... on Societe { eid } } } }")
             self.assertEquals(result.data["Affaire"][0]["eid"], str(self.affaire1))
             self.assertEquals(result.data["Affaire"][0]["concerne"][0]["eid"], str(societe1))
-            result = cnx.execute_graphql("mutation m($s: ID, $a: ID) { deleteFromAffaire(from: {eid: $a}, concerne_as_Societe: {eid: $s}) }", s=societe1, a=self.affaire1)
+            result = GraphQLQuerier.execute(cnx, "mutation m($s: ID, $a: ID) { deleteFromAffaire(from: {eid: $a}, concerne_as_Societe: {eid: $s}) }", s=societe1, a=self.affaire1)
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
-            result = cnx.execute_graphql("{ Affaire { eid, concerne { ... on Societe { eid } } } }")
+            result = GraphQLQuerier.execute(cnx, "{ Affaire { eid, concerne { ... on Societe { eid } } } }")
             self.assertEquals(result.data["Affaire"][0]["eid"], str(self.affaire1))
             self.assertEquals(len(result.data["Affaire"][0]["concerne"]), 0)
             pass
@@ -562,7 +563,7 @@
         Test listing entity types using GraphQL-based reflection
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("{ __schema { queryType { fields { name } } } }")
+            result = GraphQLQuerier.execute(cnx, "{ __schema { queryType { fields { name } } } }")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["__schema"]["queryType"]["fields"]), 45)
@@ -577,7 +578,7 @@
         Test listing entity types using CubicWeb-based reflection
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("{ CWEType(final: false) { name } }")
+            result = GraphQLQuerier.execute(cnx, "{ CWEType(final: false) { name } }")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["CWEType"]), 45)
@@ -592,7 +593,7 @@
         Test listing the fields of an entity using GraphQL-based reflection
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("query q($name: String!) { __type(name: $name) { name, fields { name, type { name, kind } } } }", name="Affaire")
+            result = GraphQLQuerier.execute(cnx, "query q($name: String!) { __type(name: $name) { name, fields { name, type { name, kind } } } }", name="Affaire")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["__type"]["fields"]), 28)
@@ -607,7 +608,7 @@
         Test listing the fields of an entity using CubicWeb-based reflection
         """
         with self.admin_access.repo_cnx() as cnx:
-            result = cnx.execute_graphql("query q($name: String!) { CWRelation(from_entity: {name: $name}) { relation_type { name }, to_entity { name } } }", name="Affaire")
+            result = GraphQLQuerier.execute(cnx, "query q($name: String!) { CWRelation(from_entity: {name: $name}) { relation_type { name }, to_entity { name } } }", name="Affaire")
             self.assertIsNone(result.errors)
             self.assertIsNotNone(result.data)
             self.assertEquals(len(result.data["CWRelation"]), 16)