From 9458a598b545652991cefbff6abbb3de2020407e Mon Sep 17 00:00:00 2001 From: Bhavesh Odedra Date: Mon, 21 Jun 2021 12:04:00 -0700 Subject: [PATCH] [ADD] auditlog: test case for capture record --- auditlog/models/rule.py | 3 +- auditlog/tests/test_auditlog.py | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/auditlog/models/rule.py b/auditlog/models/rule.py index 13f7beef4..6e7bf1273 100644 --- a/auditlog/models/rule.py +++ b/auditlog/models/rule.py @@ -387,7 +387,8 @@ class AuditlogRule(models.Model): model_model = self.env[res_model] name = model_model.browse(res_id).name_get() model_id = self.pool._auditlog_model_cache[res_model] - auditlog_rule = self.env['auditlog.rule'].search([("model_id", "=", model_id)]) + auditlog_rule = self.env['auditlog.rule'].search( + [("model_id", "=", model_id)]) res_name = name and name[0] and name[0][1] vals = { 'name': res_name, diff --git a/auditlog/tests/test_auditlog.py b/auditlog/tests/test_auditlog.py index 9c490eb52..84c904bbd 100644 --- a/auditlog/tests/test_auditlog.py +++ b/auditlog/tests/test_auditlog.py @@ -107,6 +107,58 @@ class AuditlogCommon(object): ]) self.assertEqual(len(logs), len(groups)) + def test_LogCreation5(self): + """Fifth test, create a record and check that the same number of logs + has been generated. And then delete it, check that it has created log + with 0 fields updated. + """ + self.groups_rule.subscribe() + + auditlog_log = self.env['auditlog.log'] + testgroup5 = self.env['res.groups'].create({ + 'name': 'testgroup5', + }) + self.assertTrue(auditlog_log.search([ + ('model_id', '=', self.groups_model_id), + ('method', '=', 'create'), + ('res_id', '=', testgroup5.id), + ]).ensure_one()) + testgroup5.unlink() + log_record = auditlog_log.search([ + ('model_id', '=', self.groups_model_id), + ('method', '=', 'unlink'), + ('res_id', '=', testgroup5.id), + ]).ensure_one() + self.assertTrue(log_record) + if not self.groups_rule.capture_record: + self.assertEqual(len(log_record.line_ids), 0) + + def test_LogCreation6(self): + """Six test, create a record and check that the same number of logs + has been generated. And then delete it, check that it has created log + with x fields updated as per rule + """ + self.groups_rule.subscribe() + + auditlog_log = self.env['auditlog.log'] + testgroup6 = self.env['res.groups'].create({ + 'name': 'testgroup6', + }) + self.assertTrue(auditlog_log.search([ + ('model_id', '=', self.groups_model_id), + ('method', '=', 'create'), + ('res_id', '=', testgroup6.id), + ]).ensure_one()) + testgroup6.unlink() + log_record = auditlog_log.search([ + ('model_id', '=', self.groups_model_id), + ('method', '=', 'unlink'), + ('res_id', '=', testgroup6.id), + ]).ensure_one() + self.assertTrue(log_record) + if self.groups_rule.capture_record: + self.assertTrue(len(log_record.line_ids) > 0) + class TestAuditlogFull(TransactionCase, AuditlogCommon): @@ -146,3 +198,24 @@ class TestAuditlogFast(TransactionCase, AuditlogCommon): def tearDown(self): self.groups_rule.unlink() super(TestAuditlogFast, self).tearDown() + + +class TestAuditlogFullCaptureRecord(TransactionCase, AuditlogCommon): + + def setUp(self): + super(TestAuditlogFullCaptureRecord, self).setUp() + self.groups_model_id = self.env.ref('base.model_res_groups').id + self.groups_rule = self.env['auditlog.rule'].create({ + 'name': 'testrule for groups with capture unlink record', + 'model_id': self.groups_model_id, + 'log_read': True, + 'log_create': True, + 'log_write': True, + 'log_unlink': True, + 'log_type': 'full', + 'capture_record': True, + }) + + def tearDown(self): + self.groups_rule.unlink() + super(TestAuditlogFullCaptureRecord, self).tearDown()