[FIX] web: sampleserver generates __range

The sample server did not create a __range value when grouping by 'date'
This resulted in a traceback as it is expected to at least be false.

This commit computes it properly.

task-3098681

closes odoo/odoo#113163

X-original-commit: cf074f1dc01da39e75aa30e4ac21eafa29d528d9
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Renaud Thiry
2023-02-20 19:25:04 +01:00
parent b41e5cb1df
commit 689aa32ee3
2 changed files with 49 additions and 2 deletions
+28 -2
View File
@@ -1,6 +1,12 @@
/** @odoo-module **/
import { parseDate, serializeDate, serializeDateTime } from "@web/core/l10n/dates";
import {
deserializeDate,
deserializeDateTime,
parseDate,
serializeDate,
serializeDateTime,
} from "@web/core/l10n/dates";
import { ORM } from "@web/core/orm_service";
import { registry } from "@web/core/registry";
import { groupBy as arrayGroupBy, sortBy as arraySortBy } from "@web/core/utils/arrays";
@@ -487,8 +493,21 @@ export class SampleServer {
group[countKey] = records.length;
const firstElem = records[0];
for (const gb of normalizedGroupBys) {
const { alias, fieldName } = gb;
const { alias, fieldName, type } = gb;
group[alias] = this._formatValue(firstElem[fieldName], gb);
if (["date", "datetime"].includes(type)) {
group.__range = {};
const val = firstElem[fieldName];
if (val) {
const deserialize = type === "date" ? deserializeDate : deserializeDateTime;
const serialize = type === "date" ? serializeDate : serializeDateTime;
const from = deserialize(val).startOf(gb.interval);
const to = SampleServer.INTERVALS[gb.interval](from);
group.__range[alias] = { from: serialize(from), to: serialize(to) };
} else {
group.__range[alias] = false;
}
}
}
Object.assign(group, this._aggregateFields(measures, records));
result.push(group);
@@ -725,6 +744,13 @@ SampleServer.FORMATS = {
quarter: "'Q'q yyyy",
year: "y",
};
SampleServer.INTERVALS = {
day: (dt) => dt.plus({ days: 1 }),
week: (dt) => dt.plus({ weeks: 1 }),
month: (dt) => dt.plus({ months: 1 }),
quarter: (dt) => dt.plus({ months: 3 }),
year: (dt) => dt.plus({ years: 1 }),
};
SampleServer.DISPLAY_FORMATS = Object.assign({}, SampleServer.FORMATS, { day: "dd MMM yyyy" });
SampleServer.MAIN_RECORDSET_SIZE = 16;
@@ -6951,6 +6951,27 @@ QUnit.module("Views", (hooks) => {
assert.ok(document.activeElement.dataset.name === "foo");
});
QUnit.test("empty list with sample data: group by date", async (assert) => {
await makeView({
type: "list",
arch: `
<tree sample="1">
<field name="date"/>
</tree>`,
serverData,
domain: Domain.FALSE.toList(),
resModel: "foo",
groupBy: ["date:day"],
});
assert.containsOnce(target, ".o_list_view .o_view_sample_data");
const groupHeaders = [...target.querySelectorAll(".o_group_header")];
assert.ok(groupHeaders.length);
await click(target.querySelector(".o_group_has_content.o_group_header"));
assert.containsN(target, ".o_data_row", 4);
});
QUnit.test("non empty list with sample data", async function (assert) {
await makeView({
type: "list",