gpt4 book ai didi

在 ExUnit 中为库测试查询

转载 作者:行者123 更新时间:2023-11-28 20:36:59 26 4
gpt4 key购买 nike

我正在该库的 ExUnit 中测试查询(不是查询返回的数据)。

https://github.com/tanweerdev/fat_ecto

这是我的代码:

 test "returns the query where field like" do
opts = %{
"$where" => %{"first_name" => %{"$like" => "%Ham %"}}
}

assert build(QM.Test.Model, opts) ==
end

控制台返回的函数结果是这样的:

 left:  #Ecto.Query<from m in QM.Test.Model,
where: like(m.first_name, ^"%Ham %")>

此处的主要目标是查看 build 函数是否正确构建查询。

我不是在测试 ecto。我正在测试构建查询的函数

如何将此结果插入正确的中以使测试通过

任何建议。

谢谢。

最佳答案

一种可能是使用 Ecto.Adapters.SQL.to_sql/3 检查生成的 SQL :

assert Ecto.Adapters.SQL.to_sql(
:all, repo, build(QM.Test.Model, opts)
) == {"SELECT ... WHERE first_name LIKE '$1'", ["%Ham %"]}

关于在 ExUnit 中为库测试查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50716040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com