Skip to content
This repository has been archived by the owner on Nov 16, 2024. It is now read-only.

Commit

Permalink
test working
Browse files Browse the repository at this point in the history
  • Loading branch information
eccentricOrange authored Jul 13, 2022
1 parent d947a6a commit d443e11
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 91 deletions.
16 changes: 12 additions & 4 deletions npbc_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def validate_undelivered_string(*strings: str) -> None:

# if we get here, all strings passed the regex check

def extract_number(string: str, month: int, year: int) -> date | None:
def extract_number(string: str, month: int, year: int) -> date:
"""if the date is simply a number, it's a single day. so we just identify that date"""

day = int(string)
Expand All @@ -125,6 +125,10 @@ def extract_range(string: str, month: int, year: int) -> Generator[date, None, N
for day in range(start, end + 1):
yield date(year, month, day)

else:
# if we reach here, the check failed and the month doesn't have that many days
raise npbc_exceptions.InvalidUndeliveredString(f'{datetime(year=year, month=month, day=1):%B %Y} does not have days between {start} and {end}.')


def extract_weekday(string: str, month: int, year: int) -> Generator[date, None, None]:
"""if the date is the plural of a weekday name, we identify all dates in that month which are the given weekday"""
Expand All @@ -136,7 +140,7 @@ def extract_weekday(string: str, month: int, year: int) -> Generator[date, None,
yield date(year, month, day)


def extract_nth_weekday(string: str, month: int, year: int) -> date | None:
def extract_nth_weekday(string: str, month: int, year: int) -> date:
"""if the date is a number and a weekday name (singular), we identify the date that is the nth occurrence of the given weekday in the month"""

n, weekday_name = npbc_regex.HYPHEN_SPLIT_REGEX.split(string)
Expand All @@ -159,6 +163,8 @@ def extract_nth_weekday(string: str, month: int, year: int) -> date | None:
# return the date that is the nth occurrence of the given weekday in the month
return valid_dates[n - 1]

# if we reach here, the check failed and the weekday does not occur n times in the month
raise npbc_exceptions.InvalidUndeliveredString(f'{datetime(year=year, month=month, day=1):%B %Y} does not have {n} {weekday_name}s.')

def extract_all(month: int, year: int) -> Generator[date, None, None]:
"""if the text is "all", we identify all the dates in the month"""
Expand Down Expand Up @@ -213,16 +219,18 @@ def parse_undelivered_strings(month: int, year: int, *strings: str) -> set[date]

# check for each of the patterns
for string in strings:
if string:
try:
dates.update(parse_undelivered_string(month, year, string))

except npbc_exceptions.InvalidUndeliveredString:
except npbc_exceptions.InvalidUndeliveredString as e:
print(
f"""Congratulations! You broke the program!
You managed to write a string that the program considers valid, but isn't actually.
Please report it to the developer.
\nThe string you wrote was: {string}
This data has not been counted."""
This data has not been counted.\n
Exact error message: {e}"""
)

return dates
Expand Down
165 changes: 78 additions & 87 deletions test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,75 +13,70 @@


def test_get_number_of_each_weekday():
test_function = npbc_core.get_number_of_each_weekday

assert tuple(test_function(1, 2022)) == (5, 4, 4, 4, 4, 5, 5)
assert tuple(test_function(2, 2022)) == (4, 4, 4, 4, 4, 4, 4)
assert tuple(test_function(3, 2022)) == (4, 5, 5 ,5, 4, 4, 4)
assert tuple(test_function(2, 2020)) == (4, 4, 4, 4, 4, 5, 4)
assert tuple(test_function(12, 1954)) == (4, 4, 5, 5, 5, 4, 4)
assert tuple(npbc_core.get_number_of_each_weekday(1, 2022)) == (5, 4, 4, 4, 4, 5, 5)
assert tuple(npbc_core.get_number_of_each_weekday(2, 2022)) == (4, 4, 4, 4, 4, 4, 4)
assert tuple(npbc_core.get_number_of_each_weekday(3, 2022)) == (4, 5, 5 ,5, 4, 4, 4)
assert tuple(npbc_core.get_number_of_each_weekday(2, 2020)) == (4, 4, 4, 4, 4, 5, 4)
assert tuple(npbc_core.get_number_of_each_weekday(12, 1954)) == (4, 4, 5, 5, 5, 4, 4)


def test_validate_undelivered_string():
test_function = npbc_core.validate_undelivered_string

with raises(InvalidUndeliveredString):
test_function("a")
test_function("monday")
test_function("1-mondays")
test_function("1monday")
test_function("1 monday")
test_function("monday-1")
test_function("monday-1")

test_function("")
test_function("1")
test_function("6")
test_function("31")
test_function("31","")
test_function("3","1")
test_function("3","1","")
test_function("3","1")
test_function("3","1")
test_function("3","1")
test_function("1","2","3-9")
test_function("1","2","3-9","11","12","13-19")
test_function("1","2","3-9","11","12","13-19","21","22","23-29")
test_function("1","2","3-9","11","12","13-19","21","22","23-29","31")
test_function("1","2","3","4","5","6","7","8","9")
test_function("mondays")
test_function("mondays,tuesdays")
test_function("mondays","tuesdays","wednesdays")
test_function("mondays","5-21")
test_function("mondays","5-21","tuesdays","5-21")
test_function("1-monday")
test_function("2-monday")
test_function("all")
test_function("All")
test_function("aLl")
test_function("alL")
test_function("aLL")
test_function("ALL")
npbc_core.validate_undelivered_string("a")
npbc_core.validate_undelivered_string("monday")
npbc_core.validate_undelivered_string("1-mondays")
npbc_core.validate_undelivered_string("1monday")
npbc_core.validate_undelivered_string("1 monday")
npbc_core.validate_undelivered_string("monday-1")
npbc_core.validate_undelivered_string("monday-1")

npbc_core.validate_undelivered_string("")
npbc_core.validate_undelivered_string("1")
npbc_core.validate_undelivered_string("6")
npbc_core.validate_undelivered_string("31")
npbc_core.validate_undelivered_string("31","")
npbc_core.validate_undelivered_string("3","1")
npbc_core.validate_undelivered_string("3","1","")
npbc_core.validate_undelivered_string("3","1")
npbc_core.validate_undelivered_string("3","1")
npbc_core.validate_undelivered_string("3","1")
npbc_core.validate_undelivered_string("1","2","3-9")
npbc_core.validate_undelivered_string("1","2","3-9","11","12","13-19")
npbc_core.validate_undelivered_string("1","2","3-9","11","12","13-19","21","22","23-29")
npbc_core.validate_undelivered_string("1","2","3-9","11","12","13-19","21","22","23-29","31")
npbc_core.validate_undelivered_string("1","2","3","4","5","6","7","8","9")
npbc_core.validate_undelivered_string("mondays")
npbc_core.validate_undelivered_string("mondays,tuesdays")
npbc_core.validate_undelivered_string("mondays","tuesdays","wednesdays")
npbc_core.validate_undelivered_string("mondays","5-21")
npbc_core.validate_undelivered_string("mondays","5-21","tuesdays","5-21")
npbc_core.validate_undelivered_string("1-monday")
npbc_core.validate_undelivered_string("2-monday")
npbc_core.validate_undelivered_string("all")
npbc_core.validate_undelivered_string("All")
npbc_core.validate_undelivered_string("aLl")
npbc_core.validate_undelivered_string("alL")
npbc_core.validate_undelivered_string("aLL")
npbc_core.validate_undelivered_string("ALL")


def test_undelivered_string_parsing():
MONTH = 5
YEAR = 2017
test_function = npbc_core.parse_undelivered_strings


assert test_function(MONTH, YEAR, '') == set(())
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '') == set(())

assert test_function(MONTH, YEAR, '1') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '1') == set((
date(year=YEAR, month=MONTH, day=1),
))

assert test_function(MONTH, YEAR, '1-2') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '1-2') == set((
date(year=YEAR, month=MONTH, day=1),
date(year=YEAR, month=MONTH, day=2)
))

assert test_function(MONTH, YEAR, '5-17') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '5-17') == set((
date(year=YEAR, month=MONTH, day=5),
date(year=YEAR, month=MONTH, day=6),
date(year=YEAR, month=MONTH, day=7),
Expand All @@ -97,7 +92,7 @@ def test_undelivered_string_parsing():
date(year=YEAR, month=MONTH, day=17)
))

assert test_function(MONTH, YEAR, '5-17', '19') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '5-17', '19') == set((
date(year=YEAR, month=MONTH, day=5),
date(year=YEAR, month=MONTH, day=6),
date(year=YEAR, month=MONTH, day=7),
Expand All @@ -114,7 +109,7 @@ def test_undelivered_string_parsing():
date(year=YEAR, month=MONTH, day=19)
))

assert test_function(MONTH, YEAR, '5-17', '19-21') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '5-17', '19-21') == set((
date(year=YEAR, month=MONTH, day=5),
date(year=YEAR, month=MONTH, day=6),
date(year=YEAR, month=MONTH, day=7),
Expand All @@ -133,7 +128,7 @@ def test_undelivered_string_parsing():
date(year=YEAR, month=MONTH, day=21)
))

assert test_function(MONTH, YEAR, '5-17', '19-21', '23') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '5-17', '19-21', '23') == set((
date(year=YEAR, month=MONTH, day=5),
date(year=YEAR, month=MONTH, day=6),
date(year=YEAR, month=MONTH, day=7),
Expand All @@ -153,15 +148,15 @@ def test_undelivered_string_parsing():
date(year=YEAR, month=MONTH, day=23)
))

assert test_function(MONTH, YEAR, 'mondays') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, 'mondays') == set((
date(year=YEAR, month=MONTH, day=1),
date(year=YEAR, month=MONTH, day=8),
date(year=YEAR, month=MONTH, day=15),
date(year=YEAR, month=MONTH, day=22),
date(year=YEAR, month=MONTH, day=29)
))

assert test_function(MONTH, YEAR, 'mondays', 'wednesdays') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, 'mondays', 'wednesdays') == set((
date(year=YEAR, month=MONTH, day=1),
date(year=YEAR, month=MONTH, day=8),
date(year=YEAR, month=MONTH, day=15),
Expand All @@ -174,11 +169,11 @@ def test_undelivered_string_parsing():
date(year=YEAR, month=MONTH, day=31)
))

assert test_function(MONTH, YEAR, '2-monday') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '2-monday') == set((
date(year=YEAR, month=MONTH, day=8),
))

assert test_function(MONTH, YEAR, '2-monday', '3-wednesday') == set((
assert npbc_core.parse_undelivered_strings(MONTH, YEAR, '2-monday', '3-wednesday') == set((
date(year=YEAR, month=MONTH, day=8),
date(year=YEAR, month=MONTH, day=17)
))
Expand All @@ -192,30 +187,28 @@ def test_calculating_cost_of_one_paper():
array((False, False, True, True, True, False, True))
)

test_function = npbc_core.calculate_cost_of_one_paper

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set(()),
*COST_AND_DELIVERY_DATA
) == 41

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set(()),
array((0, 0, 2, 2, 5, 0, 1)),
array((False, False, True, True, True, False, False))
) == 36

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set((
date(year=2022, month=1, day=8),
)),
*COST_AND_DELIVERY_DATA
) == 41

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set((
date(year=2022, month=1, day=8),
Expand All @@ -224,7 +217,7 @@ def test_calculating_cost_of_one_paper():
*COST_AND_DELIVERY_DATA
) == 41

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set((
date(year=2022, month=1, day=8),
Expand All @@ -233,15 +226,15 @@ def test_calculating_cost_of_one_paper():
*COST_AND_DELIVERY_DATA
) == 41

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set((
date(year=2022, month=1, day=2),
)),
*COST_AND_DELIVERY_DATA
) == 40

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set((
date(year=2022, month=1, day=2),
Expand All @@ -250,7 +243,7 @@ def test_calculating_cost_of_one_paper():
*COST_AND_DELIVERY_DATA
) == 40

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set((
date(year=2022, month=1, day=6),
Expand All @@ -259,7 +252,7 @@ def test_calculating_cost_of_one_paper():
*COST_AND_DELIVERY_DATA
) == 34

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set((
date(year=2022, month=1, day=6),
Expand All @@ -269,7 +262,7 @@ def test_calculating_cost_of_one_paper():
*COST_AND_DELIVERY_DATA
) == 34

assert test_function(
assert npbc_core.calculate_cost_of_one_paper(
DAYS_PER_WEEK,
set((
date(year=2022, month=1, day=6),
Expand All @@ -285,23 +278,21 @@ def test_calculating_cost_of_one_paper():


def test_validate_month_and_year():
test_function = npbc_core.validate_month_and_year

test_function(1, 2020)
test_function(12, 2020)
test_function(1, 2021)
test_function(12, 2021)
test_function(1, 2022)
test_function(12, 2022)
npbc_core.validate_month_and_year(1, 2020)
npbc_core.validate_month_and_year(12, 2020)
npbc_core.validate_month_and_year(1, 2021)
npbc_core.validate_month_and_year(12, 2021)
npbc_core.validate_month_and_year(1, 2022)
npbc_core.validate_month_and_year(12, 2022)

with raises(InvalidMonthYear):
test_function(-54, 2020)
test_function(0, 2020)
test_function(13, 2020)
test_function(45, 2020)
test_function(1, -5)
test_function(12, -5)
test_function(1.6, 10) # type: ignore
test_function(12.6, 10) # type: ignore
test_function(1, '10') # type: ignore
test_function(12, '10') # type: ignore
npbc_core.validate_month_and_year(-54, 2020)
npbc_core.validate_month_and_year(0, 2020)
npbc_core.validate_month_and_year(13, 2020)
npbc_core.validate_month_and_year(45, 2020)
npbc_core.validate_month_and_year(1, -5)
npbc_core.validate_month_and_year(12, -5)
npbc_core.validate_month_and_year(1.6, 10) # type: ignore
npbc_core.validate_month_and_year(12.6, 10) # type: ignore
npbc_core.validate_month_and_year(1, '10') # type: ignore
npbc_core.validate_month_and_year(12, '10') # type: ignore

0 comments on commit d443e11

Please sign in to comment.