Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0] connector_search_engine: add binding hooks to recompute and validate + include update TS #195

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions connector_search_engine/models/se_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,10 @@ def recompute_json(self, force_export: bool = False):
continue

record = self._contextualize(record)
index = record.index_id

old_data = record.data
try:
with self.env.cr.savepoint():
record.data = index.model_serializer.serialize(record.record)
record.date_recomputed = fields.Datetime.now()
record._recompute_data()
except Error as pg_error:
# PG error could make the cursor unusable
raise pg_error
Expand All @@ -185,7 +182,7 @@ def recompute_json(self, force_export: bool = False):
record.error = str(e)
continue
try:
index.json_validator.validate(record.data or {})
record._validate_data()
except ValidationError as e:
record.state = "invalid_data"
record.error = str(e)
Expand All @@ -196,6 +193,15 @@ def recompute_json(self, force_export: bool = False):
record.state = "done"
record.error = ""

def _recompute_data(self):
self.date_recomputed = fields.Datetime.now()
data = self.index_id.model_serializer.serialize(self.record)
data["updated_on"] = fields.Datetime.to_string(self.date_recomputed)
lmignon marked this conversation as resolved.
Show resolved Hide resolved
self.data = data

def _validate_data(self):
self.index_id.json_validator.validate(self.data or {})

def export_record(self) -> str:
for _backend, bindings in self.partition("backend_id").items():
adapter = bindings[0].index_id.se_adapter
Expand Down
8 changes: 6 additions & 2 deletions connector_search_engine/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,16 @@ def test_archive_unarchive_record(self):
def test_recompute_one_record(self):
self.partner_binding.recompute_json()
self.assertEqual(self.partner_binding.state, "to_export")
self.assertEqual(self.partner_binding.get_export_data(), self.partner_expected)
data = self.partner_binding.get_export_data()
data.pop("updated_on")
self.assertEqual(data, self.partner_expected)
self.assertTrue(self.partner_binding.date_recomputed)

def test_recompute_all_indexes(self):
self.env["se.index"].recompute_all_index()
self.assertEqual(self.partner_binding.get_export_data(), self.partner_expected)
data = self.partner_binding.get_export_data()
data.pop("updated_on")
self.assertEqual(data, self.partner_expected)
self.assertEqual(self.partner_binding.state, "to_export")
self.assertTrue(self.partner_binding.date_recomputed)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ def test_changing_model_remove_exporter(self):

def test_serialize(self):
self.partner_binding.recompute_json()
data = self.partner_binding.data
data.pop("updated_on")
self.assertEqual(
self.partner_binding.data,
data,
{
"active": True,
"child_ids": [
Expand Down
Loading