sdk_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package main
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "git.sxidc.com/service-supports/ds-sdk/sdk"
  6. "git.sxidc.com/service-supports/ds-sdk/sdk/data_mapping"
  7. "git.sxidc.com/service-supports/ds-sdk/sdk/raw_sql_tpl"
  8. "math/rand"
  9. "strconv"
  10. "sync"
  11. "testing"
  12. "time"
  13. )
  14. type Class struct {
  15. ID string `sqlmapping:"key;"`
  16. Name string `sqlmapping:"update:canClear;notQuery;"`
  17. StudentNum int `sqlmapping:"column:student_num;notUpdate;queryConditionCallback;"`
  18. Ignored string `sqlmapping:"-"`
  19. }
  20. const (
  21. token = "IpTTwAQweh/BP51fz5CzWKQFaXHvZe6ewvk6yOcAOkU="
  22. address = "localhost"
  23. httpPort = "10000"
  24. grpcPort = "10001"
  25. namespace = "ns-sdk-demo"
  26. dataSource = "ds-sdk-demo"
  27. deleteSql = "delete-sdk-demo"
  28. goRoutineCount = 100
  29. tableName = "test.classes"
  30. )
  31. var (
  32. sqlSpec = sdk.SqlSpec{
  33. Clauses: "- DELETE FROM {{ .table_name }} WHERE id = '{{ .id }}'",
  34. }
  35. )
  36. func TestBasic(t *testing.T) {
  37. classID := strutils.SimpleUUID()
  38. className := strutils.SimpleUUID()
  39. studentNum := rand.Int31n(100)
  40. insertExecuteParams, err := raw_sql_tpl.InsertExecuteParams{
  41. TableName: tableName,
  42. TableRows: []raw_sql_tpl.TableRow{
  43. {
  44. Column: "id",
  45. Value: "'" + classID + "'",
  46. },
  47. {
  48. Column: "name",
  49. Value: "'" + className + "'",
  50. },
  51. {
  52. Column: "student_num",
  53. Value: strconv.FormatInt(int64(studentNum), 10),
  54. },
  55. },
  56. }.Map()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. deleteExecuteParams := map[string]any{
  61. "table_name": tableName,
  62. "id": classID,
  63. }
  64. err = sdk.InitInstance(token, address, httpPort, grpcPort, namespace, dataSource)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. defer func() {
  69. err := sdk.DestroyInstance()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. }()
  74. err = sdk.GetInstance().CreateSQL(deleteSql, sqlSpec.ToMap())
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. _, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.InsertTpl, insertExecuteParams)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. _, err = sdk.GetInstance().ExecuteSql(deleteSql, deleteExecuteParams)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. wg := sync.WaitGroup{}
  87. wg.Add(goRoutineCount)
  88. start := time.Now()
  89. for i := 0; i < goRoutineCount; i++ {
  90. go func() {
  91. defer wg.Done()
  92. err = sdk.GetInstance().Transaction(func(tx *sdk.Transaction) error {
  93. err := tx.ExecuteRawSql(raw_sql_tpl.InsertTpl, insertExecuteParams)
  94. if err != nil {
  95. return err
  96. }
  97. err = tx.ExecuteSql(deleteSql, deleteExecuteParams)
  98. if err != nil {
  99. return err
  100. }
  101. return nil
  102. })
  103. if err != nil {
  104. panic(err)
  105. }
  106. }()
  107. }
  108. wg.Wait()
  109. end := time.Now()
  110. fmt.Println(end.Sub(start).Milliseconds())
  111. }
  112. func TestRawSqlTemplate(t *testing.T) {
  113. classID := strutils.SimpleUUID()
  114. className := strutils.SimpleUUID()
  115. studentNum := rand.Int31n(100)
  116. newClassName := strutils.SimpleUUID()
  117. newStudentNum := rand.Int31n(100)
  118. insertExecuteParams, err := raw_sql_tpl.InsertExecuteParams{
  119. TableName: tableName,
  120. TableRows: []raw_sql_tpl.TableRow{
  121. {
  122. Column: "id",
  123. Value: "'" + classID + "'",
  124. },
  125. {
  126. Column: "name",
  127. Value: "'" + className + "'",
  128. },
  129. {
  130. Column: "student_num",
  131. Value: strconv.FormatInt(int64(studentNum), 10),
  132. },
  133. },
  134. }.Map()
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. deleteExecuteParams, err := raw_sql_tpl.DeleteExecuteParams{
  139. TableName: tableName,
  140. Conditions: []raw_sql_tpl.Condition{
  141. {
  142. Column: "id",
  143. Value: "'" + classID + "'",
  144. },
  145. },
  146. }.Map()
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. updateExecuteParams, err := raw_sql_tpl.UpdateExecuteParams{
  151. TableName: tableName,
  152. TableRows: []raw_sql_tpl.TableRow{
  153. {
  154. Column: "name",
  155. Value: "'" + newClassName + "'",
  156. },
  157. {
  158. Column: "student_num",
  159. Value: strconv.FormatInt(int64(newStudentNum), 10),
  160. },
  161. },
  162. Conditions: []raw_sql_tpl.Condition{
  163. {
  164. Column: "id",
  165. Value: "'" + classID + "'",
  166. },
  167. },
  168. }.Map()
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. queryExecuteParams, err := raw_sql_tpl.QueryExecuteParams{
  173. TableName: tableName,
  174. Conditions: []raw_sql_tpl.Condition{
  175. {
  176. Column: "id",
  177. Value: "'" + classID + "'",
  178. },
  179. {
  180. Column: "name",
  181. Value: "'" + className + "'",
  182. },
  183. {
  184. Column: "student_num",
  185. Value: strconv.FormatInt(int64(studentNum), 10),
  186. },
  187. },
  188. Limit: 1,
  189. Offset: 0,
  190. }.Map()
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. newQueryExecuteParams, err := raw_sql_tpl.QueryExecuteParams{
  195. TableName: tableName,
  196. Conditions: []raw_sql_tpl.Condition{
  197. {
  198. Column: "id",
  199. Value: "'" + classID + "'",
  200. },
  201. {
  202. Column: "name",
  203. Value: "'" + newClassName + "'",
  204. },
  205. {
  206. Column: "student_num",
  207. Value: strconv.FormatInt(int64(newStudentNum), 10),
  208. },
  209. },
  210. Limit: 0,
  211. Offset: 0,
  212. }.Map()
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. countExecuteParams, err := raw_sql_tpl.CountExecuteParams{
  217. TableName: tableName,
  218. Conditions: []raw_sql_tpl.Condition{
  219. {
  220. Column: "id",
  221. Value: "'" + classID + "'",
  222. },
  223. {
  224. Column: "name",
  225. Value: "'" + className + "'",
  226. },
  227. {
  228. Column: "student_num",
  229. Value: strconv.FormatInt(int64(studentNum), 10),
  230. },
  231. },
  232. }.Map()
  233. if err != nil {
  234. t.Fatal(err)
  235. }
  236. newCountExecuteParams, err := raw_sql_tpl.CountExecuteParams{
  237. TableName: tableName,
  238. Conditions: []raw_sql_tpl.Condition{
  239. {
  240. Column: "id",
  241. Value: "'" + classID + "'",
  242. },
  243. {
  244. Column: "name",
  245. Value: "'" + newClassName + "'",
  246. },
  247. {
  248. Column: "student_num",
  249. Value: strconv.FormatInt(int64(newStudentNum), 10),
  250. },
  251. },
  252. }.Map()
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. err = sdk.InitInstance(token, address, httpPort, grpcPort, namespace, dataSource)
  257. if err != nil {
  258. t.Fatal(err)
  259. }
  260. defer func() {
  261. err := sdk.DestroyInstance()
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. }()
  266. _, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.InsertTpl, insertExecuteParams)
  267. if err != nil {
  268. t.Fatal(err)
  269. }
  270. queryResults, err := sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.QueryTpl, queryExecuteParams)
  271. if err != nil {
  272. t.Fatal(err)
  273. }
  274. countResults, err := sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.CountTpl, countExecuteParams)
  275. if err != nil {
  276. t.Fatal(err)
  277. }
  278. if float64(len(queryResults)) != countResults[0]["count"].(float64) {
  279. t.Fatal("总数不正确")
  280. }
  281. if queryResults[0]["id"].(string) != classID ||
  282. queryResults[0]["name"].(string) != className ||
  283. queryResults[0]["student_num"].(float64) != float64(studentNum) {
  284. t.Fatal("查询数据不正确")
  285. }
  286. _, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.UpdateTpl, updateExecuteParams)
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. queryResults, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.QueryTpl, newQueryExecuteParams)
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. countResults, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.CountTpl, newCountExecuteParams)
  295. if err != nil {
  296. t.Fatal(err)
  297. }
  298. if float64(len(queryResults)) != countResults[0]["count"].(float64) {
  299. t.Fatal("总数不正确")
  300. }
  301. if queryResults[0]["id"].(string) != classID ||
  302. queryResults[0]["name"].(string) != newClassName ||
  303. queryResults[0]["student_num"].(float64) != float64(newStudentNum) {
  304. t.Fatal("查询数据不正确")
  305. }
  306. _, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.DeleteTpl, deleteExecuteParams)
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. }
  311. func TestDataMapping(t *testing.T) {
  312. dataMapping, err := data_mapping.ParseDataMapping(&Class{})
  313. if err != nil {
  314. t.Fatal(err)
  315. }
  316. if dataMapping.Name != "main.Class" {
  317. t.Fatal("dataMapping名称不正确")
  318. }
  319. if dataMapping.SqlMapping == nil {
  320. t.Fatal("没有解析除SqlMapping")
  321. }
  322. for columnName, sqlColumn := range dataMapping.SqlMapping.ColumnMap {
  323. if columnName != "id" && columnName != "name" && columnName != "student_num" {
  324. t.Fatal("列名不正确")
  325. }
  326. if sqlColumn.Name != "id" && sqlColumn.Name != "name" && sqlColumn.Name != "student_num" {
  327. t.Fatal("列名不正确")
  328. }
  329. if sqlColumn.Name != columnName {
  330. t.Fatal("列名不正确")
  331. }
  332. if sqlColumn.IsKey && columnName != "id" {
  333. t.Fatal("键字段不正确")
  334. }
  335. if !sqlColumn.CanUpdate && (columnName != "id" && columnName != "student_num") {
  336. t.Fatal("不可更新字段不正确")
  337. }
  338. if sqlColumn.CanUpdateClear && columnName != "name" {
  339. t.Fatal("可清除字段不正确")
  340. }
  341. if !sqlColumn.CanQuery && columnName != "name" {
  342. t.Fatal("可清除字段不正确")
  343. }
  344. if sqlColumn.NeedQueryConditionCallback && columnName != "student_num" {
  345. t.Fatal("可清除字段不正确")
  346. }
  347. }
  348. }