ã¯ããã«
ã¯ã©ã¦ããã°äºæ¥æ¨é²é¨ã§ããã¯ã¨ã³ãã¨ã³ã¸ãã¢ããã¦ãã馬æ¸ã§ãã
æ©ããã®ã§å ¥ç¤¾ãã¦ãã8ã¶æãçµã¡ã¾ããã æåã¯ãããã¯ãã®ããã¯ã¨ã³ãã®å®è£ ããå§ã¾ããã¤ã³ãã©å¨ããå°ãã ããæè¿ã§ã¯ããã³ãã¨ã³ãã®å®è£ ã«ãææ¦ãã¦ãã¾ãã
ä»åã¯ããããã¨çµé¨ããã¦ããã ããä¸ããããã¼ã¿ãã¼ã¹æ¥ç¶ãä¼´ãGoã®ã¦ããããã¹ãã§å®è¡æéã®ç縮ãããå 容ãæ稿ãã¾ãã
èæ¯
ã¯ã©ã¦ããã°ã§ã¯ããã¯ã¨ã³ãã®ã¦ããããã¹ãã®ãã¡ããã¼ã¿ãã¼ã¹æ¥ç¶ãä¼´ããã¹ãã«ã¤ãã¦ã¯ã¢ãã¯ã使ç¨ãã¦ãã¾ããã
ããã¯ãªãã¹ãæ¬çªã«è¿ãå½¢ã§ã¦ããããã¹ããè¡ããã¨ã§å質ãæ ä¿ãã¦ããããã§ãã
ãã®ããç¾ç¶ã®ãã¹ãã®æ§æã§ã¯ããäºãã®ãã¹ãã®ãã¼ã¿ãå¹²æ¸ããªãããã«ç´åã«å®è¡ããããã«ãªã£ã¦ãã¾ãã
課é¡
ãã®çµæãéçºé度ãä½ä¸ãããã¨ãã課é¡ãæ±ãããã¨ã¨ãªãã¾ããã
ããã¯ãã¹ã¦ã®ãã¹ãå®è¡ã®æéããããããã§ãã
èæ¯ã§ããä¼ãããéãããäºãã®ãã¹ããã¼ã¿ãå¹²æ¸ããªãããã«ããããããã¹ãã¯ç´åã§å®è¡ããå¿ è¦ãããã¾ãã
ãã¼ã«ã«ã§ã®éçºã«ããã¦ããã¾ãCIä¸ã§ã®å®è¡ã«ããã¦ãæéãããã£ã¦ãã¾ãã¨ãã課é¡ã¨ãªãã¾ããã
æ¤è¨
ãããã®èª²é¡ã解決ãããããããã¤ãã®æ¡ãå ã«æ¤è¨ãé²ãã¾ããã
- ã¤ã³ã¡ã¢ãªãã¼ã¿ãã¼ã¹ã®å©ç¨
- ãã¼ã¿ãã¼ã¹ãã¼ã«ãä½ãã空ãç¶æ³ã«å¿ãã¦å©ç¨
- ãã¹ããã¨ã«ãã©ã³ã¶ã¯ã·ã§ã³ãå©ç¨
- ãã¹ããã¨ã«ãã¼ã¿ãã¼ã¹ãçæã»å©ç¨
1. ã¤ã³ã¡ã¢ãªãã¼ã¿ãã¼ã¹ã®å©ç¨
ãã¹ãã«ããã¦æ°¸ç¶åã¯å¿ è¦ãªããããã¢ã¯ã»ã¹é度ãä¸ããç®çã§æ¤è¨ã«ä¸ããã¾ããã
MySQLäºæã謳ããã®ãåå¨ããORMã¨ãåä½ãããããæ¬çªã¨åçã®åä½ãæå¾ ã§ãã¾ãã
ä¸é¨ãMySQLã«éäºæãªé¨åãæ®ã£ã¦ããã®ã§ãæ¡ç¨ã«ã¯ååãªèª¿æ»ãå¿ è¦ã§ãã
2. ãã¼ã¿ãã¼ã¹ãã¼ã«
åä¸ã®ãã¼ã¿ãã¼ã¹ãåç §ãã¦ãããã¨ãåå ã§ç´åã«åä½ããããå¾ãªããããCPUã®ã³ã¢æ°ã«å¿ãã¦è¤æ°ã®ãã¼ã¿ãã¼ã¹ã¤ã³ã¹ã¿ã³ã¹ãç«ã¡ä¸ãããã¼ã¿ãã¼ã¹ãã¼ã«ãæ¤è¨ã«ä¸ããã¾ããã
ãã¹ãã¯å®è¡æã«ç©ºãã¦ãããã¼ã¿ãã¼ã¹ãæ¢ãã¦å®è¡ãã¾ãã
ãã¹ãå®äºå¾ã«ã¯ã¯ãªã¼ã³ã¢ãããè¡ããã¾ãã
ãã®ããããã¼ã¿ãã¼ã¹ãã¼ã«ã§ç¢ºä¿ãã¦ããåã¾ã§ã¯ä¸¦åã«å®è¡ãå¯è½ã¨ãªãã¾ãã
3. ãã©ã³ã¶ã¯ã·ã§ã³ã®å©ç¨
åä¸ã®ãã¼ã¿ãã¼ã¹ã«å¯¾ããæä»å¶å¾¡ãåé¡ãªã®ã§ããã°ããã¹ããã¨ã«ãã©ã³ã¶ã¯ã·ã§ã³ãã¯ãããã¹ãå®äºå¾ã«ãã¼ã«ããã¯ãããæ¹æ³ãæ¤è¨ã«ä¸ããã¾ããã
è¤æ°ã®ãã¹ããåæã«å®è¡ããã¨ãã¦ãããã©ã³ã¶ã¯ã·ã§ã³ã«ãããäºãã®ãã¼ã¿ãå¹²æ¸ãããã¨ããªããªãã¾ãã
ãã©ã³ã¶ã¯ã·ã§ã³ãã¯ããã¦ããéã¯ãå¥ã®ãã¹ãããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãããã¨ãã§ãããå¾ ã¡æéãçºçãã¦ãã¾ããã¨ã«ã¯æ³¨æãå¿ è¦ã§ãã
4. ãã¹ããã¨ã«ãã¼ã¿ãã¼ã¹ãçæ
ãã¡ããåããåä¸ã®ãã¼ã¿ãã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãåé¡ãªã®ã§ããã°ããã¹ããã¨ã«ãã¼ã¿ãã¼ã¹ãçæãããã¨ã§è§£æ±ºããã®ã§ã¯ãªãããã¨ãã観ç¹ããæ¤è¨ã«ä¸ããã¾ããã
ãã¹ãç¨ã®MySQLã³ã³ããã¨ãã¦ã¯1ã¤ã§ããããã®ä¸ã«ãã¼ã¿ãã¼ã¹ãä½æãããã¹ãå®äºå¾ã«Dropãã¾ãã
ãã¼ã¿ãã¼ã¹ã®ååããããã£ã³ã°ããåé¡ã«ã¤ãã¦ã¯ããã¹ããã¨ã«ããã·ã¥å¤ãçæãã¦ãã¼ã¿ãã¼ã¹åã«ã¤ãããã¨ã§è§£æ±ºãå³ãã¾ãã
ã¡ãªããã»ãã¡ãªãã
ç¶ãã¦ãããããã®æ¡ã«ã¤ãã¦ã¡ãªããã»ãã¡ãªãããã¾ã¨ãã¾ããã
æ¹é | é度 | å°å ¥ã³ã¹ã | ãã¹ã¦ã®åã«å¯¾å¿ | æ¢åæ¹ä¿®ã³ã¹ã |
---|---|---|---|---|
1. ã¤ã³ã¡ã¢ãªãã¼ã¿ãã¼ã¹ | â | ⯠| â | ⯠|
2. ãã¼ã¿ãã¼ã¹ãã¼ã« | â³ | â³ | ⯠| ⯠|
3. ãã©ã³ã¶ã¯ã·ã§ã³ | â³ | ⯠| ⯠| â³ |
4. ãã¹ããã¨ãã¼ã¿ãã¼ã¹çæ | ⯠| ⯠| ⯠| ⯠|
ããããå ã«ã
- è¤éãªæ©æ§ã®å°å ¥ã¯å±äººåãã¦ãã¾ãå¯è½æ§ããã
- åç´ãªæ©æ§ã§ããã°å¿ç¨ãå¹ã
ã¨ãã観ç¹ãããä»åã¯4.ãæ¡ç¨ãããã¨ã«ãã¾ããã
ã©ã解決ããã
ãã¹ãç¨ã®ãã¼ã¿ãã¼ã¹ã³ã³ããã¨ãã¦ã¯1ã¤ãå©ç¨ããã®ã§ããããã®ä¸ã§ãã¹ããã¨ã«ãã¼ã¿ãã¼ã¹ãä½æãããäºãã®ãã¹ããå¹²æ¸ããªãããã«ãã¾ããã
詳ããã¯æ¬¡ã®ã»ã¯ã·ã§ã³ã§èª¬æãã¾ãã
å ·ä½çãªã¢ããã¼ã
1. ãã¹ãç¨ã®ãã¼ã¿ãã¼ã¹ãçæããæ§é ä½ãä½æ
ãã¹ãç¨ã®ãã¼ã¿ãã¼ã¹å¨ãã管çãããdatabase_generatoræ§é ä½ã¨çæç¨ã®é¢æ°ãä½æãã¾ãã
ãã®æ§é ä½ã¯ã
- ãã¹ãç¨ã®ãã¼ã¿ãã¼ã¹ã®ä½æ
- åæãã¼ã¿ãã¼ã¹ç¨ã®Dumpãã¡ã¤ã«ä½æ
- ãã¼ã¿ãã¼ã¹åé¤
- ã³ãã¯ã·ã§ã³ã¯ãã¼ãº
ã®æ©è½ãæã£ã¦ãã¾ãã
type DatabaseGenerator interface { DumpDB() error GenerateDB() (gormDB *gorm.DB, error) DropDB() error CloseConnection() error } type databaseGenerator struct { cred Credential // ç§å¿æ å ±ãä¿æããstruct db *sql.DB dbName string } func New() (DatabaseGenerator, error) { cred := config.GetDBCredential() dsn := fmt.Sprintf( "%s:%s@tcp(%s:%d)", cred.User, cred.Password, cred.Host, cred.DBPort, ) sqlDB, _ := sql.Open("mysql", dsn) return &databaseGenerator{ cred: cred, db: sqlDB, }, nil }
2. ãã¹ãã®Mainé¢æ°ã§ãã¹ãç¨ã®ãã¼ã¿ãã¼ã¹ãDump
ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãseedãã¡ã¤ã«ãå ã«ãã¹ãç¨ã®åæãã¼ã¿ãã¼ã¹ãä½æããDumpãã¾ãã
func (dg *databaseGenerator) DumpDB() error { // gormã¤ã³ã¹ã¿ã³ã¹ã®çæããã¼ã¿ãã¼ã¹åã¯ã©ã³ãã ãªsuffixãå ¥ã gormDB, dbName, _ := dg.createTestDB() // seedããã±ã¼ã¸ã¯ãã¤ã°ã¬ã¼ã·ã§ã³ãSeedã®é©ç¨ã管çãã¾ã seed := NewSeed(gormDB) seed.Migrate() // ãã¹ãç¨ã®ã³ã³ããã«mariadb-clientãå¿ è¦ã§ã cmd := exec.Command( "mysqldump", "--single-transaction", "--skip-lock-tables", fmt.Sprintf("-h%s", dg.cred.Host), fmt.Sprintf("-u%s", dg.cred.User), fmt.Sprintf("-p%s", dg.cred.Password), dbName, ) output, _ := cmd.CombineOutput() file, _ := os.Create(<DUMP_FILE_PATH>) defer file.Close() _, _ := file.Write(output) return nil }
詳ãã解説ã¯å²æãã¾ãããcreateTestDBãã¡ã³ã¯ã·ã§ã³ã¯hashã使ã£ãã©ã³ãã ãªsuffixãä»ä¸ãããã¼ã¿ãã¼ã¹åã§gorm.DBã®ã¤ã³ã¹ã¿ã³ã¹ãçæããåããã¦dbNameãè¿ãã¾ãã
ãã®å¾ãseedããã±ã¼ã¸ã«ãããã¤ã°ã¬ã¼ã·ã§ã³ãè¡ããmariadb-clientã®ã³ãã³ããmysqldumpã使ã£ã¦ãã¤ã°ã¬ã¼ã·ã§ã³ãå®äºãããã¼ã¿ãã¼ã¹ãDumpãã¾ãã
ãã®å¦çããã¹ãã®ã¡ã¤ã³é¢æ°ã§å®è¡ãã¾ãã
func TestMain(m *testing.M) { dbGen, _ := database_generator.New() _, := dbGen.Dump() os.Exit(m.Run()) }
3. Dumpããã®å¾©å
åãã¹ãã§å®è¡ãããã¼ã¿ãã¼ã¹ãçæããããã®é¢æ°ãæºåãã¾ãã
ãã®é¢æ°ã§ã¯ãå ç¨ã®Dumpã§ä½æããããã¡ã¤ã«ãå ã«ãã¼ã¿ãã¼ã¹ã復å ããgorm.DBãè¿ãã¾ãã
func (dg *database_generator) GenerateDB() (*gorm.DB, error) { gormDB, _, _ := dg.createTestDB() schema, _ := os.ReadFile(<DUMP_FILE_PATH>) _ := gormDB.Exec(string(shcema)).Error return gormDB, nil }
4. ãã¼ã¿ãã¼ã¹ä½æããã¹ãå®è¡ããã¼ã¿ãã¼ã¹åé¤ãã³ãã¯ã·ã§ã³ã¯ãã¼ãº
åãã¹ãã±ã¼ã¹ã®å®è¡åã«ãdatabase_generateã¤ã³ã¹ã¿ã³ã¹ãçæãããã¼ã¿ãã¼ã¹ãä½æãã¾ãã
ãã®ãã¼ã¿ãã¼ã¹ã¯ãã®ãã¹ãã±ã¼ã¹ã§ã®ã¿ä½¿ããããã¨ã«ãªãã¾ãã
func Test_Hoge(t *testing.T) { dbGen, _ := database_generator.New() testDB, _ := dbGen.GenerateDB() // setup // ãã¹ãç¨ã®ãã¼ã¿æå ¥ãªã©ãè¡ã // run tests ... // teardown _ := dbGen.DropDB() _ := dbGen.CloseConnection() }
ãã¹ãã®å®è¡ãå®äºãããã¨ããã¼ã¿ãã¼ã¹ã®åé¤ã¨ã³ãã¯ã·ã§ã³ãéãã¾ãã
5. 並åã§å®è¡ããããã®å¦çã追å
ããã±ã¼ã¸å ã®ãã¹ãã並åã§å®è¡ãããããt.Parallel()ã追å ãã¾ãã
func Test_Hoge(t *testing.T) {
t.Parallel()
...
}
ããã±ã¼ã¸å ã§ä¸¦åã«ãã¹ããå®è¡ããããã«ã¯ããã®è¨è¿°ãå¿ è¦ã§ãã
6. ãã¹ãå®è¡ã³ãã³ããç·¨é
ããã±ã¼ã¸å ã§ä¸¦åå®è¡ãããæ°ãã³ãã³ãã§æå®ãã¾ã
æå®ããªãã£ãå ´åãå©ç¨å¯è½ãªCPUã®æ°ãæå®ããã¾ã
go test ./... -parallel <並ååãããæ°>
è¦å´ããç¹
1. åä½ç¢ºèªã«ã¨ã«ããæéãããã
å®è£ å¾ã«æ£ããåä½ããã確èªããã®ã§ããããã®å¾ ã¡æéãé常ã«å¤ãçºçãã¾ãã
ã¾ã並ååã«ä¼´ãããã¼ã¿ãã¼ã¹ä»¥å¤ã®ã¨ããã§ãã¼ã¿å¹²æ¸ãçºçããã±ã¼ã¹ããã£ããããåå ã®ç¹å®ã«ã¯è¦å´ãã¾ããã
2. ç°å¢å·®ç°
ãã¼ã«ã«ã§ã¯ä¸æãåä½ãã¦ãããã®ã®ãCIä¸ã§ãã¹ããããã¨ä¸æããããªããã¨ããç°å¢å·®ç°ã«æ©ã¾ããã¾ããã
ç°å¢ãéãç¶æ³ã§å®è¡ãããã®ã«ã¤ãã¦ã¯ãæ©ã段éã§ããããã®ç°å¢ä¸ã®ãã¹ããå¿ è¦ã ã¨åçãã¾ããã
ãããã«
ãããã®å¯¾å¿ã«ãããç´20ï¼ ç¨åº¦ãå®è¡æéãåæ¸ãããã¨ãã§ãã¾ããã
ãããã¼ã¿ãã¼ã¹æ¥ç¶ãä¼´ãGoã®ã¦ããããã¹ãã«èª²é¡ãæ±ãã¦ããã£ãããã¾ããããä»åã®è¨äºãåèã«ãªãã¨å¹¸ãã§ãã
ãèªã¿ããã ããããã¨ããããã¾ããã