ORM Lab (CodeGrade)
- Due No Due Date
- Points 1
- Submitting an external tool
Learning Goals
- Map a Python class to a database table.
- Persist a one-to-many relationship.
- Define property methods to manage attributes.
Instructions
This is a test-driven lab. Run pipenv install to create your virtual environment and pipenv shell to enter the virtual environment. Then run pytest -x to run your tests. Use these instructions and pytest 's error messages to complete your work in the lib/ folder.

This lab involves enhancing the company data model from the ORM lessons. You will implement a class named Review that encapsulates an annual performance review for an employee. There is a one-to-many relationship between Employee and Review.
- An employee may have many reviews.
- A review is for one employee.
- A review includes an attribute for the
yearas well as asummaryof the employee's performance.
The Department and Employee classes contain the completed solutions from the ORM lessons. You will add functionality for the new Review class in this lab.
Environment
The environment is initialized in lib/__init__.py to generate a sqlite3.Connection object CONN, and a sqlite3.Cursor object CURSOR.
Implementing Review
The Review class contains the following methods already implemented for you:
__init__initializes attributes for the reviewyear,summary, and associatedemployee. It also initializes theidattribute to a default value ofNone.__repr__returns a formatted string containing the attribute values.create_tablecreates a "reviews" database table.drop_tabledrops the "reviews" database table.
Edit the Review class to add the following ORM methods (you will add property methods in a subsequent step):
save()
The instance method save() should persist the Review object to the "reviews" table:
- Insert a new row with the
year,summary, andemployee_idvalues of the currentReviewinstance. - Update the object id attribute using the primary key value of new row.
- Save the object in local dictionary using table row's PK as dictionary key.
create()
This is a class method that should:
- Create a new
Reviewinstance using the parameter values. - Save the new
Reviewinstance to the "reviews" table. - Return the new
Reviewinstance.
Think about how you can re-use the save() method to help with this one.
instance_from_db()
This class method should return a Review instance having the attribute values from the table row. You should check the dictionary for an existing instance using the row's primary key, and set the instance attributes to the row data if found. If the dictionary does not contain a previously persisted object with that id, create a new Review instance from the row data and add it to the dictionary. The method should return the cached object.
find_by_id()
This class method takes in an id as a parameter, and should return a single Review instance corresponding to the row in the "reviews" table with that same id, or None is no such row exists in the table.
update()
This instance method should update the year, summary and employee_id columns for a "reviews" table row based on the id of the current object.
delete()
This instance method should delete a "reviews" table row based on the id of the current object. It will also remove the instance from the all dictionary and set the current object's id attribute to None.
get_all()
This class method should return a list of Review instances for every row in the "reviews" table.
You can test your methods by running the tests in the "lib/testing/review_orm_test.py" file:
pytest lib/testing/review_orm_test.py
You can also experiment by running python lib/debug.py and calling the new ORM methods at the ipbd> prompt:
ipdb> Review.get_all()
Review Properties
Let's add property methods to set rules for the Review attributes as follows:
yearshould be an integer that is greater than or equal to 2000.summaryshould be a non-empty string.employee_idshould be the id of anEmployeeclass instance that has been persisted into the "employees" table.
You can test your methods by running the tests in the "lib/testing/review_property_test.py" file:
pytest lib/testing/review_property_test.py
Update Employee to get a list of associated Review instances
Update the Employee class with a new method reviews() for getting associated Review instances that have been persisted to the database.
reviews()
This instance method should query the "reviews" table to get all rows where the foreign key column employee_id matches the id of the current Employee instance. The method should return a list of Review instances for each matching table row.
NOTE: To avoid issues with circular imports, embed import the Review class within the reviews() method rather than at the module level.
You can test your methods by running the tests in the "lib/testing/employee_orm_test.py" file:
pytest lib/testing/employee_orm_test.py
Submit your completed lab
Check to make sure all tests pass:
pytest -x
Once all of your tests pass, commit and push your work using git to submit your solution.