gitee.com/eden-framework/sqlx@v0.0.3/generator/__examples__/database/user_test.go (about)

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