【问题标题】:Getting testcontainers working with marshmallow让测试容器与棉花糖一起工作
【发布时间】:2020-07-29 10:50:44
【问题描述】:

我正在尝试让 python 测试容器与棉花糖一起运行测试,但我似乎无法让它工作。当我运行测试时,我总是得到 connection denied。我创建了一个 sqlalchemy 引擎并对其进行了测试,这工作正常,但是当将 testcontainer 连接字符串作为棉花糖配置传递时,它就不起作用了。下面是我的基本测试类。

class BaseTestCase(TestCase):
    base_url = '/api/v1'

    def create_app(self):
        config = MySqlContainer('mysql:8.0.19')
        with config as mysql:
            print(mysql.get_connection_url())
            e = sqlalchemy.create_engine(mysql.get_connection_url())
            result = e.execute("select version()")
            for row in result:
                print("Printing::::::::::::::::::::::" + str(row))
            result.close()
            logging.getLogger('connexion.operation').setLevel('ERROR')
            connex_app = connexion.App(__name__, specification_dir='../../api/')
            connex_app.app.json_encoder = JSONEncoder
            connex_app.add_api('static/openapi.yaml')
            app = connex_app.app
            app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
            app.config['SQLALCHEMY_DATABASE_URI'] = mysql.get_connection_url()
            app.config['LOG_LEVEL'] = 'DEBUG'

            bcrypt.init_app(app)
            db.init_app(app)
            ma.init_app(app)

            print("Finished setting up")

            return app

当我使用 sqlite 作为连接字符串时,我可以让它工作。

class BaseTestCase(TestCase):
    base_url = '/api/v1'

    def create_app(self):
        class Config:
            PORT = 5000
            SQLALCHEMY_TRACK_MODIFICATIONS = False
            FLASK_ENV = 'local'
            SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
            LOG_LEVEL = 'DEBUG'

        logging.getLogger('connexion.operation').setLevel('ERROR')
        connex_app = connexion.App(__name__, specification_dir='../../api/')
        connex_app.app.json_encoder = JSONEncoder
        connex_app.add_api('static/openapi.yaml')
        app = connex_app.app
        app.config.from_object(Config)
        bcrypt.init_app(app)
        db.init_app(app)
        ma.init_app(app)

        print("Finished setting up test")

        return app

任何帮助将不胜感激。

保罗

【问题讨论】:

    标签: python sqlalchemy marshmallow testcontainers


    【解决方案1】:

    我决定使用 testcontainers.compose 并且连接字符串似乎有效。

    class BaseTestCase(TestCase):
        base_url = '/api/v1'
    
        def create_app(self):
            self.compose = testcontainers.compose.DockerCompose(".")
            self.compose.start()
            time.sleep(10)
    
            class Config:
                PORT = 3306
                SQLALCHEMY_TRACK_MODIFICATIONS = False
                FLASK_ENV = 'local'
                SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:password@0.0.0.0:3306/test-db'
                LOG_LEVEL = 'DEBUG'
    
            logging.getLogger('connexion.operation').setLevel('ERROR')
            connex_app = connexion.App(__name__, specification_dir='../../api/')
            connex_app.app.json_encoder = JSONEncoder
            connex_app.add_api('static/openapi.yaml')
            app = connex_app.app
            app.config.from_object(Config)
            bcrypt.init_app(app)
            db.init_app(app)
            ma.init_app(app)
    
            print("Finished setting up test")
    
            return app
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 2016-05-04
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多