github.com/wfusion/gofusion@v1.1.14/test/db/cases/model_test.go (about) 1 package cases 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/suite" 9 10 "github.com/wfusion/gofusion/db" 11 "github.com/wfusion/gofusion/log" 12 13 testDB "github.com/wfusion/gofusion/test/db" 14 ) 15 16 func TestModel(t *testing.T) { 17 testingSuite := &Model{Test: new(testDB.Test)} 18 testingSuite.Init(testingSuite) 19 suite.Run(t, testingSuite) 20 } 21 22 type Model struct { 23 *testDB.Test 24 } 25 26 func (t *Model) BeforeTest(suiteName, testName string) { 27 t.Catch(func() { 28 log.Info(context.Background(), "right before %s %s", suiteName, testName) 29 }) 30 } 31 32 func (t *Model) AfterTest(suiteName, testName string) { 33 t.Catch(func() { 34 log.Info(context.Background(), "right after %s %s", suiteName, testName) 35 }) 36 } 37 38 func (t *Model) TestMysql() { 39 t.testDefault(nameMysqlRead, nameMysqlWrite) 40 } 41 42 func (t *Model) TestPostgres() { 43 t.testDefault(namePostgres, namePostgres) 44 } 45 46 func (t *Model) TestOpengauss() { 47 t.testDefault(nameOpenGauss, nameOpenGauss) 48 } 49 50 func (t *Model) TestSqlserver() { 51 t.testDefault(nameSqlserver, nameSqlserver) 52 } 53 54 func (t *Model) testDefault(read, write string) { 55 t.Run("DataModel", func() { t.testDataModel(read, write) }) 56 t.Run("Joins", func() { t.testJoins(read, write) }) 57 t.Run("SoftDelete", func() { t.testSoftDelete(read, write) }) 58 t.Run("BusinessSoftDelete", func() { t.testBusinessSoftDelete(read, write) }) 59 t.Run("SoftDeleteUnscoped", func() { t.testSoftDeleteUnscoped(read, write) }) 60 } 61 62 func (t *Model) testDataModel(read, write string) { 63 t.Catch(func() { 64 ctx := context.Background() 65 orm := db.Use(ctx, write, db.AppName(t.AppName())).WithContext(ctx) 66 67 t.NoError(orm.Migrator().AutoMigrate(new(modelWithData))) 68 t.NoError(orm.Migrator().AutoMigrate(new(modelWithBusiness))) 69 t.NoError(orm.Migrator().AutoMigrate(new(modelWithBusinessAndUser))) 70 defer func() { 71 t.NoError(orm.Migrator().DropTable(new(modelWithData))) 72 t.NoError(orm.Migrator().DropTable(new(modelWithBusiness))) 73 t.NoError(orm.Migrator().DropTable(new(modelWithBusinessAndUser))) 74 }() 75 76 mwd1 := &modelWithData{Name: "az1"} 77 t.NoError(orm.Create(mwd1).Error) 78 t.NoError(orm.Where("name = 'az1'").Delete(mwd1).Error) 79 80 mwd2 := &modelWithData{Name: "az2"} 81 t.NoError(orm.Create(mwd2).Error) 82 t.NoError(orm.Where("name = 'az2'").Delete(mwd2).Error) 83 84 //ret := orm.Exec("insert into model_with_data(create_time, modify_time, name) values(1685702487694,1685702487694,'az3')") 85 //t.NoError(ret.Error) 86 //id, _ := ret.Statement.Schema.PrioritizedPrimaryField.ValueOf(ret.Statement.Context, ret.Statement.ReflectValue) 87 //t.NoError(orm.Delete(mwd2, "name = 'az3' AND id = ?", id).Error) 88 89 mwb := &modelWithBusiness{Name: "test"} 90 t.NoError(orm.Create(mwb).Error) 91 t.NoError(orm.Delete(mwb).Error) 92 93 mwbu := &modelWithBusinessAndUser{Name: "test"} 94 t.NoError(orm.Create(mwbu).Error) 95 t.NoError(orm.Delete(mwbu).Error) 96 }) 97 } 98 99 func (t *Model) testJoins(read, write string) { 100 t.Catch(func() { 101 ctx := context.Background() 102 orm := db.Use(ctx, write, db.AppName(t.AppName())).WithContext(ctx) 103 104 t.NoError(orm.Migrator().AutoMigrate(new(modelWithData))) 105 t.NoError(orm.Migrator().AutoMigrate(new(modelWithDataExtend))) 106 defer func() { 107 t.NoError(orm.Migrator().DropTable(new(modelWithData))) 108 t.NoError(orm.Migrator().DropTable(new(modelWithDataExtend))) 109 }() 110 mwd := &modelWithData{Name: "az1"} 111 t.NoError(orm.Create(mwd).Error) 112 mwdTableName := mwd.TableName() 113 114 mwdt := &modelWithDataExtend{OtherName: "test", ModelID: mwd.ID} 115 t.NoError(orm.Create(mwdt).Error) 116 mwdtTableName := mwdt.TableName() 117 118 var mwdList []*modelWithData 119 joins := modelWithDataDAL(read, write, t.AppName()). 120 ReadDB(ctx). 121 Joins(fmt.Sprintf("left join %s on %s.model_id = %s.id", 122 mwdtTableName, mwdtTableName, mwdTableName)) 123 t.NoError(joins.Find(&mwdList).Error) 124 t.NotEmpty(mwdList) 125 126 t.NoError(orm.Delete(mwd).Error) 127 t.NoError(orm.Delete(mwdt).Error) 128 }) 129 } 130 131 func (t *Model) testSoftDelete(read, write string) { 132 t.Catch(func() { 133 ctx := context.Background() 134 orm := db.Use(ctx, write, db.AppName(t.AppName())).WithContext(ctx) 135 t.NoError(orm.Migrator().AutoMigrate(new(modelWithSoftDeleted))) 136 defer func() { 137 t.NoError(orm.Migrator().DropTable(new(modelWithSoftDeleted))) 138 }() 139 mwb := &modelWithSoftDeleted{Name: "test"} 140 t.NoError(orm.Create(mwb).Error) 141 defer func() { 142 t.NoError(orm.Unscoped().Model(mwb).Delete(nil, "id = ?", mwb.ID).Error) 143 }() 144 145 var found *modelWithSoftDeleted 146 t.NoError(orm.First(&found, "id = ?", mwb.ID).Error) 147 t.Equal(mwb.Name, found.Name) 148 found = nil 149 150 mwb.Name = "test2" 151 t.NoError(orm.Updates(mwb).Error) 152 t.NoError(orm.First(&found, "id = ?", mwb.ID).Error) 153 t.Equal(mwb.Name, found.Name) 154 found = nil 155 156 t.NoError(orm.Delete(mwb).Error) 157 t.Error(orm.First(&found, "id = ?", mwb.ID).Error) 158 }) 159 } 160 161 func (t *Model) testBusinessSoftDelete(read, write string) { 162 t.Catch(func() { 163 ctx := context.Background() 164 orm := db.Use(ctx, write, db.AppName(t.AppName())).WithContext(ctx) 165 t.NoError(orm.Migrator().AutoMigrate(new(modelBizWithSoftDeleted))) 166 defer func() { 167 t.NoError(orm.Migrator().DropTable(new(modelBizWithSoftDeleted))) 168 }() 169 mwb := &modelBizWithSoftDeleted{Name: "test"} 170 t.NoError(orm.Create(mwb).Error) 171 defer func() { 172 t.NoError(orm.Unscoped().Model(mwb).Delete(nil, "id = ?", mwb.ID).Error) 173 }() 174 175 var found *modelBizWithSoftDeleted 176 t.NoError(orm.First(&found, "id = ?", mwb.ID).Error) 177 t.Equal(mwb.Name, found.Name) 178 found = nil 179 mwb.Name = "test2" 180 t.NoError(orm.Updates(mwb).Error) 181 t.NoError(orm.First(&found, "id = ?", mwb.ID).Error) 182 t.Equal(mwb.Name, found.Name) 183 found = nil 184 185 t.NoError(orm.Delete(mwb).Error) 186 t.Error(orm.First(&found, "id = ?", mwb.ID).Error) 187 }) 188 } 189 190 func (t *Model) testSoftDeleteUnscoped(read, write string) { 191 t.Catch(func() { 192 ctx := context.Background() 193 orm := db.Use(ctx, write, db.AppName(t.AppName())).WithContext(ctx) 194 t.NoError(orm.Migrator().AutoMigrate(new(modelWithSoftDeleted))) 195 defer func() { 196 t.NoError(orm.Migrator().DropTable(new(modelWithSoftDeleted))) 197 }() 198 dal := db.NewDAL[modelWithSoftDeleted, []*modelWithSoftDeleted](read, write, db.AppName(t.AppName())) 199 200 mwb := &modelWithSoftDeleted{Name: "test"} 201 t.NoError(dal.InsertOne(ctx, mwb)) 202 defer func() { 203 _, err := dal.Delete(ctx, "id = ?", mwb.ID, db.Unscoped()) 204 t.NoError(err) 205 }() 206 207 found, err := dal.QueryFirst(ctx, "id = ?", mwb.ID) 208 t.NoError(err) 209 t.Equal(mwb.Name, found.Name) 210 211 _, err = dal.Delete(ctx, "id = ?", mwb.ID) 212 t.NoError(err) 213 214 found, err = dal.QueryFirst(ctx, "id = ?", mwb.ID) 215 t.NoError(err) 216 t.Empty(found) 217 218 found, err = dal.QueryFirst(ctx, "id = ?", mwb.ID, db.Unscoped()) 219 t.NoError(err) 220 t.Equal(mwb.Name, found.Name) 221 }) 222 }