diff --git a/pyproject.toml b/pyproject.toml index 6116461..33a0eda 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ssb-konjunk" -version = "0.1.0" +version = "0.1.1" description = "SSB Konjunk" authors = ["Edvard Garmannslund "] license = "MIT" diff --git a/src/ssb_konjunk/timestamp.py b/src/ssb_konjunk/timestamp.py index c27d6bc..009d7ad 100644 --- a/src/ssb_konjunk/timestamp.py +++ b/src/ssb_konjunk/timestamp.py @@ -18,6 +18,18 @@ def _check_valid_day(day: int) -> None: ) +def _check_valid_week(week: int) -> None: + """Function to check that day arg is valid.""" + if week > 52: + raise ValueError( + f"The arg for week is bigger than possible max is 52 you have: {week}." + ) + if week < 1: + raise ValueError( + f"The arg for week is smaller than possible min is 1 you have: {week}." + ) + + def _check_valid_month(month: int) -> None: """Function to check that month arg is valid.""" if month > 12: @@ -54,6 +66,30 @@ def _check_valid_quarter(quarter: int) -> None: ) +def _check_valid_trimester(trimester: int) -> None: + """Function to check that day arg is valid.""" + if trimester > 3: + raise ValueError( + f"The arg for trimester is bigger than possible max is 3 you have: {trimester}." + ) + if trimester < 1: + raise ValueError( + f"The arg for trimester is smaller than possible min is 1 you have: {trimester}." + ) + + +def _check_valid_half_year(half_year: int) -> None: + """Function to check that day arg is valid.""" + if half_year > 2: + raise ValueError( + f"The arg for half_year is bigger than possible max is 2 you have: {half_year}." + ) + if half_year < 1: + raise ValueError( + f"The arg for half_year is smaller than possible min is 1 you have: {half_year}." + ) + + def _check_valid_year(year1: int, year2: int | None = None) -> None: """Function to check that year is valid.""" if len(str(year1)) != 4: @@ -76,14 +112,23 @@ def _check_valid_args(*args: int, frequency: str) -> None: """Function to check if valid args.""" if len(args) == 2: _check_valid_year(args[0]) + if frequency == "W": + _check_valid_week(args[1]) if frequency == "M": _check_valid_month(args[1]) if frequency == "B": _check_valid_term(args[1]) if frequency == "Q": _check_valid_quarter(args[1]) + if frequency == "T": + _check_valid_trimester(args[1]) + if frequency == "H": + _check_valid_half_year(args[1]) elif len(args) == 4: _check_valid_year(args[0], args[2]) + if frequency == "W": + _check_valid_week(args[1]) + _check_valid_week(args[3]) if frequency == "M": _check_valid_month(args[1]) _check_valid_month(args[3]) @@ -93,7 +138,12 @@ def _check_valid_args(*args: int, frequency: str) -> None: if frequency == "Q": _check_valid_quarter(args[1]) _check_valid_quarter(args[3]) - + if frequency == "T": + _check_valid_trimester(args[1]) + _check_valid_trimester(args[3]) + if frequency == "H": + _check_valid_half_year(args[1]) + _check_valid_half_year(args[3]) else: raise ValueError( f"You have the wrong number of args, youre args are {args}. You can have 2 or 4 for frequency: {frequency}" @@ -102,9 +152,9 @@ def _check_valid_args(*args: int, frequency: str) -> None: def _check_frequency_suport(frequency: str) -> None: """Function to check if frequency requested is supported.""" - if frequency not in ["Y", "Q", "B", "M", "D"]: + if frequency not in ["Y", "Q", "B", "M", "D", "W", "T", "H"]: raise ValueError( - f"The function does not support frequency: {frequency} yet. Please use one the supported ones: Y,Q,B,M,D" + f"The function does not support frequency: {frequency} yet. Please use one the supported ones: Y,Q,B,M,D,W,T,H" ) @@ -151,9 +201,9 @@ def get_timestamp_special(*args: int, frequency: str) -> str | None: """ _check_valid_args(*args, frequency=frequency) - if frequency == "M": - frequency = "-" - + if frequency in ["M", "W"]: + if frequency == "M": + frequency = "-" if len(args) == 2: return f"p{args[0]}{frequency}{args[1]:02}" elif len(args) == 4: diff --git a/tests/test_timestamp.py b/tests/test_timestamp.py index a87a2c9..f00cf23 100644 --- a/tests/test_timestamp.py +++ b/tests/test_timestamp.py @@ -3,9 +3,12 @@ import pytest from ssb_konjunk.timestamp import _check_valid_day +from ssb_konjunk.timestamp import _check_valid_half_year from ssb_konjunk.timestamp import _check_valid_month from ssb_konjunk.timestamp import _check_valid_quarter from ssb_konjunk.timestamp import _check_valid_term +from ssb_konjunk.timestamp import _check_valid_trimester +from ssb_konjunk.timestamp import _check_valid_week from ssb_konjunk.timestamp import _check_valid_year from ssb_konjunk.timestamp import check_even from ssb_konjunk.timestamp import get_ssb_timestamp @@ -46,6 +49,9 @@ def test_test_get_timestamp_special() -> None: def test_get_ssb_timestamp() -> None: """Test of function get_ssb_timestamp.""" + # Testing week + assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="W") == "p2020W01_p2021W02" + assert get_ssb_timestamp(2020, 1, frequency="W") == "p2020W01" # Testing month assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="M") == "p2020-01_p2021-02" assert get_ssb_timestamp(2020, 1) == "p2020-01" @@ -55,6 +61,12 @@ def test_get_ssb_timestamp() -> None: # Testing term assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="B") == "p2020B1_p2021B2" assert get_ssb_timestamp(2020, 1, frequency="B") == "p2020B1" + # Testing trimester + assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="T") == "p2020T1_p2021T2" + assert get_ssb_timestamp(2020, 1, frequency="T") == "p2020T1" + # Testing half year + assert get_ssb_timestamp(2020, 1, 2021, 2, frequency="H") == "p2020H1_p2021H2" + assert get_ssb_timestamp(2020, 1, frequency="H") == "p2020H1" # Testing day assert ( get_ssb_timestamp(2020, 1, 1, 2020, 1, 31, frequency="D") @@ -90,6 +102,16 @@ def test_check_valid_day() -> None: _check_valid_day(day) +def test_check_valid_week() -> None: + """Test of function _check_valid_week.""" + week = 53 + with pytest.raises( + ValueError, + match=f"The arg for week is bigger than possible max is 52 you have: {week}.", + ): + _check_valid_week(week) + + def test_check_valid_month() -> None: """Test of function _check_valid_month.""" month = 13 @@ -120,6 +142,26 @@ def test_check_valid_quarter() -> None: _check_valid_quarter(quarter) +def test_check_valid_trimester() -> None: + """Test of function _check_valid_trimester.""" + trimester = 5 + with pytest.raises( + ValueError, + match=f"The arg for trimester is bigger than possible max is 3 you have: {trimester}.", + ): + _check_valid_trimester(trimester) + + +def test_check_valid_half_year() -> None: + """Test of function _check_valid_half_year.""" + half_year = 5 + with pytest.raises( + ValueError, + match=f"The arg for half_year is bigger than possible max is 2 you have: {half_year}.", + ): + _check_valid_half_year(half_year) + + def test_check_valid_year() -> None: """Test of function _check_valid_year.""" year1 = 2030