def test_rabbit(): config = make_config("rabbit") assert hasattr(config, "RABBIT_DRIVER") assert hasattr(config, "RABBIT_HOST") assert hasattr(config, "RABBIT_PORT") assert hasattr(config, "RABBIT_USER") assert hasattr(config, "RABBIT_PASS")
def test_postgres(): config = make_config("postgres") assert hasattr(config, "TEST_DB_URL") assert hasattr(config, "DB_DRIVER") assert hasattr(config, "DB_HOST") assert hasattr(config, "DB_PORT") assert hasattr(config, "DB_USER") assert hasattr(config, "DB_PASS")
def test_base(): class MyConfig(ServiceConfig): SOME_THING = props.Int() config = make_config(base=MyConfig) assert hasattr(config, "SOME_THING") assert hasattr(config, "SERVE") assert hasattr(config, "SERVE_HOST") assert hasattr(config, "SERVE_PORT")
def step_impl(context): config = make_config("postgres") config.DB_HOST = context.host @service(config) def my_simple_api(app): db.init_app(app=app, migrate=None) # optional instance of flask_migrate.Migrate context.app = my_simple_api.test_app() with context.app.app_context(): db.create_all() context.service = my_simple_api
id = db.Column(db.Integer, primary_key=True, autoincrement=True) created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) name = db.Column(db.String, nullable=False) class CreateItemSchema: name = props.String(min_length=3, max_length=32) def create_item(name): obj = MyModel(name=name) db.session.add(obj) db.session.commit() @service(config_class=make_config("postgres", "celery", "rabbit")) def my_celery_api(app): app.register_blueprint(blueprint) db.init_app(app=app, migrate=None) # optional instance of flask_migrate.Migrate celery.init_app(app) @celery.task def my_task(item_name): create_item(name=item_name) @blueprint.route("/", methods=["POST"]) @request_schema(json=CreateItemSchema) def create_item(json):
db = SQLAlchemy() token_handler = token.Handler() @db.serializable("created", "name") class MyModel(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) name = db.Column(db.String, nullable=False) class CreateItemSchema: name = props.String(min_length=3, max_length=32) @service(config_class=make_config("postgres")) def my_jwt_api(app): app.register_blueprint(blueprint) app.init_sqlalchemy( database=db, migrate=None ) # optional instance of flask_migrate.Migrate token_handler.configure(key="secret", lifespan=600) app.init_token_handler(token_handler) @blueprint.route("/auth", methods=["GET"]) def auth(): jwt = token_handler.generate_token(scopes=("read:thing", "write:thing")) return jsonify({"jwt": jwt})
def test_fields(): config = make_config(thing=props.Int(), other=props.String()) assert hasattr(config, "THING") assert hasattr(config, "OTHER")
def test_invalid(): with pytest.raises(KeyError): make_config("nope")
def test_default_base(): config = make_config() assert hasattr(config, "SERVE") assert hasattr(config, "SERVE_HOST") assert hasattr(config, "SERVE_PORT")
def test_celery(): config = make_config("celery") assert hasattr(config, "CELERY_BROKER_BACKEND") assert hasattr(config, "CELERY_RESULTS_BACKEND") assert hasattr(config, "CELERY_REDIS_DATABASE_ID")
def test_redis(): config = make_config("redis") assert hasattr(config, "REDIS_DRIVER") assert hasattr(config, "REDIS_HOST") assert hasattr(config, "REDIS_PORT") assert hasattr(config, "REDIS_PASS")