Allow mocking the ODOO_RPC_URL for testing

Depending of the test environment, the url of the odoo container may
change.

Using pytest in a local shell, Odoo will be available at 0.0.0.0
address.

Using pytest in a docker-in-docker environment, the url for the Odoo
container will be exposed on a different address depending on the
configuration of the docker-in-docker environment.

The mock check whether the user has configured a différent URL for the
Odoo RPC calls, and set it accordingly using mock feature.

In the gitlab-ci, the ODOO_RPC_URL should be used to set the correct
address for the RPC calls.
This commit is contained in:
Rémy Taymans 2023-11-09 16:21:20 +01:00
parent 8ea0195aaf
commit a0307847b9
6 changed files with 27 additions and 6 deletions

View File

@ -6,6 +6,7 @@ from loguru import logger
# Wait for the launch of odoo instance 60 seconds
_ODOO_RPC_MAX_TRY = 60
_ODOO_RPC_URL = "0.0.0.0"
class OdooInstance:
@ -28,7 +29,7 @@ class OdooInstance:
# Connection
try:
rpc_connexion = odoorpc.ODOO(
"0.0.0.0",
_ODOO_RPC_URL,
"jsonrpc",
port=port,
timeout=ctx.obj["config"]["odoo_rpc_timeout"],

View File

@ -81,3 +81,13 @@ def build_ctx_from_config_file() -> dict:
ctx.obj["env_folder_path"] = env_folder_path
ctx.obj["src_folder_path"] = env_folder_path / Path("src")
return ctx
def mock_odoo_rpc_url(mocker):
"""Mock the _ODOO_RPC_URL for testing purpose"""
odoo_rpc_url = os.environ.get("ODOO_RPC_URL")
if odoo_rpc_url:
mocker.patch(
"odoo_openupgrade_wizard.tools.tools_odoo_instance._ODOO_RPC_URL",
odoo_rpc_url,
)

View File

@ -6,16 +6,19 @@ from odoo_openupgrade_wizard.tools.tools_postgres import (
from . import (
build_ctx_from_config_file,
cli_runner_invoke,
mock_odoo_rpc_url,
move_to_test_folder,
)
def test_cli_install_from_csv():
def test_cli_install_from_csv(mocker):
move_to_test_folder()
mock_odoo_rpc_url(mocker)
# Initialize database
db_name = "database_test_cli___install_from_csv"
ctx = build_ctx_from_config_file()
ensure_database(ctx, db_name, state="absent")
cli_runner_invoke(

View File

@ -5,12 +5,14 @@ from odoo_openupgrade_wizard.tools.tools_odoo import get_odoo_env_path
from . import (
build_ctx_from_config_file,
cli_runner_invoke,
mock_odoo_rpc_url,
move_to_test_folder,
)
def test_cli_generate_module_analysis():
def test_cli_generate_module_analysis(mocker):
move_to_test_folder()
mock_odoo_rpc_url(mocker)
ctx = build_ctx_from_config_file()
db_name = "database_test_cli___generate_module_analysis"

View File

@ -6,12 +6,14 @@ from odoo_openupgrade_wizard.tools.tools_postgres import ensure_database
from . import (
build_ctx_from_config_file,
cli_runner_invoke,
mock_odoo_rpc_url,
move_to_test_folder,
)
def test_cli_dumpdb():
def test_cli_dumpdb(mocker):
move_to_test_folder()
mock_odoo_rpc_url(mocker)
# Initialize database
db_name = "database_test_cli___dumpdb"
@ -62,8 +64,9 @@ def test_cli_dumpdb():
filestore_path.unlink()
def test_cli_dumpdb_failure():
def test_cli_dumpdb_failure(mocker):
move_to_test_folder()
mock_odoo_rpc_url(mocker)
# Initialize database
db_name = "database_test_cli___dumpdb"

View File

@ -6,12 +6,14 @@ from odoo_openupgrade_wizard.tools.tools_postgres import ensure_database
from . import (
build_ctx_from_config_file,
cli_runner_invoke,
mock_odoo_rpc_url,
move_to_test_folder,
)
def test_cli_copydb():
def test_cli_copydb(mocker):
move_to_test_folder()
mock_odoo_rpc_url(mocker)
db_name = "database_test_cli___copydb"
db_dest_name = "database_test_cli___copydb__copy"