Test::...:Object(3pm)
π NAME π
Test::DatabaseRow::Object β examine database rows πβ¨
okay so first things first, bestie: this page is Test::DatabaseRow::Object(3pm) from the User Contributed Perl Documentation, and it's literally all about peeking at rows that came back from your database. no cap. π«Ά
πΈ SYNOPSIS πΈ
use Test::More tests => 1;
use Test::DatabaseRow::Object;
# setup the test
my $tdr = Test::DatabaseRow::Object->new(
sql_and_bind => "SELECT * FROM contacts WHERE cid = '123'",
tests => [ name => "trelane" ],
);
# do the test and get a result back
my $result_object = $tdr->tests_ok();
# have those results render to Test::Builder
$result_object->pass_to_test_builder("my database test");
like, look at that. setup, run the test, get a result object back, hand it to Test::Builder. chef's kiss. π
β¨ DESCRIPTION β¨
This module provides the underlying functionality of Test::DatabaseRow. it's the engine room, the behind-the-scenes girly, the one doing the actual work. π
π Accessors π
These are the read only accessors of the object. They may be (optionally) set at object creation time by passing their name and value to the constructor.
Each accessor may be queried by prefixing its name with the has_ to determine if it has been set or not. so has_whatever is your vibe check for "did this thing get set?" π΅οΈββοΈ
db_resultsβ an arrayref of hashrefs, each representing a row returned from the database. Each key in the hashref should be the column name and the value the corresponding field's value. This datastructure is identical to that.
If this accessor is not passed then it will be populated on first use by executing the contents of sql_and_bind against the passed dbh. lazy loading but make it fashion. β¨
db_results_dumpedβ a string representation of the database results.
If this accessor is not passed then it will be populated on first use by using Test::Builder's explain function on db_results.
sql_and_bindβ the SQL and bind variables to execute if no results were passed into the db_results hash. This should be an arrayref containing the SQL as the first element and the bind values as further values.
This accessor will automatically coerce a simple scalar passed in into a single
If this accessor is not passed then it will be populated on first use by building SQL from the where and table accessors.
dbhβ The database handle used to execute the SQL statement insql_and_bindif nodb_resultswere passed.tableβ The table name used to build the SQL query if no value is passed tosql_and_bind. String.whereβ The data structure used to build the where clause of the SQL query if no value is passed to<sql_and_bind>.
This accessor value should be a hashref of hashrefs, with the outer keys being the SQL comparison operator, the inner keys being the field names and the inner values being the values to match against. For example:
{
'=' => { first => "Fred", last => "Flintstone", },
'like' => { address => "%Bedrock%" },
}
Values of undef will automatically converted into checks for NULLs. π
This accessor automatically coerces array refs that are passed into a pure equals hashref. For example:
[ foo => "bar", bazz => "buzz" ]
Will be coerced into:
{ "=" => { foo => "bar", bazz => "buzz" } }
See "where" in Test::DatabaseRow for a more detailed explanation.
verboseβ Truth value, default false. Controls if the diagnostic messages printed duringrow_okon failure contain details of the SQL executed or not.verbose_dataβ Truth value, default false. Controls if the diagnostic messages printed duringrow_okon failure contain a Data::Dumper style dump of the resulting rows from the database.force_utf8β Truth value, default false. Controls if the utf8 flag should be turned on on values returned from the database. See "utf8 hacks" in Test::DatabaseRow for why this might be important.
This flag only effects data that this module places into db_resutls. If you manually populate this accessor this flag will have no effect.
testsβ If set, enables specified tests on the first element ofdb_resultswhenrow_okis called.
This accessor value should be a hashref of hashrefs, with the outer keys being the Perl comparison operator, the inner keys being the field names and the inner values being the values to test against. For example:
{
'eq' => { first => "Fred", last => "Flintstone", },
'=~' => { address => "%Bedrock%" },
}
This accessor automatically coerces array refs that are passed into a hashref structure, converting things that look like strings into eq tests, things that look like numbers into == tests and things that are references to regular expressions into =~ tests. Foe example:
[ num => 123, letters => "abc", kinda => qr/substring/ ]
Will be coerced into
{
'==' => { num => 123, },
'eq' => { letters => "abc", },
'=~' => { kinda => qr/substring/ },
}
See "tests" in Test::DatabaseRow for a more detailed explanation.
check_all_rowsβ Boolean to determine if we should test all rows (duringdb_results_okandtest_ok) or just check the first row. Default true. πresultsβ If set, enable tests to check the number of rows we returned bydb_resultsis exactly this value whenrow_okis called. Integer.max_resultsβ If set, enable tests to check the number of rows we returned bydb_resultsis at most this value whenrow_okis called. Integer.min_resultsβ If set, enable tests to check the number of rows we returned bydb_resultsis at least this value whenrow_okis called. Integer.
π¦ Methods π¦
new(...)β Simple constructor. Passing arguments to the constructor sets the values of the accessors. it's giving "just tell me what you want and i'll handle it." πnumber_of_results_okβ Returns a Test::DatabaseRow::Result that represents if the number of results indb_resultsmatch the requirements for the number of results.row_at_index_ok( $row_index )β Returns a Test::DatabaseRow::Result that represents if the element corresponding to the passed row index indb_resultsmatch the tests defined intests.db_results_okβ Returns a Test::DatabaseRow::Result that represents if all elements indb_resultsmatch the tests defined intests.test_okβ Returns a Test::DatabaseRow::Result that represents if the number of results indb_resultsmatch the requirements for the number of results and all elements indb_resultsmatch the tests defined intests. the whole package deal fr fr. π
π BUGS π
Bugs (and requests for new features) can be reported though the CPAN RT system: http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-DatabaseRow
Alternatively, you can simply fork this project on github and send me pull requests. Please see http://github.com/2shortplanks/Test-DatabaseRow
not me telling you to just fork it and send a PR, but honestly? iconic behaviour. π
π AUTHOR π
Written by Mark Fowler mark@twoshortplanks.com
Copyright Profero 2003, 2004. Copyright Mark Fowler 2011.
Some code taken from Test::Builder, written by Michael Schwern. Some code taken from Regexp::Common, written by Damian Conway. Neither objected to its inclusion in this module.
Some code taken from Lingua::EN::Numbers::Ordinate, written by Sean M. Burke.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π SEE ALSO π
Test::DatabaseRow::Object, Test::DatabaseRow::Result, Test::More, DBI
and that's the whole thing bestie!! this page is from perl v5.34.0, dated 2022-06-17, and it ate, no crumbs. go forth and test your database rows. π¦πβ¨π
π¦ where this page comes from
- debian bookworm: libtest-databaserow-perl 2.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-databaserow-perl 2.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-databaserow-perl 2.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-databaserow-perl 2.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-databaserow-perl 2.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms