@ -5,17 +5,19 @@ from datetime import date, timedelta
from odoo import api , exceptions
from odoo import api , exceptions
from odoo.tests import common
from odoo.tests import common
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
import base64
class TestMessage VacuumRule ( common . TransactionCase ) :
class TestVacuumRule ( common . TransactionCase ) :
def create_mail_message ( self , message_type , subtype ) :
def create_mail_message ( self , message_type , subtype ) :
vals = {
vals = {
' message_type ' : message_type ,
' message_type ' : message_type ,
' subtype_id ' : subtype and subtype . id or False ,
' subtype_id ' : subtype and subtype . id or False ,
' date ' : self . before_400_days ,
' date ' : self . before_400_days ,
' model ' : ' mail.channel ' ,
' res_id ' : self . env . ref ( ' mail.channel_all_employees ' ) . id ,
' model ' : ' res.partner ' ,
' res_id ' : self . env . ref ( ' base.partner_root ' ) . id ,
' subject ' : ' Test ' ,
' subject ' : ' Test ' ,
' body ' : ' Body Test ' ,
' body ' : ' Body Test ' ,
}
}
@ -23,49 +25,51 @@ class TestMessageVacuumRule(common.TransactionCase):
def tearDown ( self ) :
def tearDown ( self ) :
self . registry . leave_test_mode ( )
self . registry . leave_test_mode ( )
super ( TestMessage VacuumRule , self ) . tearDown ( )
super ( TestVacuumRule , self ) . tearDown ( )
def setUp ( self ) :
def setUp ( self ) :
super ( TestMessage VacuumRule , self ) . setUp ( )
super ( TestVacuumRule , self ) . setUp ( )
self . registry . enter_test_mode ( self . env . cr )
self . registry . enter_test_mode ( self . env . cr )
self . env = api . Environment ( self . registry . test_cr , self . env . uid ,
self . env = api . Environment ( self . registry . test_cr , self . env . uid ,
self . env . context )
self . env . context )
self . subtype = self . env . ref ( ' mail.mt_comment ' )
self . subtype = self . env . ref ( ' mail.mt_comment ' )
self . message_obj = self . env [ ' mail.message ' ]
self . message_obj = self . env [ ' mail.message ' ]
self . channel_model = self . env . ref ( ' mail.model_mail_channel ' )
self . attachment_obj = self . env [ ' ir.attachment ' ]
self . partner_model = self . env . ref ( ' base.model_res_partner ' )
today = date . today ( )
today = date . today ( )
self . before_400_days = today - timedelta ( days = 400 )
self . before_400_days = today - timedelta ( days = 400 )
def test_mail_vacuum_rules ( self ) :
def test_mail_vacuum_rules ( self ) :
rule_vals = {
rule_vals = {
' name ' : ' Subtype Model ' ,
' name ' : ' Subtype Model ' ,
' ttype ' : ' message ' ,
' retention_time ' : 399 ,
' retention_time ' : 399 ,
' message_type ' : ' email ' ,
' message_type ' : ' email ' ,
' model_ids ' : [ ( 6 , 0 , [ self . channel_model . id ] ) ] ,
' model_ids ' : [ ( 6 , 0 , [ self . env . ref ( ' base.model_res_partner ' ) . id ] ) ] ,
' message_subtype_ids ' : [ ( 6 , 0 , [ self . subtype . id ] ) ] ,
' message_subtype_ids ' : [ ( 6 , 0 , [ self . subtype . id ] ) ] ,
}
}
rule = self . env [ ' message. vacuum.rule' ] . create ( rule_vals )
rule = self . env [ ' vacuum.rule ' ] . create ( rule_vals )
m1 = self . create_mail_message ( ' notification ' , self . subtype )
m1 = self . create_mail_message ( ' notification ' , self . subtype )
m2 = self . create_mail_message ( ' email ' , self . env . ref ( ' mail.mt_note ' ) )
m2 = self . create_mail_message ( ' email ' , self . env . ref ( ' mail.mt_note ' ) )
m3 = self . create_mail_message ( ' email ' , False )
m3 = self . create_mail_message ( ' email ' , False )
message_ids = [ m1 . id , m2 . id , m3 . id ]
message_ids = [ m1 . id , m2 . id , m3 . id ]
self . message_obj . autovacuum_mail_message ( )
self . message_obj . autovacuum ( ttype = ' message ' )
message = self . message_obj . search (
message = self . message_obj . search (
[ ( ' id ' , ' in ' , message_ids ) ] )
[ ( ' id ' , ' in ' , message_ids ) ] )
# no message deleted because either message_type is wrong or subtype
# no message deleted because either message_type is wrong or subtype
# is wront or subtype is empty
# is wrong or subtype is empty
self . assertEqual ( len ( message ) ,
self . assertEqual ( len ( message ) ,
3 )
3 )
rule . write ( { ' message_type ' : ' notification ' , ' retention_time ' : 405 } )
rule . write ( { ' message_type ' : ' notification ' , ' retention_time ' : 405 } )
self . message_obj . autovacuum_mail_message ( )
self . message_obj . autovacuum ( ttype = ' message ' )
message = self . message_obj . search (
message = self . message_obj . search (
[ ( ' id ' , ' in ' , message_ids ) ] )
[ ( ' id ' , ' in ' , message_ids ) ] )
# no message deleted because of retention time
# no message deleted because of retention time
self . assertEqual ( len ( message ) ,
self . assertEqual ( len ( message ) ,
3 )
3 )
rule . write ( { ' retention_time ' : 399 } )
rule . write ( { ' retention_time ' : 399 } )
self . message_obj . autovacuum_mail_message ( )
self . message_obj . autovacuum ( ttype = ' message ' )
message = self . message_obj . search (
message = self . message_obj . search (
[ ( ' id ' , ' in ' , message_ids ) ] )
[ ( ' id ' , ' in ' , message_ids ) ] )
@ -75,20 +79,66 @@ class TestMessageVacuumRule(common.TransactionCase):
rule . write ( { ' message_type ' : ' email ' ,
rule . write ( { ' message_type ' : ' email ' ,
' message_subtype_ids ' : [ ( 6 , 0 , [ ] ) ] ,
' message_subtype_ids ' : [ ( 6 , 0 , [ ] ) ] ,
' empty_subtype ' : True } )
' empty_subtype ' : True } )
self . message_obj . autovacuum_mail_message ( )
self . message_obj . autovacuum ( ttype = ' message ' )
message = self . message_obj . search (
message = self . message_obj . search (
[ ( ' id ' , ' in ' , message_ids ) ] )
[ ( ' id ' , ' in ' , message_ids ) ] )
self . assertEqual ( len ( message ) ,
self . assertEqual ( len ( message ) ,
0 )
0 )
def create_attachment ( self , name ) :
vals = {
' name ' : name ,
' datas ' : base64 . b64encode ( b ' Content ' ) ,
' datas_fname ' : name ,
' res_id ' : self . env . ref ( ' base.partner_root ' ) . id ,
' res_model ' : ' res.partner ' ,
}
return self . env [ ' ir.attachment ' ] . create ( vals )
def test_attachment_vacuum_rule ( self ) :
rule_vals = {
' name ' : ' Partner Attachments ' ,
' ttype ' : ' attachment ' ,
' retention_time ' : 100 ,
' model_ids ' : [ ( 6 , 0 , [ self . partner_model . id ] ) ] ,
' filename_pattern ' : ' test ' ,
}
self . env [ ' vacuum.rule ' ] . create ( rule_vals )
a1 = self . create_attachment ( ' Test-dummy ' )
a2 = self . create_attachment ( ' test24 ' )
# Force create date to old date to test deletion with 100 days
# retention time
before_102_days = date . today ( ) - timedelta ( days = 102 )
before_102_days_str = before_102_days . strftime (
DEFAULT_SERVER_DATE_FORMAT )
self . env . cr . execute ( """
UPDATE ir_attachment SET create_date = ' %s '
WHERE id = % s
""" % (before_102_days_str, a2.id))
a2 . write ( { ' create_date ' : date . today ( ) - timedelta ( days = 102 ) } )
a3 = self . create_attachment ( ' other ' )
self . env . cr . execute ( """
UPDATE ir_attachment SET create_date = ' %s '
WHERE id = % s
""" % (before_102_days_str, a3.id))
attachment_ids = [ a1 . id , a2 . id , a3 . id ]
self . attachment_obj . autovacuum ( ttype = ' attachment ' )
attachments = self . attachment_obj . search (
[ ( ' id ' , ' in ' , attachment_ids ) ] )
# Only one message deleted because other 2 are with bad name or to
# recent.
self . assertEqual ( len ( attachments ) ,
2 )
def test_retention_time_constraint ( self ) :
def test_retention_time_constraint ( self ) :
rule_vals = {
rule_vals = {
' name ' : ' Subtype Model ' ,
' name ' : ' Subtype Model ' ,
' ttype ' : ' message ' ,
' retention_time ' : 0 ,
' retention_time ' : 0 ,
' message_type ' : ' email ' ,
' message_type ' : ' email ' ,
}
}
with self . assertRaises ( exceptions . ValidationError ) :
with self . assertRaises ( exceptions . ValidationError ) :
self . env [ ' message.vacuum.rule ' ] . create ( rule_vals )
self . env [ ' vacuum.rule ' ] . create ( rule_vals )
def test_res_model_domain ( self ) :
def test_res_model_domain ( self ) :
partner = self . env [ ' res.partner ' ] . create ( { ' name ' : ' Test Partner ' } )
partner = self . env [ ' res.partner ' ] . create ( { ' name ' : ' Test Partner ' } )
@ -100,19 +150,20 @@ class TestMessageVacuumRule(common.TransactionCase):
rule_vals = {
rule_vals = {
' name ' : ' Partners ' ,
' name ' : ' Partners ' ,
' ttype ' : ' message ' ,
' retention_time ' : 399 ,
' retention_time ' : 399 ,
' message_type ' : ' all ' ,
' message_type ' : ' all ' ,
' model_ids ' : [ ( 6 , 0 , [ partner_model . id ] ) ] ,
' model_ids ' : [ ( 6 , 0 , [ partner_model . id ] ) ] ,
' model_filter_domain ' : " [[ ' name ' , ' = ' , ' Dummy ' ]] " ,
' model_filter_domain ' : " [[ ' name ' , ' = ' , ' Dummy ' ]] " ,
' empty_subtype ' : True ,
' empty_subtype ' : True ,
}
}
rule = self . env [ ' message. vacuum.rule' ] . create ( rule_vals )
self . message_obj . autovacuum_mail_message ( )
rule = self . env [ ' vacuum.rule ' ] . create ( rule_vals )
self . message_obj . autovacuum ( ttype = ' message ' )
# no message deleted as the filter does not match
# no message deleted as the filter does not match
self . assertEqual ( len ( partner . message_ids ) , 1 )
self . assertEqual ( len ( partner . message_ids ) , 1 )
rule . write ( {
rule . write ( {
' model_filter_domain ' : " [[ ' name ' , ' = ' , ' Test Partner ' ]] "
' model_filter_domain ' : " [[ ' name ' , ' = ' , ' Test Partner ' ]] "
} )
} )
self . message_obj . autovacuum_mail_message ( )
self . message_obj . autovacuum ( ttype = ' message ' )
self . assertEqual ( len ( partner . message_ids ) , 0 )
self . assertEqual ( len ( partner . message_ids ) , 0 )