From a99beeab7efc1ba22974083fbe58c3359232cf54 Mon Sep 17 00:00:00 2001 From: wachi Date: Thu, 29 Oct 2026 15:33:51 +0700 Subject: [PATCH] [FIX] ni_observation: compute each observation's own history _compute_history searched with self.patient_id and self.type_id inside the loop, so reading the history of several observations at once (the latest-observation list, an export) gave every row the history of all their types together. The domain also compared type_id with a list using "=", which Odoo 17 and later log as a warning. Use rec and "in". Refs: nirun-life/nirun#181 --- ni_observation/__manifest__.py | 2 +- ni_observation/models/ni_observation.py | 4 +- ni_observation/tests/__init__.py | 1 + .../tests/test_observation_history.py | 56 +++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 ni_observation/tests/test_observation_history.py diff --git a/ni_observation/__manifest__.py b/ni_observation/__manifest__.py index 8d71445d7..a9b527d6e 100644 --- a/ni_observation/__manifest__.py +++ b/ni_observation/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Observation", "summary": "Clinical observations including vital signs, lab results, and measurements", - "version": "16.0.0.6.2", + "version": "16.0.0.6.3", "development_status": "Alpha", "category": "Healthcare", "author": "NSTDA, Piruin P.", diff --git a/ni_observation/models/ni_observation.py b/ni_observation/models/ni_observation.py index c6e2795a6..fbe7d6764 100644 --- a/ni_observation/models/ni_observation.py +++ b/ni_observation/models/ni_observation.py @@ -43,8 +43,8 @@ def _compute_history(self): for rec in self: rec.history_ids = self.env["ni.observation"].search( [ - ("patient_id", "=", self.patient_id.id), - ("type_id", "=", self.type_id.get_display_type().ids), + ("patient_id", "=", rec.patient_id.id), + ("type_id", "in", rec.type_id.get_display_type().ids), ("value", "!=", False), ], order="occurrence desc", diff --git a/ni_observation/tests/__init__.py b/ni_observation/tests/__init__.py index d6030cf19..5fc5280ee 100644 --- a/ni_observation/tests/__init__.py +++ b/ni_observation/tests/__init__.py @@ -3,3 +3,4 @@ from . import test_compute_code from . import test_inverse_value from . import test_latest_observation +from . import test_observation_history diff --git a/ni_observation/tests/test_observation_history.py b/ni_observation/tests/test_observation_history.py new file mode 100644 index 000000000..e1434d46b --- /dev/null +++ b/ni_observation/tests/test_observation_history.py @@ -0,0 +1,56 @@ +# Copyright (c) 2026 NSTDA + +from odoo import fields +from odoo.tests import common + + +class TestObservationHistory(common.TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + partner = cls.env["res.partner"].create({"name": "History Test Patient"}) + cls.patient = cls.env["ni.patient"].create({"partner_id": partner.id}) + cls.encounter = cls.env["ni.encounter"].create( + { + "patient_id": cls.patient.id, + "class_id": cls.env.ref("ni_patient.class_IMP").id, + "period_start": fields.Datetime.now(), + } + ) + cls.type_a, cls.type_b = cls.env["ni.observation.type"].create( + [ + { + "name": f"History Test {code}", + "code": f"test-history-{code}", + "value_type": "int", + "min": 0, + "max": 100, + } + for code in ("a", "b") + ] + ) + + def _observe(self, obs_type, value, occurrence): + return self.env["ni.observation"].create( + { + "patient_id": self.patient.id, + "encounter_id": self.encounter.id, + "type_id": obs_type.id, + "occurrence": occurrence, + "value_int": value, + } + ) + + def test_history_of_several_observations(self): + """Each observation's history holds its own type only, also when + several observations are read together.""" + a1 = self._observe(self.type_a, 1, "2025-01-01 08:00:00") + a2 = self._observe(self.type_a, 2, "2025-01-02 08:00:00") + b1 = self._observe(self.type_b, 3, "2025-01-02 08:00:00") + self.env.invalidate_all() + observations = a2 | b1 + observations.mapped("history_ids") + self.assertEqual(a2.history_ids, a2 | a1) + self.assertEqual(a2.history_count, 2) + self.assertEqual(b1.history_ids, b1) + self.assertEqual(b1.history_count, 1)