[IMP] database_cleanup: improve tests
parent
f80f43c2c0
commit
e791803881
|
@ -1,14 +1,27 @@
|
||||||
# Copyright 2021 Camptocamp SA
|
# Copyright 2021 Camptocamp SA
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
from odoo.modules.registry import Registry
|
from contextlib import contextmanager
|
||||||
from odoo.tests.common import TransactionCase
|
|
||||||
|
import odoo
|
||||||
|
from odoo.tests import common
|
||||||
|
from odoo.tests.common import BaseCase, tagged
|
||||||
|
|
||||||
|
ADMIN_USER_ID = common.ADMIN_USER_ID
|
||||||
|
|
||||||
|
|
||||||
class Common(TransactionCase):
|
@contextmanager
|
||||||
|
def environment():
|
||||||
|
"""Return an environment with a new cursor for the current database; the
|
||||||
|
cursor is committed and closed after the context block.
|
||||||
|
"""
|
||||||
|
registry = odoo.registry(common.get_db_name())
|
||||||
|
with registry.cursor() as cr:
|
||||||
|
yield odoo.api.Environment(cr, ADMIN_USER_ID, {})
|
||||||
|
|
||||||
|
|
||||||
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
|
@tagged("post_install", "-at_install")
|
||||||
|
class Common(BaseCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(Common, self).setUp()
|
super().setUp()
|
||||||
# this reloads our registry, and we don't want to run tests twice
|
|
||||||
# we also need the original registry for further tests, so save a
|
|
||||||
# reference to it
|
|
||||||
self.original_registry = Registry.registries[self.env.cr.dbname]
|
|
||||||
|
|
|
@ -3,22 +3,24 @@
|
||||||
|
|
||||||
from odoo.tests.common import tagged
|
from odoo.tests.common import tagged
|
||||||
|
|
||||||
from .common import Common
|
from .common import Common, environment
|
||||||
|
|
||||||
|
|
||||||
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestCreateIndexesLine(Common):
|
class TestCreateIndexesLine(Common):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCreateIndexesLine, self).setUp()
|
super().setUp()
|
||||||
# delete some index and check if our module recreated it
|
with environment() as env:
|
||||||
self.env.cr.execute("drop index res_partner_name_index")
|
# delete some index and check if our module recreated it
|
||||||
|
env.cr.execute("drop index res_partner_name_index")
|
||||||
|
|
||||||
def test_deleted_index(self):
|
def test_deleted_index(self):
|
||||||
wizard = self.env["cleanup.create_indexes.wizard"].create({})
|
with environment() as env:
|
||||||
wizard.purge_all()
|
wizard = env["cleanup.create_indexes.wizard"].create({})
|
||||||
self.env.cr.execute(
|
wizard.purge_all()
|
||||||
"select indexname from pg_indexes where "
|
env.cr.execute(
|
||||||
"indexname='res_partner_name_index' and tablename='res_partner' "
|
"select indexname from pg_indexes where "
|
||||||
)
|
"indexname='res_partner_name_index' and tablename='res_partner' "
|
||||||
self.assertEqual(self.env.cr.rowcount, 1)
|
)
|
||||||
|
self.assertEqual(env.cr.rowcount, 1)
|
||||||
|
|
|
@ -5,38 +5,43 @@ from psycopg2 import ProgrammingError
|
||||||
from odoo.tests.common import tagged
|
from odoo.tests.common import tagged
|
||||||
from odoo.tools import mute_logger
|
from odoo.tools import mute_logger
|
||||||
|
|
||||||
from .common import Common
|
from .common import Common, environment
|
||||||
|
|
||||||
|
|
||||||
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestCleanupPurgeLineColumn(Common):
|
class TestCleanupPurgeLineColumn(Common):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCleanupPurgeLineColumn, self).setUp()
|
super().setUp()
|
||||||
# create an orphaned column
|
with environment() as env:
|
||||||
self.env.cr.execute(
|
# create an orphaned column
|
||||||
"alter table res_partner add column database_cleanup_test int"
|
env.cr.execute(
|
||||||
)
|
"alter table res_partner add column database_cleanup_test int"
|
||||||
|
)
|
||||||
|
|
||||||
def test_empty_column(self):
|
def test_empty_column(self):
|
||||||
# We need use a model that is not blocked (Avoid use res.users)
|
with environment() as env:
|
||||||
partner_model = self.env["ir.model"].search(
|
# We need use a model that is not blocked (Avoid use res.users)
|
||||||
[("model", "=", "res.partner")], limit=1
|
partner_model = env["ir.model"].search(
|
||||||
)
|
[("model", "=", "res.partner")], limit=1
|
||||||
wizard = self.env["cleanup.purge.wizard.column"].create(
|
)
|
||||||
{
|
wizard = env["cleanup.purge.wizard.column"].create(
|
||||||
"purge_line_ids": [
|
{
|
||||||
(
|
"purge_line_ids": [
|
||||||
0,
|
(
|
||||||
0,
|
0,
|
||||||
{"model_id": partner_model.id, "name": "database_cleanup_test"},
|
0,
|
||||||
)
|
{
|
||||||
]
|
"model_id": partner_model.id,
|
||||||
}
|
"name": "database_cleanup_test",
|
||||||
)
|
},
|
||||||
wizard.purge_all()
|
)
|
||||||
# must be removed by the wizard
|
]
|
||||||
with self.assertRaises(ProgrammingError):
|
}
|
||||||
with self.env.registry.cursor() as cr:
|
)
|
||||||
with mute_logger("odoo.sql_db"):
|
wizard.purge_all()
|
||||||
cr.execute("select database_cleanup_test from res_partner")
|
# must be removed by the wizard
|
||||||
|
with self.assertRaises(ProgrammingError):
|
||||||
|
with env.registry.cursor() as cr:
|
||||||
|
with mute_logger("odoo.sql_db"):
|
||||||
|
cr.execute("select database_cleanup_test from res_partner")
|
||||||
|
|
|
@ -3,28 +3,30 @@
|
||||||
|
|
||||||
from odoo.tests.common import tagged
|
from odoo.tests.common import tagged
|
||||||
|
|
||||||
from .common import Common
|
from .common import Common, environment
|
||||||
|
|
||||||
|
|
||||||
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestCleanupPurgeLineData(Common):
|
class TestCleanupPurgeLineData(Common):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCleanupPurgeLineData, self).setUp()
|
super().setUp()
|
||||||
# create a data entry pointing nowhere
|
with environment() as env:
|
||||||
self.env.cr.execute("select max(id) + 1 from res_users")
|
# create a data entry pointing nowhere
|
||||||
self.env["ir.model.data"].create(
|
env.cr.execute("select max(id) + 1 from res_users")
|
||||||
{
|
env["ir.model.data"].create(
|
||||||
"module": "database_cleanup",
|
{
|
||||||
"name": "test_no_data_entry",
|
"module": "database_cleanup",
|
||||||
"model": "res.users",
|
"name": "test_no_data_entry",
|
||||||
"res_id": self.env.cr.fetchone()[0],
|
"model": "res.users",
|
||||||
}
|
"res_id": env.cr.fetchone()[0],
|
||||||
)
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def test_pointing_nowhere(self):
|
def test_pointing_nowhere(self):
|
||||||
wizard = self.env["cleanup.purge.wizard.data"].create({})
|
with environment() as env:
|
||||||
wizard.purge_all()
|
wizard = env["cleanup.purge.wizard.data"].create({})
|
||||||
# must be removed by the wizard
|
wizard.purge_all()
|
||||||
with self.assertRaises(ValueError):
|
# must be removed by the wizard
|
||||||
self.env.ref("database_cleanup.test_no_data_entry")
|
with self.assertRaises(ValueError):
|
||||||
|
env.ref("database_cleanup.test_no_data_entry")
|
||||||
|
|
|
@ -3,36 +3,38 @@
|
||||||
|
|
||||||
from odoo.tests.common import tagged
|
from odoo.tests.common import tagged
|
||||||
|
|
||||||
from .common import Common
|
from .common import Common, environment
|
||||||
|
|
||||||
|
|
||||||
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestCleanupPurgeLineMenu(Common):
|
class TestCleanupPurgeLineMenu(Common):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCleanupPurgeLineMenu, self).setUp()
|
super().setUp()
|
||||||
# create a new empty menu
|
with environment() as env:
|
||||||
self.menu = self.env["ir.ui.menu"].create({"name": "database_cleanup_test"})
|
# create a new empty menu
|
||||||
|
self.menu = env["ir.ui.menu"].create({"name": "database_cleanup_test"})
|
||||||
|
|
||||||
def test_empty_menu(self):
|
def test_empty_menu(self):
|
||||||
wizard = self.env["cleanup.purge.wizard.menu"].create(
|
with environment() as env:
|
||||||
{
|
wizard = env["cleanup.purge.wizard.menu"].create(
|
||||||
"purge_line_ids": [
|
{
|
||||||
(
|
"purge_line_ids": [
|
||||||
0,
|
(
|
||||||
0,
|
0,
|
||||||
{
|
0,
|
||||||
"menu_id": self.menu.id,
|
{
|
||||||
},
|
"menu_id": self.menu.id,
|
||||||
)
|
},
|
||||||
]
|
)
|
||||||
}
|
]
|
||||||
)
|
}
|
||||||
wizard.purge_all()
|
)
|
||||||
self.assertFalse(
|
wizard.purge_all()
|
||||||
self.env["ir.ui.menu"].search(
|
self.assertFalse(
|
||||||
[
|
env["ir.ui.menu"].search(
|
||||||
("name", "=", "database_cleanup_test"),
|
[
|
||||||
]
|
("name", "=", "database_cleanup_test"),
|
||||||
|
]
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
|
@ -3,42 +3,38 @@
|
||||||
|
|
||||||
from odoo.tests.common import tagged
|
from odoo.tests.common import tagged
|
||||||
|
|
||||||
from .common import Common
|
from .common import Common, environment
|
||||||
|
|
||||||
|
|
||||||
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestCleanupPurgeLineColumn(Common):
|
class TestCleanupPurgeLineColumn(Common):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCleanupPurgeLineColumn, self).setUp()
|
super().setUp()
|
||||||
# create a nonexistent model
|
with environment() as env:
|
||||||
self.model_name = "x_database.cleanup.test.model"
|
# create a nonexistent model
|
||||||
self.model_values = {
|
self.model_name = "x_database.cleanup.test.model"
|
||||||
"name": "Database cleanup test model",
|
self.model_values = {
|
||||||
"model": self.model_name,
|
"name": "Database cleanup test model",
|
||||||
}
|
"model": self.model_name,
|
||||||
self.model = self.env["ir.model"].create(self.model_values)
|
}
|
||||||
self.env.cr.execute(
|
self.model = env["ir.model"].create(self.model_values)
|
||||||
"insert into ir_attachment (name, res_model, res_id, type) values "
|
env.cr.execute(
|
||||||
"('test attachment', %s, 42, 'binary')",
|
"insert into ir_attachment (name, res_model, res_id, type) values "
|
||||||
[self.model_name],
|
"('test attachment', %s, 42, 'binary')",
|
||||||
)
|
[self.model_name],
|
||||||
self.env.registry.models.pop(self.model_name)
|
)
|
||||||
|
env.registry.models.pop(self.model_name)
|
||||||
def tearDown(self):
|
|
||||||
"""We recreate the model to avoid registry Exception at loading"""
|
|
||||||
super(TestCleanupPurgeLineColumn, self).tearDown()
|
|
||||||
# FIXME: issue origin is not clear but it must be addressed.
|
|
||||||
self.model = self.env["ir.model"].create(self.model_values)
|
|
||||||
|
|
||||||
def test_empty_model(self):
|
def test_empty_model(self):
|
||||||
wizard = self.env["cleanup.purge.wizard.model"].create({})
|
with environment() as env:
|
||||||
wizard.purge_all()
|
wizard = env["cleanup.purge.wizard.model"].create({})
|
||||||
# must be removed by the wizard
|
wizard.purge_all()
|
||||||
self.assertFalse(
|
# must be removed by the wizard
|
||||||
self.env["ir.model"].search(
|
self.assertFalse(
|
||||||
[
|
env["ir.model"].search(
|
||||||
("model", "=", self.model_name),
|
[
|
||||||
]
|
("model", "=", self.model_name),
|
||||||
|
]
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
|
@ -3,25 +3,27 @@
|
||||||
|
|
||||||
from odoo.tests.common import tagged
|
from odoo.tests.common import tagged
|
||||||
|
|
||||||
from .common import Common
|
from .common import Common, environment
|
||||||
|
|
||||||
|
|
||||||
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestCleanupPurgeLineModule(Common):
|
class TestCleanupPurgeLineModule(Common):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCleanupPurgeLineModule, self).setUp()
|
super().setUp()
|
||||||
# create a nonexistent module
|
with environment() as env:
|
||||||
self.module = self.env["ir.module.module"].create(
|
# create a nonexistent module
|
||||||
{
|
self.module = env["ir.module.module"].create(
|
||||||
"name": "database_cleanup_test",
|
{
|
||||||
"state": "to upgrade",
|
"name": "database_cleanup_test",
|
||||||
}
|
"state": "to upgrade",
|
||||||
)
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def test_remove_to_upgrade_module(self):
|
def test_remove_to_upgrade_module(self):
|
||||||
wizard = self.env["cleanup.purge.wizard.module"].create({})
|
with environment() as env:
|
||||||
module_names = wizard.purge_line_ids.filtered(lambda x: not x.purged).mapped(
|
wizard = env["cleanup.purge.wizard.module"].create({})
|
||||||
"name"
|
module_names = wizard.purge_line_ids.filtered(
|
||||||
)
|
lambda x: not x.purged
|
||||||
self.assertTrue("database_cleanup_test" in module_names)
|
).mapped("name")
|
||||||
|
self.assertTrue("database_cleanup_test" in module_names)
|
||||||
|
|
|
@ -3,41 +3,44 @@
|
||||||
|
|
||||||
from odoo.tests.common import tagged
|
from odoo.tests.common import tagged
|
||||||
|
|
||||||
from .common import Common
|
from .common import Common, environment
|
||||||
|
|
||||||
|
|
||||||
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestCleanupPurgeLineProperty(Common):
|
class TestCleanupPurgeLineProperty(Common):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCleanupPurgeLineProperty, self).setUp()
|
super().setUp()
|
||||||
# Create one property for tests
|
with environment() as env:
|
||||||
self.partner_name_field_id = self.env["ir.model.fields"].search(
|
# Create one property for tests
|
||||||
[("name", "=", "name"), ("model_id.model", "=", "res.partner")], limit=1
|
self.partner_name_field_id = env["ir.model.fields"].search(
|
||||||
)
|
[("name", "=", "name"), ("model_id.model", "=", "res.partner")], limit=1
|
||||||
|
)
|
||||||
|
|
||||||
def test_property_to_not_removed(self):
|
def test_property_to_not_removed(self):
|
||||||
self.property = self.env["ir.property"].create(
|
with environment() as env:
|
||||||
{
|
self.property = env["ir.property"].create(
|
||||||
"fields_id": self.partner_name_field_id.id,
|
{
|
||||||
"type": "char",
|
"fields_id": self.partner_name_field_id.id,
|
||||||
"value_text": "My default partner name",
|
"type": "char",
|
||||||
"res_id": False,
|
"value_text": "My default partner name",
|
||||||
}
|
"res_id": False,
|
||||||
)
|
}
|
||||||
wizard = self.env["cleanup.purge.wizard.property"].create({})
|
)
|
||||||
wizard.purge_all()
|
wizard = env["cleanup.purge.wizard.property"].create({})
|
||||||
self.assertTrue(self.property.exists())
|
wizard.purge_all()
|
||||||
|
self.assertTrue(self.property.exists())
|
||||||
|
|
||||||
def test_property_no_value(self):
|
def test_property_no_value(self):
|
||||||
self.property = self.env["ir.property"].create(
|
with environment() as env:
|
||||||
{
|
self.property = env["ir.property"].create(
|
||||||
"fields_id": self.partner_name_field_id.id,
|
{
|
||||||
"type": "char",
|
"fields_id": self.partner_name_field_id.id,
|
||||||
"value_text": False,
|
"type": "char",
|
||||||
"res_id": False,
|
"value_text": False,
|
||||||
}
|
"res_id": False,
|
||||||
)
|
}
|
||||||
wizard = self.env["cleanup.purge.wizard.property"].create({})
|
)
|
||||||
wizard.purge_all()
|
wizard = env["cleanup.purge.wizard.property"].create({})
|
||||||
self.assertFalse(self.property.exists())
|
wizard.purge_all()
|
||||||
|
self.assertFalse(self.property.exists())
|
||||||
|
|
|
@ -5,21 +5,19 @@ from psycopg2 import ProgrammingError
|
||||||
from odoo.tests.common import tagged
|
from odoo.tests.common import tagged
|
||||||
from odoo.tools import mute_logger
|
from odoo.tools import mute_logger
|
||||||
|
|
||||||
from .common import Common
|
from .common import Common, environment
|
||||||
|
|
||||||
|
|
||||||
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
# Use post_install to get all models loaded more info: odoo/odoo#13458
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestCleanupPurgeLineTable(Common):
|
class TestCleanupPurgeLineTable(Common):
|
||||||
def setUp(self):
|
|
||||||
super(TestCleanupPurgeLineTable, self).setUp()
|
|
||||||
# create an orphaned table
|
|
||||||
self.env.cr.execute("create table database_cleanup_test (test int)")
|
|
||||||
|
|
||||||
def test_empty_table(self):
|
def test_empty_table(self):
|
||||||
wizard = self.env["cleanup.purge.wizard.table"].create({})
|
with environment() as env:
|
||||||
wizard.purge_all()
|
# create an orphaned table
|
||||||
with self.assertRaises(ProgrammingError):
|
env.cr.execute("create table database_cleanup_test (test int)")
|
||||||
with self.env.registry.cursor() as cr:
|
wizard = env["cleanup.purge.wizard.table"].create({})
|
||||||
with mute_logger("odoo.sql_db"):
|
wizard.purge_all()
|
||||||
cr.execute("select * from database_cleanup_test")
|
with self.assertRaises(ProgrammingError):
|
||||||
|
with env.registry.cursor() as cr:
|
||||||
|
with mute_logger("odoo.sql_db"):
|
||||||
|
cr.execute("select * from database_cleanup_test")
|
||||||
|
|
Loading…
Reference in New Issue