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

Fjernet next_month la til delta_month #11

Merged
merged 2 commits into from
Feb 22, 2024
Merged
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
59 changes: 32 additions & 27 deletions src/ssb_konjunk/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import re
from calendar import monthrange
from datetime import datetime
from datetime import timedelta


def _input_valid_int() -> int:
Expand Down Expand Up @@ -129,32 +128,6 @@ def extract_start_end_dates(file_name: str) -> tuple[datetime, datetime]:
return start_date, end_date


def next_month(desired_year: int, desired_month: int) -> str:
"""Get the next month of a given year and month.

Args:
desired_year: year
desired_month: month

Returns:
str: next month in the format 'YYYY-MM'
"""
# Create a datetime object for the desired year and month
current_date = datetime(desired_year, desired_month, 1)

# Add one month to the current date
next_date = current_date + timedelta(days=32)

# Get the year and month of the next date
next_year = next_date.year
next_month = next_date.month

# Format the year and month as 'YYYY-MM'
next_month_str = f"{next_year}-{next_month:02d}"

return next_month_str


def months_in_term(term: int) -> tuple[int, int]:
"""Gives out months as ints from term as int.

Expand Down Expand Up @@ -198,3 +171,35 @@ def find_file_for_month_daily(
):
break
return file


def delta_month(month: int, periods: int) -> int:
"""Function to shift month backwards or forward.

Args:
month: Current month you are using.
periods: Periods you want to move, can be positive or negative int.

Returns:
int: Month we have shifted to.

Raises:
ValueError: If periods are are more or less than a year.
ValueError: If period is 0.
"""
if periods < -11 or periods > 11:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Legg inn sjekk på at måned er valid (mellom 1 og 12, og int) og at period er int?

Valideringsfunksjon for måned har jeg div varianter av, kan samle til en og legge inn

raise ValueError(
"Input periods must be between -11 and 11. If youre doing a going a year back in time, please change year."
)
elif periods == 0:
raise ValueError("Input periods is 0, you should remove the function!")
else:
# Taking period adding periods.
new_month = month + (periods)
# If new_month is above twelve, i take negative twelve to get month from new year.
if new_month > 12:
new_month = new_month - 12
# If new_month is below 1, I take 12 negative new_month. This should give the right month from last year.
elif new_month < 1:
new_month = new_month + 12
return new_month
38 changes: 15 additions & 23 deletions tests/test_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pytest

from ssb_konjunk.prompts import days_in_month
from ssb_konjunk.prompts import delta_month
from ssb_konjunk.prompts import extract_start_end_dates
from ssb_konjunk.prompts import next_month

"""Test of function days in month"""

Expand Down Expand Up @@ -73,28 +73,20 @@ def test_extract_start_end_dates_valid() -> None:
assert end_date == expected_end_date


"""Tests of function next_month"""
"""Test of function delta_month"""


def test_next_month_valid() -> None:
# Test with a valid year and month
desired_year = 2023
desired_month = 11
expected_next_month = "2023-12"

assert next_month(desired_year, desired_month) == expected_next_month


def test_next_month_edge_case() -> None:
# Test with December, expecting January of the next year
desired_year = 2022
desired_month = 12
expected_next_month = "2023-01"

assert next_month(desired_year, desired_month) == expected_next_month

def test_delta_month() -> None:
# Test for valid change forward in time
assert delta_month(6, 3) == 9
# Test for valid change backwards in time.
assert delta_month(6, -3) == 3
# Test for valid change past 12.
assert delta_month(11, 3) == 2
# Test for valid change past 1.
assert delta_month(1, -2) == 11

def test_next_month_invalid() -> None:
# Test with an invalid month (13), expecting an error
with pytest.raises(ValueError, match="month must be in 1..12"):
next_month(2022, 13)
with pytest.raises(ValueError):
delta_month(12, -12)
with pytest.raises(ValueError):
delta_month(12, 0)