104 lines
4.1 KiB
Python
104 lines
4.1 KiB
Python
"""Controller tests using HttpCase."""
|
|
|
|
import json
|
|
from datetime import date
|
|
|
|
from odoo.tests import tagged
|
|
from odoo.tests.common import HttpCase, new_test_user
|
|
|
|
|
|
@tagged('post_install', '-at_install')
|
|
class TestAssetsController(HttpCase):
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.user = new_test_user(
|
|
self.env, login='assets_test_user',
|
|
groups='base.group_user,account.group_account_invoice',
|
|
)
|
|
|
|
def _jsonrpc(self, endpoint, params):
|
|
self.authenticate('assets_test_user', 'assets_test_user')
|
|
url = f'/fusion/assets/{endpoint}'
|
|
body = {'jsonrpc': '2.0', 'method': 'call', 'params': params, 'id': 1}
|
|
response = self.url_open(
|
|
url, data=json.dumps(body),
|
|
headers={'Content-Type': 'application/json'},
|
|
)
|
|
self.assertEqual(
|
|
response.status_code, 200,
|
|
f"{endpoint} returned {response.status_code}: {response.text[:300]}",
|
|
)
|
|
result = response.json()
|
|
if 'error' in result:
|
|
self.fail(f"{endpoint} errored: {result['error']}")
|
|
return result.get('result', {})
|
|
|
|
def test_list_returns_dict(self):
|
|
result = self._jsonrpc('list', {'company_id': self.env.company.id})
|
|
self.assertIn('assets', result)
|
|
self.assertIn('total', result)
|
|
|
|
def test_get_detail_returns_asset(self):
|
|
asset = self.env['fusion.asset'].create({
|
|
'name': 'Ctrl Test Asset', 'cost': 5000,
|
|
'acquisition_date': date(2026, 1, 1),
|
|
'method': 'straight_line', 'useful_life_years': 5,
|
|
})
|
|
result = self._jsonrpc('get_detail', {'asset_id': asset.id})
|
|
self.assertEqual(result['asset']['id'], asset.id)
|
|
self.assertIn('depreciation_lines', result)
|
|
|
|
def test_compute_schedule_creates_lines(self):
|
|
asset = self.env['fusion.asset'].create({
|
|
'name': 'CompTest', 'cost': 4000,
|
|
'acquisition_date': date(2026, 1, 1),
|
|
'method': 'straight_line', 'useful_life_years': 4,
|
|
})
|
|
result = self._jsonrpc('compute_schedule', {'asset_id': asset.id})
|
|
self.assertEqual(result['lines_created'], 4)
|
|
|
|
def test_post_depreciation_after_running(self):
|
|
asset = self.env['fusion.asset'].create({
|
|
'name': 'PostTest', 'cost': 3000,
|
|
'acquisition_date': date(2026, 1, 1),
|
|
'in_service_date': date(2026, 1, 1),
|
|
'method': 'straight_line', 'useful_life_years': 3,
|
|
})
|
|
self.env['fusion.asset.engine'].compute_depreciation_schedule(asset)
|
|
asset.action_set_running()
|
|
result = self._jsonrpc('post_depreciation', {'asset_id': asset.id})
|
|
self.assertEqual(result['posted_count'], 1)
|
|
|
|
def test_dispose_marks_asset_disposed(self):
|
|
asset = self.env['fusion.asset'].create({
|
|
'name': 'DispTest', 'cost': 6000,
|
|
'acquisition_date': date(2026, 1, 1),
|
|
'in_service_date': date(2026, 1, 1),
|
|
'method': 'straight_line', 'useful_life_years': 3,
|
|
})
|
|
self.env['fusion.asset.engine'].compute_depreciation_schedule(asset)
|
|
asset.action_set_running()
|
|
result = self._jsonrpc('dispose', {
|
|
'asset_id': asset.id, 'sale_amount': 4000,
|
|
'sale_date': '2027-06-01', 'disposal_type': 'sale',
|
|
})
|
|
self.assertIn('disposal_id', result)
|
|
asset.invalidate_recordset(['state'])
|
|
self.assertEqual(asset.state, 'disposed')
|
|
|
|
def test_get_anomalies_returns_list(self):
|
|
result = self._jsonrpc('get_anomalies', {'company_id': self.env.company.id})
|
|
self.assertIn('anomalies', result)
|
|
|
|
def test_suggest_useful_life_returns_dict(self):
|
|
result = self._jsonrpc('suggest_useful_life', {'description': 'Dell laptop'})
|
|
self.assertIn('useful_life_years', result)
|
|
self.assertIn('depreciation_method', result)
|
|
self.assertEqual(result['useful_life_years'], 4)
|
|
|
|
def test_get_partner_history(self):
|
|
partner = self.env['res.partner'].create({'name': 'History Test Partner'})
|
|
result = self._jsonrpc('get_partner_history', {'partner_id': partner.id})
|
|
self.assertEqual(result['partner_id'], partner.id)
|