Skip to content

Commit 020aca1

Browse files
committed
Added new goldenfile tests for lasagna import errors.
1 parent 71f6dfe commit 020aca1

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Functions used in preparing Guido's gorgeous lasagna.
2+
3+
Learn about Guido, the creator of the Python language: https://en.wikipedia.org/wiki/Guido_van_Rossum
4+
"""
5+
6+
# TODO: define the 'EXPECTED_BAKE_TIME' constant
7+
# TODO: consider defining the 'PREPARATION_TIME' constant
8+
# equal to the time it takes to prepare a single layer
9+
10+
11+
# TODO: define the 'bake_time_remaining()' function
12+
def bake_time_remaining():
13+
"""Calculate the bake time remaining.
14+
15+
:param elapsed_bake_time: int - baking time already elapsed.
16+
:return: int - remaining bake time (in minutes) derived from 'EXPECTED_BAKE_TIME'.
17+
18+
Function that takes the actual minutes the lasagna has been in the oven as
19+
an argument and returns how many minutes the lasagna still needs to bake
20+
based on the `EXPECTED_BAKE_TIME`.
21+
"""
22+
23+
pass
24+
25+
26+
# TODO: define the 'preparation_time_in_minutes()' function
27+
# and consider using 'PREPARATION_TIME' here
28+
29+
30+
# TODO: define the 'elapsed_time_in_minutes()' function
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
import pytest
3+
4+
# For this first exercise, it is really important to be clear about how we are importing names for tests.
5+
# To that end, we are putting a try/catch around imports and throwing specific messages to help students
6+
# decode that they need to create and title their constants and functions in a specific way.
7+
try:
8+
from example_lasagna_constant_import_error import (EXPECTED_BAKE_TIME,
9+
bake_time_remaining,
10+
preparation_time_in_minutes,
11+
elapsed_time_in_minutes)
12+
13+
# Here, we are separating the constant import errors from the function name import errors
14+
except ImportError as import_fail:
15+
message = import_fail.args[0].split('(', maxsplit=1)
16+
item_name = import_fail.args[0].split()[3]
17+
18+
if 'EXPECTED_BAKE_TIME' in item_name:
19+
# pylint: disable=raise-missing-from
20+
raise ImportError(f'\n\nMISSING CONSTANT --> \nWe can not find or import the constant {item_name} in your'
21+
" 'lasagna.py' file.\nDid you misname or forget to define it?") from None
22+
else:
23+
item_name = item_name[:-1] + "()'"
24+
# pylint: disable=raise-missing-from
25+
raise ImportError("\n\nMISSING FUNCTION --> In your 'lasagna.py' file, we can not find or import the"
26+
f' function named {item_name}. \nDid you misname or forget to define it?') from None
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 3,
3+
"status": "error",
4+
"message": "ImportError: \n\nMISSING CONSTANT --> \nWe can not find or import the constant 'EXPECTED_BAKE_TIME' in your 'lasagna.py' file.\nDid you misname or forget to define it?",
5+
"tests": []
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Functions used in preparing Guido's gorgeous lasagna.
2+
3+
Learn about Guido, the creator of the Python language: https://en.wikipedia.org/wiki/Guido_van_Rossum
4+
"""
5+
6+
7+
EXPECTED_BAKE_TIME = 40
8+
PREPARATION_TIME = 2
9+
10+
11+
# TODO: define the 'bake_time_remaining()' function (misspelled here on purpose)
12+
def bake_time():
13+
"""Calculate the bake time remaining.
14+
15+
:param elapsed_bake_time: int - baking time already elapsed.
16+
:return: int - remaining bake time (in minutes) derived from 'EXPECTED_BAKE_TIME'.
17+
18+
Function that takes the actual minutes the lasagna has been in the oven as
19+
an argument and returns how many minutes the lasagna still needs to bake
20+
based on the `EXPECTED_BAKE_TIME`.
21+
"""
22+
23+
pass
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
import pytest
3+
4+
# For this first exercise, it is really important to be clear about how we are importing names for tests.
5+
# To that end, we are putting a try/catch around imports and throwing specific messages to help students
6+
# decode that they need to create and title their constants and functions in a specific way.
7+
try:
8+
from example_lasagna_function_import_error import (EXPECTED_BAKE_TIME,
9+
bake_time_remaining,
10+
preparation_time_in_minutes,
11+
elapsed_time_in_minutes)
12+
13+
# Here, we are separating the constant import errors from the function name import errors
14+
except ImportError as import_fail:
15+
message = import_fail.args[0].split('(', maxsplit=1)
16+
item_name = import_fail.args[0].split()[3]
17+
18+
if 'EXPECTED_BAKE_TIME' in item_name:
19+
# pylint: disable=raise-missing-from
20+
raise ImportError(f'\n\nMISSING CONSTANT --> \nWe can not find or import the constant {item_name} in your'
21+
" 'lasagna.py' file.\nDid you misname or forget to define it?") from None
22+
else:
23+
item_name = item_name[:-1] + "()'"
24+
# pylint: disable=raise-missing-from
25+
raise ImportError("\n\nMISSING FUNCTION --> In your 'lasagna.py' file, we can not find or import the"
26+
f' function named {item_name}. \nDid you misname or forget to define it?') from None
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 3,
3+
"status": "error",
4+
"message": "ImportError: \n\nMISSING FUNCTION --> In your 'lasagna.py' file, we can not find or import the function named 'bake_time_remaining()'. \nDid you misname or forget to define it?",
5+
"tests": []
6+
}

0 commit comments

Comments
 (0)