def init_db(cls, app): """ Initialize database session """ logger.info("Initializing database") cls.app = app # initialize SQLAlchemy from Flask app # will create .db file in directory that models.py is located in db.init_app(app) app.app_context().push() # purpose? db.create_all()
def setUp(self): # binds the app to the current context self.client = app.test_client() self.blog = {'title': 'my journey', 'blog': 'To Andela'} with app.app_context(): # create all database tables db.create_all()
def setUp(self): # binds the app to the current context app = self.create_app() with app.app_context(): # create all database tables db.create_all() db.session.commit()
def setUp(self): # binds the app to the current context self.client = app.test_client() self.song = {'title': 'queen of my heart', 'artist': 'westlife boys'} with app.app_context(): # create all database tables db.create_all()
def signup_admin(): # let's inicizlize db db.init_app(app) # let's create db with app.app_context(): db.create_all() name = 'admin' password = '******' user = User.query.filter_by(name=name).first( ) # if this returns a user, then the email already exists in database if user: # if a user is found, we want to redirect back to signup page so user can try again # return redirect(url_for('auth.signup')) return 'it is ALREADY exists ADMIN' # create new user with the form data. Hash the password so plaintext version isn't saved. new_user = User(public_id=str(uuid.uuid4()), name=name, password=generate_password_hash(password, method='sha256'), admin=True) # add the new user to the database db.session.add(new_user) db.session.commit()
def test_blank_category_name(self): """Tests for blank category name""" category_data = { "category_name": "" } with app.app_context(): self.assertEqual(self.validate.validate_category(category_data), ("Category_name cannot be blank"))
def app(): app_test.config['DEBUG'] = True app_test.config['TESTING'] = True app_test.config['SERVER_NAME'] = 'localhost' ctx = app_test.app_context() ctx.push() yield app_test ctx.pop()
def setUp(self): # Create all tables with app.app_context(): db.create_all() self.app = app.test_client self.app_context = app.app_context
def setUp(self): app.config['TESTING'] = True self.app = app.test_client() with app.app_context(): connection = db() connection.drop_tables() connection.create_tables()
def tearDown(self): with app.app_context(): conn = DbConn() self.cur = conn.create_connection() conn.drop_tables('products') conn.drop_tables('categories') conn.drop_tables('blacklisted') conn.drop_tables('users')
def test_return_if_product_name_and_quantity_missing(self): # Tests to ensure that if product_name and quantity not provided sale_data = { "price": "50000", "category": "shoes" } with app.app_context(): self.assertEqual(self.validate.validate_sale(sale_data), ("Add product name and quantity"))
def test_invalid_category_name(self): """Tests for invalid category name""" category_data = { "category_name": "#$#@$@@$#@" } with app.app_context(): self.assertEqual (self.validate.validate_category(category_data), ("category name should contain alphanumerics only"))
def test_invalid_category_fields(self): """Tests for invalid category fields""" category_data = { "category_name": "jeans", "price": "50000" } with app.app_context(): self.assertEqual(self.validate.validate_category(category_data), ('Invalid fields added'))
def setUp(self): self.client = app.test_client() with app.app_context(): conn = DbConn() self.cur = conn.create_connection() conn.create_users_table() conn.delete_default_admin() conn.create_default_admin() conn.create_categories_table()
def send_mail(mail_data): msg = Message( mail_data['subject'], sender=app.config['MAIL_USERNAME'], recipients=mail_data['recipients'] ) msg.body = mail_data['mail_body'] with app.app_context(): mail.send(msg)
def tearDown(self): with app.app_context(): conn = DbConn() self.cur = conn.create_connection() conn.drop_tables('sales_records') conn.drop_tables('products') conn.drop_tables('categories') conn.delete_default_admin() conn.drop_tables('users')
def test_return_if_product_name_contains_special_characters(self): # Tests to ensure that product name contains special characters sale_data = { "product_quantity": "10", "product_name": "s@$@#@###", "price": "50000" } with app.app_context(): self.assertEqual(self.validate.validate_sale(sale_data), ("productname should contain alphanumerics only"))
def test_wrong_key_values(self): # Tests that the function raises an exception with wrong key value data = { "product_name": "shirts", "%&": "5000", "price": "17000" } with app.app_context(): self.assertEqual(self.validate.validate_product(data), ("Invalid Key Fields"))
def test_empty_product_price(self): # Tests to ensure the function fails if price is empty data = { "product_name": "cabbage", "product_quantity": "3", "price": "" } with app.app_context(): self.assertEqual(self.validate.validate_product(data), ("price cannot be blank"))
def test_return_if_product_name_for_sale_is_blank(self): # Tests to ensure that product name is not blank sale_data = { "product_quantity": "5", "product_name": "", "price": "50000" } with app.app_context(): self.assertEqual(self.validate.validate_sale(sale_data), ("Product_name cannot be blank"))
def client(): """ Creates a test client and an app context for running the tests. Removes the context after testing is finished. """ testing_client = app.test_client() context = app.app_context() context.push() yield testing_client context.pop()
def test_validate_sales(self): # Tests to ensure that correct sales definition passes sale_data = { "product_quantity": "4", "product_name": "blouse", "price": "50000" } with app.app_context(): self.assertEqual(self.validate.validate_sale(sale_data), ("Sale_valid"))
def test_return_if_quantity_contains_non_integers(self): # Tests to ensure that product name contains strings sale_data = { "product_quantity": "quantity", "product_name": "shorts", "price": "50000" } with app.app_context(): self.assertEqual(self.validate.validate_sale(sale_data), ("quantity should contain integers only"))
def test_empty_product_quantity(self): # Tests the function fails if product quantity is empty data = { "product_name": "jeans", "product_quantity": "", "price": "40000" } with app.app_context(): self.assertEqual(self.validate.validate_product(data), ("product_quantity cannot be blank"))
def test_product_name_characters(self): # Tests the product name doesnot accept non alphanumeric characters data = { "product_name": "&%^&short##$$#sleeved shirt", "product_quantity": "54", "category_name": "shirt", "price": "40000" } with app.app_context(): self.assertEqual(self.validate.validate_product(data), ("productname should contain alphanumerics only"))
def test_price_value(self): # Tests the price accepts integers only data = { "product_name": "shorts", "product_quantity": "54", "category_name": "short", "price": "thisisprice" } with app.app_context(): self.assertEqual(self.validate.validate_product(data), ("price should contain integers only"))
def server_side_event(scheduled=True, supplierID=None): """ Function to publish server side event """ with app.app_context(): channel = f"supplierID_{supplierID}" print(channel) sse.publish(next(get_data()), type='newOrder', channel=channel) if scheduled: print("Event Scheduled at ", datetime.datetime.now()) else: print(f"Event triggered for channel=supplierID_{supplierID} at ", datetime.datetime.now())
def setUp(self): # binds the app to the current context self.client = app.test_client() self.user = { 'username': '******', 'email': '*****@*****.**', 'password': '******' } with app.app_context(): # create all database tables db.create_all()
def send_async_email(app, msg): """ An asynchronous email function to allow the email to be sent by a background thread. :param app: The application instance of the current_app Flask variable :param msg: The Message instance to be sent """ # Flask uses contexts to avoid having to pass arguments across functions. # mail.send() uses config values from the application context with app.app_context(): mail.send(msg)
def test_product_quantity_value(self): # Tests the product quantity only accepts integers data = { "product_name": "shirts", "product_quantity": "productquantity", "category_name": "shirt", "price": "17000" } with app.app_context(): self.assertEqual(self.validate.validate_product(data), ("quantity should contain integers only"))
def app(request): '''Flask application for test''' flask_app.config['TESTING'] = True ctx = flask_app.app_context() ctx.push() def teardown(): ctx.pop() request.addfinalizer(teardown) return flask_app