yapc::eu::2011 - mostly lazy dbix::class testing

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

Upload: chisel-wright

Post on 07-Nov-2014

1.872 views

Category:

Technology


0 download

DESCRIPTION

It's often useful to check that your DBIx::Class classes provide the methods you expect, and no-one has sneakily stolen or broken methods when you weren't looking. Being lazy I wanted to do this with as little fuss and typing as possible. Thanks to Test::DBIx::Class::Schema I can do all of the above and would like to share thoughts and musings for the future of the module.

TRANSCRIPT

Page 1: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Page 2: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

In A Nutshell

DBIx::Class schema sanity checking tests

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

Page 3: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Why?

Ongoing Quest To Be As LAZY As Possible

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

Page 4: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Page 5: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Page 6: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Non-Lazy

What about . . . ?

I relationships

I custom methods

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

Page 7: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Non-Lazy

I Decide to test something new?

I How many .t files do you need to edit?

I More than zero?

I Too much work!!

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

Page 8: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Getting Lazy

Test::DBIx::Class::Schema

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

Page 9: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Test::DBIx::Class::Schema

My Attempt At Laziness

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

Page 10: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Page 11: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Page 12: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

An example (running)

$schematest->run_tests();

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

Page 13: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

You get. . .

I can ok( @columns )

I can ok( @relations )

I can ok( @customs ) # row subs

I can ok( @resultsets ) # rs subs

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

Page 14: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

You also get. . .

I $thing->$column called ok

I $thing->$relation called ok

I test that $column exists in the database

I ensure related-source exists

I test self.* and foreign.* columns for relationships

I test proxied relationships

I PASS/FAIL for relationship validity

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

Page 15: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

You don’t get

Functional Testing

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

Page 16: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Benefits

I only maintaining a list (or three)

I lots of sanity checking

I find out if someone deletes columns from the database

I upgrading ’TDCS’ improves *.t files for free

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

Page 17: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Recommendations

I Test one table/class per file

I Use Test::Aggregate

I Factor out ->new call for $schema

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

Page 18: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

In The Real World

I on the CPAN since 2008

I used in production code since 2009I recent burst of improvements

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

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

Page 19: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

Did It Work?

YES!IMNSHO

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

Page 20: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

What Next?

Wishlist

I relationship reciprocity

I relationships that are coderefs

I db columns that aren’t in the schema?

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

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

Page 21: YAPC::EU::2011 - Mostly Lazy DBIx::Class Testing

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

End Credits

QUESTIONS?Me:

I [email protected]

I CPANID: CHISEL

I github: github.com/chiselwright

Module:

I metacpan.org/release/Test-DBIx-Class-Schema

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

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