3
3
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
4
4
5
5
from odoo import SUPERUSER_ID
6
+ from odoo .exceptions import UserError
6
7
from odoo .tests .common import TransactionCase
7
8
8
9
@@ -13,15 +14,58 @@ def setUp(self):
13
14
self .sql_report_demo .write ({"mail_user_ids" : [(4 , SUPERUSER_ID )]})
14
15
15
16
def test_sql_query_mail (self ):
16
- mail_obj = self .env ["mail.mail" ]
17
- mails = mail_obj .search (
17
+ """Check the general execution"""
18
+ self .check_before_change ()
19
+ self .check_execution ()
20
+
21
+ def test_not_able_add_user (self ):
22
+ """if there are field_ids, mail_user_ids can not be set"""
23
+ sql_report_demo_with_partner = self .env .ref (
24
+ "sql_export.sql_export_partner_with_variables"
25
+ )
26
+ with self .assertRaises (UserError ):
27
+ sql_report_demo_with_partner .write (
28
+ {"mail_user_ids" : [(4 , self .env .ref ("base.user_demo" ).id )]}
29
+ )
30
+
31
+ def test_sql_query_mail_company (self ):
32
+ """Check the general execution with %(company_id)s"""
33
+ self .check_before_change ()
34
+ self .sql_report_demo .write (
35
+ {
36
+ "mail_user_ids" : [(4 , self .env .ref ("base.user_demo" ).id )],
37
+ "query" : """SELECT name, street
38
+ FROM res_partner where company_id = %(company_id)s""" ,
39
+ }
40
+ )
41
+ self .check_execution ()
42
+
43
+ def test_sql_query_mail_company_user (self ):
44
+ """Check the general execution with %(company_id)s and %(user_id)s)"""
45
+ self .check_before_change ()
46
+ self .sql_report_demo .write (
47
+ {
48
+ "mail_user_ids" : [(4 , self .env .ref ("base.user_demo" ).id )],
49
+ "query" : """SELECT name, street FROM res_partner
50
+ where company_id = %(company_id)s and id in (
51
+ select partner_id from res_users where id = %(user_id)s)""" ,
52
+ }
53
+ )
54
+ self .check_execution ()
55
+
56
+ def check_before_change (self ):
57
+ """Check if there are no mails before changing the sql report"""
58
+ mails = self .env ["mail.mail" ].search (
18
59
[("model" , "=" , "sql.export" ), ("res_id" , "=" , self .sql_report_demo .id )]
19
60
)
20
61
self .assertFalse (mails )
62
+
63
+ def check_execution (self ):
64
+ """Check if the cron could be created and the mail sending is working"""
21
65
self .sql_report_demo .create_cron ()
22
66
self .assertTrue (self .sql_report_demo .cron_ids )
23
67
self .sql_report_demo .cron_ids .method_direct_trigger ()
24
- mails = mail_obj .search (
68
+ mails = self . env [ "mail.mail" ] .search (
25
69
[("model" , "=" , "sql.export" ), ("res_id" , "=" , self .sql_report_demo .id )]
26
70
)
27
71
self .assertTrue (mails )
0 commit comments