Skip to content

Commit

Permalink
fixup! [IMP] database_cleanup: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yankinmax committed Feb 10, 2023
1 parent 4e9b35f commit 0ad6bc4
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 194 deletions.
36 changes: 15 additions & 21 deletions database_cleanup/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from contextlib import contextmanager
from odoo.modules.registry import Registry
from odoo.tests import TransactionCase

import odoo
from odoo.tests import common
from odoo.tests.common import BaseCase, tagged

ADMIN_USER_ID = common.ADMIN_USER_ID


@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):
class Common(TransactionCase):
def setUp(self):
super().setUp()
super(Common, self).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]

def tearDown(self):
super(Common, self).tearDown()
# Force rollback to avoid unstable test database
self.env.cr.rollback()
# reset afterwards
Registry.registries[self.env.cr.dbname] = self.original_registry
24 changes: 11 additions & 13 deletions database_cleanup/tests/test_create_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@

from odoo.tests.common import tagged

from .common import Common, environment
from .common import Common


# Use post_install to get all models loaded more info: odoo/odoo#13458
@tagged("post_install", "-at_install")
class TestCreateIndexesLine(Common):
def setUp(self):
super().setUp()
with environment() as env:
# delete some index and check if our module recreated it
env.cr.execute("drop index res_partner_name_index")
super(TestCreateIndexesLine, self).setUp()
# delete some index and check if our module recreated it
self.env.cr.execute("drop index res_partner_name_index")

def test_deleted_index(self):
with environment() as env:
wizard = env["cleanup.create_indexes.wizard"].create({})
wizard.purge_all()
env.cr.execute(
"select indexname from pg_indexes where "
"indexname='res_partner_name_index' and tablename='res_partner' "
)
self.assertEqual(env.cr.rowcount, 1)
wizard = self.env["cleanup.create_indexes.wizard"].create({})
wizard.purge_all()
self.env.cr.execute(
"select indexname from pg_indexes where "
"indexname='res_partner_name_index' and tablename='res_partner' "
)
self.assertEqual(self.env.cr.rowcount, 1)
59 changes: 27 additions & 32 deletions database_cleanup/tests/test_purge_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,38 @@
from odoo.tests.common import tagged
from odoo.tools import mute_logger

from .common import Common, environment
from .common import Common


# Use post_install to get all models loaded more info: odoo/odoo#13458
@tagged("post_install", "-at_install")
class TestCleanupPurgeLineColumn(Common):
def setUp(self):
super().setUp()
with environment() as env:
# create an orphaned column
env.cr.execute(
"alter table res_partner add column database_cleanup_test int"
)
super(TestCleanupPurgeLineColumn, self).setUp()
# create an orphaned column
self.env.cr.execute(
"alter table res_partner add column database_cleanup_test int"
)

