sdk.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package instance
  2. import (
  3. "git.sxidc.com/service-supports/dps-sdk"
  4. "git.sxidc.com/service-supports/dps-sdk/client"
  5. "github.com/stretchr/testify/assert"
  6. "testing"
  7. )
  8. func initClient(t *testing.T, address string, databaseID string) {
  9. err := dps.InitInstance(address, "v1", databaseID)
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. }
  14. func destroyClient(t *testing.T, databaseID string) {
  15. err := dps.DestroyInstance("v1", databaseID)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. }
  20. type ToolKit struct {
  21. t *testing.T
  22. }
  23. func newToolKit(t *testing.T) *ToolKit {
  24. return &ToolKit{t: t}
  25. }
  26. func (toolKit *ToolKit) autoMigrate(items []client.AutoMigrateItem) *ToolKit {
  27. err := dps.AutoMigrate(items...)
  28. if err != nil {
  29. toolKit.t.Fatal(err)
  30. }
  31. return toolKit
  32. }
  33. func (toolKit *ToolKit) transaction(txFunc client.TransactionFunc) *ToolKit {
  34. err := dps.Transaction(txFunc)
  35. if err != nil {
  36. toolKit.t.Fatal(err)
  37. }
  38. return toolKit
  39. }
  40. func (toolKit *ToolKit) insert(req *client.InsertRequest) *ToolKit {
  41. err := dps.Insert(req, nil)
  42. if err != nil {
  43. toolKit.t.Fatal(err)
  44. }
  45. return toolKit
  46. }
  47. func (toolKit *ToolKit) insertBatch(req *client.InsertBatchRequest) *ToolKit {
  48. err := dps.InsertBatch(req, nil)
  49. if err != nil {
  50. toolKit.t.Fatal(err)
  51. }
  52. return toolKit
  53. }
  54. func (toolKit *ToolKit) delete(req *client.DeleteRequest) *ToolKit {
  55. err := dps.Delete(req, nil)
  56. if err != nil {
  57. toolKit.t.Fatal(err)
  58. }
  59. return toolKit
  60. }
  61. func (toolKit *ToolKit) deleteBatch(req *client.DeleteBatchRequest) *ToolKit {
  62. err := dps.DeleteBatch(req, nil)
  63. if err != nil {
  64. toolKit.t.Fatal(err)
  65. }
  66. return toolKit
  67. }
  68. func (toolKit *ToolKit) update(req *client.UpdateRequest) *ToolKit {
  69. err := dps.Update(req, nil)
  70. if err != nil {
  71. toolKit.t.Fatal(err)
  72. }
  73. return toolKit
  74. }
  75. func (toolKit *ToolKit) reply(req *client.ReplayRequest) *ToolKit {
  76. err := dps.Replay(req)
  77. if err != nil {
  78. toolKit.t.Fatal(err)
  79. }
  80. return toolKit
  81. }
  82. func (toolKit *ToolKit) queryByWhereAndOrderBy(req *client.QueryByWhereAndOrderByRequest, retInfosMap *[]map[string]any, retTotalCount *int64) *ToolKit {
  83. infosMap, totalCount, err := dps.QueryByWhereAndOrderBy(req)
  84. if err != nil {
  85. toolKit.t.Fatal(err)
  86. }
  87. if retInfosMap != nil {
  88. *retInfosMap = make([]map[string]any, 0)
  89. *retInfosMap = infosMap
  90. }
  91. if retTotalCount != nil {
  92. *retTotalCount = totalCount
  93. }
  94. return toolKit
  95. }
  96. func (toolKit *ToolKit) commonQuery(req *client.CommonQueryRequest, retInfosMap *[]map[string]any, retTotalCount *int64) *ToolKit {
  97. infosMap, totalCount, err := dps.CommonQuery(req)
  98. if err != nil {
  99. toolKit.t.Fatal(err)
  100. }
  101. if retInfosMap != nil {
  102. *retInfosMap = make([]map[string]any, 0)
  103. *retInfosMap = infosMap
  104. }
  105. if retTotalCount != nil {
  106. *retTotalCount = totalCount
  107. }
  108. return toolKit
  109. }
  110. func (toolKit *ToolKit) queryByKeys(req *client.QueryByKeysRequest, retInfoMap *map[string]any) *ToolKit {
  111. infoMap, err := dps.QueryByKeys(req)
  112. if err != nil {
  113. toolKit.t.Fatal(err)
  114. }
  115. if retInfoMap != nil {
  116. *retInfoMap = make(map[string]any)
  117. *retInfoMap = infoMap
  118. }
  119. return toolKit
  120. }
  121. func (toolKit *ToolKit) countWhere(req *client.CountWhereRequest, retCount *int64) *ToolKit {
  122. count, err := dps.CountWhere(req)
  123. if err != nil {
  124. toolKit.t.Fatal(err)
  125. }
  126. if retCount != nil {
  127. *retCount = count
  128. }
  129. return toolKit
  130. }
  131. func (toolKit *ToolKit) commonCount(req *client.CommonCountRequest, retCount *int64) *ToolKit {
  132. count, err := dps.CommonCount(req)
  133. if err != nil {
  134. toolKit.t.Fatal(err)
  135. }
  136. if retCount != nil {
  137. *retCount = count
  138. }
  139. return toolKit
  140. }
  141. func (toolKit *ToolKit) eventQueryByKeys(req *client.EventQueryByKeysRequest, retInfos *[]client.EventInfo, retTotalCount *int64) *ToolKit {
  142. infos, totalCount, err := dps.EventQueryByKeys(req)
  143. if err != nil {
  144. toolKit.t.Fatal(err)
  145. }
  146. if retInfos != nil {
  147. *retInfos = make([]client.EventInfo, 0)
  148. *retInfos = infos
  149. }
  150. if retTotalCount != nil {
  151. *retTotalCount = totalCount
  152. }
  153. return toolKit
  154. }
  155. func (toolKit *ToolKit) commonEventQuery(req *client.CommonEventQueryRequest, retInfos *[]client.EventInfo, retTotalCount *int64) *ToolKit {
  156. infos, totalCount, err := dps.CommonEventQuery(req)
  157. if err != nil {
  158. toolKit.t.Fatal(err)
  159. }
  160. if retInfos != nil {
  161. *retInfos = make([]client.EventInfo, 0)
  162. *retInfos = infos
  163. }
  164. if retTotalCount != nil {
  165. *retTotalCount = totalCount
  166. }
  167. return toolKit
  168. }
  169. func (toolKit *ToolKit) countEventByKeys(req *client.CountEventByKeysRequest, retCount *int64) *ToolKit {
  170. count, err := dps.CountEventByKeys(req)
  171. if err != nil {
  172. toolKit.t.Fatal(err)
  173. }
  174. if retCount != nil {
  175. *retCount = count
  176. }
  177. return toolKit
  178. }
  179. func (toolKit *ToolKit) commonCountEvent(req *client.CommonCountEventRequest, retCount *int64) *ToolKit {
  180. count, err := dps.CommonCountEvent(req)
  181. if err != nil {
  182. toolKit.t.Fatal(err)
  183. }
  184. if retCount != nil {
  185. *retCount = count
  186. }
  187. return toolKit
  188. }
  189. func (toolKit *ToolKit) eventHistoryQueryByKeys(req *client.EventQueryByKeysRequest, retInfos *[]client.EventInfo, retTotalCount *int64) *ToolKit {
  190. infos, totalCount, err := dps.EventHistoryQueryByKeys(req)
  191. if err != nil {
  192. toolKit.t.Fatal(err)
  193. }
  194. if retInfos != nil {
  195. *retInfos = make([]client.EventInfo, 0)
  196. *retInfos = infos
  197. }
  198. if retTotalCount != nil {
  199. *retTotalCount = totalCount
  200. }
  201. return toolKit
  202. }
  203. func (toolKit *ToolKit) commonEventHistoryQuery(req *client.CommonEventQueryRequest, retInfos *[]client.EventInfo, retTotalCount *int64) *ToolKit {
  204. infos, totalCount, err := dps.CommonEventHistoryQuery(req)
  205. if err != nil {
  206. toolKit.t.Fatal(err)
  207. }
  208. if retInfos != nil {
  209. *retInfos = make([]client.EventInfo, 0)
  210. *retInfos = infos
  211. }
  212. if retTotalCount != nil {
  213. *retTotalCount = totalCount
  214. }
  215. return toolKit
  216. }
  217. func (toolKit *ToolKit) countEventHistoryByKeys(req *client.CountEventByKeysRequest, retCount *int64) *ToolKit {
  218. count, err := dps.CountEventHistoryByKeys(req)
  219. if err != nil {
  220. toolKit.t.Fatal(err)
  221. }
  222. if retCount != nil {
  223. *retCount = count
  224. }
  225. return toolKit
  226. }
  227. func (toolKit *ToolKit) commonCountEventHistory(req *client.CommonCountEventRequest, retCount *int64) *ToolKit {
  228. count, err := dps.CommonCountEventHistory(req)
  229. if err != nil {
  230. toolKit.t.Fatal(err)
  231. }
  232. if retCount != nil {
  233. *retCount = count
  234. }
  235. return toolKit
  236. }
  237. func (toolKit *ToolKit) assertEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) *ToolKit {
  238. assert.Equal(toolKit.t, expected, actual, msgAndArgs)
  239. return toolKit
  240. }
  241. func (toolKit *ToolKit) assertNotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) *ToolKit {
  242. assert.NotEqual(toolKit.t, expected, actual, msgAndArgs)
  243. return toolKit
  244. }
  245. func (toolKit *ToolKit) assertNotEmpty(object interface{}, msgAndArgs ...interface{}) *ToolKit {
  246. assert.NotEmpty(toolKit.t, object, msgAndArgs)
  247. return toolKit
  248. }