delete_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package ugc
  2. import (
  3. "fmt"
  4. "testing"
  5. "go-common/app/job/main/tv/model/ugc"
  6. arccli "go-common/app/service/main/archive/api"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestUgcTxDelArc(t *testing.T) {
  10. var (
  11. tx, _ = d.DB.Begin(ctx)
  12. aid = int64(0)
  13. )
  14. Convey("TxDelArc", t, func(ctx C) {
  15. err := d.TxDelArc(tx, aid)
  16. ctx.Convey("Then err should be nil.", func(ctx C) {
  17. ctx.So(err, ShouldBeNil)
  18. })
  19. })
  20. }
  21. func TestUgcTxDelVideos(t *testing.T) {
  22. var (
  23. tx, _ = d.DB.Begin(ctx)
  24. aid = int64(0)
  25. )
  26. Convey("TxDelVideos", t, func(ctx C) {
  27. err := d.TxDelVideos(tx, aid)
  28. ctx.Convey("Then err should be nil.", func(ctx C) {
  29. ctx.So(err, ShouldBeNil)
  30. })
  31. })
  32. }
  33. func TestUgcTxDelVideo(t *testing.T) {
  34. var (
  35. tx, _ = d.DB.Begin(ctx)
  36. cid = int64(0)
  37. )
  38. Convey("TxDelVideo", t, func(ctx C) {
  39. err := d.TxDelVideo(tx, cid)
  40. ctx.Convey("Then err should be nil.", func(ctx C) {
  41. ctx.So(err, ShouldBeNil)
  42. })
  43. })
  44. }
  45. func TestDao_DelVideoArc(t *testing.T) {
  46. Convey("TestDao_DelVideoArc", t, WithDao(func(d *Dao) {
  47. var (
  48. aid = int64(99998888)
  49. cid1 = int64(999988881)
  50. cid2 = int64(999988882)
  51. tx, _ = d.DB.Begin(ctx)
  52. arc = &arccli.Arc{Aid: aid}
  53. countVQ = "SELECT COUNT(1) FROM ugc_video WHERE aid = ? AND deleted = 0"
  54. countAQ = "SELECT COUNT(1) FROM ugc_archive WHERE aid = ? AND deleted = 0"
  55. countV, countA int
  56. arcValid bool
  57. )
  58. // add archive and two videos
  59. d.TxImportArc(tx, &arccli.Arc{Aid: aid})
  60. d.TxMnlVideos(tx, &arccli.ViewReply{
  61. Arc: arc,
  62. Pages: []*arccli.Page{
  63. {
  64. Cid: cid1,
  65. },
  66. {
  67. Cid: cid2,
  68. },
  69. },
  70. })
  71. tx.Commit()
  72. d.DB.QueryRow(ctx, countVQ, aid).Scan(&countV)
  73. So(countV, ShouldEqual, 2)
  74. // delete one video, still one active video under the archive, we keep the archive
  75. _, err := d.DelVideoArc(ctx, &ugc.DelVideos{
  76. AID: aid,
  77. CIDs: []int64{cid1},
  78. })
  79. So(err, ShouldBeNil)
  80. d.DB.QueryRow(ctx, countVQ, aid).Scan(&countV)
  81. d.DB.QueryRow(ctx, countAQ, aid).Scan(&countA)
  82. So(countV, ShouldEqual, 1)
  83. So(countA, ShouldEqual, 1)
  84. So(err, ShouldBeNil)
  85. // delete the last video, the archive should also be deleted
  86. arcValid, err = d.DelVideoArc(ctx, &ugc.DelVideos{
  87. AID: aid,
  88. CIDs: []int64{cid2},
  89. })
  90. So(err, ShouldBeNil)
  91. So(arcValid, ShouldBeFalse)
  92. d.DB.QueryRow(ctx, countVQ, aid).Scan(&countV)
  93. d.DB.QueryRow(ctx, countAQ, aid).Scan(&countA)
  94. So(countV, ShouldEqual, 0)
  95. So(countA, ShouldEqual, 0)
  96. }))
  97. }
  98. func TestDao_DelVideos(t *testing.T) {
  99. Convey("TestDao_DelVideos", t, WithDao(func(d *Dao) {
  100. var (
  101. aid = int64(99998888)
  102. cid1 = 99998887
  103. cid2 = 99998886
  104. insertSQL = "REPLACE INTO ugc_video (aid, cid) VALUES (%d, %d)"
  105. )
  106. ress, err2 := d.DB.Exec(ctx, fmt.Sprintf(insertSQL, aid, cid1))
  107. fmt.Println(fmt.Sprintf(insertSQL, aid, cid1))
  108. fmt.Println(err2)
  109. fmt.Println(ress.RowsAffected())
  110. d.DB.Exec(ctx, fmt.Sprintf(insertSQL, aid, cid2))
  111. var count int
  112. d.DB.QueryRow(ctx, "SELECT COUNT(1) FROM ugc_video WHERE aid = ? AND deleted = 0", aid).Scan(&count)
  113. So(count, ShouldEqual, 2)
  114. err := d.DelVideos(ctx, aid)
  115. So(err, ShouldBeNil)
  116. d.DB.QueryRow(ctx, "SELECT COUNT(1) FROM ugc_video WHERE aid = ? AND deleted = 0", aid).Scan(&count)
  117. So(count, ShouldEqual, 0)
  118. }))
  119. }