Here’s a list of the variables our function can accept and their possible values:Ĭust_name can be blank, a string containing the name of a valid customer, or a string that does not contain the name of a valid customer. However, in your testing, it would be very wise to make sure you test with data that will work correctly and data that could cause errors. We’re going to assume for this exercise that we have already validated that the inputs to the function are of the correct type. Let’s look at all the ways the get_orders_by_customer function can be used. This is our DataAccessLayer instance from the db.py file.īecause our tables are inside of the dal object, we access them from there. username = cust_name ) if shipped is not None : cust_orders = cust_orders. cookies ) cust_orders = select ( columns ) cust_orders = cust_orders. app.py to test from db import dal from sqlalchemy.sql import select def get_orders_by_customer ( cust_name, shipped = None, details = False ) : columns = joins = dal. We are going to write tests for the get_orders_by_customer function we built in Chapter 2, which is found the app.py file, shown in Example 4-2.Įxample 4-2. This provides an instance of the DataAccessLayer class that can be imported throughout our application. ![]() This provides a way to initialize a connection with a specific connection string like a factory. ![]() In the complete file, we create all the tables that we have been using since Chapter 1, not just cookies. engine = create_engine ( conn_string or self. DataAccessLayer class from datetime import datetime from sqlalchemy import ( MetaData, Table, Column, Integer, Numeric, String, DateTime, ForeignKey, Boolean, create_engine ) class DataAccessLayer : connection = None engine = None conn_string = None metadata = MetaData ( ) cookies = Table ( ' cookies ', metadata, Column ( ' cookie_id ', Integer ( ), primary_key = True ), Column ( ' cookie_name ', String ( 50 ), index = True ), Column ( ' cookie_recipe_url ', String ( 255 ) ), Column ( ' cookie_sku ', String ( 55 ) ), Column ( ' quantity ', Integer ( ) ), Column ( ' unit_cost ', Numeric ( 12, 2 ) ) ) def db_init ( self, conn_string ) : self.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |