def _database_setup(): ''' reset database ''' cc.get_database().drop_tables([Customer]) cc.get_database().create_tables([Customer]) mongo = MongoDBConnection() with mongo: db = mongo.connection.get_database(name=config.DATABASE_NAME) db.drop_collection("customers") db.drop_collection("products") db.drop_collection("rentals")
def bulk_add_customers(customers): ''' Adds a bulk amount of customers :param customers: sequence of dictionaries, expects keys: customer_id, first_name, last_name, home_address,phone_number, email_address, status, credit_limit ''' batch_size = 120 customer_list = list() database = cc.get_database() for index, customer in enumerate(customers, start=1): try: customer['credit_limit'] = float(customer['credit_limit']) except ValueError as err: logging.error(err) raise ValueError(f"Invalid value: record {index}: " \ f"invalid credit_limit: '{customer['credit_limit']}'") if not isinstance(customer['status'], bool): text = f"Invalid value: record {index}: status is not a bool: '{customer['status']}'" logging.error(text) raise ValueError(text) customer_list.append(customer) if index % batch_size == 0: _insert_customer_list(customer_list, database) customer_list = list() if len(customer_list) > 0: _insert_customer_list(customer_list, database)
def setup_teardown(): ''' Fixture to execute before and after tests ''' db = cc.get_database() db.drop_tables([Customer]) db.create_tables([Customer]) data = data_setup() bo.bulk_add_customers(data) yield
class Meta: ''' BaseModel Meta class ''' database = cc.get_database()
""" import logging import peewee as pw import assignment.create_customers as cc logging.basicConfig(level=logging.INFO) LOGGER = logging.getLogger(__name__) class BaseModel(pw.Model): ''' Base class for all database models ''' class Meta: ''' BaseModel Meta class ''' database = cc.get_database() class Customer(BaseModel): """ This class defines Customer, which maintains customer details """ customer_id = pw.CharField(primary_key=True, max_length=30) first_name = pw.CharField(max_length=30) last_name = pw.CharField(max_length=30) home_address = pw.CharField(max_length=100) phone_number = pw.CharField(max_length=20) email_address = pw.CharField(max_length=100) status = pw.BooleanField() credit_limit = pw.FloatField() cc.get_database().create_tables([Customer])
''' Tests the basic_operations module ''' import logging from pytest import fixture from pytest import raises import datetime import assignment.create_customers as cc # Use Test environment database cc.get_database(env="test") import assignment.basic_operations as bo from assignment.customers_model import Customer def logging_setup(): log = "%(asctime)s %(filename)s:%(lineno)-3d %(levelname)s %(message)s" formatter = logging.Formatter(log) logger = logging.getLogger() logger.setLevel(logging.INFO) file_handler = logging.FileHandler('db.log') file_handler.setLevel(logging.INFO) file_handler.setFormatter(formatter) console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(formatter) logger.addHandler(file_handler)
def setup(self): ''' Fixture to execute before and after tests ''' db = cc.get_database() db.drop_tables([Customer]) db.create_tables([Customer])