123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669 |
- package test
- import (
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/data_service"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/operations"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
- "git.sxidc.com/go-tools/utils/strutils"
- "github.com/pkg/errors"
- "math/rand"
- "testing"
- "time"
- )
- func TestOperations(t *testing.T) {
- dbOperations, err := operations.NewOperations(&operations.Config{
- UserName: "test",
- Password: "123456",
- Address: "localhost",
- Port: "30432",
- Database: "test",
- MaxConnections: 40,
- MaxIdleConnections: 10,
- LogLevel: "error",
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- testDBExecutor(t, dbOperations)
- }
- func TestDataService(t *testing.T) {
- dataService, err := data_service.NewExecutor(&data_service.Config{
- Token: "8qe+uPgpQ2JWxu3lSyOx5EWC24/c+zJOxvFhvRLRTzU=",
- Address: "localhost",
- HttpPort: "10000",
- GrpcPort: "10001",
- Namespace: "baize",
- DataSource: "baize",
- HttpTimeoutSec: 10,
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- testDBExecutor(t, dataService)
- }
- func TestDatabase(t *testing.T) {
- dbOperations, err := operations.NewOperations(&operations.Config{
- UserName: "test",
- Password: "123456",
- Address: "localhost",
- Port: "30432",
- Database: "test",
- MaxConnections: 40,
- MaxIdleConnections: 10,
- LogLevel: "error",
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- dataService, err := data_service.NewExecutor(&data_service.Config{
- Token: "8qe+uPgpQ2JWxu3lSyOx5EWC24/c+zJOxvFhvRLRTzU=",
- Address: "localhost",
- HttpPort: "10000",
- GrpcPort: "10001",
- Namespace: "baize",
- DataSource: "baize",
- HttpTimeoutSec: 10,
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- testDatabaseEntity(t, dbOperations, dbOperations)
- testDatabase(t, dbOperations, dbOperations)
- testDatabaseEntity(t, dataService, dataService)
- testDatabase(t, dataService, dataService)
- err = database.Transaction(dbOperations, func(tx database.Executor) error {
- testDatabaseEntity(t, dbOperations, tx)
- testDatabase(t, dbOperations, tx)
- return nil
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- err = database.Transaction(dbOperations, func(tx database.Executor) error {
- testDatabaseEntity(t, dataService, tx)
- testDatabase(t, dataService, tx)
- return nil
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- }
- func testDBExecutor(t *testing.T, dbExecutor database.Executor) {
- id := strutils.SimpleUUID()
- name := strutils.SimpleUUID()
- age := rand.Int31()
- enterTime := time.Now().Local()
- _, err := dbExecutor.ExecuteRawSql(`
- INSERT INTO
- test.students (id, name, age, enter_time)
- VALUES
- (?, ?, ?, ?)
- `, id, name, age, enterTime)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- results, err := dbExecutor.ExecuteRawSql(`
- SELECT
- *
- FROM
- test.students
- WHERE
- id = ?
- `, id)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if results[0].ColumnValueString("id") != id {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id, results[0].ColumnValueString("id")))
- }
- if results[0].ColumnValueString("name") != name {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name, results[0].ColumnValueString("name")))
- }
- if results[0].ColumnValueInt32("age") != age {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age, results[0].ColumnValueInt32("age")))
- }
- if results[0].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime, results[0].ColumnValueTime("enter_time")))
- }
- _, err = dbExecutor.ExecuteRawSql(`
- DELETE FROM
- test.students
- WHERE
- id = ?
- `, id)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- results, err = dbExecutor.ExecuteRawSql(`
- SELECT
- *
- FROM
- test.students
- WHERE
- id = ?
- `, id)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if len(results) != 0 {
- t.Fatalf("Delete Error: %+v\n", results)
- }
- _, err = dbExecutor.ExecuteRawSqlTemplate(`
- INSERT INTO
- {{ .table_name }} (id, name, age, enter_time)
- VALUES
- (?, ?, ?, ?)
- `, map[string]any{"table_name": "test.students"}, id, name, age, enterTime)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- results, err = dbExecutor.ExecuteRawSqlTemplate(`
- SELECT
- *
- FROM
- {{ .table_name }}
- WHERE
- id = ?
- `, map[string]any{"table_name": "test.students"}, id)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if results[0].ColumnValueString("id") != id {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id, results[0].ColumnValueString("id")))
- }
- if results[0].ColumnValueString("name") != name {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name, results[0].ColumnValueString("name")))
- }
- if results[0].ColumnValueInt32("age") != age {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age, results[0].ColumnValueInt32("age")))
- }
- if results[0].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime, results[0].ColumnValueTime("enter_time")))
- }
- _, err = dbExecutor.ExecuteRawSqlTemplate(`
- DELETE FROM
- {{ .table_name }}
- WHERE
- id = ?
- `, map[string]any{"table_name": "test.students"}, id)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- results, err = dbExecutor.ExecuteRawSqlTemplate(`
- SELECT
- *
- FROM
- {{ .table_name }}
- WHERE
- id = ?
- `, map[string]any{"table_name": "test.students"}, id)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if len(results) != 0 {
- t.Fatalf("Delete Error: %+v\n", results)
- }
- }
- type Student struct {
- ID string `sqlmapping:"column:id" sqlresult:"column:id"`
- Name string `sqlmapping:"column:name" sqlresult:"column:name"`
- Age int32 `sqlmapping:"column:age" sqlresult:"column:age"`
- EnterTime time.Time `sqlmapping:"column:enter_time" sqlresult:"column:enter_time"`
- }
- func testDatabaseEntity(t *testing.T, writeDBExecutor database.Executor, readDBExecutor database.Executor) {
- tableName := "test.students"
- id1 := strutils.SimpleUUID()
- name1 := "1" + strutils.SimpleUUID()
- age1 := rand.Int31()
- enterTime1 := time.Now().Local()
- id2 := strutils.SimpleUUID()
- name2 := "2" + strutils.SimpleUUID()
- age2 := rand.Int31()
- enterTime2 := time.Now().Local()
- student1 := &Student{
- ID: id1,
- Name: name1,
- Age: age1,
- EnterTime: enterTime1,
- }
- newStudent1 := &Student{
- ID: id1,
- Name: name2,
- Age: age2,
- EnterTime: enterTime2,
- }
- student2 := &Student{
- ID: id2,
- Name: name2,
- Age: age2,
- EnterTime: enterTime2,
- }
- err := database.InsertEntity(writeDBExecutor, tableName, student1)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- result, err := database.QueryOne(readDBExecutor, &sql.QueryOneExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if result.ColumnValueString("id") != id1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id1, result.ColumnValueString("id")))
- }
- if result.ColumnValueString("name") != name1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name1, result.ColumnValueString("name")))
- }
- if result.ColumnValueInt32("age") != age1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age1, result.ColumnValueInt32("age")))
- }
- if result.ColumnValueTime("enter_time").Format(time.DateTime) != enterTime1.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime1, result.ColumnValueTime("enter_time")))
- }
- err = database.UpdateEntity(writeDBExecutor, tableName, newStudent1)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- result, err = database.QueryOne(readDBExecutor, &sql.QueryOneExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if result.ColumnValueString("id") != id1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id1, result.ColumnValueString("id")))
- }
- if result.ColumnValueString("name") != name2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name2, result.ColumnValueString("name")))
- }
- if result.ColumnValueInt32("age") != age2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age2, result.ColumnValueInt32("age")))
- }
- if result.ColumnValueTime("enter_time").Format(time.DateTime) != enterTime2.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime2, result.ColumnValueTime("enter_time")))
- }
- err = database.DeleteEntity(writeDBExecutor, tableName, newStudent1)
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- exist, err := database.CheckExist(readDBExecutor, &sql.CheckExistExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if exist {
- t.Fatalf("%+v\n", errors.New("Delete Error"))
- }
- err = database.InsertEntity(writeDBExecutor, tableName, []any{student1, student2})
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- results, totalCount, err := database.Query(readDBExecutor, &sql.QueryExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().In("id", []string{id1, id2}),
- OrderBy: "name ASC",
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if len(results) != 2 || totalCount != 2 {
- t.Fatalf("%+v\n", errors.Errorf("Insert Batch Error: %v", results))
- }
- if results[0].ColumnValueString("id") != id1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id1, result.ColumnValueString("id")))
- }
- if results[0].ColumnValueString("name") != name1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name1, result.ColumnValueString("name")))
- }
- if results[0].ColumnValueInt32("age") != age1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age1, result.ColumnValueInt32("age")))
- }
- if results[0].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime1.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime1, result.ColumnValueTime("enter_time")))
- }
- if results[1].ColumnValueString("id") != id2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id2, result.ColumnValueString("id")))
- }
- if results[1].ColumnValueString("name") != name2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name2, result.ColumnValueString("name")))
- }
- if results[1].ColumnValueInt32("age") != age2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age2, result.ColumnValueInt32("age")))
- }
- if results[1].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime2.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime2, result.ColumnValueTime("enter_time")))
- }
- hasOnlyOne, err := database.CheckHasOnlyOne(readDBExecutor, &sql.CheckHasOnlyOneExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if !hasOnlyOne {
- t.Fatalf("%+v\n", errors.New("Check HasOnlyOne Error"))
- }
- count, err := database.Count(readDBExecutor, &sql.CountExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id2),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if count != 1 {
- t.Fatalf("%+v\n", errors.New("Count Error"))
- }
- }
- func testDatabase(t *testing.T, writeDBExecutor database.Executor, readDBExecutor database.Executor) {
- tableName := "test.students"
- id1 := strutils.SimpleUUID()
- name1 := "1" + strutils.SimpleUUID()
- age1 := rand.Int31()
- enterTime1 := time.Now().Local()
- id2 := strutils.SimpleUUID()
- name2 := "2" + strutils.SimpleUUID()
- age2 := rand.Int31()
- enterTime2 := time.Now().Local()
- tableRow1 := sql.NewTableRow().
- Add("id", id1).
- Add("name", name1).
- Add("age", age1).
- Add("enter_time", enterTime1)
- newTableRow1 := sql.NewTableRow().
- Add("name", name2).
- Add("age", age2).
- Add("enter_time", enterTime2)
- tableRow2 := sql.NewTableRow().
- Add("id", id2).
- Add("name", name2).
- Add("age", age2).
- Add("enter_time", enterTime2)
- err := database.Insert(writeDBExecutor, &sql.InsertExecuteParams{
- TableName: tableName,
- TableRow: tableRow1,
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- result, err := database.QueryOne(readDBExecutor, &sql.QueryOneExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if result.ColumnValueString("id") != id1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id1, result.ColumnValueString("id")))
- }
- if result.ColumnValueString("name") != name1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name1, result.ColumnValueString("name")))
- }
- if result.ColumnValueInt32("age") != age1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age1, result.ColumnValueInt32("age")))
- }
- if result.ColumnValueTime("enter_time").Format(time.DateTime) != enterTime1.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime1, result.ColumnValueTime("enter_time")))
- }
- err = database.Update(writeDBExecutor, &sql.UpdateExecuteParams{
- TableName: tableName,
- TableRow: newTableRow1,
- Conditions: sql.NewConditions().Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- result, err = database.QueryOne(readDBExecutor, &sql.QueryOneExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if result.ColumnValueString("id") != id1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id1, result.ColumnValueString("id")))
- }
- if result.ColumnValueString("name") != name2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name2, result.ColumnValueString("name")))
- }
- if result.ColumnValueInt32("age") != age2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age2, result.ColumnValueInt32("age")))
- }
- if result.ColumnValueTime("enter_time").Format(time.DateTime) != enterTime2.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime2, result.ColumnValueTime("enter_time")))
- }
- err = database.Delete(writeDBExecutor, &sql.DeleteExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- exist, err := database.CheckExist(readDBExecutor, &sql.CheckExistExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if exist {
- t.Fatalf("%+v\n", errors.New("Delete Error"))
- }
- err = database.InsertBatch(writeDBExecutor, &sql.InsertBatchExecuteParams{
- TableName: tableName,
- TableRowBatch: []sql.TableRow{*tableRow1, *tableRow2},
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- results, totalCount, err := database.Query(readDBExecutor, &sql.QueryExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().In("id", []string{id1, id2}),
- OrderBy: "name ASC",
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if len(results) != 2 || totalCount != 2 {
- t.Fatalf("%+v\n", errors.Errorf("Insert Batch Error: %v", results))
- }
- if results[0].ColumnValueString("id") != id1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id1, result.ColumnValueString("id")))
- }
- if results[0].ColumnValueString("name") != name1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name1, result.ColumnValueString("name")))
- }
- if results[0].ColumnValueInt32("age") != age1 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age1, result.ColumnValueInt32("age")))
- }
- if results[0].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime1.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime1, result.ColumnValueTime("enter_time")))
- }
- if results[1].ColumnValueString("id") != id2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- id2, result.ColumnValueString("id")))
- }
- if results[1].ColumnValueString("name") != name2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- name2, result.ColumnValueString("name")))
- }
- if results[1].ColumnValueInt32("age") != age2 {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- age2, result.ColumnValueInt32("age")))
- }
- if results[1].ColumnValueTime("enter_time").Format(time.DateTime) != enterTime2.Format(time.DateTime) {
- t.Fatalf("%+v\n", errors.Errorf("Result Error: except: %v, actual: %v",
- enterTime2, result.ColumnValueTime("enter_time")))
- }
- hasOnlyOne, err := database.CheckHasOnlyOne(readDBExecutor, &sql.CheckHasOnlyOneExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id1),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if !hasOnlyOne {
- t.Fatalf("%+v\n", errors.New("Check HasOnlyOne Error"))
- }
- count, err := database.Count(readDBExecutor, &sql.CountExecuteParams{
- TableName: tableName,
- Conditions: sql.NewConditions().
- Equal("id", id2),
- })
- if err != nil {
- t.Fatalf("%+v\n", err)
- }
- if count != 1 {
- t.Fatalf("%+v\n", errors.New("Count Error"))
- }
- }
|