Skip to content

Commit

Permalink
Add test for fsm_location
Browse files Browse the repository at this point in the history
  • Loading branch information
kittiu committed Jul 5, 2019
1 parent 0fffaf2 commit ffeed8d
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 33 deletions.
2 changes: 2 additions & 0 deletions fieldservice/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
'demo': [
'demo/fsm_demo.xml',
'demo/fsm_equipment.xml',
'demo/fsm_location.xml',
'demo/fsm_person.xml',
],
'qweb': [
'static/src/xml/*.xml',
Expand Down
31 changes: 9 additions & 22 deletions fieldservice/demo/fsm_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
<field name="phone">ABC</field>
<field name="email">tlp@email.com</field>
</record>
<record id="location_partner_1" model="res.partner">
<field name="name">Loc Partner 1</field>
</record>
<record id="location_partner_2" model="res.partner">
<field name="name">Loc Partner 1</field>
</record>
<record id="location_partner_3" model="res.partner">
<field name="name">Loc Partner 1</field>
</record>

<!-- res.partner with contacts -->
<record id="s1" model="res.partner">
Expand Down Expand Up @@ -42,7 +51,6 @@
<field name="email">tp@email.com</field>
</record>

<!-- Test Location -->
<!-- fsm.region -->
<record id="test_region" model="fsm.region">
<field name="name">Test Region</field>
Expand All @@ -59,26 +67,5 @@
<field name="name">Test Territory</field>
<field name="branch_id" eval="ref('test_branch')"/>
</record>
<!-- fsm.location -->
<record id="test_location" model="fsm.location">
<field name="name">Test Location</field>
<field name="phone">123</field>
<field name="email">tp@email.com</field>
<field name="partner_id" eval="ref('test_loc_partner')"/>
<field name="owner_id" eval="ref('test_loc_partner')"/>
<field name="customer_id" eval="ref('test_loc_partner')"/>
<field name="territory_id" eval="ref('test_territory')"/>
<field name="branch_id" eval="ref('test_branch')"/>
<field name="district_id" eval="ref('test_district')"/>
<field name="region_id" eval="ref('test_region')"/>
</record>

<!-- other locations -->
<record id="location_1" model="fsm.location">
<field name="name">Location 1</field>
<field name="partner_id" eval="ref('test_loc_partner')"/>
<field name="owner_id" eval="ref('test_loc_partner')"/>
<field name="customer_id" eval="ref('test_loc_partner')"/>
</record>

</odoo>
10 changes: 0 additions & 10 deletions fieldservice/demo/fsm_equipment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,4 @@
<field name="sequence">3</field>
<field name="stage_type">equipment</field>
</record>
<record id="equipment_stage_4" model="fsm.stage">
<field name="name">Equipment-4</field>
<field name="sequence">4</field>
<field name="stage_type">equipment</field>
</record>
<record id="equipment_stage_5" model="fsm.stage">
<field name="name">Equipment-5</field>
<field name="sequence">5</field>
<field name="stage_type">equipment</field>
</record>
</odoo>
59 changes: 59 additions & 0 deletions fieldservice/demo/fsm_location.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<!-- Location states -->
<record id="location_stage_1" model="fsm.stage">
<field name="name">Location-1</field>
<field name="sequence">1</field>
<field name="stage_type">location</field>
</record>
<record id="location_stage_2" model="fsm.stage">
<field name="name">Location-2</field>
<field name="sequence">2</field>
<field name="stage_type">location</field>
</record>
<record id="location_stage_3" model="fsm.stage">
<field name="name">Location-3</field>
<field name="sequence">3</field>
<field name="stage_type">location</field>
</record>

<!-- fsm.location -->
<record id="test_location" model="fsm.location">
<field name="name">Test Location</field>
<field name="phone">123</field>
<field name="email">tp@email.com</field>
<field name="partner_id" eval="ref('test_loc_partner')"/>
<field name="owner_id" eval="ref('test_loc_partner')"/>
<field name="customer_id" eval="ref('test_loc_partner')"/>
<field name="territory_id" eval="ref('test_territory')"/>
<field name="branch_id" eval="ref('test_branch')"/>
<field name="district_id" eval="ref('test_district')"/>
<field name="region_id" eval="ref('test_region')"/>
<field name="direction">New the old inventory</field>
<field name="street">14 Tower A, main street</field>
<field name="street2">Bangkok, Thailand</field>
</record>

<!-- Other locations -->
<record id="location_1" model="fsm.location">
<field name="name">Location 1</field>
<field name="partner_id" eval="ref('location_partner_1')"/>
<field name="owner_id" eval="ref('location_partner_1')"/>
<field name="customer_id" eval="ref('location_partner_1')"/>
</record>

<record id="location_2" model="fsm.location">
<field name="name">Location 2</field>
<field name="partner_id" eval="ref('location_partner_2')"/>
<field name="owner_id" eval="ref('location_partner_2')"/>
<field name="customer_id" eval="ref('location_partner_2')"/>
</record>

<record id="location_3" model="fsm.location">
<field name="name">Location 3</field>
<field name="partner_id" eval="ref('location_partner_3')"/>
<field name="owner_id" eval="ref('location_partner_3')"/>
<field name="customer_id" eval="ref('location_partner_3')"/>
</record>
</odoo>
15 changes: 15 additions & 0 deletions fieldservice/demo/fsm_person.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<!-- Equipment states -->
<record id="person_1" model="fsm.person">
<field name="name">Worker-1</field>
</record>
<record id="person_2" model="fsm.person">
<field name="name">Worker-2</field>
</record>
<record id="person_3" model="fsm.person">
<field name="name">Worker-3</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions fieldservice/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

from . import test_fsm_wizard
from . import test_fsm_equipment
from . import test_fsm_location
6 changes: 5 additions & 1 deletion fieldservice/tests/test_fsm_equipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def test_fsm_equipment(self):
equipment.next_stage()
self.assertEqual(equipment.stage_id,
self.env.ref('fieldservice.equipment_stage_2'))
equipment.next_stage()
self.assertEqual(equipment.stage_id,
self.env.ref('fieldservice.equipment_stage_3'))
self.assertTrue(equipment.hide) # hide as max stage
equipment.previous_stage()
self.assertEqual(equipment.stage_id,
self.env.ref('fieldservice.equipment_stage_1'))
self.env.ref('fieldservice.equipment_stage_2'))
138 changes: 138 additions & 0 deletions fieldservice/tests/test_fsm_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Copyright (C) 2019 - TODAY, Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase, Form
from odoo.exceptions import ValidationError


