# Homework 5
In this homework, you will implement a python class that could be used as an interface between a python application and a postgres database. That will include writing the necessary queries, as well as the application code to fetch the data from the database (or perform the necessary inserts, updates, and deletes, as necessary).
The database is an example of a database a restaurant might use to manage its food offerings. The functionality we will implement will not be complete, as the needed functionality would depend on the nature of the application using our library.
## Whats in the application
`restaurant-setup.sql` contains SQL code that can be run as a superuser (usually `postgres`, but you could have other superusers as well) to create the database and user expected by the testing framework.
`restaurant-data.sql` contains SQL code to set up the database and populate the database. This will be run by the testing framework before each test, so that the database is reset to a known state. You can also run it yourself, if you want to be able to explore the data. *Note that if you do run it manually, you should do so as the `restaurant` user created by the `restaurant-setup.sql` script. Otherwise, the unit tests will not be able to run properly.*
`homework_5.py` contains the class you need to implement.
`restaurant_test.py` contains unit tests for testing the implementation. I will use something similar (though not necessarily an exact match) for grading.
`data_structures.py` contains a single supporting data structures: `RecipeInstructions`.
## Setup
As with the lab in class, this relies on the [psycopg2](http://initd.org/psycopg/) module to connect to a Postgres database. Youll need to have that available when you run your python code.
It also relies on the python [unittest](https://docs.python.org/3.6/library/unittest.html) framework. I believe that comes with python, but you can also download it (or use `pip` to install it) separately, if necessary.
The test framework expects a user named `restaurant` (with password `restaurant`) to have full privileges to a database named `restaurant` on `localhost`. You can run the commands in `restaurant-setup.sql` to configure all that. (Or you could also use whatever database and user you want if you change the `connection_string` variable in `restaurant_test.py`.)
## Running
From the a terminal (or command prompt) in the `homework-5/` directory, you can run the unit test suite by running:
`python -m unittest restaurant_test.py`
That will run the unit tests checking for database connectivity and then testing the five methods you need to implement.
## Assignment
You should add code to the `RestaurantData` class of `homework_5.py` to implement the following functions:
### `find_recipe(self, recipe_name)`
This should accept a string containing a partial or full recipe name, perform a case-insensitive search of the `recipe` table, and return a list of `dict` objects (or `dict`-like objects) representing the recipes found. Note that this does not include any search for ingredients.
### `get_recipe_instructions(self, recipe_name)`
This should accept a string containing an exact recipe name. (The recipe name is the key to the recipe table, so the exact match guarantees at most one recipe.) It should return a populated `RecipeInstructions` object.
### `get_seasonal_menu(self, season)`
This should accept a string containing an exact season name. It should return a list of tuples of the form `(recipeName, isKosher)`, using the definition that a recipe is kosher if *all* of its ingredients are kosher. Note that there is a meta-season: `All`, whose recipes should appear on the menu, regardless of the season.
### `update_ingredient_price(self, ingredient_code, price)`
This should update the price of the ingredient with the new price. It should return the number of rows updated.
### `add_new_recipe(self, recipe_instructions, servings, course, season)`
This should accept a `RecipeInstruction` object, as well as values for `servings`, `course`, and `season` for the new recipe. It should return `True` if the recipe was successfully inserted, and `False` if it was not. Note that if the recipe includes an ingredient that does not exist in the `ingredient` table, **the insert should fail**.
## Grading
`restaurant_test.py` contains unit tests for each of the five methods you need to implement. You will earn 5 points each for passing unit tests for the first two method, and 10 points each for passing unit tests for the last three methods.
**Total 40 points**
## Deliverables
You should upload to Submitty a single file: `homework_5.py`. It should contain your fully implemented code.
Reviews
There are no reviews yet.