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

The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxThe-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxmbikashkanyari
 
Kenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith PereraKenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith Pereraictsugar
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?Olivia Kresic
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCRashishs7044
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCRashishs7044
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 
Fordham -How effective decision-making is within the IT department - Analysis...
Fordham -How effective decision-making is within the IT department - Analysis...Fordham -How effective decision-making is within the IT department - Analysis...
Fordham -How effective decision-making is within the IT department - Analysis...Peter Ward
 
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxFinancial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxsaniyaimamuddin
 
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdfNewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdfKhaled Al Awadi
 
Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Americas Got Grants
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesKeppelCorporation
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...ictsugar
 
Guide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFGuide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFChandresh Chudasama
 
Darshan Hiranandani [News About Next CEO].pdf
Darshan Hiranandani [News About Next CEO].pdfDarshan Hiranandani [News About Next CEO].pdf
Darshan Hiranandani [News About Next CEO].pdfShashank Mehta
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCRashishs7044
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationAnamaria Contreras
 
Flow Your Strategy at Flight Levels Day 2024
Flow Your Strategy at Flight Levels Day 2024Flow Your Strategy at Flight Levels Day 2024
Flow Your Strategy at Flight Levels Day 2024Kirill Klimov
 
Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfJos Voskuil
 

Recently uploaded (20)

The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxThe-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
 
Kenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith PereraKenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith Perera
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
 
No-1 Call Girls In Goa 93193 VIP 73153 Escort service In North Goa Panaji, Ca...
No-1 Call Girls In Goa 93193 VIP 73153 Escort service In North Goa Panaji, Ca...No-1 Call Girls In Goa 93193 VIP 73153 Escort service In North Goa Panaji, Ca...
No-1 Call Girls In Goa 93193 VIP 73153 Escort service In North Goa Panaji, Ca...
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 
Fordham -How effective decision-making is within the IT department - Analysis...
Fordham -How effective decision-making is within the IT department - Analysis...Fordham -How effective decision-making is within the IT department - Analysis...
Fordham -How effective decision-making is within the IT department - Analysis...
 
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxFinancial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
 
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdfNewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
 
Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation Slides
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
 
Guide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFGuide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDF
 
Darshan Hiranandani [News About Next CEO].pdf
Darshan Hiranandani [News About Next CEO].pdfDarshan Hiranandani [News About Next CEO].pdf
Darshan Hiranandani [News About Next CEO].pdf
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
 
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCREnjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement Presentation
 
Flow Your Strategy at Flight Levels Day 2024
Flow Your Strategy at Flight Levels Day 2024Flow Your Strategy at Flight Levels Day 2024
Flow Your Strategy at Flight Levels Day 2024
 
Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdf
 

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