class FSMLocation(TransactionCase):

def setUp(self):
super(FSMLocation, self).setUp()
self.Location = self.env['fsm.location']
self.Equipment = self.env['fsm.equipment']
self.test_location = self.env.ref('fieldservice.test_location')
self.location_1 = self.env.ref('fieldservice.location_1')
self.location_2 = self.env.ref('fieldservice.location_2')
self.location_3 = self.env.ref('fieldservice.location_3')
self.test_territory = self.env.ref('fieldservice.test_territory')
self.test_loc_partner = self.env.ref('fieldservice.'
'test_loc_partner')
self.location_partner_1 = self.env.ref('fieldservice.'
'location_partner_1')
self.location_partner_2 = self.env.ref('fieldservice.'
'location_partner_2')
self.location_partner_3 = self.env.ref('fieldservice.'
'location_partner_3')

def test_fsm_location(self):
""" Test createing new location
- Onchange parent, will get all parent info
- Default stage
- Change stage
- Create fsm.location.person if auto_populate_persons_on_location
"""
# Create an equipment
view_id = ('fieldservice.fsm_location_form_view')
with Form(self.Location, view=view_id) as f:
f.name = 'Child Location'
f.fsm_parent_id = self.test_location
location = f.save()
# Test child location equal to parent location
for x in ['owner_id', 'customer_id', 'contact_id', 'direction',
'street', 'street2', 'city', 'zip', 'state_id', 'country_id',
'tz', 'territory_id']:
self.assertEqual(location[x], self.test_location[x])

