How to patch google.cloud.storage in python - python-3.x

I have a class that import google cloud:
StorageUtils:
from google.cloud import storage
I have a app that uses StorageUtils:
App:
import StorageUtils
Then I have a test, that I want to test my app
Test:
from app import App
I want to test my app without using google cloud. The easiest way I found is to use sys.modules:
import sys
from unittest.mock import MagicMock
sys.modules["google.cloud.storage"] = MagicMock()
I found this solution quite a work around. Are there any other way using python mock?

In this case, if the file that contains the class StorageUtils is called storage_utils.py:
#test_storage.py
#patch("storage_utils.storage")
def test_storage(st):
storage = StorageUtils()

Related

How to import InfoType or other types from google-cloud/dlp for typescript in nodejs

Working with Google cloud DLP and nodejs. After import DLP from "#google-cloud/dlp, how can I import InfoType, Likelihood, IInspectContentRequest and other types from the library as it contains all types.
Solution import {protos} from "#google-cloud/dlp

How to use the varibale tryton in blueprint?

I am building an application in flask and a library called flask_tryton I do the process of creating the file init.py with the function create_app() as you see the tryton variable is used in that way that a connection to a database tryton is an api for flask, my problem is in the blueprint that does not allow using that variable to make my endpoints someone knows how I can use that variable in the party file which is a blueprint
import os
import uuid
from flask import Flask
from flask_tryton import Tryton
def create_app():
app = Flask(name)
app.config['TRYTON_DATABASE'] = os.environ.get('TRYTON_DATABASE')
app.config['TRYTON_CONFIG'] = os.environ.get('TRYTON_CONFIG')
app.config.from_mapping(
SECRET_KEY=uuid.uuid4().hex,
)
tryton = Tryton(app, configure_jinja=True)
from . import party
app.register_blueprint(party.bp)
return app
The problem I have is that it does not allow me to use the tryton variable in the party blueprint I am new to flask and I am trying to make this adaptation with tryton.

How to import airbnbapi?

Following this tutorial: https://github.com/zxol/airbnbapi
and would like to import SDK
var airbnb = require('airbnbapijs')
I do not see any component like airbnbapijs. Shouldn't it exist to be import into my web app?

Secondary import when using Firebase?

Why do we need to import what seems like the same module twice when using Firebase?
import { firestore, initializeApp } from 'firebase';
import 'firebase/firestore';
Would be interesting to hear why this case may come up in general node/js es6 modules and not just here.
I usually do something like this:
import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';
The first line imports the main Firebase dependency, so that you call firebase.initializeApp(...).
The second and third line then import specific Firebase product SDKs on top of that, so that you can access firebase.firestore() and firebase.auth().
This pulls in the minimal amount of JavaScript that is needed for my specific app.
Your first line pulls in the SDKs for all Firebase projects, and then imports a few objects out of there. This is quite wasteful, as you're unlikely to be using all products in an app.
I'm actually not sure what the second line does in your case.

Google Cloud Console - Flask - main.py vs package

OK, so I have been through some tutorials to get a flask app onto google cloud, which is fine.
I have also been through the flask tutorial to build a flaskr blog:
http://flask.pocoo.org/docs/1.0/tutorial/
It occurred to me that a sensible thing to do would be to create a database (MySQL in mycase) on google and then modify the code so that it uses that. This is fine and I can get it to work on my local machine.
However, now that I am coming to deploying this, I have hit a problem.
The google cloud tutorials tend to use a flask app that is initiated in a single file such as main.py, eg:
from flask import Flask, render_template
app = Flask(__name__)
....
The flask tutorial mentioned above uses a package and puts the code to create_app() in the __init__.py file and at present I cannot get this to start in the same way. (see sample code).
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev'
)
Are there some adjustments that I need to make to something like the app.yaml file to get it to recognise flask as the flaskr package or do I need to rewrite the whole thing so that it uses a main.py file ?
I feel that this is one of the points in time where I could really pick up a bad habit. What in general is the preferred way to write flask apps on google cloud ?
I am using the standard environment in google.
Thanks for your advice.
Mark
Since you have an application factory, you can create the app anywhere. Just create it in main.py, since this is what App Engine expects:
from my_package import create_app
app = create_app()

Resources