sdk_test.go 8.5 KB

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