Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Trojan Perl Distributions

by perrin (Chancellor)
on May 05, 2004 at 18:52 UTC ( [id://350876]=note: print w/replies, xml ) Need Help??


in reply to Trojan Perl Distributions

With a module like Class::DBI, the functionality of the module is very dependent on interaction with DBI. A test using mock objects would not be a very good test. The most common way to deal with this now is to use DBD::SQLite, which allows you to make a private database for testing.

Replies are listed 'Best First'.
Re: Re: Trojan Perl Distributions
by stvn (Monsignor) on May 05, 2004 at 19:53 UTC
    A test using mock objects would not be a very good test.

    Why do you say that? What really makes it different from testing with the real object? When DBD::SQLite writes to the database, is that testing Class::DBI or is it testing DBD::SQLite? Class::DBI really should not care what happens after it sends its SQL to the DBD driver, and to properly test Class::DBI, you should check that it generated the SQL you expected it to. By testing a SQLite database you are not really testing Class::DBI, but the side-effects of Class::DBI's interaction with DBD::SQLite.

    So why not use something like DBD::Mock? It behaves pretty much like your standard DBD driver, and you can both feed it mock results and read back a history of SQL statements you have run. IMO this would be a much more effective (and less intrusive) test of Class::DBI's functionality. I have actually been using DBD::Mock to test our inhouse OO-Relational mapping tool and I would never go back to using a real DB to test. It has not only increased the quality of my tests, but it takes me less than half the time and code it did to test it with a real database. I recommend it highly.

    -stvn
      You make a good point, and DBD::Mock is pretty cool. However, the difficulty is that DBD::Mock just does whatever you tell it to do.

      When I was developing the code for Class::DBI's single-instance functionality, I found some tests that failed because there was a mistake in their assumptions about primary keys. If the test suite had been written to use DBD::Mock, it would have just returned whatever it was told to, and this bug would not have been found by the tests. In other words, the failure had to do with the interaction between multiple methods, not with the failure of a single method. It was only because the test did a real query on real data that had been created earlier in the test that I was able to see the problem.

        I see what you are saying, but no matter what integration tests like your example are tricky to write. But keep in mind that while you do feed the results into DBD::Mock, there is nothing to prevent you from feeding in incorrect results, or results you know will mess things up. In fact, that is one of the things it is best at, simulating error conditions that would otherwise require you to make a mess out of your database.

        In alot of cases, I like to develop against a real database, because in the end, that is the situation it needs to work in. It just makes sense to me to do it this way. After I am satisfied my module works, I have been converting the live DB test code to use DBD::Mock and adding tests at the end to also verify my SQL statements. The conversion from DBD::mysql to DBD::Mock is pretty minimal, it only requires that a seed my results, which is much easier than writing the code to make, manage and destroy test database.

        -stvn
Re: Re: Trojan Perl Distributions
by barbie (Deacon) on May 05, 2004 at 19:06 UTC
    But DBD::SQLite is quite a big dependency to have, as it includes the complete SQLite database code. The PPM includes the binary libraries. Plus the fact that several modules depend on specific features of a particular database, which are not part of SQLite. However, I do agree that it is another alternative.

    --
    Barbie | Birmingham Perl Mongers | http://birmingham.pm.org/

      It's not a dependency unless you want to run the tests. It just skips them if you don't have SQLite installed. I think that's more reasonable than trying to do a test that doesn't really prove anything.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://350876]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found