Skip to content

Commit c63b53d

Browse files
committed
unit tests for XML RPC
1 parent d55dabe commit c63b53d

File tree

6 files changed

+144
-11
lines changed

6 files changed

+144
-11
lines changed

src/odoolib/connector/_connector.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ class Connector(Sender):
4242
The base abstract class representing a connection to an Odoo Server.
4343
"""
4444

45+
PROTOCOL: Optional[str] = None
46+
4547
def __init__(self):
46-
self._logger = logging.getLogger(f"{str.join('.', __name__.split('.')[:-1])}")
48+
self._logger = logging.getLogger(
49+
f"{str.join('.', __name__.split('.')[:-1])}"
50+
+ (f".{self.PROTOCOL}" if self.PROTOCOL is not None else "")
51+
)
4752
self.url: Optional[str] = None
4853

4954
def get_service(self, service_name: str) -> Service:

src/odoolib/connector/_sender.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@
2727
##############################################################################
2828

2929
import asyncio
30-
from typing import Any, Optional
30+
from typing import Any
3131

3232

3333
class Sender(object):
3434
"""
3535
The base abstract class for sending RPC requests
3636
"""
3737

38-
PROTOCOL: Optional[str] = None
39-
4038
def send(self, service_name: str, method: str, *args) -> Any:
4139
"""
4240
stub out method for children to override

src/odoolib/connector/xml_rpc.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#
3131
##############################################################################
3232

33-
import logging
3433
from typing import Optional
3534
from xmlrpc.client import ServerProxy, Transport
3635

@@ -56,8 +55,7 @@ def __init__(
5655
:param hostname: The hostname of the computer holding the instance of Odoo.
5756
:param port: The port used by the Odoo instance for XMLRPC (default to 8069).
5857
"""
59-
super().__init__()
60-
self._logger = logging.getLogger(f"{self._logger.name}.xmlrpc")
58+
super(XmlRpcConnector, self).__init__()
6159
self.url = (
6260
"http://%s:%d/xmlrpc" % (hostname, port)
6361
if version is None

src/odoolib/connector/xml_rpcs.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#
3131
##############################################################################
3232

33-
import logging
3433
from typing import Optional
3534
from xmlrpc.client import SafeTransport
3635

@@ -52,9 +51,6 @@ def __init__(
5251
transport: Optional[SafeTransport] = None,
5352
):
5453
super(XmlRpcsConnector, self).__init__(hostname, port, version, transport)
55-
self._logger = logging.getLogger(
56-
f"{str.join('.', self._logger.name.split('.')[:-1])}.xmlrpcs"
57-
)
5854
self.url = (
5955
"https://%s:%d/xmlrpc" % (hostname, port)
6056
if version is None

tests/test_xml_rpc.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
#
4+
# Copyright (C) 2025 Jimmy McCann
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
#
10+
# 1. Redistributions of source code must retain the above copyright notice, this
11+
# list of conditions and the following disclaimer.
12+
# 2. Redistributions in binary form must reproduce the above copyright notice,
13+
# this list of conditions and the following disclaimer in the documentation
14+
# and/or other materials provided with the distribution.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
#
27+
##############################################################################
28+
29+
import unittest
30+
from unittest.mock import MagicMock, patch
31+
32+
from odoolib import XmlRpcConnector
33+
34+
35+
class TestXmlRpcConnector(unittest.TestCase):
36+
def setUp(self):
37+
self.hostname = "localhost"
38+
self.port = 8069
39+
self.version = "2"
40+
self.connector = XmlRpcConnector(self.hostname)
41+
42+
def test_initialization(self):
43+
self.assertEqual(
44+
self.connector.url,
45+
f"http://{self.hostname}:{self.port}/xmlrpc/{self.version}",
46+
)
47+
self.assertEqual(self.connector._logger.name, "odoolib.connector.xmlrpc")
48+
self.assertIsNone(self.connector._transport)
49+
50+
@patch("odoolib.connector.xml_rpc.ServerProxy")
51+
def test_send(self, mock_server_proxy):
52+
mock_service = MagicMock()
53+
mock_method = MagicMock(return_value="mock_response")
54+
mock_service.some_method = mock_method
55+
mock_server_proxy.return_value = mock_service
56+
57+
response = self.connector.send("common", "some_method", "arg1", "arg2")
58+
59+
mock_server_proxy.assert_called_once_with(
60+
f"http://{self.hostname}:{self.port}/xmlrpc/{self.version}/common",
61+
transport=None,
62+
)
63+
mock_method.assert_called_once_with("arg1", "arg2")
64+
self.assertEqual(response, "mock_response")
65+
66+
67+
if __name__ == "__main__":
68+
unittest.main()

tests/test_xml_rpcs.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
#
4+
# Copyright (C) 2025 Jimmy McCann
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
#
10+
# 1. Redistributions of source code must retain the above copyright notice, this
11+
# list of conditions and the following disclaimer.
12+
# 2. Redistributions in binary form must reproduce the above copyright notice,
13+
# this list of conditions and the following disclaimer in the documentation
14+
# and/or other materials provided with the distribution.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
#
27+
##############################################################################
28+
29+
import unittest
30+
from unittest.mock import MagicMock, patch
31+
32+
from odoolib import XmlRpcsConnector
33+
34+
35+
class TestXmlRpcsConnector(unittest.TestCase):
36+
def setUp(self):
37+
self.hostname = "localhost"
38+
self.port = 8069
39+
self.version = "2"
40+
self.connector = XmlRpcsConnector(self.hostname)
41+
42+
def test_initialization(self):
43+
self.assertEqual(
44+
self.connector.url,
45+
f"https://{self.hostname}:{self.port}/xmlrpc/{self.version}",
46+
)
47+
self.assertEqual(self.connector._logger.name, "odoolib.connector.xmlrpcs")
48+
self.assertIsNone(self.connector._transport)
49+
50+
@patch("odoolib.connector.xml_rpc.ServerProxy")
51+
def test_send(self, mock_server_proxy):
52+
mock_service = MagicMock()
53+
mock_method = MagicMock(return_value="mock_response")
54+
mock_service.some_method = mock_method
55+
mock_server_proxy.return_value = mock_service
56+
57+
response = self.connector.send("common", "some_method", "arg1", "arg2")
58+
59+
mock_server_proxy.assert_called_once_with(
60+
f"https://{self.hostname}:{self.port}/xmlrpc/{self.version}/common",
61+
transport=None,
62+
)
63+
mock_method.assert_called_once_with("arg1", "arg2")
64+
self.assertEqual(response, "mock_response")
65+
66+
67+
if __name__ == "__main__":
68+
unittest.main()

0 commit comments

Comments
 (0)