# Test initial stage
self.assertEqual(location.stage_id,
self.env.ref('fieldservice.location_stage_1'))
# Test change state
location.next_stage()
self.assertEqual(location.stage_id,
self.env.ref('fieldservice.location_stage_2'))
location.next_stage()
self.assertEqual(location.stage_id,
self.env.ref('fieldservice.location_stage_3'))
self.assertTrue(location.hide) # hide as max stage
location.previous_stage()
self.assertEqual(location.stage_id,
self.env.ref('fieldservice.location_stage_2'))
# Test create fsm.location.person, when has if territory has person_ids
self.env.user.company_id.auto_populate_persons_on_location = True
person_ids = [self.env.ref('fieldservice.person_1').id,
self.env.ref('fieldservice.person_2').id,
self.env.ref('fieldservice.person_3').id]
self.test_territory.write({'person_ids': [(6, 0, person_ids)]})
location.territory_id = self.test_territory
self.assertEqual(len(location.person_ids), 0)
location._onchange_territory_id()
self.assertEqual(len(location.person_ids), 3)

def test_fsm_multi_sublocation(self):
""" Test create location with many sub locations
- Test recursion exceptoin
- Test count all equipments, contacts, sublocations
"""
# Test Location > Location 1 > Location 2 > Location 3
self.location_3.fsm_parent_id = self.location_2
self.location_2.fsm_parent_id = self.location_1
self.location_1.fsm_parent_id = self.test_location
# Test sublocation_count of each level
self.assertEqual((self.test_location.sublocation_count,
self.location_1.sublocation_count,
self.location_2.sublocation_count,
self.location_3.sublocation_count), (3, 2, 1, 0))
loc_ids = self.test_location.action_view_sublocation()['domain'][0][2]
loc_1_ids = self.location_1.action_view_sublocation()['domain'][0][2]
loc_2_ids = [self.location_2.action_view_sublocation()['res_id']]
loc_3_ids = self.location_3.action_view_sublocation()['domain'][0][2]
self.assertEqual((len(loc_ids), len(loc_1_ids), len(loc_2_ids),
len(loc_3_ids)), (3, 2, 1, 0))

# Test recursion exception
with self.assertRaises(ValidationError):
self.test_location.fsm_parent_id = self.location_3
self.test_location.fsm_parent_id = False # Set back

# Add equipments on each locations, and test counting
location_vs_num_eq = {self.test_location.id: 1, # Topup = 9
self.location_1.id: 1, # Topup = 8
self.location_2.id: 5, # Topup = 7
self.location_3.id: 2} # Topup = 2
for loc_id, num_eq in location_vs_num_eq.items():
for i in range(num_eq):
self.Equipment.create({
'name': 'Eq-%s-%s' % (str(loc_id), str(i+1)),
'location_id': loc_id,
'current_location_id': loc_id, })
# Test valid equipments at each location
self.assertEqual((self.test_location.equipment_count,
self.location_1.equipment_count,
self.location_2.equipment_count,
self.location_3.equipment_count), (2, 6, 7, 2)) # !!
# Test smart button to open equipment
loc_eq_ids = self.test_location.action_view_equipment()['domain'][0][2]
loc_1_eq_ids = self.location_1.action_view_equipment()['domain'][0][2]
loc_2_eq_ids = self.location_2.action_view_equipment()['domain'][0][2]
loc_3_eq_ids = self.location_3.action_view_equipment()['domain'][0][2]
self.assertEqual((len(loc_eq_ids), len(loc_1_eq_ids),
len(loc_2_eq_ids), len(loc_3_eq_ids)), (9, 8, 7, 2))

# Set service_location_id, on relavant res.partner, test contact count
self.test_loc_partner.service_location_id = self.test_location
self.location_partner_1.service_location_id = self.location_1
self.location_partner_2.service_location_id = self.location_2
self.location_partner_3.service_location_id = self.location_3
# Test valid equipments at each location
self.assertEqual((self.test_location.contact_count,
self.location_1.contact_count,
self.location_2.contact_count,
self.location_3.contact_count), (4, 3, 2, 1))
# Test smart button to open contacts
cont_ids = self.test_location.action_view_contacts()['domain'][0][2]
cont_1_ids = self.location_1.action_view_contacts()['domain'][0][2]
cont_2_ids = self.location_2.action_view_contacts()['domain'][0][2]
cont_3_ids = [self.location_3.action_view_contacts()['res_id']]
self.assertEqual((len(cont_ids), len(cont_1_ids),
len(cont_2_ids), len(cont_3_ids)), (4, 3, 2, 1))

0 comments on commit ffeed8d

Please sign in to comment.