Scala ç factory_girl ã¿ãããªã®ãä½ãã¾ããã
https://github.com/tototoshi/seedbed
Scala 㧠factory_girl ã£ã½ããã®ãä½ãã¾ããã*1ããªããæ軽ãªæãã«ãªã£ã¦ãã¾ãã
ä¾åã©ã¤ãã©ãªã¯ç¹ã«ããã¾ããã®ã§ã©ã®ãã¼ã¿ãã¼ã¹ã©ã¤ãã©ãªã使ã£ã¦ãã¦ã大ä¸å¤«ã§ãã
åä½ã¯ postgresql 㨠h2 ã§ã®ã¿ç¢ºèªãã¦ã¾ãããä»ã®ãã¼ã¿ãã¼ã¹ã§ãåãã¨æãã¾ãã
ã¨ãããã snapshot çã sonatype ã«ããã¨ãã¾ããã
resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/" libraryDependencies += Seq( "com.github.tototoshi" %% "seedbed" % "0.1.0-SNAPSHOT" // and database driver )
使ãæ¹
以ä¸ã®ãµã³ãã«ã³ã¼ãã§ãªãã¨ãªãããã£ã¦ããããã¨æãã¾ãã
define ã§ãã¼ãã«ãã¨ã®ããã©ã«ãå¤ãå®ç¾©ã create ã§ãã¼ã¿ã1件æ¿å
¥ãget/list ã¯ãã¼ã¿ãã¼ã¹ããã¬ã³ã¼ããåå¾ããã®ã«ä½¿ç¨ãã¾ãã
ãã¼ã¿ã®åãæ±ãã¯å²ãåã£ã¦ Map[String, Any] ã§ãã£ã¦ãã¾ãã¾ããåºæ¬çã«åå®å ¨ã¨ãããã¯æ軽ããéè¦ãã¦ãã¾ããåçè¨èªãªæ°åã§ä½¿ã£ã¦ãã ãããAPI 㯠factory_girl ãã phactory (PHP ã® factory_girl çãªãã¤) ã«è¿ãã§ãã
ããã¾ã§ãã¹ãç¨ã©ã¤ãã©ãªãªã®ã§ãã¡ã¤ã³ã®ãããã¯ãã³ã¼ãã«å ¥ããã®ã¯ããã¦ãã ããã
/* CREATE table beatles ( id serial primary key, first_name varchar(10) not null, middle_name varchar(10), last_name varchar(10) not null, birth_date date not null ); */ import seedbed._ // ãã¼ã¿ãã¼ã¹ã®è¨å® trait TestDBConfiguration extends Configuration { val driver: String = "org.postgresql.Driver" val url: String = "jdbc:postgresql://localhost/seedbed_test" val user: String = "user" val password: String = "password" } // Seedbed ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãçæ val s = new Seedbed with TestDBConfiguration // ããã©ã«ãå¤ã®è¨å® // ããã©ããããã° s.define("beatles") ã ãã§ã OKãDB ã®ã¡ã¿ãã¼ã¿ãè¦ã¦ãããªã«ãã¦ããã¾ãã s.define("beatles", Map( "first_name" -> "John", "middle_name" -> "Winston", "last_name" -> "Lennon", "birth_date" -> new SimpleDateFormat("yyyy-MM-dd").parse("1940-10-09") )) // ãã¼ã¿ãã¼ã¹ã¸ã¬ã³ã¼ãã1件æ¿å ¥ s.create("beatles") // ããã©ã«ãå¤ãä¸é¨æ¸ãæãã¦æ¿å ¥ s.create("beatles", Map("first_name" -> "Paul")) // ãã¼ã¿ãã¼ã¹ãã first_name=Paul ãªãã¼ã¿ãåå¾ s.get("beatles", Map("first_name" -> "Paul")) //=>Some(Map(first_name -> Paul, ...)) s.get("beatles", Map("first_name" -> "John")) //=>Some(Map(first_name -> John, ...)) // ãã¼ã¿ãã¼ã¹ãããã¼ã¿ãã¾ã¨ãã¦åå¾ãã s.list("beatles")
äºå®ããã
- association ã¨ã sequence ã®æ©è½ã¯ãã®ãã¡æ¬²ãããªã£ããå®è£ ãã¾ãã
- joda-time ãããã«ã¯ä¾åãã¦ãããæ°ããã¦ããã
*1:skinny ã«ã¯ FactoryGirl http://skinny-framework.org/documentation/factory-girl.html ãããããã©ãskinny-orm ä¾åãªã®ã§èªåã®ç¨éã«ã¯åããªãã