Skip to content

Commit

Permalink
[ADD]增加staff_job.py测试及加上方法注释
Browse files Browse the repository at this point in the history
  • Loading branch information
floraXiao committed Jun 15, 2018
1 parent 673ebde commit 938bbee
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
9 changes: 7 additions & 2 deletions staff_hire/models/staff_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class staff_job(models.Model):

@api.model
def _default_address_id(self):
'''默认工作地点'''
return self.env.user.company_id

address_id = fields.Many2one(
Expand Down Expand Up @@ -41,13 +42,15 @@ def _default_address_id(self):

@api.depends('no_of_recruitment', 'staff_ids.job_id', 'staff_ids.active')
def _compute_employees(self):
'''计算该职位员工个数'''
employee_data = self.env['staff'].read_group([('job_id', 'in', self.ids)], ['job_id'], ['job_id'])
result = dict((data['job_id'][0], data['job_id_count']) for data in employee_data)
for job in self:
job.no_of_employee = result.get(job.id, 0)
job.expected_employees = result.get(job.id, 0) + job.no_of_recruitment

def _compute_document_ids(self):
'''计算该职位简历数'''
applicants = self.mapped('application_ids').filtered(lambda self: not self.staff_id)
app_to_job = dict((applicant.id, applicant.job_id.id) for applicant in applicants)
attachments = self.env['ir.attachment'].search([
Expand All @@ -56,9 +59,9 @@ def _compute_document_ids(self):
'&', ('res_model', '=', 'hire.applicant'), ('res_id', 'in', applicants.ids)])
result = dict.fromkeys(self.ids, self.env['ir.attachment'])
for attachment in attachments:
if attachment.res_model == 'hire.applicant':
if attachment.res_model == 'hire.applicant': # 在招聘模型上上传简历
result[app_to_job[attachment.res_id]] |= attachment
else:
else: # 在职位模型上上传简历
result[attachment.res_id] |= attachment

for job in self:
Expand All @@ -67,13 +70,15 @@ def _compute_document_ids(self):

@api.multi
def _compute_application_count(self):
'''计算该职位招聘数'''
read_group_result = self.env['hire.applicant'].read_group([('job_id', '=', self.id)], ['job_id'], ['job_id'])
result = dict((data['job_id'][0], data['job_id_count']) for data in read_group_result)
for job in self:
job.application_count = result.get(job.id, 0)

@api.multi
def action_get_attachment_tree_view(self):
'''跳转到简历界面'''
action = self.env.ref('base.action_attachment').read()[0]
action['context'] = {
'default_res_model': self._name,
Expand Down
1 change: 1 addition & 0 deletions staff_hire/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import test_staff_hire
import test_staff
import test_staff_job
70 changes: 70 additions & 0 deletions staff_hire/tests/test_staff_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
from odoo.tests.common import TransactionCase
from odoo.exceptions import UserError


class TestStaffJob(TransactionCase):

def setUp(self):
super(TestStaffJob, self).setUp()
self.hire = self.env.ref('staff_hire.hire_lucy')
self.hire.onchange_job_id()
self.job_id = self.env.ref('staff.staff_job_1')

def test_default_address_id(self):
'''默认工作地点'''
hire = self.env['staff.job'].create({
'name': u'hr',
'department_id': self.env.ref('staff.department_1').id,
})
self.assertTrue(hire.address_id == self.env.user.company_id)

def test_compute_employees(self):
'''计算该职位员工个数'''
self.hire.create_employee_from_applicant()
self.assertTrue(self.job_id.no_of_employee == 1)

def test_compute_document_ids(self):
'''计算该职位简历数'''
# 在招聘模型上上传简历
self.env['ir.attachment'].create({
'name': u'Lucy 的简历',
'res_model': 'hire.applicant',
'res_id': self.hire.id,
})
self.job_id._compute_document_ids()
self.assertTrue(self.job_id.documents_count == 1)
# 在职位模型上上传简历
self.env['ir.attachment'].create({
'name': u'Lucy 的简历',
'res_model': 'staff.job',
'res_id': self.job_id.id,
})
self.job_id._compute_document_ids()
self.assertTrue(self.job_id.documents_count == 2)

def test_compute_application_count(self):
'''计算该职位招聘数'''
self.job_id._compute_application_count()
self.assertTrue(self.job_id.application_count == 1)

def test_action_get_attachment_tree_view(self):
'''跳转到简历界面'''
self.job_id.action_get_attachment_tree_view()

def test_set_open(self):
'''启动招聘'''
self.job_id.set_close()
self.job_id.set_open()
self.assertTrue(self.job_id.state == 'open')
# 重复操作报错
with self.assertRaises(UserError):
self.job_id.set_open()

def test_set_close(self):
'''结束招聘'''
self.job_id.set_close()
self.assertTrue(self.job_id.state == 'close')
# 重复操作报错
with self.assertRaises(UserError):
self.job_id.set_close()

0 comments on commit 938bbee

Please sign in to comment.