-
Notifications
You must be signed in to change notification settings - Fork 574
fix(usage): count whole years when resolving the billing period #8513
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fbf7294
fix(usage): count whole years when resolving the billing period
talissoncosta 1a33ab2
test(usage): split the Given/When/Then comments the linter wants
talissoncosta b3cca30
chore: Update documentation artefacts
talissoncosta 9025cc9
test(usage): assert the previous period's end in February
talissoncosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from datetime import datetime | ||
|
|
||
| from dateutil.relativedelta import relativedelta | ||
|
|
||
|
|
||
| def months_elapsed(since: datetime, now: datetime) -> int: | ||
| """Whole months between two datetimes, years included.""" | ||
| elapsed = relativedelta(now, since) | ||
| return elapsed.years * 12 + elapsed.months | ||
|
|
||
|
|
||
| def period_start(billing_term_starts_at: datetime, now: datetime) -> datetime: | ||
| """ | ||
| Start of the monthly allowance window a term is currently in. A term can | ||
| run longer than a month, so this is the most recent monthly anniversary of | ||
| its start. | ||
| """ | ||
| return billing_term_starts_at + relativedelta( | ||
| months=months_elapsed(billing_term_starts_at, now) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
api/tests/unit/organisations/test_unit_organisations_billing_periods.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
|
|
||
| from organisations.billing_periods import months_elapsed, period_start | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "since, now, expected", | ||
| [ | ||
| ("2026-09-01T00:00:00+00:00", "2026-09-10T00:00:00+00:00", 0), | ||
| ("2026-01-05T00:00:00+00:00", "2026-09-10T00:00:00+00:00", 8), | ||
| # The year is the part #6099 dropped. | ||
| ("2024-09-03T00:00:00+00:00", "2026-09-10T00:00:00+00:00", 24), | ||
| ("2025-08-07T00:00:00+00:00", "2026-09-11T00:00:00+00:00", 13), | ||
| ], | ||
| ) | ||
| def test_months_elapsed__spans_years__counts_them( | ||
| since: str, now: str, expected: int | ||
| ) -> None: | ||
| # Given / When | ||
| elapsed = months_elapsed(datetime.fromisoformat(since), datetime.fromisoformat(now)) | ||
|
|
||
| # Then | ||
| assert elapsed == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "term_starts_at, now, expected", | ||
| [ | ||
| # Annual term over a year old. Dropping the year lands in 2025. | ||
| ( | ||
| "2024-09-03T00:00:00+00:00", | ||
| "2026-09-10T00:00:00+00:00", | ||
| "2026-09-03T00:00:00+00:00", | ||
| ), | ||
| # February is too short for a 31st, so the window opens on the 28th. | ||
| ( | ||
| "2026-01-31T00:00:00+00:00", | ||
| "2026-03-01T00:00:00+00:00", | ||
| "2026-02-28T00:00:00+00:00", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_period_start__long_term__opens_on_the_latest_anniversary( | ||
| term_starts_at: str, now: str, expected: str | ||
| ) -> None: | ||
| # Given / When | ||
| start = period_start( | ||
| datetime.fromisoformat(term_starts_at), datetime.fromisoformat(now) | ||
| ) | ||
|
|
||
| # Then | ||
| assert start == datetime.fromisoformat(expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.