Golang MongoDb GridFs 测试

huangapple go评论116阅读模式
英文:

Golang MongoDb GridFs Testing

问题

我使用Golang中的Gorilla Mux实现了一个REST API。该API可以上传/下载MongoDb GridFs中的文件。我想为我的API编写集成测试。是否有一个带有GridFs支持的嵌入式MongoDb包在Go中?我们如何使用GridFs测试API?我们需要对真实的MongoDB进行测试吗?
Java似乎有一个名为library的库(https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo)。

作为测试的一部分,我想启动嵌入式MongoDB,并在测试结束时停止它。

英文:

I have a rest API implemented using Gorilla Mux in Golang. This API uploads/downloads files from MongoDb GridFs. I want to write integration tests for my API. <br>Is there a embedded MongoDb package with GridFs support in Go? How do we test APIs using GridFs? Do we need to test against a real MongoDB?
<br>
Java seems to have such a <a href="https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo">library</a>

As part of the test, I would like to start embedded MongoDB and stop it at the end of the test.

答案1

得分: 3

据我所知,Go语言中没有内置的MongoDB。我通常使用mgo的gopkg.in/mgo.v2/dbtest来进行测试,你可以像平常一样使用以下命令安装:

  1. go get -u "gopkg.in/mgo.v2/dbtest"

虽然它需要在$PATH中有mongod,但dbtest会处理其余的事情。

你可以通过以下代码获取一个服务器:

  1. package StackOverflowTests
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "gopkg.in/mgo.v2/dbtest"
  7. )
  8. func TestFoo(t *testing.T) {
  9. d, _ := ioutil.TempDir(os.TempDir(), "mongotools-test")
  10. server := dbtest.DBServer{}
  11. server.SetPath(d)
  12. // 注意,服务器将自动启动
  13. session := server.Session()
  14. // 根据需要以编程方式插入数据
  15. setupDatabaseAndCollections(session)
  16. // 进行测试
  17. foo.Bar(session)
  18. // 进行其他测试
  19. // 由于...
  20. session.Close()
  21. // ...如果仍然有连接打开,"server.Wipe()"将引发panic
  22. // 例如,因为你在代码中对原始session进行了.Copy()。非常有用!
  23. server.Wipe()
  24. // 关闭服务器
  25. server.Stop()
  26. }

请注意,你不需要定义IP或端口,它们会自动提供(在本地主机的非保留范围内使用一个免费开放端口)。

英文:

There is no embedded MongoDB for Go as far as I am aware of.

What I do is to use mgo's own gopkg.in/mgo.v2/dbtest which you can install as usual with

  1. go get -u &quot;gopkg.in/mgo.v2/dbtest&quot;

Although it requires a mongod inside your $PATH, the dbtest takes care of all the rest.

You get a server with

  1. package StackOverflowTests
  2. import (
  3. &quot;io/ioutil&quot;
  4. &quot;os&quot;
  5. &quot;testing&quot;
  6. &quot;gopkg.in/mgo.v2/dbtest&quot;
  7. )
  8. func TestFoo(t *testing.T) {
  9. d, _ := ioutil.TempDir(os.TempDir(), &quot;mongotools-test&quot;)
  10. server := dbtest.DBServer{}
  11. server.SetPath(d)
  12. // Note that the server will be started automagically
  13. session := server.Session()
  14. // Insert data programmatically as needed
  15. setupDatabaseAndCollections(session)
  16. // Do your testing stuff
  17. foo.Bar(session)
  18. // Whatever tests you do
  19. // We can not use &quot;defer session.Close()&quot; because...
  20. session.Close()
  21. // ... &quot;server.Wipe()&quot; will panic if there are still connections open
  22. // for example because you did a .Copy() on the
  23. // original session in your code. VERY useful!
  24. server.Wipe()
  25. // Tear down the server
  26. server.Stop()
  27. }

Note that you neither have to define an IP or port, which are provided automagically (a free open port in the non-reserved range of localhost is used).

huangapple
  • 本文由 发表于 2015年11月29日 13:08:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/33979614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定