草庐IT

integration-testing-mongodb-sprin

全部标签

unit-testing - 如何测试在 golang 中打印到控制台的方法?

我有下一个结构。packageloggerimport"fmt"typeIPrinterinterface{Print(valuestring)}typeConsolePrinterstruct{}func(cp*ConsolePrinter)Print(valuestring){fmt.Printf("thisisvalue:%s",value)}测试范围说我需要测试ConsolePrinterPrint方法。如何覆盖这个方法?谢谢。 最佳答案 根据@icza写的评论,我在下面编写了测试。funcTestPrint(t*testi

mongodb - 当我运行 docker compose 时,我的 golang(摄取)容器无法显示 "Error establishing Mongo session"

我运行了一个docker-composeup,我在我的golang容器上收到一条错误消息,提示“ErrorestablishingMongosession”,然后容器退出。我不确定问题是否始于我的golang容器或mongo。此时我已经尝试了很多事情。这是我的golang容器的docker日志文件。golang的docker日志Torunindebugmode,runwith'-dtrue'optiontime="2019-08-20T20:12:12Z"level=infomsg="LogginginINFOmode"time="2019-08-20T20:12:12Z"level=

mongodb - 在golang mgo中让字段空白

我正在尝试将beego与mongo结合使用。你可以在以下位置找到我的来源:https://github.com/wsourabh/bapi但是在调用v1/账户/:id我总是得到这样的回应curl-vlocalhost:8080/v1/accounts/0df542560fbfc39a4bdb24d0ca44d37e*Trying127.0.0.1...*Connectedtolocalhost(127.0.0.1)port8080(#0)>GET/v1/accounts/0df542560fbfc39a4bdb24d0ca44d37eHTTP/1.1>Host:localhost:80

mongodb - 访问 map 时的 Goroutine block

以下代码片段取自旨在从MongoDB读取文档并将其写入Postgres数据库的程序。该程序是使用生产者/消费者模式实现的:生产者Goroutine从Mongo读取并将获取的文档发送到channel。consumerGoroutine从channel中读取数据,构造一个INSERTINTOsql语句并将数据插入到Postgres数据库中。不幸的是,消费者似乎不确定地阻止。我相信当访问从producer到consumer的chan传递的map数据结构时,就会发生这种情况,但不能确定。生产者的简化代码:funcproducer(opschanBatchOp,...){//Iterateove

unit-testing - golang 这应该是一个集成测试吗?

我有一个实现数据库接口(interface)的方法,该方法将一个“对象”插入到数据库中。typedatabaseinterface{createLog(logDoc)(bool,error)}typemongostruct{databasestringcollectionstring}func(mmongo)createLog(llogDoc)(bool,error){s,err:=mgo.Dial("mongo")defers.Close()iferr!=nil{returnfalse,err}err=s.DB(m.database).C(m.collection).Insert(l

mongodb - 有没有办法通过 Find() 获取 slice ?

现在我在做:sess:=mongodb.DB("mybase").C("mycollection")varusers[]struct{Usernamestring`bson:"username"`}err=sess.Find(nil).Select(bson.M{"username":1,"_id":0}).All(&users)iferr!=nil{fmt.Println(err)}varmyUsers[]stringfor_,user:=rangeusers{myUsers=append(myUsers,user.Username)}有没有更有效的方法直接从Find(或其他搜索函数

mongodb - 如何编码/解码 mongodb 游标?

我需要建立一个“页面”列表,所以其中一部分会有一个cursor.问题是我找不到编码(到字符串)和解码光标的方法。任何的想法?Cursor接口(interface)没有“编码”方法(有ID,但未记录)并且无法从字符串(或int)创建新游标。typeCursorinterface{//GettheIDofthecursor.ID()int64//Getthenextresultfromthecursor.//Returnstrueiftherewerenoerrorsandthereisanextresult.Next(context.Context)boolDecode(interfac

unit-testing - 安装go lang后无法运行go test程序

尝试运行用Go编写的测试时出现以下错误。我安装了Golang和dep。我是Go的新手,我不确定这里的问题是什么。有人可以帮助我吗?xxxx-dxxxx:testxxxx$gotest#_/Users/xxxx/dev/xxxx/test/xxxx/testapplication_cluster_test.go:10:2:cannotfindpackage"github.com/stretchr/testify/assert"inanyof:/usr/local/Cellar/go/1.10.3/libexec/src/github.com/stretchr/testify/assert

mongodb - golang 中的聚合查询

关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭4年前。Improvethisquestion我需要用mgo在golang中编写一个查询,这是在mongo中的查询:db.some.aggregate([{$match:{"data.id":"11111"}},{$project:{_id:0,url:{$concat:["https://www.someurl.com/","$data.aID"]},items:{"$map":{"input":"$data.items","as":"it","i

unit-testing - 无法在 GoLand IDE 内的测试套件中运行单个测试?

我正在使用testify'stestsuite支持编写单元测试。这导致我的测试文件有一个TestFooBar(t*testing.T)启动suite.Run而我所有的单独测试都成为我的测试套件的一部分struct方法签名如-func(suite*myTestSuite)TestMyStuff()。我观察到GoLand可以识别所有带有类似于TestFooBar(t*testing.T)签名的方法,并在其旁边放置一个绿色的播放图标。它将允许我单独运行/调试这些方法。但是,作为上述测试套件一部分的所有测试方法都不会被识别,也无法在IDE中单独运行或调试。有什么方法可以告诉GoLandmyTe