database_sql_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package test
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "git.sxidc.com/go-tools/utils/template"
  6. "github.com/pkg/errors"
  7. "math/rand"
  8. "testing"
  9. "time"
  10. )
  11. func TestDatabaseSqlConditions(t *testing.T) {
  12. conditions := sql.NewConditions().
  13. AddCondition("name1 = ?", "foo").
  14. Equal("age", 20).
  15. Like("name2", "%foo%").
  16. In("id1", []string{"111", "222"}).
  17. NotIn("id2", []string{"33", "444"}).
  18. Not("id3", "555").
  19. LessThan("num1", 10).
  20. LessThanAndEqual("num2", 20).
  21. GreaterThan("num3", 30).
  22. GreaterThanAndEqual("num4", 40)
  23. exceptQueries := []string{
  24. `name1 = ?`,
  25. `"age" = ?`,
  26. `"name2" LIKE ?`,
  27. `"id1" IN ?`,
  28. `"id2" NOT IN ?`,
  29. `"id3" != ?`,
  30. `"num1" < ?`,
  31. `"num2" <= ?`,
  32. `"num3" > ?`,
  33. `"num4" >= ?`,
  34. }
  35. exceptArgs := []any{
  36. "foo",
  37. 20,
  38. "%foo%",
  39. []string{"111", "222"},
  40. []string{"33", "444"},
  41. "555",
  42. 10,
  43. 20,
  44. 30,
  45. 40,
  46. }
  47. exceptAnd := `WHERE name1 = ? AND "age" = ? AND "name2" LIKE ? AND "id1" IN ? AND "id2" NOT IN ? AND "id3" != ? AND "num1" < ? AND "num2" <= ? AND "num3" > ? AND "num4" >= ?`
  48. for i, query := range conditions.Queries() {
  49. if exceptQueries[i] != query {
  50. t.Fatalf("%+v\n", errors.Errorf("Query Error: except %v, actural %v",
  51. exceptQueries[i], query))
  52. }
  53. }
  54. for i, arg := range conditions.Args() {
  55. argStringSlice, ok := arg.([]string)
  56. if ok {
  57. for j, stringArg := range argStringSlice {
  58. exceptArg, ok := exceptArgs[i].([]string)
  59. if !ok {
  60. t.Fatalf("%+v\n", errors.New("参数类型不匹配"))
  61. }
  62. if exceptArg[j] != stringArg {
  63. t.Fatalf("%+v\n", errors.Errorf("Args Error: except %v, actural %v",
  64. exceptQueries[i], stringArg))
  65. }
  66. }
  67. continue
  68. }
  69. if exceptArgs[i] != arg {
  70. t.Fatalf("%+v\n", errors.Errorf("Args Error: except %v, actural %v",
  71. exceptQueries[i], arg))
  72. }
  73. }
  74. if conditions.And() != exceptAnd {
  75. t.Fatalf("%+v\n", errors.Errorf("And Error:\nexcept %v\nactural %v\n",
  76. exceptAnd, conditions.And()))
  77. }
  78. }
  79. func TestDatabaseSqlTableRow(t *testing.T) {
  80. tableRow := sql.NewTableRow().
  81. Add("name", "test").
  82. Add("age", 20)
  83. exceptColumns := []string{
  84. `"name"`,
  85. `"age"`,
  86. }
  87. exceptValues := []any{
  88. "test",
  89. 20,
  90. }
  91. for i, column := range tableRow.Columns() {
  92. if exceptColumns[i] != column {
  93. t.Fatalf("%+v\n", errors.Errorf("Column Error: except %v, actural %v",
  94. exceptColumns[i], column))
  95. }
  96. }
  97. for i, value := range tableRow.Values() {
  98. if exceptValues[i] != value {
  99. t.Fatalf("%+v\n", errors.Errorf("Value Error: except %v, actural %v",
  100. exceptValues[i], value))
  101. }
  102. }
  103. }
  104. func TestDatabaseSqlResult(t *testing.T) {
  105. timeResult := time.Now().Local().Format("2006-01-02T15:04:05") + "+08:00"
  106. stringResult := strutils.SimpleUUID()
  107. boolResult := rand.Intn(2) == 0
  108. intResult := rand.Int()
  109. int8Result := int8(rand.Int())
  110. int16Result := int16(rand.Int())
  111. int32Result := int32(rand.Int())
  112. int64Result := int64(rand.Int())
  113. uintResult := uint(rand.Int())
  114. uint8Result := uint8(rand.Int())
  115. uint16Result := uint16(rand.Int())
  116. uint32Result := uint32(rand.Int())
  117. uint64Result := uint64(rand.Int())
  118. float32Result := rand.Float32()
  119. float64Result := rand.Float64()
  120. result := sql.Result{
  121. "time": timeResult,
  122. "string": stringResult,
  123. "bool": boolResult,
  124. "int": intResult,
  125. "int8": int8Result,
  126. "int16": int16Result,
  127. "int32": int32Result,
  128. "int64": int64Result,
  129. "uint": uintResult,
  130. "uint8": uint8Result,
  131. "uint16": uint16Result,
  132. "uint32": uint32Result,
  133. "uint64": uint64Result,
  134. "float32": float32Result,
  135. "float64": float64Result,
  136. }
  137. if result.ColumnValueStringAsTime("time").Format("2006-01-02T15:04:05")+"+08:00" != timeResult {
  138. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  139. timeResult, result.ColumnValueStringAsTime("time")))
  140. }
  141. if result.ColumnValueString("string") != stringResult {
  142. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  143. stringResult, result.ColumnValueString("string")))
  144. }
  145. if result.ColumnValueBool("bool") != boolResult {
  146. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  147. boolResult, result.ColumnValueBool("bool")))
  148. }
  149. if result.ColumnValueInt("int") != intResult {
  150. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  151. intResult, result.ColumnValueInt("int")))
  152. }
  153. if result.ColumnValueInt8("int8") != int8Result {
  154. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  155. int8Result, result.ColumnValueInt8("int8")))
  156. }
  157. if result.ColumnValueInt16("int16") != int16Result {
  158. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  159. int16Result, result.ColumnValueInt16("int16")))
  160. }
  161. if result.ColumnValueInt32("int32") != int32Result {
  162. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  163. int32Result, result.ColumnValueInt32("int32")))
  164. }
  165. if result.ColumnValueInt64("int64") != int64Result {
  166. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  167. int64Result, result.ColumnValueInt64("int64")))
  168. }
  169. if result.ColumnValueUint("uint") != uintResult {
  170. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  171. uintResult, result.ColumnValueUint("uint")))
  172. }
  173. if result.ColumnValueUint8("uint8") != uint8Result {
  174. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  175. uint8Result, result.ColumnValueUint8("uint8")))
  176. }
  177. if result.ColumnValueUint16("uint16") != uint16Result {
  178. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  179. uint16Result, result.ColumnValueUint16("uint16")))
  180. }
  181. if result.ColumnValueUint32("uint32") != uint32Result {
  182. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  183. uint32Result, result.ColumnValueUint32("uint32")))
  184. }
  185. if result.ColumnValueUint64("uint64") != uint64Result {
  186. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  187. uint64Result, result.ColumnValueUint64("uint64")))
  188. }
  189. if result.ColumnValueFloat32("float32") != float32Result {
  190. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  191. float32Result, result.ColumnValueFloat32("float32")))
  192. }
  193. if result.ColumnValueFloat64("float64") != float64Result {
  194. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  195. float64Result, result.ColumnValueFloat64("float64")))
  196. }
  197. }
  198. func TestDatabaseSqlTemplate(t *testing.T) {
  199. tableName := "test.students"
  200. insertMap, err := sql.InsertExecuteParams{
  201. TableName: tableName,
  202. TableRow: sql.NewTableRow().
  203. Add("id", "").
  204. Add("name", "").
  205. Add("age", "").
  206. Add("enter_time", ""),
  207. }.Map()
  208. if err != nil {
  209. t.Fatalf("%+v\n", err)
  210. }
  211. checkDatabaseSqlClause(t, sql.InsertTpl, insertMap, `
  212. INSERT INTO
  213. test.students ("id","name","age","enter_time")
  214. VALUES
  215. (?, ?, ?, ?)
  216. `)
  217. insertBatchMap, err := sql.InsertBatchExecuteParams{
  218. TableName: tableName,
  219. TableRowBatch: []sql.TableRow{
  220. *sql.NewTableRow().
  221. Add("id", "").
  222. Add("name", "").
  223. Add("age", "").
  224. Add("enter_time", ""),
  225. *sql.NewTableRow().
  226. Add("id", "").
  227. Add("name", "").
  228. Add("age", "").
  229. Add("enter_time", ""),
  230. },
  231. }.Map()
  232. if err != nil {
  233. t.Fatalf("%+v\n", err)
  234. }
  235. checkDatabaseSqlClause(t, sql.InsertTpl, insertBatchMap, `
  236. INSERT INTO
  237. test.students ("id","name","age","enter_time")
  238. VALUES
  239. (?, ?, ?, ?),(?, ?, ?, ?)
  240. `)
  241. deleteMap, err := sql.DeleteExecuteParams{
  242. TableName: tableName,
  243. Conditions: sql.NewConditions().
  244. Equal("id", ""),
  245. }.Map()
  246. if err != nil {
  247. t.Fatalf("%+v\n", err)
  248. }
  249. checkDatabaseSqlClause(t, sql.DeleteTpl, deleteMap, `
  250. DELETE FROM
  251. test.students
  252. WHERE
  253. "id" = ? AND 1 = 1
  254. `)
  255. updateMap, err := sql.UpdateExecuteParams{
  256. TableName: tableName,
  257. TableRow: sql.NewTableRow().
  258. Add("name", "").
  259. Add("age", "").
  260. Add("enter_time", ""),
  261. Conditions: sql.NewConditions().
  262. Equal("id", ""),
  263. }.Map()
  264. if err != nil {
  265. t.Fatalf("%+v\n", err)
  266. }
  267. checkDatabaseSqlClause(t, sql.UpdateTpl, updateMap, `
  268. UPDATE
  269. test.students
  270. SET
  271. "name" = ?, "age" = ?, "enter_time" = ?
  272. WHERE
  273. "id" = ? AND 1 = 1
  274. `)
  275. queryMap, err := sql.QueryExecuteParams{
  276. TableName: tableName,
  277. SelectClauses: []string{"name", "age"},
  278. Conditions: sql.NewConditions().
  279. Equal("name", "").
  280. Equal("age", ""),
  281. OrderBy: "name DESC",
  282. PageNo: 1,
  283. PageSize: 10,
  284. }.Map()
  285. if err != nil {
  286. t.Fatalf("%+v\n", err)
  287. }
  288. checkDatabaseSqlClause(t, sql.QueryTpl, queryMap, `
  289. SELECT
  290. name, age
  291. FROM
  292. test.students
  293. WHERE
  294. "name" = ? AND "age" = ? AND 1 = 1
  295. ORDER BY name DESC
  296. LIMIT 10
  297. OFFSET 0
  298. `)
  299. queryOneMap, err := sql.QueryOneExecuteParams{
  300. TableName: tableName,
  301. SelectClauses: []string{"name", "age"},
  302. Conditions: sql.NewConditions().
  303. Equal("id", ""),
  304. }.Map()
  305. if err != nil {
  306. t.Fatalf("%+v\n", err)
  307. }
  308. checkDatabaseSqlClause(t, sql.QueryTpl, queryOneMap, `
  309. SELECT
  310. name, age
  311. FROM
  312. test.students
  313. WHERE
  314. "id" = ? AND 1 = 1
  315. `)
  316. countMap, err := sql.CountExecuteParams{
  317. TableName: tableName,
  318. Conditions: sql.NewConditions().
  319. Equal("name", "").
  320. Equal("age", ""),
  321. }.Map()
  322. if err != nil {
  323. t.Fatalf("%+v\n", err)
  324. }
  325. checkDatabaseSqlClause(t, sql.CountTpl, countMap, `
  326. SELECT
  327. COUNT(*)
  328. FROM
  329. test.students
  330. WHERE
  331. "name" = ? AND "age" = ? AND 1 = 1
  332. `)
  333. checkExistMap, err := sql.CheckExistExecuteParams{
  334. TableName: tableName,
  335. Conditions: sql.NewConditions().
  336. Equal("name", "").
  337. Equal("age", ""),
  338. }.Map()
  339. if err != nil {
  340. t.Fatalf("%+v\n", err)
  341. }
  342. checkDatabaseSqlClause(t, sql.CountTpl, checkExistMap, `
  343. SELECT
  344. COUNT(*)
  345. FROM
  346. test.students
  347. WHERE
  348. "name" = ? AND "age" = ? AND 1 = 1
  349. `)
  350. checkHasOnlyOneMap, err := sql.CheckHasOnlyOneExecuteParams{
  351. TableName: tableName,
  352. Conditions: sql.NewConditions().
  353. Equal("name", "").
  354. Equal("age", ""),
  355. }.Map()
  356. if err != nil {
  357. t.Fatalf("%+v\n", err)
  358. }
  359. checkDatabaseSqlClause(t, sql.CountTpl, checkHasOnlyOneMap, `
  360. SELECT
  361. COUNT(*)
  362. FROM
  363. test.students
  364. WHERE
  365. "name" = ? AND "age" = ? AND 1 = 1
  366. `)
  367. }
  368. func checkDatabaseSqlClause(t *testing.T, sqlStr string, templateMap map[string]any, clause string) {
  369. parsedSql, err := template.ParseTemplateStringToString(sqlStr, templateMap)
  370. if err != nil {
  371. t.Fatalf("%+v\n", errors.New(err.Error()))
  372. }
  373. if parsedSql != clause {
  374. t.Fatalf("%+v\n", errors.Errorf("Clause Error:\nexcept: %v\nactural: %v\n",
  375. clause, parsedSql))
  376. }
  377. }