privilege_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "go-common/app/admin/main/vip/model"
  6. "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestDaoPrivilegeResourcesList(t *testing.T) {
  9. convey.Convey("PrivilegeResourcesList", t, func() {
  10. res, err := d.PrivilegeResourcesList(context.TODO())
  11. convey.So(err, convey.ShouldBeNil)
  12. convey.So(res, convey.ShouldNotBeNil)
  13. })
  14. }
  15. // go test -test.v -test.run TestDaoAddPrivilege
  16. func TestDaoAddPrivilege(t *testing.T) {
  17. tx := d.BeginGormTran(context.Background())
  18. p := &model.Privilege{
  19. Name: "超高清",
  20. Title: "超高清标题",
  21. Explain: "超高清描述描述超多字超多字超多字超多字超多字超多字超多字超多字超多字超多字超多字超多字超多字",
  22. Type: 1,
  23. Operator: "admin",
  24. State: 0,
  25. Deleted: 0,
  26. IconURL: "https://activity.hdslb.com/blackboard/activity9757/static/img/title_screen.c529058.jpg",
  27. IconGrayURL: "https://activity.hdslb.com/blackboard/activity9757/static/img/title_screen.c529058.jpg",
  28. Order: 1,
  29. LangType: 1,
  30. }
  31. convey.Convey("get max id", t, func() {
  32. ep := new(model.Privilege)
  33. db := d.vip.Table(_vipPrivileges).Order("order_num DESC").First(&ep)
  34. convey.So(db.Error, convey.ShouldBeNil)
  35. d.vip.Table(_vipPrivilegesResources).Where("pid > ?", ep.ID).Delete(model.PrivilegeResources{})
  36. p.ID = ep.ID + 1
  37. })
  38. convey.Convey("AddPrivilege", t, func() {
  39. id, err := d.AddPrivilege(tx, p)
  40. convey.So(err, convey.ShouldBeNil)
  41. convey.So(id, convey.ShouldNotBeNil)
  42. })
  43. convey.Convey("AddPrivilegeResources", t, func() {
  44. a, err := d.AddPrivilegeResources(tx, &model.PrivilegeResources{
  45. PID: p.ID,
  46. Link: "web",
  47. Type: model.WebResources,
  48. })
  49. convey.So(err, convey.ShouldBeNil)
  50. convey.So(a, convey.ShouldNotBeNil)
  51. a, err = d.AddPrivilegeResources(tx, &model.PrivilegeResources{
  52. PID: p.ID,
  53. Link: "app",
  54. Type: model.AppResources,
  55. })
  56. convey.So(err, convey.ShouldBeNil)
  57. convey.So(a, convey.ShouldNotBeNil)
  58. })
  59. convey.Convey("AddPrivilege Commit", t, func() {
  60. err := tx.Commit().Error
  61. convey.So(err, convey.ShouldBeNil)
  62. })
  63. convey.Convey("PrivilegeList", t, func() {
  64. res, err := d.PrivilegeList(context.TODO(), 1)
  65. convey.So(err, convey.ShouldBeNil)
  66. convey.So(res, convey.ShouldNotBeNil)
  67. })
  68. convey.Convey("clean data", t, func() {
  69. d.vip.Delete(p)
  70. d.vip.Table(_vipPrivilegesResources).Where("pid >= ?", p.ID).Delete(model.PrivilegeResources{})
  71. })
  72. }
  73. func TestDaoMaxOrder(t *testing.T) {
  74. convey.Convey("MaxOrder", t, func() {
  75. order, err := d.MaxOrder(context.TODO())
  76. convey.So(err, convey.ShouldBeNil)
  77. convey.So(order, convey.ShouldNotBeNil)
  78. })
  79. }
  80. // go test -test.v -test.run TestDaoUpdateStatePrivilege
  81. func TestDaoUpdateStatePrivilege(t *testing.T) {
  82. convey.Convey("UpdateStatePrivilege", t, func() {
  83. a, err := d.UpdateStatePrivilege(context.TODO(), &model.Privilege{
  84. ID: 3,
  85. State: 1,
  86. })
  87. convey.So(err, convey.ShouldBeNil)
  88. convey.So(a, convey.ShouldNotBeNil)
  89. })
  90. }
  91. // go test -test.v -test.run TestDaoDeletePrivilege
  92. func TestDaoDeletePrivilege(t *testing.T) {
  93. convey.Convey("DeletePrivilege", t, func() {
  94. a, err := d.DeletePrivilege(context.TODO(), 3)
  95. convey.So(err, convey.ShouldBeNil)
  96. convey.So(a, convey.ShouldNotBeNil)
  97. })
  98. }
  99. // go test -test.v -test.run TestDaoUpdatePrivilege
  100. func TestDaoUpdatePrivilege(t *testing.T) {
  101. convey.Convey("UpdatePrivilege", t, func() {
  102. tx := d.BeginGormTran(context.TODO())
  103. a, err := d.UpdatePrivilege(tx, &model.Privilege{
  104. ID: 3,
  105. Name: "超高清",
  106. Title: "超高清标题",
  107. Explain: "超高清描述描述超多字超多字超多字超多字超多字超多字超多字超多字超多字超多字超多字超多字超多字",
  108. Type: 1,
  109. Operator: "admin",
  110. State: 0,
  111. Deleted: 0,
  112. IconURL: "https://activity.hdslb.com/blackboard/activity9757/static/img/title_screen.c529058.jpg",
  113. IconGrayURL: "https://activity.hdslb.com/blackboard/activity9757/static/img/title_screen.c529058.jpg",
  114. })
  115. convey.So(err, convey.ShouldBeNil)
  116. convey.So(a, convey.ShouldNotBeNil)
  117. err = tx.Commit().Error
  118. convey.So(err, convey.ShouldBeNil)
  119. })
  120. }
  121. // go test -test.v -test.run TestDaoUpdatePrivilegeResources
  122. func TestDaoUpdatePrivilegeResources(t *testing.T) {
  123. convey.Convey("UpdatePrivilegeResources", t, func() {
  124. tx := d.BeginGormTran(context.TODO())
  125. aff, err := d.UpdatePrivilegeResources(tx, &model.PrivilegeResources{
  126. PID: 3,
  127. Link: "app2",
  128. Type: model.AppResources,
  129. })
  130. convey.So(err, convey.ShouldBeNil)
  131. convey.So(aff, convey.ShouldNotBeNil)
  132. err = tx.Commit().Error
  133. convey.So(err, convey.ShouldBeNil)
  134. })
  135. }
  136. // go test -test.v -test.run TestDaoUpdateOrder
  137. func TestDaoUpdateOrder(t *testing.T) {
  138. convey.Convey("UpdateOrder", t, func() {
  139. a, err := d.UpdateOrder(context.TODO(), 4, 5)
  140. convey.So(err, convey.ShouldBeNil)
  141. convey.So(a, convey.ShouldNotBeNil)
  142. })
  143. }