mysql_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/smartystreets/goconvey/convey"
  6. "go-common/app/service/live/dao-anchor/api/grpc/v1"
  7. "go-common/app/service/live/dao-anchor/model"
  8. )
  9. func TestDaoNormalizeRoomIDs(t *testing.T) {
  10. var (
  11. c = context.TODO()
  12. inputIDs = []int64{5910, 5901, 63, 53, 5010, 115, 666}
  13. turnedFlags = []bool{false, false, true, true, false, true, true}
  14. )
  15. convey.Convey("When normalize a given list of ids", t, func(ctx convey.C) {
  16. normalized, err := d.dbNormalizeRoomIDs(c, inputIDs)
  17. ctx.Convey("Then short-id is turned into room-id while room-id keeps untouched", func(ctx convey.C) {
  18. ctx.So(err, convey.ShouldBeNil)
  19. ctx.So(len(normalized), convey.ShouldEqual, len(inputIDs))
  20. for i, turned := range turnedFlags {
  21. if turned {
  22. ctx.So(normalized[i] > inputIDs[i], convey.ShouldBeTrue)
  23. } else {
  24. ctx.So(normalized[i] == inputIDs[i], convey.ShouldBeTrue)
  25. }
  26. }
  27. // ctx.So(d.shortIDMapping.caches[0].Len(), convey.ShouldEqual, len(inputIDs))
  28. })
  29. ctx.Convey("Then order of result will be preserved", func(ctx convey.C) {
  30. roomIds := []int64{5901, 57796, 5010, 1011}
  31. results, err := d.dbNormalizeRoomIDs(c, roomIds)
  32. ctx.So(err, convey.ShouldBeNil)
  33. for i := range results {
  34. ctx.So(roomIds[i], convey.ShouldEqual, results[i])
  35. }
  36. })
  37. })
  38. }
  39. func TestDaoFetchAreas(t *testing.T) {
  40. var (
  41. c = context.TODO()
  42. )
  43. convey.Convey("When given a valid main area id", t, func(ctx convey.C) {
  44. req := &v1.FetchAreasReq{
  45. AreaId: 3,
  46. }
  47. resp, err := d.fetchAreas(c, req)
  48. ctx.Convey("Then we will get a list of its subarea's info along with its info", func(ctx convey.C) {
  49. ctx.So(err, convey.ShouldBeNil)
  50. ctx.So(resp.Info.AreaId, convey.ShouldEqual, req.AreaId)
  51. ctx.So(resp.Info.AreaName, convey.ShouldNotBeEmpty)
  52. ctx.So(resp.Areas, convey.ShouldNotBeEmpty)
  53. })
  54. })
  55. convey.Convey("When given a non-existed main area id", t, func(ctx convey.C) {
  56. req := &v1.FetchAreasReq{
  57. AreaId: 999,
  58. }
  59. _, err := d.fetchAreas(c, req)
  60. ctx.Convey("Then we will get nothing", func(ctx convey.C) {
  61. ctx.So(err, convey.ShouldNotBeNil)
  62. })
  63. })
  64. }
  65. func TestDaoFetchAnchorInfo(t *testing.T) {
  66. var (
  67. c = context.TODO()
  68. )
  69. convey.Convey("When to fetch anchor info for a given uid", t, func(ctx convey.C) {
  70. uid := []int64{2}
  71. const RoomID = 1024
  72. resp := make(map[int64]*v1.RoomData)
  73. err := d.dbFetchAnchorInfo(c, uid, resp, false)
  74. ctx.Convey("Then we will get room data for the anchor", func(ctx convey.C) {
  75. ctx.So(err, convey.ShouldBeNil)
  76. ctx.So(resp, convey.ShouldNotBeEmpty)
  77. data, ok := resp[RoomID]
  78. ctx.So(ok, convey.ShouldBeTrue)
  79. alv := data.AnchorLevel
  80. ctx.So(alv.MaxLevel, convey.ShouldEqual, model.MaxAnchorLevel)
  81. ctx.So(alv.Level, convey.ShouldEqual, 1)
  82. ctx.So(alv.Color, convey.ShouldEqual, 0)
  83. ctx.So(alv.Left, convey.ShouldEqual, 0)
  84. ctx.So(alv.Right, convey.ShouldEqual, 49)
  85. })
  86. })
  87. }