SlideShare a Scribd company logo
1 of 36
id:shiba_yu36

2011/11/26 Kansai.pm #14
•
    • shiba_yu36@hatena
    • @shiba_yu36
    • shibayu36@github
•
    •
# fixture.yml
- name: entry1
  data:
    id: 1
    title: my policy
- name: entry2
  data:
    id: 2
    title: please join
use Test::More;
use Test::Fixture::DBI;

construct_fixture(
    dbh => $dbh,
    fixture => '/path/to/fixture.yaml',
);
use Test::More;

my $data = DBIx::Sample->create(
    id    => 1,
    title => 'entry1',
);
my $data2 = DBIx::Sample->create(
    id    => 2,
    title => 'entry2',
);

# add test ...
sub create_sample (;%) {
    my %args = @_;

    unless (defined $args{id}) {
        $args{id} = int(rand(1000000));
    }

    my $sample = DBIx::Sample->create(id => $args{id});

    unless ($args{title}) {
        $args{title} = String::Random->new->randregex('[a-z]
{40}');
    }
    $sample->title($args{title});

    return $sample;
}
use Test::More;

my $data = create_sample;
my $data2 = create_sample(title =>
'entry1');

# add test ...
CREATE TABLE `sample` (
   id int(10) unsigned NOT NULL,
   title varchar(20) NOT NULL,
   body text
);
my $factory_maker = DBIx::DataFactory->new({
    username => 'root',
    password => '',
    dsn      => 'dbi:mysql:dbname=sample',
});
$factory_maker->create_factory_method(
    method   => 'create_sample',
    table    => 'sample',
    auto_inserted_columns => {
        id => {
            type => 'Int',
            size => 10,
        },
        title => {
            type => 'Str',
            size => 20,
        },
    },
);
my $values = $factory_maker->create_sample;
warn $values->{id};
# +-----------+----------------------+------+
# | id        | title                | body |
# +-----------+----------------------+------+
# | 452882908 | E54521Hpjkp9Wv1i9Rkb | NULL |
# +-----------+----------------------+------+
my $values = $factory_maker->create_sample(
    title => 'title',
    body => 'body',
);
# +------------+-------+------+
# | id         | title | body |
# +------------+-------+------+
# | 3931487566 | title | body |
# +------------+-------+------+
$factory_maker->create_factory_method(
    method   => 'create_sample',
    table    => 'sample',
    auto_inserted_columns => {
        id => sub { int rand(1000000000) },
        title => {
            type => 'Str',
            size => 20,
        },
    },
);
Create Sample Data Factories
Create Sample Data Factories
Create Sample Data Factories

More Related Content

What's hot

Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin
 
Database performance 101
Database performance 101Database performance 101
Database performance 101Leon Fayer
 
PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databaseLeon Fayer
 
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
Difference between mysql_fetch_array and mysql_fetch_assoc in PHPDifference between mysql_fetch_array and mysql_fetch_assoc in PHP
Difference between mysql_fetch_array and mysql_fetch_assoc in PHPVineet Kumar Saini
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntityBasuke Suzuki
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntityBasuke Suzuki
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHPmarkstory
 
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for DeveloperNHN FORWARD
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesDesarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesLuis Curo Salvatierra
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3markstory
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the newRemy Sharp
 
Cycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveCycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveEugene Zharkov
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Pythonpugpe
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Masahiro Nagano
 

What's hot (20)

Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"
 
Database performance 101
Database performance 101Database performance 101
Database performance 101
 
Database api
Database apiDatabase api
Database api
 
PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a database
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
 
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
Difference between mysql_fetch_array and mysql_fetch_assoc in PHPDifference between mysql_fetch_array and mysql_fetch_assoc in PHP
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesDesarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móviles
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the new
 
Cycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveCycle.js: Functional and Reactive
Cycle.js: Functional and Reactive
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
6. CodeIgniter copy2
6. CodeIgniter copy26. CodeIgniter copy2
6. CodeIgniter copy2
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Python
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
 

Viewers also liked

Visual Studioでテストデータを作ろう
Visual Studioでテストデータを作ろうVisual Studioでテストデータを作ろう
Visual Studioでテストデータを作ろうYoshitaka Seo
 
Demand-Driven Context-Sensitive Alias Analysis for Java
Demand-Driven Context-Sensitive Alias Analysis for JavaDemand-Driven Context-Sensitive Alias Analysis for Java
Demand-Driven Context-Sensitive Alias Analysis for JavaDacong (Tony) Yan
 
Cambridge speaking & listening exam entry 1 phase 1a
Cambridge speaking & listening exam entry 1 phase 1aCambridge speaking & listening exam entry 1 phase 1a
Cambridge speaking & listening exam entry 1 phase 1aDawn-Stafford
 
ご注文は監視自動化ですか?
ご注文は監視自動化ですか?ご注文は監視自動化ですか?
ご注文は監視自動化ですか?Masahito Zembutsu
 
Trinity Conversation Questions
Trinity Conversation QuestionsTrinity Conversation Questions
Trinity Conversation Questionsprofecolaborador
 
Useful phrases for work and everyday life
Useful phrases for work and everyday lifeUseful phrases for work and everyday life
Useful phrases for work and everyday liferaja1910
 
Speaking English (Linking Words)
Speaking English  (Linking Words)Speaking English  (Linking Words)
Speaking English (Linking Words)sandeep J
 
Modern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesMarkus Winand
 
PPT ON ENGLISH
PPT ON ENGLISHPPT ON ENGLISH
PPT ON ENGLISHTime Rahul
 
More level-1-students-book
More level-1-students-bookMore level-1-students-book
More level-1-students-booktrung vu
 
データベース設計徹底指南
データベース設計徹底指南データベース設計徹底指南
データベース設計徹底指南Mikiya Okuno
 

Viewers also liked (15)

Visual Studioでテストデータを作ろう
Visual Studioでテストデータを作ろうVisual Studioでテストデータを作ろう
Visual Studioでテストデータを作ろう
 
Blog entry1
Blog entry1Blog entry1
Blog entry1
 
Demand-Driven Context-Sensitive Alias Analysis for Java
Demand-Driven Context-Sensitive Alias Analysis for JavaDemand-Driven Context-Sensitive Alias Analysis for Java
Demand-Driven Context-Sensitive Alias Analysis for Java
 
Cambridge speaking & listening exam entry 1 phase 1a
Cambridge speaking & listening exam entry 1 phase 1aCambridge speaking & listening exam entry 1 phase 1a
Cambridge speaking & listening exam entry 1 phase 1a
 
ご注文は監視自動化ですか?
ご注文は監視自動化ですか?ご注文は監視自動化ですか?
ご注文は監視自動化ですか?
 
Trinity Conversation Questions
Trinity Conversation QuestionsTrinity Conversation Questions
Trinity Conversation Questions
 
Tom's TEFL - Time And Daily Routine
Tom's TEFL - Time And Daily RoutineTom's TEFL - Time And Daily Routine
Tom's TEFL - Time And Daily Routine
 
English for everyday activities
English for everyday activitiesEnglish for everyday activities
English for everyday activities
 
Useful phrases for work and everyday life
Useful phrases for work and everyday lifeUseful phrases for work and everyday life
Useful phrases for work and everyday life
 
Speaking English (Linking Words)
Speaking English  (Linking Words)Speaking English  (Linking Words)
Speaking English (Linking Words)
 
English book 1 teacher
English book 1 teacherEnglish book 1 teacher
English book 1 teacher
 
Modern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial Databases
 
PPT ON ENGLISH
PPT ON ENGLISHPPT ON ENGLISH
PPT ON ENGLISH
 
More level-1-students-book
More level-1-students-bookMore level-1-students-book
More level-1-students-book
 
データベース設計徹底指南
データベース設計徹底指南データベース設計徹底指南
データベース設計徹底指南
 

Similar to Create Sample Data Factories

DBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たちDBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たちRyo Miyake
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutesBarang CK
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 MinutesAzim Kurt
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11Michelangelo van Dam
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxMichelangelo van Dam
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairMark
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018Adam Tomat
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPVineet Kumar Saini
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Editionddiers
 
PHP webboard
PHP webboardPHP webboard
PHP webboardtumetr1
 
Rapid Prototyping with PEAR
Rapid Prototyping with PEARRapid Prototyping with PEAR
Rapid Prototyping with PEARMarkus Wolff
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Michael Schwern
 
第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 DatasourceKaz Watanabe
 

Similar to Create Sample Data Factories (20)

DBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たちDBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たち
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Drupal 8 database api
Drupal 8 database apiDrupal 8 database api
Drupal 8 database api
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018
 
php2.pptx
php2.pptxphp2.pptx
php2.pptx
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
 
PHP webboard
PHP webboardPHP webboard
PHP webboard
 
QA for PHP projects
QA for PHP projectsQA for PHP projects
QA for PHP projects
 
Rapid Prototyping with PEAR
Rapid Prototyping with PEARRapid Prototyping with PEAR
Rapid Prototyping with PEAR
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
Smarty
SmartySmarty
Smarty
 
Django quickstart
Django quickstartDjango quickstart
Django quickstart
 
第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource
 

More from Yuki Shibazaki

130427 kansai-emacs-github
130427 kansai-emacs-github130427 kansai-emacs-github
130427 kansai-emacs-githubYuki Shibazaki
 
Cinnamon - simple deploy tool
Cinnamon - simple deploy toolCinnamon - simple deploy tool
Cinnamon - simple deploy toolYuki Shibazaki
 
120225perl入学式02自己紹介
120225perl入学式02自己紹介120225perl入学式02自己紹介
120225perl入学式02自己紹介Yuki Shibazaki
 
没入コミュニケーションに利用する3次元仮想キャラクタの半自動生成
没入コミュニケーションに利用する3次元仮想キャラクタの半自動生成没入コミュニケーションに利用する3次元仮想キャラクタの半自動生成
没入コミュニケーションに利用する3次元仮想キャラクタの半自動生成Yuki Shibazaki
 

More from Yuki Shibazaki (10)

131026 kansai-emacs
131026 kansai-emacs131026 kansai-emacs
131026 kansai-emacs
 
130713 kyotopm-lt
130713 kyotopm-lt130713 kyotopm-lt
130713 kyotopm-lt
 
130427 kansai-emacs-github
130427 kansai-emacs-github130427 kansai-emacs-github
130427 kansai-emacs-github
 
130412 kayac-cinnamon
130412 kayac-cinnamon130412 kayac-cinnamon
130412 kayac-cinnamon
 
Cinnamon - simple deploy tool
Cinnamon - simple deploy toolCinnamon - simple deploy tool
Cinnamon - simple deploy tool
 
130207 kyotorb
130207 kyotorb130207 kyotorb
130207 kyotorb
 
121221社内lt
121221社内lt121221社内lt
121221社内lt
 
120225perl入学式02自己紹介
120225perl入学式02自己紹介120225perl入学式02自己紹介
120225perl入学式02自己紹介
 
120225perl入学式02
120225perl入学式02120225perl入学式02
120225perl入学式02
 
没入コミュニケーションに利用する3次元仮想キャラクタの半自動生成
没入コミュニケーションに利用する3次元仮想キャラクタの半自動生成没入コミュニケーションに利用する3次元仮想キャラクタの半自動生成
没入コミュニケーションに利用する3次元仮想キャラクタの半自動生成
 

Recently uploaded

Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear RegressionRavindra Nath Shukla
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024christinemoorman
 
DEPED Work From Home WORKWEEK-PLAN.docx
DEPED Work From Home  WORKWEEK-PLAN.docxDEPED Work From Home  WORKWEEK-PLAN.docx
DEPED Work From Home WORKWEEK-PLAN.docxRodelinaLaud
 
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
Tech Startup Growth Hacking 101  - Basics on Growth MarketingTech Startup Growth Hacking 101  - Basics on Growth Marketing
Tech Startup Growth Hacking 101 - Basics on Growth MarketingShawn Pang
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdfRenandantas16
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLSeo
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.Aaiza Hassan
 
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsCash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsApsara Of India
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxAndy Lambert
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...anilsa9823
 
Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni
 
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...noida100girls
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Lviv Startup Club
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMANIlamathiKannappan
 
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfCatalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfOrient Homes
 
RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechNewman George Leech
 

Recently uploaded (20)

Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear Regression
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024
 
DEPED Work From Home WORKWEEK-PLAN.docx
DEPED Work From Home  WORKWEEK-PLAN.docxDEPED Work From Home  WORKWEEK-PLAN.docx
DEPED Work From Home WORKWEEK-PLAN.docx
 
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
Tech Startup Growth Hacking 101  - Basics on Growth MarketingTech Startup Growth Hacking 101  - Basics on Growth Marketing
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.
 
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsCash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptx
 
KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
 
Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.
 
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
 
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfCatalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
 
RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman Leech
 

Create Sample Data Factories

  • 2.
  • 3. • shiba_yu36@hatena • @shiba_yu36 • shibayu36@github • •
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. # fixture.yml - name: entry1 data: id: 1 title: my policy - name: entry2 data: id: 2 title: please join
  • 11. use Test::More; use Test::Fixture::DBI; construct_fixture( dbh => $dbh, fixture => '/path/to/fixture.yaml', );
  • 12.
  • 13.
  • 14.
  • 15. use Test::More; my $data = DBIx::Sample->create( id => 1, title => 'entry1', ); my $data2 = DBIx::Sample->create( id => 2, title => 'entry2', ); # add test ...
  • 16.
  • 17.
  • 18.
  • 19. sub create_sample (;%) { my %args = @_; unless (defined $args{id}) { $args{id} = int(rand(1000000)); } my $sample = DBIx::Sample->create(id => $args{id}); unless ($args{title}) { $args{title} = String::Random->new->randregex('[a-z] {40}'); } $sample->title($args{title}); return $sample; }
  • 20. use Test::More; my $data = create_sample; my $data2 = create_sample(title => 'entry1'); # add test ...
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. CREATE TABLE `sample` ( id int(10) unsigned NOT NULL, title varchar(20) NOT NULL, body text );
  • 27. my $factory_maker = DBIx::DataFactory->new({ username => 'root', password => '', dsn => 'dbi:mysql:dbname=sample', });
  • 28. $factory_maker->create_factory_method( method => 'create_sample', table => 'sample', auto_inserted_columns => { id => { type => 'Int', size => 10, }, title => { type => 'Str', size => 20, }, }, );
  • 29. my $values = $factory_maker->create_sample; warn $values->{id}; # +-----------+----------------------+------+ # | id | title | body | # +-----------+----------------------+------+ # | 452882908 | E54521Hpjkp9Wv1i9Rkb | NULL | # +-----------+----------------------+------+
  • 30. my $values = $factory_maker->create_sample( title => 'title', body => 'body', ); # +------------+-------+------+ # | id | title | body | # +------------+-------+------+ # | 3931487566 | title | body | # +------------+-------+------+
  • 31.
  • 32.
  • 33. $factory_maker->create_factory_method( method => 'create_sample', table => 'sample', auto_inserted_columns => { id => sub { int rand(1000000000) }, title => { type => 'Str', size => 20, }, }, );

Editor's Notes

  1. テストデータどうしてますか?というタイトルで発表します、shiba_yu36です\n
  2. まずは自己紹介から\n
  3. 柴崎優季\nhatena, twitter, github\nはてなエンジニアバイト\n
  4. perlとかjavascriptをよく使います\n
  5. perlで作ったものとして\nbit.lyのapiラッパーであるWebService::Bitly、CSRFを自動的に防ぐMojoliciousプラグイン、あとは今回後で紹介するDBIx::DataFactory\n最近はCPANに上げる前にレビューをしてもらえるPrePANとかも手伝ってます\n
  6. では本題です\n
  7. \n
  8. 色々方法はあると思う\nFixtureを使う、ORMとかを直接使ってデータを入れる、テストデータを生成するためのユーティリティ関数を用意してそれを使うなどの方法が考えられます\n1つずつ考えていきますね\n
  9. \n
  10. たとえばymlファイルにこんな感じでデータを書いておいて、\n
  11. テストスクリプト内で読み込んでやると、テスト用のデータが生成されます\n
  12. データと実際のテストが分離していて読みづらいことがある\n\n
  13. もうfixtureとか使わずに直接テストスクリプト内でデータを入れたらいいんじゃないかって思いました\n
  14. 次の方法なんですが、直接データを入れる方法についてです\n
  15. DBIx::SampleをなんらかのORMとかとすると、こんな感じでcreateする\nこうしておくことでテストとデータの関係が見やすくなる\n
  16. 結局さっき言ってた追加したデータでテストが落ちる\ncolumnが10個くらいあったらやばい、ちょっとしたテストを書きたいだけでも長くなる\n
  17. \n
  18. というわけで次の方法ですが、テストデータ生成用のユーティリティメソッドを作る方法です\n
  19. 例えばこんな感じです。idとtitleのみのデータを入れることができるtableがあったとして、こういうふうにしておくと、データが指定してあればそれを入れる、そうでなければidに整数値をランダムで入れたり、titleに文字列をランダムで入れたりするようなメソッドができます。\n
  20. 使い方はこんな感じですね。上の例だとランダムでデータが入るし、下だとtitleには指定したデータが入ります\nこうすれば、毎回書くの面倒じゃなくなるし、ランダムで入れる分データの競合も少なくなる、ちょっとしたテストを書きたいときでも手軽に書ける\n
  21. \n
  22. \n
  23. \n
  24. DBIx::DataFactoryというモジュールです\n
  25. DBIx::DataFactoryというモジュールです\n
  26. このようなschemaがあるとします。\nidが10桁の整数で、titleが20文字、bodyはtextです。\n
  27. それでこのtableにデータを入れるためのメソッドをDBIx::DataFactoryを使って実装するとこのようになります\nインスタンスをdsnなど指定して作ります\n
  28. method名、table名、自動的に生成したいcolumnを指定してメソッドを作成します。\n自動的に生成したいcolumnはデフォルトにあるtypeを指定してどんなデータを入れるか決めます\n今回だったらidにIntタイプで10桁、titleにStrタイプで20桁を指定しています\n
  29. 作成したメソッドの使い方はこんな感じです。何も指定しないと、自動で生成する設定にしたcolumnに値が入ります。insertしたvaluesが帰ってきます\n
  30. データを指定すると、指定したカラムには指定されたデータが、そうでないところには自動で生成するデータが入ります\n
  31. 自動生成データとしてデフォルトで指定できるtypeは今のところ4種類です\nまた、プラガブルにしているので、自分でTypeを作成することも出来ます\n
  32. もっと柔軟にデータを作れるように、coderefで作れるようにもしています。\n
  33. こんな感じ\n
  34. \n
  35. YAPC::Asiaに行ったらモジュールが出来てたので、行くといいですね\n
  36. \n