github.com/kunlun-qilian/sqlx/v3@v3.0.0/generator/__examples__/database/user_test.go (about)

     1  package database
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"testing"
     6  
     7  	_ "github.com/go-sql-driver/mysql"
     8  	"github.com/google/uuid"
     9  	"github.com/kunlun-qilian/sqlx/v3/builder"
    10  	"github.com/kunlun-qilian/sqlx/v3/migration"
    11  	"github.com/kunlun-qilian/sqlx/v3/mysqlconnector"
    12  	"github.com/kunlun-qilian/sqlx/v3/postgresqlconnector"
    13  	"github.com/onsi/gomega"
    14  )
    15  
    16  var (
    17  	mysqlConnector = &mysqlconnector.MysqlConnector{
    18  		Host:  "root@tcp(0.0.0.0:3306)",
    19  		Extra: "charset=utf8mb4&parseTime=true&interpolateParams=true&autocommit=true&loc=Local",
    20  	}
    21  
    22  	postgresConnector = &postgresqlconnector.PostgreSQLConnector{
    23  		Host:       "postgres://postgres@0.0.0.0:5432",
    24  		Extra:      "sslmode=disable",
    25  		Extensions: []string{"postgis"},
    26  	}
    27  )
    28  
    29  func TestUser(t *testing.T) {
    30  	t.Run("CRUD", func(t *testing.T) {
    31  		for _, connector := range []driver.Connector{
    32  			mysqlConnector,
    33  			postgresConnector,
    34  		} {
    35  			db := DBTest.OpenDB(connector)
    36  
    37  			db.D().Tables.Range(func(table *builder.Table, idx int) {
    38  				_, err := db.ExecExpr(db.Dialect().DropTable(table))
    39  				gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    40  			})
    41  
    42  			err := migration.Migrate(db, nil)
    43  			gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    44  
    45  			t.Run("Create flow", func(t *testing.T) {
    46  				user := User{}
    47  				user.Name = uuid.New().String()
    48  				user.Geom = GeomString{
    49  					V: "Point(0 0)",
    50  				}
    51  
    52  				errForCreate := user.Create(db)
    53  				gomega.NewWithT(t).Expect(errForCreate).To(gomega.BeNil())
    54  				gomega.NewWithT(t).Expect(user.ID, uint64(0))
    55  
    56  				user.Gender = GenderMale
    57  				{
    58  					err := user.CreateOnDuplicateWithUpdateFields(db, []string{"Gender"})
    59  					gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    60  				}
    61  				{
    62  					userForFetch := User{
    63  						Name: user.Name,
    64  					}
    65  					err := userForFetch.FetchByName(db)
    66  
    67  					gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    68  					gomega.NewWithT(t).Expect(userForFetch.Gender).To(gomega.Equal(user.Gender))
    69  				}
    70  			})
    71  			t.Run("delete flow", func(t *testing.T) {
    72  				user := User{}
    73  				user.Name = uuid.New().String()
    74  				user.Geom = GeomString{
    75  					V: "Point(0 0)",
    76  				}
    77  
    78  				errForCreate := user.Create(db)
    79  				gomega.NewWithT(t).Expect(errForCreate).To(gomega.BeNil())
    80  
    81  				{
    82  					userForDelete := User{
    83  						Name: user.Name,
    84  					}
    85  					err := userForDelete.SoftDeleteByName(db)
    86  					gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    87  
    88  					userForSelect := &User{
    89  						Name: user.Name,
    90  					}
    91  					errForSelect := userForSelect.FetchByName(db)
    92  					gomega.NewWithT(t).Expect(errForSelect).NotTo(gomega.BeNil())
    93  				}
    94  			})
    95  			db.D().Tables.Range(func(table *builder.Table, idx int) {
    96  				_, err := db.ExecExpr(db.Dialect().DropTable(table))
    97  				gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    98  			})
    99  		}
   100  	})
   101  	t.Run("List", func(t *testing.T) {
   102  		for _, connector := range []driver.Connector{
   103  			mysqlConnector,
   104  			postgresConnector,
   105  		} {
   106  			db := DBTest.OpenDB(connector)
   107  
   108  			db.D().Tables.Range(func(table *builder.Table, idx int) {
   109  				_, err := db.ExecExpr(db.Dialect().DropTable(table))
   110  				gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
   111  			})
   112  
   113  			err := migration.Migrate(db, nil)
   114  			gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
   115  
   116  			createUser := func() {
   117  				user := User{}
   118  				user.Name = uuid.New().String()
   119  				user.Geom = GeomString{
   120  					V: "Point(0 0)",
   121  				}
   122  
   123  				err := user.Create(db)
   124  				gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
   125  			}
   126  
   127  			for i := 0; i < 10; i++ {
   128  				createUser()
   129  			}
   130  
   131  			list, err := (&User{}).List(db, nil)
   132  			gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
   133  			gomega.NewWithT(t).Expect(list).To(gomega.HaveLen(10))
   134  
   135  			count, err := (&User{}).Count(db, nil)
   136  			gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
   137  			gomega.NewWithT(t).Expect(count).To(gomega.Equal(10))
   138  
   139  			names := make([]string, 0)
   140  			for _, user := range list {
   141  				names = append(names, user.Name)
   142  			}
   143  
   144  			{
   145  				list, err := (&User{}).BatchFetchByNameList(db, names)
   146  				gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
   147  				gomega.NewWithT(t).Expect(list).To(gomega.HaveLen(10))
   148  			}
   149  
   150  			db.D().Tables.Range(func(table *builder.Table, idx int) {
   151  				_, err := db.ExecExpr(db.Dialect().DropTable(table))
   152  				gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
   153  			})
   154  		}
   155  	})
   156  }