dimanche 16 juin 2019

Flask test_client unable to see database fixture

In my test file I have a seeded db test fixture that uses a test db fixture, test_engine, created in conftest.py. I test the module fine but when I use flask test_client to test my routes the test_client is unable to see my test database. I checked here for reference and consulted this answer to no avail.

# stdlib
from unittest.mock import patch
import os
import json

# 3p
import pytest
from sqlalchemy.orm import sessionmaker
import requests

# project
from platform_utils_configuration_service import app
from platform_utils_configuration_service.repositories.config_repository import ConfigRepository

config_repo = ConfigRepository()

@pytest.fixture(autouse=True, scope='function')
def with_test_data(session, test_engine):
    config_repo.add_integration(
        session,
        integration_id=1,
        name='Integration 1',
        data_provider_entity_id=700,
        v1_id=None,
        config= # noqa
        {'rover': {
            'params': {
                'domain': 'https://www.example.com/',
                'database': 'database',
                'platform': 'SQL Server'
            },
            "workflow_id": 2
        }},
        created_by=1,
        updated_by=1
    )

    config_repo.add_entity_configuration(
        session,
        entity_configuration_id=1,
        parent_id=None,
        integration_id=1,
        entity_id=750,
        config=  # noqa
        {'rover': {
            'params': {
                'username': 'server',
                'servername': 'remote_server',
                'scheduler_offset': 2
            }
        }},
        created_by=1,
        updated_by=1,
    )

    session.close()

    yield

    test_engine.execute("SET FOREIGN_KEY_CHECKS = 0")
    test_engine.execute("TRUNCATE TABLE entity_configurations")
    test_engine.execute("TRUNCATE TABLE entity_configurations_history")
    test_engine.execute("SET FOREIGN_KEY_CHECKS = 1")


@pytest.fixture(scope='module')
def Session(test_engine):
    return sessionmaker(bind=test_engine)


@pytest.fixture(scope='module')
def session(Session):
    session = Session()
    try:
        yield session

    finally:
        session.close()



    @pytest.fixture
    def client():
        def mock_jwt_required(realm):
            return

        app.config['TESTING'] = True
        app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://mysql:root@mysql/configuration_service_test'
        app.config['WTF_CSRF_ENABLED'] = False
        client = app.test_client()
        jwt_patcher = patch('flask_jwt._jwt_required', 
        side_effect=mock_jwt_required)
        jwt_patcher.start()
        yield client
        jwt_patcher.stop()

    def test_get_config_by_id(client, session):
        res = client.get('/config?entity_configuration_id=1')
        import pdb; pdb.set_trace()
        assert res.status_code == 200

Aucun commentaire:

Enregistrer un commentaire