DBã¹ãã¼ãããKotlinã®ãã¹ããã£ã¯ã¹ãã£ãèªåçæããgradleãã©ã°ã¤ã³ãä½ã£ã
ãµã¼ãã¼ãµã¤ãKotlinã§DBæ¥ç¶ãã¹ãããéããã¹ããã¼ã¿ã®ã»ããã¢ããã«ã¯DbSetup ã便å©ã§ããDbSetupã¯ãxmlãªã©ã®å¤é¨ãã¡ã¤ã«ãã§ã¯ãªããã³ã¼ããã§ãã¹ããã£ã¯ã¹ãã£ãçæã§ããJavaã©ã¤ãã©ãªã§ã以ä¸ã®ãããªKotlinç¨ã®DSLãæä¾ãã¦ããã¦ããã®ã§éå®ãã¦ãã¾ãã
insertInto("users") { mappedValues( "id" to 1, "name" to "åå ç§å¾³", "job" to "engineer", "status" to "ACTIVE", ... ) }
ãã ãããã ã¨ã«ã©ã æ°ãæ°ååã«ãªã£ã¦ããã¨è¨è¿°ãé¢åã ãï¼ä»äºã ã¨100ã«ã©ã è¿ããã¼ãã«ããããã§ãããããï¼ãã¡ãã£ã¨ã ãå¤ãç°ãªããã¿ã¼ã³ãè²ã ä½ããããã¨ãã£ãéã«ããã©ããªæãã¦ã¾ããã
DBã¹ãã¼ãããã³ã¼ããèªåçæãã¦ããã¦ããã¤Rubyã®factory_botã®ãããªä½¿ãå¿å°ã ã£ãã楽ã ãªã¨æããfactlinã¨ããgradle pluginãä½ã£ãã®ã§ç´¹ä»ãã¾ãã
- æ¢åã®DBã¹ãã¼ããå ã«Kotlinã®ã³ã¼ããèªåçæããï¼èªåçæãããã³ã¼ãã¯DbSetupã«ä¾åï¼
- èªåçæãããã³ã¼ãã使ãã¨Rubyã®factory_botã®ãããªä½¿ãå¿å°ã§ãã¹ããã£ã¯ã¹ãã£ãã»ããã¢ããã§ãã
- èªåçæãããã³ã¼ãã®ãã³ãã¬ã¼ããããã©ã«ãå¤ã¯ã«ã¹ã¿ãã¤ãºå¯è½
ã¨ãã£ã代ç©ã§ããä¾ãã°ãpostgresã§ãããªãã¼ãã«å®ç¾©ããã£ãå ´å
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(256) NOT NULL, job VARCHAR(256) NOT NULL DEFAULT 'engineer', status VARCHAR(256) NOT NULL DEFAULT 'ACTIVE', age INTEGER NOT NULL, score NUMERIC NOT NULL, is_admin BOOLEAN NOT NULL, birth_day DATE NOT NULL, nick_name VARCHAR(256), created_timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL, updated_timestamp TIMESTAMP WITHOUT TIME ZONE ); COMMENT ON TABLE users IS 'user table'; COMMENT ON COLUMN users.id IS 'primary key'; COMMENT ON COLUMN users.name IS 'user name'; COMMENT ON COLUMN users.job IS 'job name'; COMMENT ON COLUMN users.status IS 'activate status'; COMMENT ON COLUMN users.age IS 'user age'; COMMENT ON COLUMN users.score IS 'game score'; COMMENT ON COLUMN users.is_admin IS 'user is admin user or not'; COMMENT ON COLUMN users.birth_day IS 'user birth day'; COMMENT ON COLUMN users.nick_name IS 'nick name';
ãã®ãã©ã°ã¤ã³ãå®è¡ããã¨ã以ä¸ã®ãããªKotlinã®ã³ã¼ããèªåçæããã¾ãã
data class UsersFixture ( val id: Int = 0, // primary key val name: String = "", // user name val job: String = "", // job name val status: String = "", // activate status val age: Int = 0, // user age val score: BigDecimal = 0.toBigDecimal(), // game score val is_admin: Boolean = false, // user is admin user or not val birth_day: LocalDate = LocalDate.now(), // user birth day val nick_name: String? = null, // nick name val created_timestamp: LocalDateTime = LocalDateTime.now(), val updated_timestamp: LocalDateTime? = null ) fun DbSetupBuilder.insertUsersFixture(f: UsersFixture) { insertInto("users") { mappedValues( "id" to f.id, "name" to f.name, "job" to f.job, "status" to f.status, "age" to f.age, "score" to f.score, "is_admin" to f.is_admin, "birth_day" to f.birth_day, "nick_name" to f.nick_name, "created_timestamp" to f.created_timestamp, "updated_timestamp" to f.updated_timestamp ) } }
çæãããã³ã¼ãã¯ã
- ãã¹ããã£ã¯ã¹ãã£ã表ãData Class
- DbSetupã®insertIntoã¡ã½ãããã©ããããæ¡å¼µé¢æ°
ã§æ§æããã¦ã¾ãã1.Data Classã®åããããã£ã«ã¯ããã¼ã¿ãã¼ã¹ã®ã«ã©ã åã«å¿ããKotlinã®åã¨ããã©ã«ãå¤ãèªåçæããã¾ããããã©ã«ãå¤ã¯nullableãªã«ã©ã ãªãnullãnullableã§ãªãå ´åã¯ã«ã©ã ã®åã«å¿ããå¤ï¼æ°å¤ãªã0ãæååãªã空æåï¼ã¨ãã£ãå ·åã«è¨å®ããã¾ãããå¾è¿°ããã«ã¹ã¿ãã¤ãºè¨å®ã§ããç¨åº¦ã«ã¹ã¿ãã¤ãºå¯è½ã§ããã¾ããDBã«ã©ã ã«ã³ã¡ã³ããã¤ãã¦ããå ´åãã³ã¡ã³ããã¤ãããã«ãªã£ã¦ãã¾ãã
ä¸è¨ã®èªåçæãããã³ã¼ãã¯ããã¹ãã®ä¸ã§ä»¥ä¸ã®ããã«ä½¿ãã¾ããããã«ããDBã«ãã¹ããã¼ã¿ãinsertããããã®ãã¼ã¿ãç¨ãããã¹ããè¡ããã¨ãã§ãã¾ãã
dbSetup(dest) { deleteAllFrom(listOf("users")) // DBã«ãã¹ããã¼ã¿ãã»ããã¢ãããã insertUsersFixture(UsersFixture(id = 1, name = "foo")) insertUsersFixture(UsersFixture(id = 2, name = "bar")) }.launch() ...DBã®ãã¼ã¿ã使ã£ããã¹ã
èªåçæãããFixtureã¯ã©ã¹ã¯Data Classã«ãªã£ã¦ããããã¤ã«ã©ã ã®åã«æ²¿ã£ãããã©ã«ãå¤ãè¨å®ããã¦ããã®ã§ããã¡ãã£ã¨ã ãå¤ãç°ãªããã¹ããã£ã¯ã¹ãã£ããç°¡åã«çæãããã¨ãã§ãã¾ããIDEã§è£å®ãå¹ãã¦ããã¦ãå®ç¾©å ã«ã¸ã£ã³ãããã°ã«ã©ã ã®ã³ã¡ã³ãã確èªã§ãã¦ä¾¿å©ã§ãï¼100åè¿ãã«ã©ã ããããã¼ãã«ã ã¨ç¹ã«å¬ããï¼
ãã£ã¯ã¹ãã£ã®ãã¿ã¼ã³ãä½ã
èªåçæãããã¯ã©ã¹ãData Classã«ãªã£ã¦ããã¨ããç¹ããã¤ã³ãã§ãData Classã®copyã¡ã½ããã使ããã¨ã§ãRubyã®factory_botã®ããã«ããã£ã¯ã¹ãã£ã®ãã¿ã¼ã³ã«ååãã¤ãã¦åãåããããã«ãã¦ããããã¨ãå¯è½ã§ãã以ä¸ã®ããã«Kotlinã®æ¡å¼µé¢æ°ã¨copyã使ãã¾ããã¡ãªã¿ã«ãã¡ã³ããã³ã¹ããããããããï¼ã«ã©ã ãå¤æ´ããã³ã¼ããåçæããå ´åã«åãã¦ï¼ãèªåçæãããFixtureã¯ã©ã¹ã«ã¯æãå ãããå¥ã®ãã£ã¬ã¯ããªã¨ãã¡ã¤ã«ãä½ã£ã¦æ¡å¼µé¢æ°ãå®ç¾©ããã®ããªã¹ã¹ã¡ã§ãã
fun UsersFixture.default() = this.copy(job = "ã¨ã³ã¸ãã¢", age = 30) fun UsersFixture.active() = this.default().copy(status = "ACTIVE") fun UsersFixture.inActive() = this.default().copy(status = "IN_ACTIVE")
ãã¹ãã§ãããªé¢¨ã«ä½¿ãã¾ã
insertUsersFixture(UsersFixture().active().copy(id = 1, name = "ãã¹ã1")) insertUsersFixture(UsersFixture().inActive().copy(id = 2, name = "ãã¹ã2"))
ã«ã¹ã¿ãã¤ãº
èªåçæãããã³ã¼ãã¯ããç¨åº¦ã«ã¹ã¿ãã¤ãºå¯è½ã¨ãªã£ã¦ãã¾ãã
è¨å® | å 容 | ããã©ã«ã |
---|---|---|
fixtureOutputDir | èªåçæãããã³ã¼ãã®åºåå ãã£ã¬ã¯ã㪠| src/test/kotlin/com/maeharin/factlin/fixtures |
fixturePackageName | èªåçæãããã³ã¼ãã®ããã±ã¼ã¸å | com.maeharin.factlin.fixtures |
fixtureTemplatePath | èªåçæã«ä½¿ããã³ãã¬ã¼ã(FreeMarker) ã®ãã¹ã§ã | ããã©ã«ã |
exclude table names | èªåçæã®å¯¾è±¡å¤ã«ãããã¼ãã«ãªã¹ã | ãªã |
includeTables | èªåçæã®å¯¾è±¡ã«ãããã¼ãã«ãªã¹ã | ãã¹ã¦ |
cleanOutputDir | trueã«ããã¨èªåçæããã¾ãã«åºåå ãã£ã¬ã¯ããªãåé¤ãã¾ãï¼ã¡ã³ããã³ã¹æ§ã®ããã«trueã«ãããã¨ãæ¨å¥¨ï¼ | false |
customDefaultValues | ç¹å®ã®ãã¼ãã«ã®ç¹å®ã®ã«ã©ã ã®ããã©ã«ãå¤ãä¸æ¸ããå½¢å¼: [tableName, columnName, defaultValue] | ãªã |
customTypeMapper | ãã¼ã¿ãã¼ã¹ã®åãKotlinã®åã«å¤æããã«ã¼ã«ãä¸æ¸ããå½¢å¼: [databaseColumnType, KotlinType] | ããã©ã«ãã«ã¼ã« |
ãªãã¸ããªã®READMEã«è¨å®ä¾ãããã®ã§ãåç §ãã ããã
ãã²ä½¿ã£ã¦ã¿ã¦ãã ãã!
ãã®ä»çµã§ããã¾ã§1000ã±ã¼ã¹ä»¥ä¸ãã¹ãã±ã¼ã¹ãæ¸ãã¦ãã¾ããããã¨ãã«åé¡ãªãå¿«é©ã«ä½¿ãã¦ãã¾ãããããããã°ããã²ä½¿ã£ã¦ã¿ã¦ãã ããï¼
ï¼å®£ä¼ï¼Kotlin Fest 2018ã«ç»å£ãã¾ã
2018å¹´8æ25æ¥ï¼åï¼ã«æ±äº¬ã³ã³ãã¡ã¬ã³ã¹ã»ã³ã¿ã¼åå·ã«ã¦éå¬ãããKotlin Fest 2018ã«ç»å£ãã¾ãããµã¼ãã¼ãµã¤ãKotlinã®ãã¹ãã«é¢ãã¦è©±ãäºå®ã§ãã®ã§ããã²ãæ¥å ´ãã ããï¼