|
| 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.connection import Connection |
| 33 | +from odoolib.connector._connector import Connector |
| 34 | +from odoolib.model import Model |
| 35 | + |
| 36 | + |
| 37 | +class TestConnection(unittest.TestCase): |
| 38 | + def setUp(self): |
| 39 | + self.mock_connector = MagicMock(spec=Connector) |
| 40 | + self.connection = Connection( |
| 41 | + connector=self.mock_connector, |
| 42 | + database="test_db", |
| 43 | + login="admin", |
| 44 | + password="password", |
| 45 | + ) |
| 46 | + |
| 47 | + def test_initialization(self): |
| 48 | + self.assertEqual(self.connection.database, "test_db") |
| 49 | + self.assertEqual(self.connection.login, "admin") |
| 50 | + self.assertEqual(self.connection.password, "password") |
| 51 | + self.assertIsNone(self.connection.user_context) |
| 52 | + |
| 53 | + def test_get_user_context(self): |
| 54 | + mock_model = MagicMock() |
| 55 | + mock_model.context_get.return_value = {"lang": "en_US"} |
| 56 | + self.connection.get_model = MagicMock(return_value=mock_model) |
| 57 | + |
| 58 | + context = self.connection.get_user_context() |
| 59 | + self.assertEqual(context, {"lang": "en_US"}) |
| 60 | + self.assertEqual(self.connection.user_context, {"lang": "en_US"}) |
| 61 | + self.connection.get_model.assert_called_once_with("res.users") |
| 62 | + mock_model.context_get.assert_called_once() |
| 63 | + |
| 64 | + def test_get_model(self): |
| 65 | + model_instance = self.connection.get_model("res.partner") |
| 66 | + self.assertIsInstance(model_instance, Model) |
| 67 | + self.assertEqual(model_instance.model_name, "res.partner") |
| 68 | + self.assertEqual(model_instance.connection, self.connection) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + unittest.main() |
0 commit comments