[ADD] partner_gogocarto_export_api: add test suite
Add unit tests on the parser/domain helpers and end-to-end tests of the public JSON endpoint: - ``tests/test_res_partner.py``: pins the Gogocarto data contract (default domain and field set, company-selected fields, parser mapping of scalar/relational/binary/unsupported ttypes). A silent regression there corrupts the map feed while the endpoint keeps answering 200. - ``tests/test_controller.py``: anonymous ``HttpCase`` calls on ``/web/<company_id>/get_http_gogocarto_elements`` covering the flagged-partners payload, the exclusion of unflagged partners and the graceful degradation on unknown company ids. Verified on a local 18.0 instance: 9 tests, 0 failed, 0 error.
This commit is contained in:
2
partner_gogocarto_export_api/tests/__init__.py
Normal file
2
partner_gogocarto_export_api/tests/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from . import test_controller
|
||||
from . import test_res_partner
|
||||
84
partner_gogocarto_export_api/tests/test_controller.py
Normal file
84
partner_gogocarto_export_api/tests/test_controller.py
Normal file
@@ -0,0 +1,84 @@
|
||||
from odoo.tests.common import HttpCase
|
||||
|
||||
_ROUTE = "/web/{company_id}/get_http_gogocarto_elements"
|
||||
|
||||
|
||||
class TestGogocartoExportHttp(HttpCase):
|
||||
"""
|
||||
End-to-end tests of the public JSON endpoint.
|
||||
|
||||
Requests are made anonymously on purpose: the route is declared
|
||||
``auth="public"`` and Gogocarto servers fetch it without credentials.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.partner = cls.env["res.partner"].create(
|
||||
{
|
||||
"name": "Gogo Test Partner",
|
||||
"street": "1 rue du Test",
|
||||
"city": "Paris",
|
||||
"in_gogocarto": True,
|
||||
"partner_latitude": 48.8566,
|
||||
"partner_longitude": 2.3522,
|
||||
}
|
||||
)
|
||||
cls.env["res.partner"].create({"name": "Hidden Partner", "in_gogocarto": False})
|
||||
company = cls.env["res.company"].browse(1)
|
||||
company.export_gogocarto_fields = (
|
||||
cls.env["ir.model.fields"]
|
||||
.sudo()
|
||||
.search(
|
||||
[
|
||||
("model_id", "=", "res.partner"),
|
||||
("name", "in", ["street", "city"]),
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
def _get_elements(self, company_id="1"):
|
||||
"""
|
||||
Call the export endpoint anonymously and return the response.
|
||||
|
||||
:param company_id: company id to embed in the route
|
||||
:return: the HTTP response for the endpoint
|
||||
"""
|
||||
# ``authenticate(None, None)`` opens an anonymous session bound to
|
||||
# the test database; a bare cookie-less request would not resolve
|
||||
# the database when the instance hosts several of them.
|
||||
self.authenticate(None, None)
|
||||
return self.url_open(_ROUTE.format(company_id=company_id))
|
||||
|
||||
def test_returns_flagged_partners_as_json(self):
|
||||
"""
|
||||
The endpoint returns the flagged partner with its exported data.
|
||||
"""
|
||||
response = self._get_elements()
|
||||
self.assertEqual(response.status_code, 200)
|
||||
entries = [e for e in response.json() if e["id"] == self.partner.id]
|
||||
self.assertEqual(len(entries), 1)
|
||||
entry = entries[0]
|
||||
self.assertEqual(entry["name"], "Gogo Test Partner")
|
||||
self.assertEqual(entry["street"], "1 rue du Test")
|
||||
self.assertEqual(entry["city"], "Paris")
|
||||
self.assertEqual(entry["partner_latitude"], 48.8566)
|
||||
self.assertEqual(entry["partner_longitude"], 2.3522)
|
||||
|
||||
def test_excludes_unflagged_partners(self):
|
||||
"""
|
||||
Partners without the ``in_gogocarto`` flag are never exported.
|
||||
"""
|
||||
data = self._get_elements().json()
|
||||
self.assertFalse(any(e["name"] == "Hidden Partner" for e in data))
|
||||
|
||||
def test_unknown_company_returns_default_fields(self):
|
||||
"""
|
||||
An unknown company id degrades gracefully to the default fields.
|
||||
"""
|
||||
response = self._get_elements(company_id="99999999")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
entry = [e for e in response.json() if e["id"] == self.partner.id][0]
|
||||
self.assertNotIn("street", entry) # no company fields selected
|
||||
self.assertIn("name", entry)
|
||||
self.assertIn("partner_latitude", entry)
|
||||
104
partner_gogocarto_export_api/tests/test_res_partner.py
Normal file
104
partner_gogocarto_export_api/tests/test_res_partner.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from odoo.tests.common import TransactionCase
|
||||
|
||||
|
||||
class TestGogocartoParser(TransactionCase):
|
||||
"""
|
||||
Unit tests of the parser and domain helpers.
|
||||
|
||||
These pin the data contract sent to the Gogocarto importer: a silent
|
||||
regression here corrupts the map feed while the HTTP endpoint keeps
|
||||
answering 200.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.Partner = cls.env["res.partner"]
|
||||
cls.ModelFields = cls.env["ir.model.fields"].sudo()
|
||||
cls.Company = cls.env["res.company"].sudo()
|
||||
|
||||
def test_get_gogocarto_domain_default(self):
|
||||
"""
|
||||
The default selection only exports explicitly flagged partners.
|
||||
"""
|
||||
self.assertEqual(
|
||||
self.Partner._get_gogocarto_domain(1),
|
||||
[("in_gogocarto", "=", True)],
|
||||
)
|
||||
|
||||
def test_get_export_fields_without_company(self):
|
||||
"""
|
||||
An unknown company id falls back to the default field set.
|
||||
"""
|
||||
fields = self.Partner._get_export_fields(99999999)
|
||||
self.assertEqual(
|
||||
set(fields.mapped("name")),
|
||||
{"id", "name", "partner_longitude", "partner_latitude"},
|
||||
)
|
||||
|
||||
def test_get_export_fields_includes_company_fields(self):
|
||||
"""
|
||||
Fields selected on the company extend the default field set.
|
||||
"""
|
||||
company = self.Company.create({"name": "Gogo Test Co"})
|
||||
street = self.ModelFields.search(
|
||||
[("model_id", "=", "res.partner"), ("name", "=", "street")], limit=1
|
||||
)
|
||||
company.export_gogocarto_fields = street
|
||||
|
||||
fields = self.Partner._get_export_fields(company.id)
|
||||
|
||||
self.assertEqual(
|
||||
set(fields.mapped("name")),
|
||||
{
|
||||
"id",
|
||||
"name",
|
||||
"partner_longitude",
|
||||
"partner_latitude",
|
||||
"street",
|
||||
},
|
||||
)
|
||||
|
||||
def test_get_generic_parser_maps_supported_ttypes(self):
|
||||
"""
|
||||
Scalar fields map by name, relational ones to [id, name], and binary fields are
|
||||
excluded from the parser.
|
||||
"""
|
||||
fields = self.ModelFields.search(
|
||||
[
|
||||
("model_id", "=", "res.partner"),
|
||||
("name", "in", ["phone", "comment", "category_id", "image_1920"]),
|
||||
]
|
||||
)
|
||||
parser = self.Partner._get_generic_parser(fields)
|
||||
self.assertIn("phone", parser) # char
|
||||
self.assertIn("comment", parser) # html
|
||||
self.assertIn(("category_id", ["id", "name"]), parser) # many2many
|
||||
self.assertNotIn("image_1920", parser) # binary
|
||||
|
||||
def test_get_generic_parser_skips_unsupported_ttypes(self):
|
||||
"""
|
||||
A ttype outside the supported sets is silently skipped.
|
||||
"""
|
||||
field = MagicMock()
|
||||
field.ttype = "many2one_reference"
|
||||
field.name = "x_unsupported"
|
||||
self.assertEqual(self.Partner._get_generic_parser(field), [])
|
||||
|
||||
def test_get_gogocarto_parser_composition(self):
|
||||
"""
|
||||
The Gogocarto parser combines default and company fields.
|
||||
"""
|
||||
company = self.Company.create({"name": "Gogo Test Co 2"})
|
||||
street = self.ModelFields.search(
|
||||
[("model_id", "=", "res.partner"), ("name", "=", "street")], limit=1
|
||||
)
|
||||
company.export_gogocarto_fields = street
|
||||
|
||||
parser = self.Partner._get_gogocarto_parser(company.id)
|
||||
|
||||
self.assertIn("street", parser)
|
||||
self.assertIn("name", parser)
|
||||
self.assertIn("partner_latitude", parser)
|
||||
Reference in New Issue
Block a user