我想测试这个功能。我怎样才能测试它。
@api.multi
def action_sm(self):
u"""مدير القطاع."""
for request in self:
request.state = 'sm'
# SM notification
if request.employee_id.sector_manager_id:
title = 'إشعار بوجود أمر شراء'
message = 'لقد تم تقديم طلب شراء من طرف الموظف %s ' % request.employee_id.display_name
request.env['base.notification'].create({'title': title,
'message': message,
'user_id': self.employee_id.sector_manager_id.user_id.id,
'type': 'purchase_request',
'res_id': request.id,
'res_model': request._name,
'res_method_accept': 'action_management_strategy',
'res_method_refuse': 'action_refuse',
'res_action': 'purchase_request.purchase_request_action'})
和
@api.multi
@api.depends('employee_id')
def _compute_sector_manager(self):
"""Check if current user is a sector manager of the employee."""
for rec in self:
is_sector_manager = False
sector_manager_id = rec.employee_id.sector_manager_id
if sector_manager_id and sector_manager_id.user_id.id == self._uid:
is_sector_manager = True
rec.is_sector_manager = is_sector_manager
我有
class PurchaseRequest(models.Model):
_name = 'purchase.request'
_inherit = ['mail.thread']
_rec_name = 'employee_id'
name = fields.Char(string='رقم الطلب', readonly=1)
date = fields.Date(string='تاريخ الطلب', required=1, default=fields.Datetime.now, readonly=1, states={'draft': [('readonly', 0)]})
employee_id = fields.Many2one('hr.employee', string='صاحب الطلب', required=1, readonly=1, default=lambda self: self.env['hr.employee'].search([('user_id', '=', self._uid), ('emp_state', 'not in', ['suspended', 'terminated'])], limit=1))
approver_id = fields.Many2one('hr.employee', string='مدير القطاع', readonly=1)
type = fields.Selection([('material', 'مواد'),
('project', 'مشروع'),
('other', 'أخرى')], string='النوع', default='material', readonly=1, states={'draft': [('readonly', 0)]})
description = fields.Text(string='الوصف', readonly=1, states={'draft': [('readonly', 0)]})
duration = fields.Integer(string='مدة التنفيذ', required=1, readonly=1, states={'draft': [('readonly', 0)]})
estimated_budget = fields.Float(string='السعر التقديري (بالريال)', default=0.0, readonly=1, states={'draft': [('readonly', 0)]})
attachment_types = fields.Many2many('purchase.attachment.type', string='نوع المرفقات', readonly=1, states={'draft': [('readonly', 0)]})
attachment_ids = fields.Many2many('ir.attachment', string='المرفقات', readonly=1, states={'draft': [('readonly', 0)]})
is_sector_manager = fields.Boolean(string='Sector Manager', compute='_compute_sector_manager')
is_current_user = fields.Boolean(string='Current User', compute='_compute_current_user')
安全性
<record id="rule_sm_hr_purchases" model="ir.rule">
<field name="name">طلبات أوامر الشراء موظفي القطاع</field>
<field name="model_id" ref="model_purchase_request"/>
<field name="domain_force">['|',('employee_id.sector_manager_id.user_id','=',user.id),('employee_id.user_id','=',user.id)]</field>
<field name="groups" eval="[(4, ref('hr_base.group_sector_manager'))]"/>
</record>
for demo
<record id="purchase_request_1" model="purchase.request">
<field name="type">material</field>
<field name="duration">10</field>
<field name="estimated_budget">500</field>
<field name="description">شراء مع اصطحاب كراس الشروط</field>
</record>
用于测试我的功能是错误的(通知我想使用组group_sector_manage)
def test_sm_state(self):
"""Check state sm and notification."""
# test state
request = self.env.ref('purchase_request.purchase_request_1')
request.action_sm()
self.assertEqual('sm', request.state, "Error while executing 'action_sm'.")
# test notification
# notification_count = self.env['base.notification'].search_count([('user_id', '=', self.employee_id.sector_manager_id.user_id.id), ('res_id', '=', self.request.id)])
# print(notification_count)
#self.assertEqual('1', notification_count, "Error while sending 'notification'.")
我的功能测试是错误的我如何纠正它(通知)。我想使用group_sector_manage我应该如何将它放在common.py中?