Ark Luis
Ark Luis

Reputation: 275

Pytest using development sqlite db during testing - NoneType object has no attribute drivername

I was trying to access a sqlite db I use on development server, but I got following error: 'NoneType' object has no attribute 'drivername'. It happend both during calling business logic from a view or during calling db.create_all() inside test client definition (currently removed from code).

Test file code:

import os
import json
import pytest
from flask import Flask
import run
from src.utils.config import TestingConfig
from src.models.user_model import User

class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI')
    SQLALCHEMY_ECHO = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False

@pytest.fixture
def client():
    app = run.create_app(TestingConfig)

    context = app.app_context()
    context.push()

    with context:
        with app.test_client() as client:
            yield client

    context.pop()

def test_user_register(client):
    user_data = { 
        'login': 'login', 
        'password': 'password', 
        'email': '[email protected]',
        'first_name': 'first_name',
        'last_name': 'last_name',
        'index': '000000'
    }
    rv = client.post('/api/v1/users/register', data=json.dumps(user_data), content_type='application/json')
    
    user_data['user_role'] = 2
    assert '' == rv.data

Create app code:

from flask import Flask, current_app
from flask_cors import CORS
from flask_swagger_ui import get_swaggerui_blueprint
from src.utils.config import Config, DevelopmentConfig, ProductionConfig
from src.utils.extensions import db, ma, migrate, cors
from src.views import user_view, task_view, task_submission_view
from src.exceptions import exceptions_view

def create_app(config):
   app = Flask(__name__)
   app.config.from_object(config)

   app.register_blueprint(exceptions_view.handler)
   app.register_blueprint(user_view.blueprint, url_prefix='/api/v1/users')
   app.register_blueprint(task_view.blueprint, url_prefix='/api/v1/tasks')
   app.register_blueprint(task_submission_view.blueprint, url_prefix='/api/v1/taskSubmissions')
   swagger_blueprint = get_swaggerui_blueprint('/swagger', '/static/swagger.json')
   app.register_blueprint(swagger_blueprint)

   db.init_app(app)
   ma.init_app(app)
   migrate.init_app(app, db)
   cors.init_app(app)

   return app

.env fragment :

SQLALCHEMY_DATABASE_URI = "sqlite:///sqlite.db"

Upvotes: 0

Views: 426

Answers (1)

Ark Luis
Ark Luis

Reputation: 275

Answer:

When I created the app from an external script run.create_app() even though blueprints were registered correctly the configuration was wiped out after the script execution.

After some trial-and-error runs I found out that configurating app by function app.config.from_object(), more precisely during os.getenv(), was an issue - even after moving it to the test file it wasn't able to configurate the app.

Solution was to set the configuration manually (inside or outside the test file): app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sqlite.db'

Upvotes: 1

Related Questions