def test_empty_column(self):
with environment() as env:
# We need use a model that is not blocked (Avoid use res.users)
partner_model = env["ir.model"].search(
[("model", "=", "res.partner")], limit=1
)
wizard = env["cleanup.purge.wizard.column"].create(
{
"purge_line_ids": [
(
0,
0,
{
"model_id": partner_model.id,
"name": "database_cleanup_test",
},
)
]
}
)
wizard.purge_all()
# 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")
# We need use a model that is not blocked (Avoid use res.users)
partner_model = self.env["ir.model"].search(
[("model", "=", "res.partner")], limit=1
)
wizard = self.env["cleanup.purge.wizard.column"].create(
{
"purge_line_ids": [
(
0,
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"):
cr.execute("select database_cleanup_test from res_partner")
36 changes: 17 additions & 19 deletions database_cleanup/tests/test_purge_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@

from odoo.tests.common import tagged

from .common import Common, environment
from .common import Common


# Use post_install to get all models loaded more info: odoo/odoo#13458
@tagged("post_install", "-at_install")
class TestCleanupPurgeLineData(Common):
def setUp(self):
super().setUp()
with environment() as env:
# create a data entry pointing nowhere
env.cr.execute("select max(id) + 1 from res_users")
env["ir.model.data"].create(
{
"module": "database_cleanup",
"name": "test_no_data_entry",
"model": "res.users",
"res_id": env.cr.fetchone()[0],
}
)
super(TestCleanupPurgeLineData, self).setUp()
# create a data entry pointing nowhere
self.env.cr.execute("select max(id) + 1 from res_users")
self.env["ir.model.data"].create(
{
"module": "database_cleanup",
"name": "test_no_data_entry",
"model": "res.users",
"res_id": self.env.cr.fetchone()[0],
}
)

def test_pointing_nowhere(self):
with environment() as env:
wizard = env["cleanup.purge.wizard.data"].create({})
wizard.purge_all()
# must be removed by the wizard
with self.assertRaises(ValueError):
env.ref("database_cleanup.test_no_data_entry")
wizard = self.env["cleanup.purge.wizard.data"].create({})
wizard.purge_all()
# must be removed by the wizard
with self.assertRaises(ValueError):
self.env.ref("database_cleanup.test_no_data_entry")
50 changes: 24 additions & 26 deletions database_cleanup/tests/test_purge_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,36 @@

from odoo.tests.common import tagged

from .common import Common, environment
from .common import Common


# Use post_install to get all models loaded more info: odoo/odoo#13458
@tagged("post_install", "-at_install")
class TestCleanupPurgeLineMenu(Common):
def setUp(self):
super().setUp()
with environment() as env:
# create a new empty menu
self.menu = env["ir.ui.menu"].create({"name": "database_cleanup_test"})
super(TestCleanupPurgeLineMenu, self).setUp()
# create a new empty menu
self.menu = self.env["ir.ui.menu"].create({"name": "database_cleanup_test"})

def test_empty_menu(self):
with environment() as env:
wizard = env["cleanup.purge.wizard.menu"].create(
{
"purge_line_ids": [
(
0,
0,
{
"menu_id": self.menu.id,
},
)
]
}
)
wizard.purge_all()
self.assertFalse(
env["ir.ui.menu"].search(
[
("name", "=", "database_cleanup_test"),
]
)
wizard = self.env["cleanup.purge.wizard.menu"].create(
{
"purge_line_ids": [
(
0,
0,
{
"menu_id": self.menu.id,
},
)
]
}
)
wizard.purge_all()
self.assertFalse(
self.env["ir.ui.menu"].search(
[
("name", "=", "database_cleanup_test"),
]
)
)
56 changes: 30 additions & 26 deletions database_cleanup/tests/test_purge_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,42 @@

from odoo.tests.common import tagged

from .common import Common, environment
from .common import Common


# Use post_install to get all models loaded more info: odoo/odoo#13458
@tagged("post_install", "-at_install")
class TestCleanupPurgeLineColumn(Common):
def setUp(self):
super().setUp()
with environment() as env:
# create a nonexistent model
self.model_name = "x_database.cleanup.test.model"
self.model_values = {
"name": "Database cleanup test model",
"model": self.model_name,
}
self.model = env["ir.model"].create(self.model_values)
env.cr.execute(
"insert into ir_attachment (name, res_model, res_id, type) values "
"('test attachment', %s, 42, 'binary')",
[self.model_name],
)
env.registry.models.pop(self.model_name)
super(TestCleanupPurgeLineColumn, self).setUp()
# create a nonexistent model
self.model_name = "x_database.cleanup.test.model"
self.model_values = {
"name": "Database cleanup test model",
"model": self.model_name,
}
self.model = self.env["ir.model"].create(self.model_values)
self.env.cr.execute(
"insert into ir_attachment (name, res_model, res_id, type) values "
"('test attachment', %s, 42, 'binary')",
[self.model_name],
)
self.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):
with environment() as env:
wizard = env["cleanup.purge.wizard.model"].create({})
wizard.purge_all()
# must be removed by the wizard
self.assertFalse(
env["ir.model"].search(
[
("model", "=", self.model_name),
]
)
wizard = self.env["cleanup.purge.wizard.model"].create({})
wizard.purge_all()
# must be removed by the wizard
self.assertFalse(
self.env["ir.model"].search(
[
("model", "=", self.model_name),
]
)
)
30 changes: 14 additions & 16 deletions database_cleanup/tests/test_purge_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@

from odoo.tests.common import tagged

from .common import Common, environment
from .common import Common


# Use post_install to get all models loaded more info: odoo/odoo#13458
@tagged("post_install", "-at_install")
class TestCleanupPurgeLineModule(Common):
def setUp(self):
super().setUp()
with environment() as env:
# create a nonexistent module
self.module = env["ir.module.module"].create(
{
"name": "database_cleanup_test",
"state": "to upgrade",
}
)
super(TestCleanupPurgeLineModule, self).setUp()
# create a nonexistent module
self.module = self.env["ir.module.module"].create(
{
"name": "database_cleanup_test",
"state": "to upgrade",
}
)

def test_remove_to_upgrade_module(self):
with environment() as env:
wizard = env["cleanup.purge.wizard.module"].create({})
module_names = wizard.purge_line_ids.filtered(
lambda x: not x.purged
).mapped("name")
self.assertTrue("database_cleanup_test" in module_names)
wizard = self.env["cleanup.purge.wizard.module"].create({})
module_names = wizard.purge_line_ids.filtered(lambda x: not x.purged).mapped(
"name"
)
self.assertTrue("database_cleanup_test" in module_names)
Loading

0 comments on commit 0ad6bc4

Please sign in to comment.