gitee.com/eden-framework/sqlx@v0.0.3/mysqlconnector/mysql_connector_test.go (about)

     1  package mysqlconnector
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"gitee.com/eden-framework/sqlx/builder"
     9  	"gitee.com/eden-framework/sqlx/builder/buidertestingutils"
    10  	"github.com/onsi/gomega"
    11  )
    12  
    13  func TestMysqlConnector(t *testing.T) {
    14  	c := &MysqlConnector{}
    15  
    16  	table := builder.T("t",
    17  		builder.Col("F_id").Type(uint64(0), ",autoincrement"),
    18  		builder.Col("f_old_name").Type("", ",deprecated=f_name"),
    19  		builder.Col("F_name").Type("", ",size=128,default=''"),
    20  		builder.Col("F_geo").Type(&Point{}, ""),
    21  		builder.Col("F_created_at").Type(int64(0), ",default='0'"),
    22  		builder.Col("F_updated_at").Type(int64(0), ",default='0'"),
    23  		builder.PrimaryKey(builder.Cols("F_id")),
    24  		builder.UniqueIndex("I_name", builder.Cols("F_name")).Using("BTREE"),
    25  		builder.Index("I_created_at", builder.Cols("F_created_at")).Using("BTREE"),
    26  		builder.Index("I_geo", builder.Cols("F_geo")).Using("SPATIAL"),
    27  	)
    28  
    29  	t.Run("CreateDatabase", func(t *testing.T) {
    30  		gomega.NewWithT(t).Expect(c.CreateDatabase("db")).
    31  			To(buidertestingutils.BeExpr( /* language=MySQL */ `CREATE DATABASE db;`))
    32  	})
    33  	t.Run("DropDatabase", func(t *testing.T) {
    34  		gomega.NewWithT(t).Expect(c.DropDatabase("db")).
    35  			To(buidertestingutils.BeExpr( /* language=MySQL */ `DROP DATABASE db;`))
    36  	})
    37  	t.Run("AddIndex", func(t *testing.T) {
    38  		gomega.NewWithT(t).Expect(c.AddIndex(table.Key("I_name"))).
    39  			To(buidertestingutils.BeExpr( /* language=MySQL */ `CREATE UNIQUE INDEX i_name ON t (f_name) USING BTREE;`))
    40  	})
    41  	t.Run("AddPrimaryKey", func(t *testing.T) {
    42  		gomega.NewWithT(t).Expect(c.AddIndex(table.Key("PRIMARY"))).
    43  			To(buidertestingutils.BeExpr( /* language=MySQL */ "ALTER TABLE t ADD PRIMARY KEY (f_id);"))
    44  	})
    45  	t.Run("AddSpatialIndex", func(t *testing.T) {
    46  		gomega.NewWithT(t).Expect(
    47  			c.AddIndex(table.Key("I_geo")),
    48  		).To(buidertestingutils.BeExpr( /* language=MySQL */ "CREATE SPATIAL INDEX i_geo ON t (f_geo);"))
    49  	})
    50  	t.Run("DropIndex", func(t *testing.T) {
    51  		gomega.NewWithT(t).Expect(
    52  			c.DropIndex(table.Key("I_name")),
    53  		).To(buidertestingutils.BeExpr( /* language=MySQL */ "DROP INDEX i_name ON t;"))
    54  	})
    55  	t.Run("DropPrimaryKey", func(t *testing.T) {
    56  		gomega.NewWithT(t).Expect(
    57  			c.DropIndex(table.Key("PRIMARY")),
    58  		).To(buidertestingutils.BeExpr( /* language=MySQL */ "ALTER TABLE t DROP PRIMARY KEY;"))
    59  	})
    60  	t.Run("CreateTableIsNotExists", func(t *testing.T) {
    61  		gomega.NewWithT(t).Expect(
    62  			c.CreateTableIsNotExists(table)[0],
    63  		).To(buidertestingutils.BeExpr( /* language=MySQL */
    64  			`CREATE TABLE IF NOT EXISTS t (
    65  	f_id bigint unsigned NOT NULL AUTO_INCREMENT,
    66  	f_name varchar(128) NOT NULL DEFAULT '',
    67  	f_geo POINT NOT NULL,
    68  	f_created_at bigint NOT NULL DEFAULT '0',
    69  	f_updated_at bigint NOT NULL DEFAULT '0',
    70  	PRIMARY KEY (f_id)
    71  ) ENGINE=InnoDB CHARSET=utf8mb4;`))
    72  	})
    73  	t.Run("DropTable", func(t *testing.T) {
    74  		gomega.NewWithT(t).Expect(
    75  			c.DropTable(table)).
    76  			To(buidertestingutils.BeExpr( /* language=MySQL */ "DROP TABLE IF EXISTS t;"))
    77  	})
    78  	t.Run("TruncateTable", func(t *testing.T) {
    79  		gomega.NewWithT(t).Expect(
    80  			c.TruncateTable(table),
    81  		).
    82  			To(buidertestingutils.BeExpr( /* language=MySQL */ "TRUNCATE TABLE t;"))
    83  	})
    84  	t.Run("AddColumn", func(t *testing.T) {
    85  		gomega.NewWithT(t).Expect(
    86  			c.AddColumn(table.Col("F_name"))).
    87  			To(buidertestingutils.BeExpr( /* language=MySQL */ "ALTER TABLE t ADD COLUMN f_name varchar(128) NOT NULL DEFAULT '';"))
    88  	})
    89  	t.Run("ModifyColumn", func(t *testing.T) {
    90  		gomega.NewWithT(t).Expect(
    91  			c.ModifyColumn(table.Col("F_name")),
    92  		).To(buidertestingutils.BeExpr( /* language=MySQL */ "ALTER TABLE t MODIFY COLUMN f_name varchar(128) NOT NULL DEFAULT '';"))
    93  	})
    94  	t.Run("DropColumn", func(t *testing.T) {
    95  		gomega.NewWithT(t).Expect(
    96  			c.DropColumn(table.Col("F_name")),
    97  		).To(buidertestingutils.BeExpr( /* language=MySQL */ "ALTER TABLE t DROP COLUMN f_name;"))
    98  	})
    99  }
   100  
   101  type Point struct {
   102  	X float64
   103  	Y float64
   104  }
   105  
   106  func (Point) DataType(engine string) string {
   107  	return "POINT"
   108  }
   109  
   110  func (Point) ValueEx() string {
   111  	return `ST_GeomFromText(?)`
   112  }
   113  
   114  func (p Point) Value() (driver.Value, error) {
   115  	return fmt.Sprintf("POINT(%v %v)", p.X, p.Y), nil
   116  }