database_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. package test
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  4. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/data_service"
  5. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/operations"
  6. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  7. "git.sxidc.com/go-tools/utils/strutils"
  8. "github.com/pkg/errors"
  9. "math/rand"
  10. "testing"
  11. "time"
  12. )
  13. func TestOperations(t *testing.T) {
  14. dbOperations, err := operations.NewOperations(&operations.Config{
  15. UserName: "test",
  16. Password: "123456",
  17. Address: "localhost",
  18. Port: "30432",
  19. Database: "test",
  20. MaxConnections: 40,
  21. MaxIdleConnections: 10,
  22. LogLevel: "error",
  23. })
  24. if err != nil {
  25. t.Fatalf("%+v\n", err)
  26. }
  27. testDBExecutor(t, dbOperations)
  28. }
  29. func TestDataService(t *testing.T) {
  30. dataService, err := data_service.NewExecutor(&data_service.Config{
  31. Token: "8qe+uPgpQ2JWxu3lSyOx5EWC24/c+zJOxvFhvRLRTzU=",
  32. Address: "localhost",
  33. HttpPort: "10000",
  34. GrpcPort: "10001",
  35. Namespace: "baize",
  36. DataSource: "baize",
  37. HttpTimeoutSec: 10,
  38. })
  39. if err != nil {
  40. t.Fatalf("%+v\n", err)
  41. }
  42. testDBExecutor(t, dataService)
  43. }
  44. func TestDatabase(t *testing.T) {
  45. dbOperations, err := operations.NewOperations(&operations.Config{
  46. UserName: "test",
  47. Password: "123456",
  48. Address: "localhost",
  49. Port: "30432",
  50. Database: "test",
  51. MaxConnections: 40,
  52. MaxIdleConnections: 10,
  53. LogLevel: "error",
  54. })
  55. if err != nil {
  56. t.Fatalf("%+v\n", err)
  57. }
  58. dataService, err := data_service.NewExecutor(&data_service.Config{
  59. Token: "8qe+uPgpQ2JWxu3lSyOx5EWC24/c+zJOxvFhvRLRTzU=",
  60. Address: "localhost",
  61. HttpPort: "10000",
  62. GrpcPort: "10001",
  63. Namespace: "baize",
  64. DataSource: "baize",
  65. HttpTimeoutSec: 10,
  66. })
  67. if err != nil {
  68. t.Fatalf("%+v\n", err)
  69. }
  70. testDatabaseEntity(t, dbOperations, dbOperations)
  71. testDatabase(t, dbOperations, dbOperations)
  72. testDatabaseEntity(t, dataService, dataService)
  73. testDatabase(t, dataService, dataService)
  74. err = database.Transaction(dbOperations, func(tx database.Executor) error {
  75. testDatabaseEntity(t, dbOperations, tx)
  76. testDatabase(t, dbOperations, tx)
  77. return nil
  78. })
  79. if err != nil {
  80. t.Fatalf("%+v\n", err)
  81. }
  82. err = database.Transaction(dbOperations, func(tx database.Executor) error {
  83. testDatabaseEntity(t, dataService, tx)
  84. testDatabase(t, dataService, tx)
  85. return nil
  86. })
  87. if err != nil {
  88. t.Fatalf("%+v\n", err)
  89. }
  90. }
  91. func testDBExecutor(t *testing.T, dbExecutor database.Executor) {
  92. id := strutils.SimpleUUID()
  93. name := strutils.SimpleUUID()
  94. age := rand.Int31()
  95. enterTime := time.Now().Local()
  96. _, err := dbExecutor.ExecuteRawSql(`
  97. INSERT INTO
  98. test.students (id, name, age, enter_time)
  99. VALUES
  100. (?, ?, ?, ?)
  101. `, id, name, age, enterTime)
  102. if err != nil {
  103. t.Fatalf("%+v\n", err)
  104. }
  105. results, err := dbExecutor.ExecuteRawSql(`
  106. SELECT
  107. *
  108. FROM
  109. test.students
  110. WHERE
  111. id = ?
  112. `, id)
  113. if err != nil {
  114. t.Fatalf("%+v\n", err)
  115. }
  116. if results[0].ColumnValueString("id") != id {
  117. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  118. id, results[0].ColumnValueString("id")))
  119. }
  120. if results[0].ColumnValueString("name") != name {
  121. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  122. name, results[0].ColumnValueString("name")))
  123. }
  124. if results[0].ColumnValueInt32("age") != age {
  125. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  126. age, results[0].ColumnValueInt32("age")))
  127. }
  128. if results[0].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime.Format(time.DateTime) {
  129. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  130. enterTime, results[0].ColumnValueTime("enter_time")))
  131. }
  132. _, err = dbExecutor.ExecuteRawSql(`
  133. DELETE FROM
  134. test.students
  135. WHERE
  136. id = ?
  137. `, id)
  138. if err != nil {
  139. t.Fatalf("%+v\n", err)
  140. }
  141. results, err = dbExecutor.ExecuteRawSql(`
  142. SELECT
  143. *
  144. FROM
  145. test.students
  146. WHERE
  147. id = ?
  148. `, id)
  149. if err != nil {
  150. t.Fatalf("%+v\n", err)
  151. }
  152. if len(results) != 0 {
  153. t.Fatalf("Delete Error: %+v\n", results)
  154. }
  155. _, err = dbExecutor.ExecuteRawSqlTemplate(`
  156. INSERT INTO
  157. {{ .table_name }} (id, name, age, enter_time)
  158. VALUES
  159. (?, ?, ?, ?)
  160. `, map[string]any{"table_name": "test.students"}, id, name, age, enterTime)
  161. if err != nil {
  162. t.Fatalf("%+v\n", err)
  163. }
  164. results, err = dbExecutor.ExecuteRawSqlTemplate(`
  165. SELECT
  166. *
  167. FROM
  168. {{ .table_name }}
  169. WHERE
  170. id = ?
  171. `, map[string]any{"table_name": "test.students"}, id)
  172. if err != nil {
  173. t.Fatalf("%+v\n", err)
  174. }
  175. if results[0].ColumnValueString("id") != id {
  176. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  177. id, results[0].ColumnValueString("id")))
  178. }
  179. if results[0].ColumnValueString("name") != name {
  180. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  181. name, results[0].ColumnValueString("name")))
  182. }
  183. if results[0].ColumnValueInt32("age") != age {
  184. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  185. age, results[0].ColumnValueInt32("age")))
  186. }
  187. if results[0].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime.Format(time.DateTime) {
  188. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  189. enterTime, results[0].ColumnValueTime("enter_time")))
  190. }
  191. _, err = dbExecutor.ExecuteRawSqlTemplate(`
  192. DELETE FROM
  193. {{ .table_name }}
  194. WHERE
  195. id = ?
  196. `, map[string]any{"table_name": "test.students"}, id)
  197. if err != nil {
  198. t.Fatalf("%+v\n", err)
  199. }
  200. results, err = dbExecutor.ExecuteRawSqlTemplate(`
  201. SELECT
  202. *
  203. FROM
  204. {{ .table_name }}
  205. WHERE
  206. id = ?
  207. `, map[string]any{"table_name": "test.students"}, id)
  208. if err != nil {
  209. t.Fatalf("%+v\n", err)
  210. }
  211. if len(results) != 0 {
  212. t.Fatalf("Delete Error: %+v\n", results)
  213. }
  214. }
  215. type Student struct {
  216. ID string `sqlmapping:"column:id" sqlresult:"column:id"`
  217. Name string `sqlmapping:"column:name" sqlresult:"column:name"`
  218. Age int32 `sqlmapping:"column:age" sqlresult:"column:age"`
  219. EnterTime time.Time `sqlmapping:"column:enter_time" sqlresult:"column:enter_time"`
  220. }
  221. func testDatabaseEntity(t *testing.T, writeDBExecutor database.Executor, readDBExecutor database.Executor) {
  222. tableName := "test.students"
  223. id1 := strutils.SimpleUUID()
  224. name1 := "1" + strutils.SimpleUUID()
  225. age1 := rand.Int31()
  226. enterTime1 := time.Now().Local()
  227. id2 := strutils.SimpleUUID()
  228. name2 := "2" + strutils.SimpleUUID()
  229. age2 := rand.Int31()
  230. enterTime2 := time.Now().Local()
  231. student1 := &Student{
  232. ID: id1,
  233. Name: name1,
  234. Age: age1,
  235. EnterTime: enterTime1,
  236. }
  237. newStudent1 := &Student{
  238. ID: id1,
  239. Name: name2,
  240. Age: age2,
  241. EnterTime: enterTime2,
  242. }
  243. student2 := &Student{
  244. ID: id2,
  245. Name: name2,
  246. Age: age2,
  247. EnterTime: enterTime2,
  248. }
  249. err := database.InsertEntity(writeDBExecutor, tableName, student1)
  250. if err != nil {
  251. t.Fatalf("%+v\n", err)
  252. }
  253. result, err := database.QueryOne(readDBExecutor, &sql.QueryOneExecuteParams{
  254. TableName: tableName,
  255. Conditions: sql.NewConditions().
  256. Equal("id", id1),
  257. })
  258. if err != nil {
  259. t.Fatalf("%+v\n", err)
  260. }
  261. if result.ColumnValueString("id") != id1 {
  262. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  263. id1, result.ColumnValueString("id")))
  264. }
  265. if result.ColumnValueString("name") != name1 {
  266. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  267. name1, result.ColumnValueString("name")))
  268. }
  269. if result.ColumnValueInt32("age") != age1 {
  270. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  271. age1, result.ColumnValueInt32("age")))
  272. }
  273. if result.ColumnValueTime("enter_time").Format(time.DateTime) != enterTime1.Format(time.DateTime) {
  274. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  275. enterTime1, result.ColumnValueTime("enter_time")))
  276. }
  277. err = database.UpdateEntity(writeDBExecutor, tableName, newStudent1)
  278. if err != nil {
  279. t.Fatalf("%+v\n", err)
  280. }
  281. result, err = database.QueryOne(readDBExecutor, &sql.QueryOneExecuteParams{
  282. TableName: tableName,
  283. Conditions: sql.NewConditions().
  284. Equal("id", id1),
  285. })
  286. if err != nil {
  287. t.Fatalf("%+v\n", err)
  288. }
  289. if result.ColumnValueString("id") != id1 {
  290. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  291. id1, result.ColumnValueString("id")))
  292. }
  293. if result.ColumnValueString("name") != name2 {
  294. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  295. name2, result.ColumnValueString("name")))
  296. }
  297. if result.ColumnValueInt32("age") != age2 {
  298. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  299. age2, result.ColumnValueInt32("age")))
  300. }
  301. if result.ColumnValueTime("enter_time").Format(time.DateTime) != enterTime2.Format(time.DateTime) {
  302. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  303. enterTime2, result.ColumnValueTime("enter_time")))
  304. }
  305. err = database.DeleteEntity(writeDBExecutor, tableName, newStudent1)
  306. if err != nil {
  307. t.Fatalf("%+v\n", err)
  308. }
  309. exist, err := database.CheckExist(readDBExecutor, &sql.CheckExistExecuteParams{
  310. TableName: tableName,
  311. Conditions: sql.NewConditions().
  312. Equal("id", id1),
  313. })
  314. if err != nil {
  315. t.Fatalf("%+v\n", err)
  316. }
  317. if exist {
  318. t.Fatalf("%+v\n", errors.New("Delete Error"))
  319. }
  320. err = database.InsertEntity(writeDBExecutor, tableName, []any{student1, student2})
  321. if err != nil {
  322. t.Fatalf("%+v\n", err)
  323. }
  324. results, totalCount, err := database.Query(readDBExecutor, &sql.QueryExecuteParams{
  325. TableName: tableName,
  326. Conditions: sql.NewConditions().In("id", []string{id1, id2}),
  327. OrderBy: "name ASC",
  328. })
  329. if err != nil {
  330. t.Fatalf("%+v\n", err)
  331. }
  332. if len(results) != 2 || totalCount != 2 {
  333. t.Fatalf("%+v\n", errors.Errorf("Insert Batch Error: %v", results))
  334. }
  335. if results[0].ColumnValueString("id") != id1 {
  336. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  337. id1, result.ColumnValueString("id")))
  338. }
  339. if results[0].ColumnValueString("name") != name1 {
  340. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  341. name1, result.ColumnValueString("name")))
  342. }
  343. if results[0].ColumnValueInt32("age") != age1 {
  344. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  345. age1, result.ColumnValueInt32("age")))
  346. }
  347. if results[0].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime1.Format(time.DateTime) {
  348. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  349. enterTime1, result.ColumnValueTime("enter_time")))
  350. }
  351. if results[1].ColumnValueString("id") != id2 {
  352. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  353. id2, result.ColumnValueString("id")))
  354. }
  355. if results[1].ColumnValueString("name") != name2 {
  356. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  357. name2, result.ColumnValueString("name")))
  358. }
  359. if results[1].ColumnValueInt32("age") != age2 {
  360. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  361. age2, result.ColumnValueInt32("age")))
  362. }
  363. if results[1].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime2.Format(time.DateTime) {
  364. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  365. enterTime2, result.ColumnValueTime("enter_time")))
  366. }
  367. hasOnlyOne, err := database.CheckHasOnlyOne(readDBExecutor, &sql.CheckHasOnlyOneExecuteParams{
  368. TableName: tableName,
  369. Conditions: sql.NewConditions().
  370. Equal("id", id1),
  371. })
  372. if err != nil {
  373. t.Fatalf("%+v\n", err)
  374. }
  375. if !hasOnlyOne {
  376. t.Fatalf("%+v\n", errors.New("Check HasOnlyOne Error"))
  377. }
  378. count, err := database.Count(readDBExecutor, &sql.CountExecuteParams{
  379. TableName: tableName,
  380. Conditions: sql.NewConditions().
  381. Equal("id", id2),
  382. })
  383. if err != nil {
  384. t.Fatalf("%+v\n", err)
  385. }
  386. if count != 1 {
  387. t.Fatalf("%+v\n", errors.New("Count Error"))
  388. }
  389. }
  390. func testDatabase(t *testing.T, writeDBExecutor database.Executor, readDBExecutor database.Executor) {
  391. tableName := "test.students"
  392. id1 := strutils.SimpleUUID()
  393. name1 := "1" + strutils.SimpleUUID()
  394. age1 := rand.Int31()
  395. enterTime1 := time.Now().Local()
  396. id2 := strutils.SimpleUUID()
  397. name2 := "2" + strutils.SimpleUUID()
  398. age2 := rand.Int31()
  399. enterTime2 := time.Now().Local()
  400. tableRow1 := sql.NewTableRow().
  401. Add("id", id1).
  402. Add("name", name1).
  403. Add("age", age1).
  404. Add("enter_time", enterTime1)
  405. newTableRow1 := sql.NewTableRow().
  406. Add("name", name2).
  407. Add("age", age2).
  408. Add("enter_time", enterTime2)
  409. tableRow2 := sql.NewTableRow().
  410. Add("id", id2).
  411. Add("name", name2).
  412. Add("age", age2).
  413. Add("enter_time", enterTime2)
  414. err := database.Insert(writeDBExecutor, &sql.InsertExecuteParams{
  415. TableName: tableName,
  416. TableRow: tableRow1,
  417. })
  418. if err != nil {
  419. t.Fatalf("%+v\n", err)
  420. }
  421. result, err := database.QueryOne(readDBExecutor, &sql.QueryOneExecuteParams{
  422. TableName: tableName,
  423. Conditions: sql.NewConditions().
  424. Equal("id", id1),
  425. })
  426. if err != nil {
  427. t.Fatalf("%+v\n", err)
  428. }
  429. if result.ColumnValueString("id") != id1 {
  430. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  431. id1, result.ColumnValueString("id")))
  432. }
  433. if result.ColumnValueString("name") != name1 {
  434. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  435. name1, result.ColumnValueString("name")))
  436. }
  437. if result.ColumnValueInt32("age") != age1 {
  438. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  439. age1, result.ColumnValueInt32("age")))
  440. }
  441. if result.ColumnValueTime("enter_time").Format(time.DateTime) != enterTime1.Format(time.DateTime) {
  442. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  443. enterTime1, result.ColumnValueTime("enter_time")))
  444. }
  445. err = database.Update(writeDBExecutor, &sql.UpdateExecuteParams{
  446. TableName: tableName,
  447. TableRow: newTableRow1,
  448. Conditions: sql.NewConditions().Equal("id", id1),
  449. })
  450. if err != nil {
  451. t.Fatalf("%+v\n", err)
  452. }
  453. result, err = database.QueryOne(readDBExecutor, &sql.QueryOneExecuteParams{
  454. TableName: tableName,
  455. Conditions: sql.NewConditions().
  456. Equal("id", id1),
  457. })
  458. if err != nil {
  459. t.Fatalf("%+v\n", err)
  460. }
  461. if result.ColumnValueString("id") != id1 {
  462. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  463. id1, result.ColumnValueString("id")))
  464. }
  465. if result.ColumnValueString("name") != name2 {
  466. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  467. name2, result.ColumnValueString("name")))
  468. }
  469. if result.ColumnValueInt32("age") != age2 {
  470. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  471. age2, result.ColumnValueInt32("age")))
  472. }
  473. if result.ColumnValueTime("enter_time").Format(time.DateTime) != enterTime2.Format(time.DateTime) {
  474. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  475. enterTime2, result.ColumnValueTime("enter_time")))
  476. }
  477. err = database.Delete(writeDBExecutor, &sql.DeleteExecuteParams{
  478. TableName: tableName,
  479. Conditions: sql.NewConditions().Equal("id", id1),
  480. })
  481. if err != nil {
  482. t.Fatalf("%+v\n", err)
  483. }
  484. exist, err := database.CheckExist(readDBExecutor, &sql.CheckExistExecuteParams{
  485. TableName: tableName,
  486. Conditions: sql.NewConditions().
  487. Equal("id", id1),
  488. })
  489. if err != nil {
  490. t.Fatalf("%+v\n", err)
  491. }
  492. if exist {
  493. t.Fatalf("%+v\n", errors.New("Delete Error"))
  494. }
  495. err = database.InsertBatch(writeDBExecutor, &sql.InsertBatchExecuteParams{
  496. TableName: tableName,
  497. TableRowBatch: []sql.TableRow{*tableRow1, *tableRow2},
  498. })
  499. if err != nil {
  500. t.Fatalf("%+v\n", err)
  501. }
  502. results, totalCount, err := database.Query(readDBExecutor, &sql.QueryExecuteParams{
  503. TableName: tableName,
  504. Conditions: sql.NewConditions().In("id", []string{id1, id2}),
  505. OrderBy: "name ASC",
  506. })
  507. if err != nil {
  508. t.Fatalf("%+v\n", err)
  509. }
  510. if len(results) != 2 || totalCount != 2 {
  511. t.Fatalf("%+v\n", errors.Errorf("Insert Batch Error: %v", results))
  512. }
  513. if results[0].ColumnValueString("id") != id1 {
  514. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  515. id1, result.ColumnValueString("id")))
  516. }
  517. if results[0].ColumnValueString("name") != name1 {
  518. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  519. name1, result.ColumnValueString("name")))
  520. }
  521. if results[0].ColumnValueInt32("age") != age1 {
  522. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  523. age1, result.ColumnValueInt32("age")))
  524. }
  525. if results[0].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime1.Format(time.DateTime) {
  526. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  527. enterTime1, result.ColumnValueTime("enter_time")))
  528. }
  529. if results[1].ColumnValueString("id") != id2 {
  530. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  531. id2, result.ColumnValueString("id")))
  532. }
  533. if results[1].ColumnValueString("name") != name2 {
  534. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  535. name2, result.ColumnValueString("name")))
  536. }
  537. if results[1].ColumnValueInt32("age") != age2 {
  538. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  539. age2, result.ColumnValueInt32("age")))
  540. }
  541. if results[1].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime2.Format(time.DateTime) {
  542. t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
  543. enterTime2, result.ColumnValueTime("enter_time")))
  544. }
  545. hasOnlyOne, err := database.CheckHasOnlyOne(readDBExecutor, &sql.CheckHasOnlyOneExecuteParams{
  546. TableName: tableName,
  547. Conditions: sql.NewConditions().
  548. Equal("id", id1),
  549. })
  550. if err != nil {
  551. t.Fatalf("%+v\n", err)
  552. }
  553. if !hasOnlyOne {
  554. t.Fatalf("%+v\n", errors.New("Check HasOnlyOne Error"))
  555. }
  556. count, err := database.Count(readDBExecutor, &sql.CountExecuteParams{
  557. TableName: tableName,
  558. Conditions: sql.NewConditions().
  559. Equal("id", id2),
  560. })
  561. if err != nil {
  562. t.Fatalf("%+v\n", err)
  563. }
  564. if count != 1 {
  565. t.Fatalf("%+v\n", errors.New("Count Error"))
  566. }
  567. }