Skip to content

Commit d9833e3

Browse files
committed
unit tests for Model
1 parent 2abd39c commit d9833e3

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

tests/test_model.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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
31+
32+
from odoolib.model import Model
33+
34+
35+
class TestModel(unittest.TestCase):
36+
def setUp(self):
37+
self.mock_connection = MagicMock()
38+
self.mock_connection.database = "test_db"
39+
self.mock_connection.user_id = 1
40+
self.mock_connection.password = "pass"
41+
self.mock_connection.get_service.return_value.execute_kw = MagicMock()
42+
self.model = Model(self.mock_connection, "res.partner")
43+
44+
def test_proxy_method_call(self):
45+
self.mock_connection.get_service.return_value.execute_kw.return_value = [
46+
{"id": 1, "name": "John"}
47+
]
48+
result = self.model.read([1])
49+
self.assertEqual(result, [{"id": 1, "name": "John"}])
50+
51+
def test_proxy_method_call_list_ids(self):
52+
self.mock_connection.get_service.return_value.execute_kw.return_value = [
53+
{"id": 1, "name": "John"},
54+
{"id": 2, "name": "Jane"},
55+
]
56+
result = self.model.read([1, 2])
57+
self.assertEqual(result, [{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}])
58+
59+
def test_proxy_method_call_id_not_found(self):
60+
self.mock_connection.get_service.return_value.execute_kw.return_value = []
61+
result = self.model.read([999])
62+
self.assertFalse(result)
63+
64+
def test_search_read(self):
65+
self.model.search = MagicMock(return_value=[1])
66+
self.model.read = MagicMock(return_value=[{"id": 1, "name": "X"}])
67+
result = self.model.search_read([("is_company", "=", True)], ["name"])
68+
self.assertEqual(result, [{"id": 1, "name": "X"}])
69+
70+
def test_search_read_no_result(self):
71+
self.model.search = MagicMock(return_value=[])
72+
result = self.model.search_read([("is_company", "=", False)])
73+
self.assertEqual(result, [])
74+
75+
76+
if __name__ == "__main__":
77+
unittest.main()

tests/test_service.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import unittest
3030
from unittest.mock import MagicMock
31+
3132
from odoolib.service import Service
3233

3334

@@ -42,7 +43,9 @@ def test_sync_method_call(self):
4243

4344
result = self.service.some_method("arg1", 42)
4445

45-
self.mock_sender.send.assert_called_once_with("common", "some_method", "arg1", 42)
46+
self.mock_sender.send.assert_called_once_with(
47+
"common", "some_method", "arg1", 42
48+
)
4649
self.assertEqual(result, "success")
4750

4851
def test_sync_method_caching_and_multiple_calls(self):

0 commit comments

Comments
 (0)