Skip to content

Commit

Permalink
Merge pull request #43 from statisticsnorway/timestamp_support
Browse files Browse the repository at this point in the history
Timestamp support
  • Loading branch information
joxssb authored Aug 9, 2024
2 parents f1b0735 + b1e946f commit 419186b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ssb-konjunk"
version = "0.1.0"
version = "0.1.1"
description = "SSB Konjunk"
authors = ["Edvard Garmannslund <[email protected]>"]
license = "MIT"
Expand Down
62 changes: 56 additions & 6 deletions src/ssb_konjunk/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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])
Expand All @@ -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}"
Expand All @@ -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"
)


Expand Down Expand Up @@ -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:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 419186b

Please sign in to comment.