Author: spadkins
Date: Thu May 29 13:15:52 2008
New Revision: 11348

Added:
   p5ee/trunk/App-Repository/t/DBI-getset-realtable.t   (contents, props 
changed)

Log:
new

Added: p5ee/trunk/App-Repository/t/DBI-getset-realtable.t
==============================================================================
--- (empty file)
+++ p5ee/trunk/App-Repository/t/DBI-getset-realtable.t  Thu May 29 13:15:52 2008
@@ -0,0 +1,208 @@
+#!/usr/local/bin/perl -w
+
+use App::Options (
+    options => [qw(dbdriver dbclass dbhost dbname dbuser dbpass)],
+    option => {
+        dbclass  => { default => "App::Repository::MySQL", },
+        dbdriver => { default => "mysql", },
+        dbhost   => { default => "localhost", },
+        dbname   => { default => "test2", },
+        dbuser   => { default => "", },
+        dbpass   => { default => "", },
+    },
+);
+
+use Test::More qw(no_plan);
+use lib "../App-Context/lib";
+use lib "../../App-Context/lib";
+use lib "lib";
+use lib "../lib";
+
+use App;
+use App::Repository;
+use strict;
+
+if (!$App::options{dbuser}) {
+    ok(1, "No dbuser given. Tests assumed OK. (add dbuser=xxx and dbpass=yyy 
to app.conf in 't' directory)");
+    exit(0);
+}
+
+my $context = App->context(
+    conf_file => "",
+    conf => {
+        Repository => {
+            default => {
+                class => $App::options{dbclass},
+                dbdriver => $App::options{dbdriver},
+                dbhost => $App::options{dbhost},
+                dbname => $App::options{dbname},
+                dbuser => $App::options{dbuser},
+                dbpass => $App::options{dbpass},
+                table => {
+                    test_person => {
+                        primary_key => ["person_id"],
+                                   alias => "tp",
+                                   tablealiases => [ "tp" ],
+                                   tablealias => {
+                                       tp => {
+                                table => "test.test_person",
+                                       },
+                                   },
+                    },
+                },
+            },
+        },
+    },
+    debug_sql => $App::options{debug_sql},
+);
+
+my $rep = $context->repository();
+
+{
+    #cheating... I know its a DBI, but I have to set up the test somehow
+    my $dbh     = $rep->{dbh};
+    eval { $dbh->do("drop table test_person"); };
+
+    my $ddl     = <<EOF;
+create table test_person (
+    person_id          integer      not null auto_increment primary key,
+    first_name         varchar(99)  null,
+    last_name          varchar(99)  null,
+    address            varchar(99)  null,
+    city               varchar(99)  null,
+    state              varchar(99)  null,
+    zip                varchar(10)  null,
+    country            char(2)      null,
+    home_phone         varchar(99)  null,
+    work_phone         varchar(99)  null,
+    email_address      varchar(99)  null,
+    gender             char(1)      null,
+    birth_dt           date         null,
+    age                integer      null,
+    index person_ie1 (last_name, first_name)
+)
+EOF
+    $dbh->do($ddl);
+
+    eval { $dbh->do("drop table if exists app_cache"); };
+    $ddl = <<EOF;
+create table app_cache (
+    cache_type         varchar(16) not null,
+    cache_key          varchar(40) not null,
+    generate_dttm      datetime    default null,
+    serializer         varchar(12) default null,
+    serialization_args varchar(64) default null,
+    data               longblob,
+    modify_dttm        timestamp   not null default CURRENT_TIMESTAMP on 
update CURRENT_TIMESTAMP,
+    PRIMARY KEY        (cache_type,cache_key),
+    KEY app_cache_ie1  (modify_dttm)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+EOF
+    $dbh->do($ddl);
+
+    $dbh->do("insert into test_person (person_id,age,first_name,gender,state) 
values (1,39,'stephen',  'M','GA')");
+    $dbh->do("insert into test_person (person_id,age,first_name,gender,state) 
values (2,37,'susan',    'F','GA')");
+    $dbh->do("insert into test_person (person_id,age,first_name,gender,state) 
values (3, 6,'maryalice','F','GA')");
+    $dbh->do("insert into test_person (person_id,age,first_name,gender,state) 
values (4, 3,'paul',     'M','GA')");
+    $dbh->do("insert into test_person (person_id,age,first_name,gender,state) 
values (5, 1,'christine','F','GA')");
+    $dbh->do("insert into test_person (person_id,age,first_name,gender,state) 
values (6,45,'tim',      'M','FL')");
+    $dbh->do("insert into test_person (person_id,age,first_name,gender,state) 
values (7,39,'keith',    'M','GA')");
+}
+
+###########################################################################
+# DATA ACCESS TESTS
+###########################################################################
+my ($person_id, $first_name, $last_name, $address, $city, $state, $zip, 
$country);
+my ($home_phone, $work_phone, $email_address, $gender, $birth_dt, $age);
+
+my $columns = [ "person_id", "age", "first_name", "gender", "state" ];
+my $rows = [
+    [ 1, 39, "stephen",   "M", "GA", ],
+    [ 2, 37, "susan",     "F", "GA", ],
+    [ 3,  6, "maryalice", "F", "GA", ],
+    [ 4,  3, "paul",      "M", "GA", ],
+    [ 5,  1, "christine", "F", "GA", ],
+    [ 6, 45, "tim",       "M", "FL", ],
+    [ 7, 39, "keith",     "M", "GA", ],
+];
+
+my ($row, $data_rows, $data_rows2, $nrows);
+
+#####################################################################
+#  $value  = $rep->get ($table, $key,     $col,   \%options);
+#  $rep->set($table, $key,     $col,   $value,    \%options);
+#####################################################################
+$data_rows = $rep->get_rows("test_person", {}, ["state"], 
{order_by=>["person_id"]});
+$first_name = $rep->get("test_person", 1, "first_name");
+is($first_name, "stephen", "get() first_name [$first_name]");
+is($rep->set("test_person", 1, "first_name", "steve"),1,"set() first name 
[steve]");
+$first_name = $rep->get("test_person", 1, "first_name");
+is($first_name, "stephen", "get() modified first_name [$first_name] got cache 
instead");
+
+exit(0);
+
+$first_name = $rep->get("test_person", {person_id => 1}, "first_name");
+is($first_name, "steve", "get() modified first_name [$first_name]");
+$age = $rep->get("test_person", 1, "age");
+is($age, 39, "get() age");
+
+ok($rep->set("test_person", 2, ["first_name","age"], ["sue",38]), "set() 2 
values");
+($first_name, $age) = $rep->get("test_person", 2, ["first_name","age"]);
+is($first_name, "sue", "get() 2 values (checking 1 of 2)");
+is($age, 38, "get() 2 values (checking 2 of 2)");
+
+ok($rep->set_row("test_person", 3, ["age", "state"], [7, "CA"]),"set_row() 2 
values");
+$row = $rep->get_row("test_person", 4, ["age", "gender"]);
+($age, $gender) = @$row;
+is($age, 3, "get_row() 2 values (checking 1 of 2)");
+is($gender, "M", "get_row() 2 values (checking 2 of 2)");
+
+ok($rep->set_row("test_person", {first_name=>'paul'}, ["age", "state"], [5, 
"CA"]),"set_row() 2 values w/ %crit");
+$row = $rep->get_row("test_person", {first_name=>'paul'}, ["age", 
"state","person_id"]);
+($age, $state, $person_id) = @$row;
+is($age,         5, "get_row() 3 values w/ %crit (checking 1 of 3) age=$age");
+is($state,    "CA", "get_row() 3 values w/ %crit (checking 2 of 3) 
state=$state");
+is($person_id,   4, "get_row() 3 values w/ %crit (checking 3 of 3) 
person_id=$person_id");
+
+ok($rep->set_row("test_person", {first_name=>'paul'}, ["age", "state"], 
{age=>6, state=>"GA", person_id=>99}),
+   "set_row() 2 values w/ %crit and values in hash");
+
+$row = $rep->get_row("test_person", {first_name=>'paul'}, ["age", 
"state","person_id"]);
+($age, $state, $person_id) = @$row;
+is($age,         5, "get_row() 3 values w/ %crit (checking 1 of 3) age=$age 
got cache instead");
+is($state,    "CA", "get_row() 3 values w/ %crit (checking 2 of 3) 
state=$state got cache instead");
+is($person_id,   4, "get_row() 3 values w/ %crit (checking 3 of 3) 
person_id=$person_id");
+
+$data_rows = $rep->get_rows("test_person", {first_name=>'paul', x=>1}, ["age", 
"state","person_id"]);
+$row = $data_rows->[0];
+($age, $state, $person_id) = @$row;
+is($age,         6, "get_row() 3 values w/ %crit (checking 1 of 3) age=$age");
+is($state,    "GA", "get_row() 3 values w/ %crit (checking 2 of 3) 
state=$state");
+is($person_id,   4, "get_row() 3 values w/ %crit (checking 3 of 3) 
person_id=$person_id");
+
+$data_rows = $rep->get_rows("test_person", {}, [ "person_id", "age", 
"first_name", "gender", "state" ], {order_by=>["person_id"]});
+is_deeply($data_rows, $rows, "get_rows() got original cached data thanks to 
cache_minimum_rows");
+
+$data_rows2 = $rep->{dbh}->selectall_arrayref("select person_id, age, 
first_name, gender, state from test_person order by person_id");
+$data_rows = $rep->get_rows("test_person", {}, [ "person_id", "age", 
"first_name", "gender", "state" ], {order_by=>["person_id"], cache_skip => 1});
+is_deeply($data_rows, $data_rows2, "get_rows() skipped cached data thanks to 
cache_skip");
+$first_name = $rep->get("test_person", 1, "first_name", { cache_skip => 1 });
+is($first_name, "steve", "get() modified first_name [$first_name] by skipping 
the cache");
+
+$data_rows = $rep->get_rows("test_person", {}, [ "person_id", "age", 
"first_name", "gender", "state" ], {order_by=>["person_id"], cache_refresh => 
1});
+is_deeply($data_rows, $data_rows2, "get_rows() refreshed cached data thanks to 
cache_refresh");
+$first_name = $rep->get("test_person", 1, "first_name", { cache_refresh => 1 
});
+is($first_name, "steve", "get() modified first_name [$first_name] by 
refreshing the cache");
+
+$data_rows = $rep->get_rows("test_person", {}, [ "person_id", "age", 
"first_name", "gender", "state" ], {order_by=>["person_id"]});
+is_deeply($data_rows, $data_rows2, "get_rows() confirmed that the cache was 
refreshed");
+$first_name = $rep->get("test_person", 1, "first_name");
+is($first_name, "steve", "get() modified first_name [$first_name] confirming 
that the cache was refreshed");
+
+{
+    my $dbh = $rep->{dbh};
+    $dbh->do("drop table test_person");
+}
+
+exit 0;
+

Reply via email to