[MIG] base_external_dbsource: Migration to 17.0

This commit is contained in:
Anxo82
2024-06-10 13:58:53 +02:00
committed by Ángel Rivas
parent 8893d9c815
commit 675f0ecaee
5 changed files with 87 additions and 60 deletions

View File

@@ -3,7 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "External Database Sources",
"version": "16.0.1.0.1",
"version": "17.0.1.0.0",
"category": "Tools",
"author": "Daniel Reis, " "LasLabs, " "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/server-backend",

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" ?>
<odoo>
<odoo noupdate="1">
<record model="base.external.dbsource" id="demo_postgre">
<field name="name">PostgreSQL local</field>
<field name="conn_string">dbname='postgres' password=%s</field>

View File

@@ -329,7 +329,7 @@ class BaseExternalDbsource(models.Model):
except AttributeError:
raise NotImplementedError(
_(
'"%(method)s" method not found, check that all assets are installed '
'"%(method)s" method not found, check that all assets are installed'
"for the %(connector)s connector type.",
method=method,
conector=self.connector,

View File

@@ -7,20 +7,21 @@ from odoo.tests import common
class TestBaseExternalDbsource(common.TransactionCase):
def setUp(self):
super(TestBaseExternalDbsource, self).setUp()
@classmethod
def setUpClass(cls):
super().setUpClass()
# Obtain current odoo instance DB connection settings
connection_info = connection_info_for(self.env.cr.dbname)[1]
connection_info = connection_info_for(cls.env.cr.dbname)[1]
# Adapt to the format expected by this module
password = connection_info.get("password", "")
connection_info["password"] = "%s"
connection_info["dbname"] = connection_info["database"]
del connection_info["database"]
# Create a proper dbsource record to test
self.dbsource = self.env["base.external.dbsource"].create(
cls.dbsource = cls.env["base.external.dbsource"].create(
{
"conn_string": " ".join(
"%s='%s'" % item for item in connection_info.items()
f"{key}='{value}'" for key, value in connection_info.items()
),
"connector": "postgresql",
"name": "test postgres with current odoo config",
@@ -57,6 +58,16 @@ class TestBaseExternalDbsource(common.TransactionCase):
expect = self.dbsource.conn_string + "PWD=%s;" % self.dbsource.password
self.assertEqual(self.dbsource.conn_string_full, expect)
self.dbsource.conn_string = "User=Derp;"
self.dbsource.password = ""
expect = self.dbsource.conn_string
self.assertEqual(self.dbsource.conn_string_full, expect)
self.dbsource.conn_string = "User=Derp;PWD=%s;"
self.dbsource.password = "password"
expect = self.dbsource.conn_string % self.dbsource.password
self.assertEqual(self.dbsource.conn_string_full, expect)
# Interface
def test_execute_asserts_query_arg(self):
@@ -228,3 +239,20 @@ class TestBaseExternalDbsource(common.TransactionCase):
with mock.patch.object(type(self.dbsource), "connection_open") as connection:
res = self.dbsource.conn_open()
self.assertEqual(res, connection().__enter__())
def test_connection_close(self):
"""It should call the close method on the connection object"""
mock_connection = mock.Mock()
self.dbsource.connection_close(mock_connection)
mock_connection.close.assert_called_once()
def test_execute_missing_query_and_params(self):
"""It should raise a TypeError if both query and execute_params are missing"""
with self.assertRaises(TypeError):
self.dbsource.execute(metadata=True)
def test_connection_close_postgresql(self):
"""It should call the close method on the connection object"""
mock_connection = mock.Mock()
self.dbsource.connection_close_postgresql(mock_connection)
mock_connection.close.assert_called_once()

View File

@@ -27,21 +27,21 @@
groups="base.group_multi_company"
options="{'no_create': True}"
/>
<field name="connector" />
</group>
<group>
<field name="password" password="True" />
</group>
</group>
<group col="1">
<group>
<field name="connector" />
</group>
<group string="Connection string" col="1">
<group string="Connection String">
<field
name="conn_string"
nolabel="1"
placeholder="Please check the tooltip for connection string examples"
/>
<group />
</group>
<group col="1">
<button
name="connection_test"
string="Test Connection"
@@ -49,7 +49,6 @@
icon="fa-refresh"
/>
</group>
</group>
</sheet>
</form>
</field>