Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单元测试环境与生产环境使用不同类型的数据库导致无法对部分内容进行单元测试 #528

Closed
DevDengChao opened this issue May 16, 2024 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@DevDengChao
Copy link
Contributor

基本信息

  • ruoyi-vue-pro 版本:2.1.0
  • 操作系统:不相关
  • 数据库:MySQL, H2

你猜测可能的原因

目前单元测试使用 H2 作为测试用例的数据库, 但部署到生成环境时通常使用的是其他类型的数据库, 例如 MySQL, 这就导致在编写 SQL 语句时需要考虑兼容性的问题.

复现步骤

第一步,编写包含日期格式的 SQL

第二步,单元测试使用的 H2 使用的函数是 https://h2database.com/html/functions.html#formatdatetime

第三步,生产环境使用的 MySQL 使用的函数是 https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

第四步, mapper 上无法编写两边都兼容的 Select 语句.

报错信息

image

@DevDengChao
Copy link
Contributor Author

应当鼓励开发者使用生成环境相同的数据库类型进行单元测试, 避免破坏 不可变技术设施 原则.

@YunaiV
Copy link
Owner

YunaiV commented May 17, 2024

https://blog.csdn.net/weixin_42710740/article/details/121742580
可以通过这个方式,曲线救国。

单元测试,要考虑独立运行,不建议跑一个 mysql 进程哈。
h2 作为内存数据库,基本算业内的最佳实践啦。包括 spring 也是这么推荐的噢

@YunaiV YunaiV self-assigned this May 17, 2024
@YunaiV YunaiV added the question Further information is requested label May 17, 2024
@YunaiV YunaiV closed this as completed May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants