Skip to content

[18.0][FIX] account_financial_risk: Freeze time and match the module's own date reference in tests - #575

Merged
OCA-git-bot merged 1 commit into
OCA:18.0from
Tecnativa:18.0-FIX-account_financial_risk_timezone_flaky_test
Sep 16, 2026
Merged

OCA-git-bot merged 1 commit into
OCA:18.0from
Tecnativa:18.0-FIX-account_financial_risk_timezone_flaky_test

Conversation

@carlosdauden

Copy link
Copy Markdown
Contributor

_max_risk_date_due() computes the "today" boundary with fields.Date.context_today() (timezone-aware: this env's admin user defaults to Europe/Brussels), while the tests date-stamp their records with the naive fields.Date.today(). Whenever the real wall-clock instant a test runs at falls within the user's timezone offset window around midnight UTC, the two disagree by a day, pushing a record dated "today" out of the "open" (not yet due) bucket the assertions expect it in - reproduced 2026-09-09 close to midnight UTC, and confirmed reproducible on demand by freezing at an unsafe instant (23:30 UTC) before settling on a safe one.

Two changes, both needed: freeze time at a fixed, safe instant (noon UTC, clear of any realistic timezone offset crossing a day boundary) with freezegun's @freeze_time, already used the same way throughout this codebase for date-dependent OCA tests, so the test's result no longer depends on real wall-clock time at all; and use
fields.Date.context_today(cls.env.user) instead of the naive fields.Date.today() for the test's own date references, so it exercises the exact same date computation _max_risk_date_due() itself uses rather than one that merely happens to agree with it under the current freeze.

@Tecnativa

ping @sergio-teruel @carlos-lopez-tecnativa @juancarlosonate-tecnativa @CarlosRoca13

…date reference in tests

_max_risk_date_due() computes the "today" boundary with
fields.Date.context_today() (timezone-aware: this env's admin user
defaults to Europe/Brussels), while the tests date-stamp their records
with the naive fields.Date.today(). Whenever the real wall-clock instant a
test runs at falls within the user's timezone offset window around
midnight UTC, the two disagree by a day, pushing a record dated "today"
out of the "open" (not yet due) bucket the assertions expect it in -
reproduced 2026-09-09 close to midnight UTC, and confirmed reproducible on
demand by freezing at an unsafe instant (23:30 UTC) before settling on a
safe one.

Two changes, both needed: freeze time at a fixed, safe instant (noon UTC,
clear of any realistic timezone offset crossing a day boundary) with
freezegun's @freeze_time, already used the same way throughout this
codebase for date-dependent OCA tests, so the test's result no longer
depends on real wall-clock time at all; and use
fields.Date.context_today(cls.env.user) instead of the naive
fields.Date.today() for the test's own date references, so it exercises
the exact same date computation _max_risk_date_due() itself uses rather
than one that merely happens to agree with it under the current freeze.
@OCA-git-bot OCA-git-bot added series:18.0 mod:account_financial_risk Module account_financial_risk labels Sep 10, 2026
@carlosdauden carlosdauden changed the title [FIX] account_financial_risk: Freeze time and match the module's own date reference in tests [18.0][FIX] account_financial_risk: Freeze time and match the module's own date reference in tests Sep 10, 2026

@juancarlosonate-tecnativa juancarlosonate-tecnativa left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@OCA-git-bot

Copy link
Copy Markdown
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

@pedrobaeza pedrobaeza added this to the 18.0 milestone Sep 15, 2026
from odoo.addons.base.tests.common import BaseCommon


@freeze_time("2024-01-15 12:00:00")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the decorator at class level has some side effects that @victoralmau can say that he has already suffered in some cases. Please put the decorator at methods level instead (including setUpClass if needed).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The side effect I encountered with this is that if a test was run with “post-install” and “freeze_time” was defined, the rest of the (post-install) tests that ran afterward used that date.

It seems that this test is not a “post-install” test and won't have that problem.

In any case, I don't exactly remember right now the version of Odoo in which the issue I described occurred.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanations. Let's continue this way if you say there's no problem, although maybe for consistency would be interesting to unify them.

@pedrobaeza

Copy link
Copy Markdown
Member

/ocabot merge patch

@OCA-git-bot

Copy link
Copy Markdown
Contributor

This PR looks fantastic, let's merge it!
Prepared branch 18.0-ocabot-merge-pr-575-by-pedrobaeza-bump-patch, awaiting test results.

@OCA-git-bot
OCA-git-bot merged commit fde367a into OCA:18.0 Sep 16, 2026
7 checks passed
@OCA-git-bot

Copy link
Copy Markdown
Contributor

Congratulations, your PR was merged at 3709eda. Thanks a lot for contributing to OCA. ❤️

@pedrobaeza
pedrobaeza deleted the 18.0-FIX-account_financial_risk_timezone_flaky_test branch September 16, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants