Skip to content

Commit

Permalink
[IMG] add unit tests added in v15
Browse files Browse the repository at this point in the history
  • Loading branch information
BT-ssteiner committed Mar 13, 2024
1 parent 73017f0 commit 9b92766
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions sql_export_mail/tests/test_sql_query_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import SUPERUSER_ID
from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase


Expand All @@ -13,15 +14,58 @@ def setUp(self):
self.sql_report_demo.write({"mail_user_ids": [(4, SUPERUSER_ID)]})

def test_sql_query_mail(self):
mail_obj = self.env["mail.mail"]
mails = mail_obj.search(
"""Check the general execution"""
self.check_before_change()
self.check_execution()

def test_not_able_add_user(self):
"""if there are field_ids, mail_user_ids can not be set"""
sql_report_demo_with_partner = self.env.ref(
"sql_export.sql_export_partner_with_variables"
)
with self.assertRaises(UserError):
sql_report_demo_with_partner.write(
{"mail_user_ids": [(4, self.env.ref("base.user_demo").id)]}
)

def test_sql_query_mail_company(self):
"""Check the general execution with %(company_id)s"""
self.check_before_change()
self.sql_report_demo.write(
{
"mail_user_ids": [(4, self.env.ref("base.user_demo").id)],
"query": """SELECT name, street
FROM res_partner where company_id = %(company_id)s""",
}
)
self.check_execution()

def test_sql_query_mail_company_user(self):
"""Check the general execution with %(company_id)s and %(user_id)s)"""
self.check_before_change()
self.sql_report_demo.write(
{
"mail_user_ids": [(4, self.env.ref("base.user_demo").id)],
"query": """SELECT name, street FROM res_partner
where company_id = %(company_id)s and id in (
select partner_id from res_users where id = %(user_id)s)""",
}
)
self.check_execution()

def check_before_change(self):
"""Check if there are no mails before changing the sql report"""
mails = self.env["mail.mail"].search(
[("model", "=", "sql.export"), ("res_id", "=", self.sql_report_demo.id)]
)
self.assertFalse(mails)

def check_execution(self):
"""Check if the cron could be created and the mail sending is working"""
self.sql_report_demo.create_cron()
self.assertTrue(self.sql_report_demo.cron_ids)
self.sql_report_demo.cron_ids.method_direct_trigger()
mails = mail_obj.search(
mails = self.env["mail.mail"].search(
[("model", "=", "sql.export"), ("res_id", "=", self.sql_report_demo.id)]
)
self.assertTrue(mails)
Expand Down

0 comments on commit 9b92766

Please sign in to comment.