
Data Formulator 数据库 Docker 集成测试环境docker-compose 统一编排与 test-dbs.ps1 实战指南【免费下载链接】data-formulator Data Formulator is an interactive AI-powered data analysis system makes it easy to connect, explore and visualize data.项目地址: https://gitcode.com/GitHub_Trending/da/data-formulatorData FormulatorREADME.md提供 MySQL、PostgreSQL、MongoDB、BigQuery 模拟器、Cosmos DB 模拟器、Superset 六类外部服务的 Docker 集成测试环境。本文以 tests/database-dockers/README.md 为核心结合 test-dbs.ps1、docker-compose.test.yml 及各服务子目录源码系统讲解测试环境的目录结构、服务分组、启动/停止/测试命令、端口覆盖与直接 compose 用法帮助你在一台机器上快速拉起全套数据源并运行数据加载器集成测试。一、为什么需要这套 Docker 测试环境Data Formulator 的数据加载层py-src/data_formulator/data_loader内置了 MySQL、PostgreSQL、MongoDB、BigQuery、Cosmos DB、Superset 等连接器。要验证这些连接器与真实外部服务的互通单元测试mock 连接远远不够需要真实可连接的数据库实例。这正是 tests/database-dockers/README.md 要解决的问题集成测试依赖 MySQL、PostgreSQL、MongoDB、BigQuery 模拟器、Cosmos DB 模拟器、Superset 等外部服务这些测试默认不包含在 pytest 路径中需要显式运行。从 pytest.ini 可以印证这一点其testpaths仅包含tests/backend与tests/frontend并未列出tests/database-dockers[pytest] testpaths tests/backend tests/frontend python_files test_*.py因此数据库集成测试由一套独立的 Docker 编排负责拉起服务再配合针对性的 pytest 调用执行。每个服务子目录保持自包含自带docker-compose.yml、Dockerfile、初始化文件与测试模块而仓库根目录下的统一编排入口 docker-compose.test.yml 与 PowerShell 助手 test-dbs.ps1 是可选且增量的增强入口——即使不统一编排原有按服务启动的方式也完全可用。二、目录结构与服务清单整个测试环境位于 tests/database-dockers 下每个服务一个自包含子目录。README 给出的结构如下目录服务默认端口mysql/MySQL 8.03307postgres/PostgreSQL 165433mongodb/MongoDB 727018bigquery/BigQuery emulator HTTP / gRPC9050/9060cosmosdb/Cosmos DB emulator8081superset/Superset8088值得注意的是各服务的默认端口均避开了标准端口如 3306、5432、27017、8080专门映射到3307、5433、27018、9050/9060、8081、8088从而避免与开发者本机可能已运行的数据库实例冲突。2.1 统一 compose 文件docker-compose.test.yml 将六个服务聚合为一份编排要点如下compose 项目名为data-formulator-test-dbs每个服务通过profiles归属到core、heavy分组如 MySQL 属于[core, mysql]Superset 属于[heavy, superset]每个容器使用固定的df-test-*命名如df-test-mysql、df-test-postgres保证容器可被稳定识别所有服务都定义了healthcheck配合docker compose up --wait实现就绪后才返回的启动语义外部端口全部通过环境变量插值并给出默认值例如${PG_PORT:-5433}:5432、${MYSQL_PORT:-3307}:3306、${BQ_GRPC_PORT:-9060}:9060。以 PostgreSQL 服务为例postgres: profiles: [core, postgres] build: context: ./postgres container_name: df-test-postgres ports: - ${PG_PORT:-5433}:5432 environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: testdb healthcheck: test: [CMD-SHELL, pg_isready -U postgres] interval: 5s timeout: 3s retries: 102.2 各服务子目录的自包含编排统一 compose 是增量入口各子目录仍保留独立编排且配置与统一文件保持一致mysql/docker-compose.ymlMySQL 8.0 镜像根密码mysql初始库testdb健康检查为mysqladmin pingpostgres/docker-compose.ymlPostgreSQL 16用户/密码均为postgres健康检查为pg_isreadymongodb/docker-compose.ymlMongoDB 7root 用户admin/admin健康检查通过mongosh执行db.adminCommand(ping)bigquery/docker-compose.ymlBigQuery 模拟器同时暴露 HTTP9050与 gRPC9060端口。2.3 重型服务Cosmos DB 与 Supersetheavy分组包含两个启动耗时更长的服务二者在统一 compose 中有特殊配置Cosmos DB 模拟器docker-compose.test.ymlcosmosdb: profiles: [heavy, cosmosdb] image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest platform: linux/amd64 container_name: df-test-cosmosdb ports: - ${COSMOS_PORT:-8081}:8081 - 10250:10250 - 10251:10251 ... environment: AZURE_COSMOS_EMULATOR_PARTITION_COUNT: 10 AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE: true AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE: 127.0.0.1使用微软官方 Linux 模拟器镜像并明确指定platform: linux/amd64在 ARM 主机上需依赖模拟层运行划分 10 个分区、开启数据持久化、将 IP 覆写为127.0.0.1健康检查通过 curl 访问https://localhost:8081/_explorer/emulator.pemstart_period为 60 秒重试上限 30 次。Supersetdocker-compose.test.yml使用apache/superset:4.1.1镜像通过entrypoint执行superset db upgrade→fab create-adminadmin/admin→superset init→superset load_examples→ 执行自定义sample_data.py→ 最终以--with-threads --reload运行在8088挂载了 superset/superset_config.py、superset/init-superset.sh 与 superset/sample_data.py数据目录通过命名卷superset-data持久化健康检查curl http://localhost:8088/healthstart_period为 120 秒。三、快速开始3.1 Windows / PowerShellWindows 与 PowerShell 用户推荐直接使用仓库根目录下的助手脚本 test-dbs.ps1# 启动常用轻量级服务core 组 .\tests\database-dockers\test-dbs.ps1 start core # 启动全部服务 .\tests\database-dockers\test-dbs.ps1 start all # 运行某一个数据库测试套件 .\tests\database-dockers\test-dbs.ps1 test mongodb # 运行全部数据库测试套件 .\tests\database-dockers\test-dbs.ps1 test all # 停止常用轻量级服务 .\tests\database-dockers\test-dbs.ps1 stop core3.2 cmd.exe 或 Anaconda Prompt如果终端是cmd.exe或 Anaconda Prompt 而非 PowerShell需要通过powershell.exe间接调用脚本。直接在cmd.exe中双击或执行.ps1文件可能弹出 Windows选择打开方式对话框powershell -NoProfile -ExecutionPolicy Bypass -File .\tests\database-dockers\test-dbs.ps1 test mongodb-NoProfile跳过 PowerShell 配置文件加载、-ExecutionPolicy Bypass绕过执行策略限制二者都用于保证脚本在受限环境下可正常运行。3.3 macOS / LinuxmacOS 与 Linux 用户无需 PowerShell 助手可以继续使用各服务的独立 compose 文件或start.sh脚本cd tests/database-dockers/mysql docker compose up -d --build --wait或者直接调用各子目录的 shell 助手例如 mysql/start.sh、postgres/start.sh、mongodb/start.sh。这些脚本在拉起数据库之外还会顺带启动 Data Formulator 后端uv run data_formulator --port 5567 --dev供开发调试使用传stop参数则拆除容器./tests/database-dockers/mysql/start.sh # 启动 MySQL DF 后端 ./tests/database-dockers/mysql/start.sh stop # 拆除 MySQL 容器四、服务分组core / heavy / allREADME 定义了三种分组便于按需启动TargetServicescoreMySQL, PostgreSQL, MongoDB, BigQuery emulatorheavyCosmos DB emulator, SupersetallEvery servicemysqlMySQL onlypostgresPostgreSQL onlymongodbMongoDB onlybigqueryBigQuery emulator onlycosmosdbCosmos DB emulator onlysupersetSuperset only从 test-dbs.ps1 的源码可以看到分组映射的定义$ServiceGroups { core (mysql, postgres, mongodb, bigquery) heavy (cosmosdb, superset) all (mysql, postgres, mongodb, bigquery, cosmosdb, superset) }同时Get-Profiles将分组名转换为 compose profileswitch ($Name) { core { return (core) } heavy { return (heavy) } all { return (core, heavy) } default { return ($Name) } }core是推荐默认分组——四个常用数据库启动快、资源占用小heavy包含 Cosmos DB 模拟器与 Superset镜像拉取、启动与初始化耗时更长Superset 首次启动约 2 分钟仅在需要测试对应连接器时再启用。五、助手命令全解析README 的Helper Commands一节要求所有命令从仓库根目录运行。从 test-dbs.ps1 的参数校验[ValidateSet]可以看出脚本共支持 8 个命令、9 个目标[ValidateSet(start, stop, restart, status, logs, seed-cosmos, test, config)] [string]$Command start, [ValidateSet(core, heavy, all, mysql, postgres, mongodb, bigquery, cosmosdb, superset)] [string]$Target core,5.1 命令速查# 只启动某一个服务 .\tests\database-dockers\test-dbs.ps1 start postgres # 启动时重建镜像 .\tests\database-dockers\test-dbs.ps1 start core -Build # 查看状态 .\tests\database-dockers\test-dbs.ps1 status # 跟随查看某一服务的日志 .\tests\database-dockers\test-dbs.ps1 logs postgres # 手动为 Cosmos DB 灌入测试数据 .\tests\database-dockers\test-dbs.ps1 seed-cosmos各命令行为对应源码switch ($Command)分支命令行为实现要点test-dbs.ps1start按 profile 启动目标服务up -d 可选--build--wait目标含cosmosdb/heavy/all时自动执行seed-cosmosstop停止目标服务docker compose stop servicesrestart先停后启停止后重新startCosmos 相关目标再次灌数据status查看容器状态docker compose ps -alogs跟随日志logs --tail 200 -f servicesseed-cosmos手动灌 Cosmos 测试数据调用 cosmosdb/seed_data.py传入--endpoint与--keytest启动服务并跑 pytest见下文运行测试config输出合并后的 compose 配置docker compose --profile core --profile heavy config可用于排查编排问题5.2 Cosmos DB 自动播种Cosmos DB 模拟器启动后必须手动灌入测试数据才能运行相关测试。README 明确指出助手脚本会在start cosmosdb、start heavy、start all以及对应的test命令中自动完成播种。自动播种由Seed-Cosmos函数实现function Seed-Cosmos { Set-TestEnv Push-Location $RepoRoot python tests/database-dockers/cosmosdb/seed_data.py --endpoint $env:COSMOS_ENDPOINT --key $env:COSMOS_KEY }也可以在 Superset 等重型服务已占用端口、Cosmos 需单独重新播种时手动执行seed-cosmos命令。六、运行集成测试6.1 使用 test 命令助手脚本的test命令会先启动目标服务再运行与之匹配的 pytest 目录# 按需启动 MongoDB然后运行 MongoDB loader 测试 .\tests\database-dockers\test-dbs.ps1 test mongodb # 启动 core 服务然后运行 core loader 测试 .\tests\database-dockers\test-dbs.ps1 test core # 启动全部服务然后运行全部 loader 测试 .\tests\database-dockers\test-dbs.ps1 test all # 传入额外 pytest 参数--% 是 PowerShell 的停止解析标记其后的参数原样传给 pytest .\tests\database-dockers\test-dbs.ps1 test postgres --% -k utf8从源码可以看到test命令内部的服务到测试目录映射$TestPaths { mysql tests/database-dockers/mysql postgres tests/database-dockers/postgres mongodb tests/database-dockers/mongodb bigquery tests/database-dockers/bigquery cosmosdb tests/database-dockers/cosmosdb superset tests/database-dockers/superset }然后执行python -m pytest paths -q extra_args。注意--%PowerShell stop-parsing token之后的参数不再被 PowerShell 解释因此-k utf8会原样传递给 pytest用于按关键字筛选测试。6.2 服务已运行时直接跑 pytest如果对应服务已经在运行可以跳过脚本直接调用 pytest。README 给出的示例同样从仓库根目录执行python -m pytest tests/database-dockers/postgres -q # 只运行某一个具体的测试用例 python -m pytest tests/database-dockers/postgres/test_postgresql_loader.py::TestPostgreSQLDataLoaderStatic::test_connect_forces_utf8_client_encoding -q第二行演示了如何精确到测试类::测试方法来定位单个用例适合调试 UTF-8 客户端编码等专项问题。6.3 测试如何感知环境变量数据库集成测试通过环境变量定位服务地址默认值在Set-TestEnv函数中集中定义test-dbs.ps1分组环境变量默认值PostgreSQLPG_HOST/PG_PORT/PG_USER/PG_PASSWORD/PG_DATABASElocalhost/5433/postgres/postgres/testdbMongoDBMONGO_HOST/MONGO_PORT/MONGO_USERNAME/MONGO_PASSWORD/MONGO_DATABASElocalhost/27018/testuser/testpass/testdbBigQueryBQ_PROJECT_ID/BQ_HTTP_ENDPOINTtest-project/http://localhost:9050Cosmos DBCOSMOS_ENDPOINT/COSMOS_KEY/COSMOS_DATABASEhttps://localhost:8081/ 模拟器固定密钥 /testdb以 MongoDB 测试为例test_mongodb_loader.py 通过os.getenv读取这些变量并构造连接配置还提供了mongo_available()探测函数serverSelectionTimeoutMS3000用于检测服务是否可用def get_test_config() - Dict[str, Any]: return { host: os.getenv(MONGO_HOST, localhost), port: int(os.getenv(MONGO_PORT, 27018)), username: os.getenv(MONGO_USERNAME, testuser), password: os.getenv(MONGO_PASSWORD, testpass), database: os.getenv(MONGO_DATABASE, testdb), collection: , }注意PowerShell 脚本运行时会自动设置这些环境变量而直接手动执行 pytest 时需要自行保证环境变量与容器端口一致即保持上述默认值或显式覆盖。七、直接使用 docker compose不依赖 PowerShell 时可以在任意 shell 中直接使用统一 compose 文件 docker-compose.test.yml。由于服务按 profile 分组启动时需显式指定 profile# Core 服务构建镜像并等待就绪 docker compose -f tests/database-dockers/docker-compose.test.yml --profile core up -d --build --wait # Heavy 服务 docker compose -f tests/database-dockers/docker-compose.test.yml --profile heavy up -d --wait # 全部服务同时启用两个 profile docker compose -f tests/database-dockers/docker-compose.test.yml --profile core --profile heavy up -d --build --wait # 停止 core 服务 docker compose -f tests/database-dockers/docker-compose.test.yml stop mysql postgres mongodb bigquery关键参数说明--profile name启用 compose 中对应 profile 下的服务core/heavy/all与 README 分组一一对应--build启动前重新构建本地镜像MySQL、PostgreSQL、MongoDB、BigQuery 子目录都有各自的Dockerfile--wait等待所有服务的健康检查通过后才返回配合healthcheck定义保证容器真正就绪stop子命令按容器名精确停止不会影响未列出的容器。macOS/Linux 下直接使用各服务独立 compose 的等价命令为cd tests/database-dockers/postgres docker compose up -d --build --wait八、端口冲突时的覆盖方式如果默认端口已被占用可以在启动前覆盖对应的环境变量。README 给出的示例$env:PG_PORT 15433 .\tests\database-dockers\test-dbs.ps1 start postgres由于统一 compose 文件中的端口定义全部使用了${VAR:-default}插值形式任意环境变量均可覆盖对应端口环境变量默认端口对应服务MYSQL_PORT3307MySQLPG_PORT5433PostgreSQLMONGO_PORT27018MongoDBBQ_PORT/BQ_GRPC_PORT9050/9060BigQuery emulatorHTTP / gRPCCOSMOS_PORT8081Cosmos DB emulatorSUPERSET_PORT8088Superset覆盖端口后若直接手动运行 pytest还需要同步覆盖对应的连接环境变量例如PG_PORT、MONGO_PORT保证测试客户端指向新端口。若使用test命令则无需担心——脚本内的Set-TestEnv会读取你预先设置的变量。九、文件清单tests/database-dockers/README.md 列出的关键文件docker-compose.test.yml统一编排全部测试服务的 compose 文件test-dbs.ps1PowerShell 助手脚本支持 start/stop/status/logs/test 等命令service/docker-compose.yml各服务原本独立的 compose 文件service/start.sh各服务原本的 shell 启动脚本存在处。仓库中还有配套的顶层编排文件 docker-compose.test.yml 与子目录内各服务的Dockerfile如 postgres/Dockerfile用于构建带有初始化 SQL、示例数据的专用测试镜像。十、使用注意事项README 的 Notes 一节给出了三条重要提醒均能在源码中得到印证独立容器而非大容器统一编排使用多个独立容器每个服务一个df-test-*容器而不是一个包含所有服务的大容器。这样日志、健康检查、端口与服务的生命周期彼此独立一个服务崩溃不影响其他服务排查问题也更方便。原有按服务的方式仍然支持各子目录的docker-compose.yml与start.sh脚本保持可用统一编排是增量增强而非替代。两种方式面向同一套df-test-*容器名。容器名冲突需要手动处理如果已经通过某服务的独立 compose 文件启动了同名的df-test-*容器例如df-test-postgres在启动统一编排入口之前需要先停止该容器否则会出现容器名冲突或端口占用导致启动失败。十一、从源码理解整体工作流综合 test-dbs.ps1 的实现一次完整的集成测试流程如下Set-TestEnv注入全部连接环境变量未设置时使用默认值Get-Profiles将core/heavy/all/单个服务名转换为 compose profile 列表Start-Services执行docker compose up -d [--build] --wait等待健康检查通过若目标涉及 Cosmos DB调用Seed-Cosmos运行 seed_data.py 灌入测试数据Get-TestTargets依据$TestPaths映射得到 pytest 目录列表在仓库根目录执行python -m pytest paths -q extra_args任一环节失败即抛错退出$ErrorActionPreference Stop、$LASTEXITCODE检查。这套设计使得启动服务 准备数据 跑测试三个步骤在 PowerShell 下一行命令完成同时保留了直接使用 compose 与 pytest 的底层灵活性适合 CI 流水线与本地开发两种场景复用。对于 MySQL、PostgreSQL、MongoDB 等轻量服务core分组是日常开发与回归测试的推荐选择仅在需要验证 Cosmos DB 或 Superset 连接器时再引入heavy分组。【免费下载链接】data-formulator Data Formulator is an interactive AI-powered data analysis system makes it easy to connect, explore and visualize data.项目地址: https://gitcode.com/GitHub_Trending/da/data-formulator创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考