From 0b0103f3823a6c760cf5cfd897d4069ff9501dc9 Mon Sep 17 00:00:00 2001 From: nans Date: Sun, 11 Oct 2020 20:13:49 +0200 Subject: [PATCH] [IMP] base_partition: add read_per_record method --- base_partition/models/models.py | 8 ++++++++ base_partition/tests/test_partition.py | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/base_partition/models/models.py b/base_partition/models/models.py index 8100969cd..8e73f1d1c 100644 --- a/base_partition/models/models.py +++ b/base_partition/models/models.py @@ -67,6 +67,14 @@ class Base(models.AbstractModel): for i in range(0, len(self), batch_size): yield self[i : i + batch_size] + def read_per_record(self, fields=None, load="_classic_read"): + result = {} + data_list = self.read(fields=fields, load=load) + for d in data_list: + key = d.pop("id") + result[key] = d + return result + def filtered_domain(self, domain): """Backport from standard. """ diff --git a/base_partition/tests/test_partition.py b/base_partition/tests/test_partition.py index 352ac1824..031ed825c 100644 --- a/base_partition/tests/test_partition.py +++ b/base_partition/tests/test_partition.py @@ -116,6 +116,16 @@ class TestPartition(TransactionCase): batches_from_default = list(records.batch()) self.assertEqual(batches_from_default, batches) + def test_read_per_record(self): + categories = self.c1 | self.c2 | self.c3 + field_list = ["name"] + data = categories.read_per_record(field_list) + self.assertEqual(len(data), len(categories)) + for record in categories: + self.assertTrue(record.id in data) + record_data = data[record.id] + self.assertEqual(list(record_data.keys()), field_list) + def test_filtered_domain(self): """Initially yo satisfy the coverage tools, this test actually documents a number of pitfalls of filtered_domain and the differences with a search.