Skip to content

Commit

Permalink
Added new goldenfile tests for lasagna import errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
BethanyG committed Feb 25, 2023
1 parent 71f6dfe commit 020aca1
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Functions used in preparing Guido's gorgeous lasagna.
Learn about Guido, the creator of the Python language: https://en.wikipedia.org/wiki/Guido_van_Rossum
"""

# TODO: define the 'EXPECTED_BAKE_TIME' constant
# TODO: consider defining the 'PREPARATION_TIME' constant
# equal to the time it takes to prepare a single layer


# TODO: define the 'bake_time_remaining()' function
def bake_time_remaining():
"""Calculate the bake time remaining.
:param elapsed_bake_time: int - baking time already elapsed.
:return: int - remaining bake time (in minutes) derived from 'EXPECTED_BAKE_TIME'.
Function that takes the actual minutes the lasagna has been in the oven as
an argument and returns how many minutes the lasagna still needs to bake
based on the `EXPECTED_BAKE_TIME`.
"""

pass


# TODO: define the 'preparation_time_in_minutes()' function
# and consider using 'PREPARATION_TIME' here


# TODO: define the 'elapsed_time_in_minutes()' function
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
import pytest

# For this first exercise, it is really important to be clear about how we are importing names for tests.
# To that end, we are putting a try/catch around imports and throwing specific messages to help students
# decode that they need to create and title their constants and functions in a specific way.
try:
from example_lasagna_constant_import_error import (EXPECTED_BAKE_TIME,
bake_time_remaining,
preparation_time_in_minutes,
elapsed_time_in_minutes)

# Here, we are separating the constant import errors from the function name import errors
except ImportError as import_fail:
message = import_fail.args[0].split('(', maxsplit=1)
item_name = import_fail.args[0].split()[3]

if 'EXPECTED_BAKE_TIME' in item_name:
# pylint: disable=raise-missing-from
raise ImportError(f'\n\nMISSING CONSTANT --> \nWe can not find or import the constant {item_name} in your'
" 'lasagna.py' file.\nDid you misname or forget to define it?") from None
else:
item_name = item_name[:-1] + "()'"
# pylint: disable=raise-missing-from
raise ImportError("\n\nMISSING FUNCTION --> In your 'lasagna.py' file, we can not find or import the"
f' function named {item_name}. \nDid you misname or forget to define it?') from None
6 changes: 6 additions & 0 deletions test/example-lasagna-constant-import-error/results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 3,
"status": "error",
"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?",
"tests": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Functions used in preparing Guido's gorgeous lasagna.
Learn about Guido, the creator of the Python language: https://en.wikipedia.org/wiki/Guido_van_Rossum
"""


EXPECTED_BAKE_TIME = 40
PREPARATION_TIME = 2


# TODO: define the 'bake_time_remaining()' function (misspelled here on purpose)
def bake_time():
"""Calculate the bake time remaining.
:param elapsed_bake_time: int - baking time already elapsed.
:return: int - remaining bake time (in minutes) derived from 'EXPECTED_BAKE_TIME'.
Function that takes the actual minutes the lasagna has been in the oven as
an argument and returns how many minutes the lasagna still needs to bake
based on the `EXPECTED_BAKE_TIME`.
"""

pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
import pytest

# For this first exercise, it is really important to be clear about how we are importing names for tests.
# To that end, we are putting a try/catch around imports and throwing specific messages to help students
# decode that they need to create and title their constants and functions in a specific way.
try:
from example_lasagna_function_import_error import (EXPECTED_BAKE_TIME,
bake_time_remaining,
preparation_time_in_minutes,
elapsed_time_in_minutes)

# Here, we are separating the constant import errors from the function name import errors
except ImportError as import_fail:
message = import_fail.args[0].split('(', maxsplit=1)
item_name = import_fail.args[0].split()[3]

if 'EXPECTED_BAKE_TIME' in item_name:
# pylint: disable=raise-missing-from
raise ImportError(f'\n\nMISSING CONSTANT --> \nWe can not find or import the constant {item_name} in your'
" 'lasagna.py' file.\nDid you misname or forget to define it?") from None
else:
item_name = item_name[:-1] + "()'"
# pylint: disable=raise-missing-from
raise ImportError("\n\nMISSING FUNCTION --> In your 'lasagna.py' file, we can not find or import the"
f' function named {item_name}. \nDid you misname or forget to define it?') from None
6 changes: 6 additions & 0 deletions test/example-lasagna-function-import-error/results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 3,
"status": "error",
"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?",
"tests": []
}

0 comments on commit 020aca1

Please sign in to comment.