mirror of
https://github.com/bringout/oca-technical.git
synced 2026-04-18 13:52:05 +02:00
Initial commit: OCA Technical packages (595 packages)
This commit is contained in:
commit
2cc02aac6e
24950 changed files with 2318079 additions and 0 deletions
|
|
@ -0,0 +1,2 @@
|
|||
from . import test_controller
|
||||
from . import test_graphene
|
||||
|
|
@ -0,0 +1,158 @@
|
|||
# Copyright 2018 ACSONE SA/NV
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
import json
|
||||
|
||||
from werkzeug.urls import url_encode
|
||||
|
||||
from odoo.tests import HttpCase
|
||||
from odoo.tests.common import HOST
|
||||
from odoo.tools import config, mute_logger
|
||||
|
||||
|
||||
class TestController(HttpCase):
|
||||
def url_open_json(self, url, json):
|
||||
return self.opener.post(
|
||||
"http://{}:{}{}".format(HOST, config["http_port"], url), json=json
|
||||
)
|
||||
|
||||
def _check_all_partners(self, all_partners, companies_only=False):
|
||||
domain = []
|
||||
if companies_only:
|
||||
domain.append(("is_company", "=", True))
|
||||
expected_names = set(self.env["res.partner"].search(domain).mapped("name"))
|
||||
actual_names = {r["name"] for r in all_partners}
|
||||
self.assertEqual(actual_names, expected_names)
|
||||
|
||||
def test_get(self):
|
||||
self.authenticate("admin", "admin")
|
||||
query = "{allPartners{name}}"
|
||||
data = {"query": query}
|
||||
r = self.url_open("/graphql/demo?" + url_encode(data))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.headers["Content-Type"], "application/json")
|
||||
self._check_all_partners(r.json()["data"]["allPartners"])
|
||||
|
||||
def test_get_with_variables(self):
|
||||
self.authenticate("admin", "admin")
|
||||
query = """
|
||||
query myQuery($companiesOnly: Boolean) {
|
||||
allPartners(companiesOnly: $companiesOnly) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
variables = {"companiesOnly": True}
|
||||
data = {"query": query, "variables": json.dumps(variables)}
|
||||
r = self.url_open("/graphql/demo?" + url_encode(data))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.headers["Content-Type"], "application/json")
|
||||
self._check_all_partners(r.json()["data"]["allPartners"], companies_only=True)
|
||||
|
||||
def test_post_form(self):
|
||||
self.authenticate("admin", "admin")
|
||||
query = "{allPartners{name}}"
|
||||
data = {"query": query}
|
||||
r = self.url_open("/graphql/demo", data=data)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.headers["Content-Type"], "application/json")
|
||||
self._check_all_partners(r.json()["data"]["allPartners"])
|
||||
|
||||
def test_post_form_with_variables(self):
|
||||
self.authenticate("admin", "admin")
|
||||
query = """
|
||||
query myQuery($companiesOnly: Boolean) {
|
||||
allPartners(companiesOnly: $companiesOnly) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
variables = {"companiesOnly": True}
|
||||
data = {"query": query, "variables": json.dumps(variables)}
|
||||
r = self.url_open("/graphql/demo", data=data)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.headers["Content-Type"], "application/json")
|
||||
self._check_all_partners(r.json()["data"]["allPartners"], companies_only=True)
|
||||
|
||||
def test_post_json_with_variables(self):
|
||||
self.authenticate("admin", "admin")
|
||||
query = """
|
||||
query myQuery($companiesOnly: Boolean) {
|
||||
allPartners(companiesOnly: $companiesOnly) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
variables = {"companiesOnly": True}
|
||||
data = {"query": query, "variables": variables}
|
||||
r = self.url_open_json("/graphql/demo", data)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.headers["Content-Type"], "application/json")
|
||||
self._check_all_partners(r.json()["data"]["allPartners"], companies_only=True)
|
||||
|
||||
def test_post_form_mutation(self):
|
||||
self.authenticate("admin", "admin")
|
||||
query = """
|
||||
mutation {
|
||||
createPartner(
|
||||
name: "Le Héro, Toto", email: "toto@example.com"
|
||||
) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
data = {"query": query}
|
||||
r = self.url_open("/graphql/demo", data=data)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.headers["Content-Type"], "application/json")
|
||||
self.assertEqual("Le Héro, Toto", r.json()["data"]["createPartner"]["name"])
|
||||
self.assertEqual(
|
||||
len(self.env["res.partner"].search([("email", "=", "toto@example.com")])), 1
|
||||
)
|
||||
|
||||
def test_get_mutation_not_allowed(self):
|
||||
"""
|
||||
Cannot perform a mutation with a GET, must use POST.
|
||||
"""
|
||||
self.authenticate("admin", "admin")
|
||||
query = """
|
||||
mutation {
|
||||
createPartner(
|
||||
name: "Le Héro, Toto", email: "toto@example.com"
|
||||
) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
data = {"query": query}
|
||||
r = self.url_open("/graphql/demo?" + url_encode(data))
|
||||
self.assertEqual(r.status_code, 405)
|
||||
self.assertEqual(r.headers["Content-Type"], "application/json")
|
||||
self.assertIn(
|
||||
"Can only perform a mutation operation from a POST request.",
|
||||
r.json()["errors"][0]["message"],
|
||||
)
|
||||
|
||||
@mute_logger("graphql.execution.executor", "graphql.execution.utils")
|
||||
def test_post_form_mutation_rollback(self):
|
||||
self.authenticate("admin", "admin")
|
||||
query = """
|
||||
mutation {
|
||||
createPartner(
|
||||
name: "Le Héro, Toto",
|
||||
email: "toto@example.com",
|
||||
raiseAfterCreate: true
|
||||
) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
data = {"query": query}
|
||||
r = self.url_open("/graphql/demo", data=data)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.headers["Content-Type"], "application/json")
|
||||
self.assertIn("as requested", r.json()["errors"][0]["message"])
|
||||
# a rollback must have occured
|
||||
self.assertEqual(
|
||||
len(self.env["res.partner"].search([("email", "=", "toto@example.com")])), 0
|
||||
)
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
# Copyright 2018 ACSONE SA/NV
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
import logging
|
||||
|
||||
from graphene.test import Client
|
||||
|
||||
from odoo.tests import TransactionCase
|
||||
|
||||
from ..schema import schema
|
||||
|
||||
|
||||
class TestGraphene(TransactionCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(TestGraphene, cls).setUpClass()
|
||||
cls.client = Client(schema)
|
||||
# disable logging for the graphql executor because we are testing
|
||||
# errors and OCA's test runner considers the two errors being logged
|
||||
# as fatal
|
||||
logging.getLogger("graphql.execution").setLevel(logging.CRITICAL)
|
||||
|
||||
def execute(self, query):
|
||||
res = self.client.execute(query, context={"env": self.env})
|
||||
if not res:
|
||||
raise RuntimeError("GraphQL query returned no data")
|
||||
if res.get("errors"):
|
||||
raise RuntimeError(
|
||||
"GraphQL query returned error: {}".format(repr(res["errors"]))
|
||||
)
|
||||
return res.get("data")
|
||||
|
||||
def test_query_all_partners(self):
|
||||
expected_names = set(self.env["res.partner"].search([]).mapped("name"))
|
||||
actual_names = {
|
||||
r["name"] for r in self.execute(" {allPartners{ name } }")["allPartners"]
|
||||
}
|
||||
self.assertEqual(actual_names, expected_names)
|
||||
|
||||
def test_query_all_partners_companies_only(self):
|
||||
expected_names = set(
|
||||
self.env["res.partner"].search([("is_company", "=", True)]).mapped("name")
|
||||
)
|
||||
actual_names = {
|
||||
r["name"]
|
||||
for r in self.execute(" {allPartners(companiesOnly: true){ name } }")[
|
||||
"allPartners"
|
||||
]
|
||||
}
|
||||
self.assertEqual(actual_names, expected_names)
|
||||
|
||||
def test_error(self):
|
||||
r = self.client.execute("{errorExample}", context={"env": self.env})
|
||||
self.assertIn("UserError example", r["errors"][0]["message"])
|
||||
|
||||
def test_mutation(self):
|
||||
mutation = """\
|
||||
mutation{
|
||||
createPartner(
|
||||
name: "toto",
|
||||
email: "toto@acsone.eu",
|
||||
) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
self.client.execute(mutation, context={"env": self.env})
|
||||
self.assertEqual(
|
||||
len(self.env["res.partner"].search([("name", "=", "toto")])), 1
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue