Mostly Lazy DBIx::Class Testing

download Mostly Lazy DBIx::Class Testing

of 21

Transcript of Mostly Lazy DBIx::Class Testing

  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    1/21

    Introduction Test::DBIx::Class::Schema Future Finally

    Mostly Lazy DBIx::Class Testing

    Chisel Wright

    Net-A-Porter

    2011

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    2/21

    Introduction Test::DBIx::Class::Schema Future Finally

    In A Nutshell

    DBIx::Class schema sanity checking tests

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    3/21

    Introduction Test::DBIx::Class::Schema Future Finally

    Why?

    Ongoing Quest To Be As LAZY As Possible

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    4/21

    Introduction Test::DBIx::Class::Schema Future Finally

    Non-Lazy

    # are specific columns defined?

    my $thingy = $model->resultset(BigBagOfFail);

    my @columns = qw/id name department/);

    can_ok($thingy, @columns);

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    5/21

    Introduction Test::DBIx::Class::Schema Future Finally

    Non-Lazy

    foreach my $column (@columns) {

    try {

    $thingy->$column();

    ok("called $column()");

    }

    catch($e) {

    diag $e;

    fail("$column() failed");}

    }

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    C S

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    6/21

    Introduction Test::DBIx::Class::Schema Future Finally

    Non-Lazy

    What about ...? relationships

    custom methods

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    I d i T DBI Cl S h F Fi ll

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    7/21

    Introduction Test::DBIx::Class::Schema Future Finally

    Non-Lazy

    Decide to test something new?

    How many .t files do you need to edit?

    More than zero?

    Too much work!!

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    I t d ti T t DBI Cl S h F t Fi ll

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    8/21

    Introduction Test::DBIx::Class::Schema Future Finally

    Getting Lazy

    Test::DBIx::Class::Schema

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    9/21

    Introduction Test::DBIx::Class::Schema Future Finally

    Test::DBIx::Class::Schema

    My Attempt At Laziness

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    10/21

    Introduction Test::DBIx::Class::Schema Future Finally

    An example (setup)

    my $schematest = Test::DBIx::Class::Schema->new({

    # required

    dsn => dbi:Pg:dbname=mydb,namespace => MyDB::Schema,

    moniker => SomeTable,

    # optional

    username => some_user,

    password => opensesame,});

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    11/21

    Introduction Test::DBIx::Class::Schema Future Finally

    An example (config)

    # tell it what to test

    $schematest->methods({

    columns => [ qw( id name ) ],

    relations => [ qw( foo ) ],

    custom => [ qw( some_method ) ],

    resultsets => [ qw( ) ],

    });

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    12/21

    Introduction Test::DBIx::Class::Schema Future Finally

    An example (running)

    $schematest->run_tests();

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    13/21

    y

    You get. . .

    can ok( @columns )

    can ok( @relations )

    can ok( @customs ) # row subs

    can ok( @resultsets ) # rs subs

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    14/21

    You also get. . .

    $thing->$column called ok

    $thing->$relation called ok

    test that $column exists in the database ensure related-source exists

    test self.* and foreign.* columns for relationships

    test proxied relationships

    PASS/FAIL for relationship validity

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    15/21

    You dont get

    Functional Testing

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    16/21

    Benefits

    only maintaining a list (or three)

    lots of sanity checking

    find out if someone deletes columns from the database

    upgrading TDCS improves *.t files for free

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    17/21

    Recommendations

    Test one table/class per file Use Test::Aggregate

    Factor out ->new call for $schema

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    18/21

    In The Real World

    on the CPAN since 2008

    used in production code since 2009 recent burst of improvements

    never used to test empty tables - OOPS! used to be quite basic

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    19/21

    Did It Work?

    YES!IMNSHO

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    20/21

    What Next?

    Wishlist

    relationship reciprocity relationships that are coderefs

    db columns that arent in the schema?

    tie-in with DBIx::Class::Schema::Loader

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    Introduction Test::DBIx::Class::Schema Future Finally

    http://find/
  • 8/6/2019 Mostly Lazy DBIx::Class Testing

    21/21

    End Credits

    QUESTIONS?

    Me: [email protected]

    CPANID: CHISEL

    github: github.com/chiselwright

    Module: metacpan.org/release/Test-DBIx-Class-Schema

    github.com/chiselwright/test-dbix-class-schema

    Mostly Lazy DBIx::Class Testing Net-A-Porter

    http://find/