sdk.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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) queryOnlyByWhereAndOrderBy(req *client.QueryByWhereAndOrderByRequest, retInfosMap *[]map[string]any) *ToolKit {
  111. infosMap, err := dps.QueryOnlyByWhereAndOrderBy(req)
  112. if err != nil {
  113. toolKit.t.Fatal(err)
  114. }
  115. if retInfosMap != nil {
  116. *retInfosMap = make([]map[string]any, 0)
  117. *retInfosMap = infosMap
  118. }
  119. return toolKit
  120. }
  121. func (toolKit *ToolKit) commonQueryOnly(req *client.CommonQueryRequest, retInfosMap *[]map[string]any) *ToolKit {
  122. infosMap, err := dps.CommonQueryOnly(req)
  123. if err != nil {
  124. toolKit.t.Fatal(err)
  125. }
  126. if retInfosMap != nil {
  127. *retInfosMap = make([]map[string]any, 0)
  128. *retInfosMap = infosMap
  129. }
  130. return toolKit
  131. }
  132. func (toolKit *ToolKit) queryByKeys(req *client.QueryByKeysRequest, retInfoMap *map[string]any) *ToolKit {
  133. infoMap, err := dps.QueryByKeys(req)
  134. if err != nil {
  135. toolKit.t.Fatal(err)
  136. }
  137. if retInfoMap != nil {
  138. *retInfoMap = make(map[string]any)
  139. *retInfoMap = infoMap
  140. }
  141. return toolKit
  142. }
  143. func (toolKit *ToolKit) countWhere(req *client.CountWhereRequest, retCount *int64) *ToolKit {
  144. count, err := dps.CountWhere(req)
  145. if err != nil {
  146. toolKit.t.Fatal(err)
  147. }
  148. if retCount != nil {
  149. *retCount = count
  150. }
  151. return toolKit
  152. }
  153. func (toolKit *ToolKit) commonCount(req *client.CommonCountRequest, retCount *int64) *ToolKit {
  154. count, err := dps.CommonCount(req)
  155. if err != nil {
  156. toolKit.t.Fatal(err)
  157. }
  158. if retCount != nil {
  159. *retCount = count
  160. }
  161. return toolKit
  162. }
  163. func (toolKit *ToolKit) eventQueryByKeys(req *client.EventQueryByKeysRequest, retInfos *[]client.EventInfo, retTotalCount *int64) *ToolKit {
  164. infos, totalCount, err := dps.EventQueryByKeys(req)
  165. if err != nil {
  166. toolKit.t.Fatal(err)
  167. }
  168. if retInfos != nil {
  169. *retInfos = make([]client.EventInfo, 0)
  170. *retInfos = infos
  171. }
  172. if retTotalCount != nil {
  173. *retTotalCount = totalCount
  174. }
  175. return toolKit
  176. }
  177. func (toolKit *ToolKit) commonEventQuery(req *client.CommonEventQueryRequest, retInfos *[]client.EventInfo, retTotalCount *int64) *ToolKit {
  178. infos, totalCount, err := dps.CommonEventQuery(req)
  179. if err != nil {
  180. toolKit.t.Fatal(err)
  181. }
  182. if retInfos != nil {
  183. *retInfos = make([]client.EventInfo, 0)
  184. *retInfos = infos
  185. }
  186. if retTotalCount != nil {
  187. *retTotalCount = totalCount
  188. }
  189. return toolKit
  190. }
  191. func (toolKit *ToolKit) eventQueryOnlyByKeys(req *client.EventQueryByKeysRequest, retInfos *[]client.EventInfo) *ToolKit {
  192. infos, err := dps.EventQueryOnlyByKeys(req)
  193. if err != nil {
  194. toolKit.t.Fatal(err)
  195. }
  196. if retInfos != nil {
  197. *retInfos = make([]client.EventInfo, 0)
  198. *retInfos = infos
  199. }
  200. return toolKit
  201. }
  202. func (toolKit *ToolKit) commonEventQueryOnly(req *client.CommonEventQueryRequest, retInfos *[]client.EventInfo) *ToolKit {
  203. infos, err := dps.CommonEventQueryOnly(req)
  204. if err != nil {
  205. toolKit.t.Fatal(err)
  206. }
  207. if retInfos != nil {
  208. *retInfos = make([]client.EventInfo, 0)
  209. *retInfos = infos
  210. }
  211. return toolKit
  212. }
  213. func (toolKit *ToolKit) countEventByKeys(req *client.CountEventByKeysRequest, retCount *int64) *ToolKit {
  214. count, err := dps.CountEventByKeys(req)
  215. if err != nil {
  216. toolKit.t.Fatal(err)
  217. }
  218. if retCount != nil {
  219. *retCount = count
  220. }
  221. return toolKit
  222. }
  223. func (toolKit *ToolKit) commonCountEvent(req *client.CommonCountEventRequest, retCount *int64) *ToolKit {
  224. count, err := dps.CommonCountEvent(req)
  225. if err != nil {
  226. toolKit.t.Fatal(err)
  227. }
  228. if retCount != nil {
  229. *retCount = count
  230. }
  231. return toolKit
  232. }
  233. func (toolKit *ToolKit) eventHistoryQueryByKeys(req *client.EventQueryByKeysRequest, retInfos *[]client.EventInfo, retTotalCount *int64) *ToolKit {
  234. infos, totalCount, err := dps.EventHistoryQueryByKeys(req)
  235. if err != nil {
  236. toolKit.t.Fatal(err)
  237. }
  238. if retInfos != nil {
  239. *retInfos = make([]client.EventInfo, 0)
  240. *retInfos = infos
  241. }
  242. if retTotalCount != nil {
  243. *retTotalCount = totalCount
  244. }
  245. return toolKit
  246. }
  247. func (toolKit *ToolKit) commonEventHistoryQuery(req *client.CommonEventQueryRequest, retInfos *[]client.EventInfo, retTotalCount *int64) *ToolKit {
  248. infos, totalCount, err := dps.CommonEventHistoryQuery(req)
  249. if err != nil {
  250. toolKit.t.Fatal(err)
  251. }
  252. if retInfos != nil {
  253. *retInfos = make([]client.EventInfo, 0)
  254. *retInfos = infos
  255. }
  256. if retTotalCount != nil {
  257. *retTotalCount = totalCount
  258. }
  259. return toolKit
  260. }
  261. func (toolKit *ToolKit) eventHistoryQueryOnlyByKeys(req *client.EventQueryByKeysRequest, retInfos *[]client.EventInfo) *ToolKit {
  262. infos, err := dps.EventHistoryQueryOnlyByKeys(req)
  263. if err != nil {
  264. toolKit.t.Fatal(err)
  265. }
  266. if retInfos != nil {
  267. *retInfos = make([]client.EventInfo, 0)
  268. *retInfos = infos
  269. }
  270. return toolKit
  271. }
  272. func (toolKit *ToolKit) commonEventHistoryQueryOnly(req *client.CommonEventQueryRequest, retInfos *[]client.EventInfo) *ToolKit {
  273. infos, err := dps.CommonEventHistoryQueryOnly(req)
  274. if err != nil {
  275. toolKit.t.Fatal(err)
  276. }
  277. if retInfos != nil {
  278. *retInfos = make([]client.EventInfo, 0)
  279. *retInfos = infos
  280. }
  281. return toolKit
  282. }
  283. func (toolKit *ToolKit) countEventHistoryByKeys(req *client.CountEventByKeysRequest, retCount *int64) *ToolKit {
  284. count, err := dps.CountEventHistoryByKeys(req)
  285. if err != nil {
  286. toolKit.t.Fatal(err)
  287. }
  288. if retCount != nil {
  289. *retCount = count
  290. }
  291. return toolKit
  292. }
  293. func (toolKit *ToolKit) commonCountEventHistory(req *client.CommonCountEventRequest, retCount *int64) *ToolKit {
  294. count, err := dps.CommonCountEventHistory(req)
  295. if err != nil {
  296. toolKit.t.Fatal(err)
  297. }
  298. if retCount != nil {
  299. *retCount = count
  300. }
  301. return toolKit
  302. }
  303. func (toolKit *ToolKit) assertEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) *ToolKit {
  304. assert.Equal(toolKit.t, expected, actual, msgAndArgs)
  305. return toolKit
  306. }
  307. func (toolKit *ToolKit) assertNotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) *ToolKit {
  308. assert.NotEqual(toolKit.t, expected, actual, msgAndArgs)
  309. return toolKit
  310. }
  311. func (toolKit *ToolKit) assertNotEmpty(object interface{}, msgAndArgs ...interface{}) *ToolKit {
  312. assert.NotEmpty(toolKit.t, object, msgAndArgs)
  313. return toolKit
  314. }