diff --git a/t/Shop/Address.pm b/t/Shop/Address.t similarity index 52% rename from t/Shop/Address.pm rename to t/Shop/Address.t index 803b2e91e..89a98d68d 100644 --- a/t/Shop/Address.pm +++ b/t/Shop/Address.t @@ -31,7 +31,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 10; +my $tests = 27; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -125,6 +125,41 @@ ok( $session->id->valid($address->getId), 'Address has a valid GUID'); ####################################################################### ok( $session->id->valid($address->getId), 'Address has a valid GUID'); +is($address->getId, $address->get('addressId'), 'getId is an alias for get addressId'); +cmp_deeply( + $address->get, + { + label => undef, + name => undef, + address1 => undef, + address2 => undef, + address3 => undef, + city => undef, + state => undef, + country => undef, + code => undef, + phoneNumber => undef, + addressId => ignore(), #checked elsewhere + addressBookId => $book->getId, + }, + 'get the whole thing and check a new, blank object' +); + +my $addressGuts = $address->get(); +$addressGuts->{'label'} = 'hacked'; +is($address->get('label'), undef, 'get returns a safe copy of the hash'); + +####################################################################### +# +# update +# +####################################################################### + +$address->update({ label => 'home'}); +is($address->get('label'), 'home', 'update updates the object properties cache'); +$address->update({ address1 => 'Shawshank Prison', 'state' => 'Maine'}); +is($address->get('address1'), 'Shawshank Prison', 'update updates the object properties cache for more than one key'); +is($address->get('state'), 'Maine', 'update updates the object properties cache for more than one key'); ####################################################################### # @@ -132,6 +167,80 @@ ok( $session->id->valid($address->getId), 'Address has a valid GUID'); # ####################################################################### +eval { $address = WebGUI::Shop::Address->new(); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidObject', 'new takes exception to not giving it an address book'); +cmp_deeply( + $e, + methods( + error => 'Need an address book.', + expected => 'WebGUI::Shop::AddressBook', + got => '', + param => ignore, + ), + 'new takes exception to not giving it address book', +); + +eval { $address = WebGUI::Shop::Address->new($session); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidObject', 'new takes exception to not giving it a session variable'); +cmp_deeply( + $e, + methods( + error => 'Need an address book.', + expected => 'WebGUI::Shop::AddressBook', + got => 'WebGUI::Session', + param => ignore, + ), + 'new takes exception to giving it a session variable', +); + +eval { $address = WebGUI::Shop::Address->new($book); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidParam', 'new takes exception to not giving it an address to instanciate'); +cmp_deeply( + $e, + methods( + error => 'Need an addressId.', + param => undef, + ), + 'new takes exception to giving it an address to instanciate', +); + +eval { $address = WebGUI::Shop::Address->new($book, 'neverAnId'); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::ObjectNotFound', 'new takes exception to not giving it a bad address instanciate'); +cmp_deeply( + $e, + methods( + error => 'Address not found.', + id => 'neverAnId', + ), + 'new takes exception to giving it a bad address to instanciate', +); + +TODO: { + local $TODO = 'More tests for new'; + ok(0, 'Make a second address book, add an address to it, then try to call a valid address from the wrong book'); +} + +my $addressCopy = WebGUI::Shop::Address->new($book, $address->getId); +cmp_deeply( + $address, + $addressCopy, + 'new: gets an exact copy of the object from the db. Also checks that update writes to the db correctly.' +); + +####################################################################### +# +# delete +# +####################################################################### + +$address->delete; +my $check = $session->db->quickScalar('select count(*) from address where addressId=?',[$address->getId]); +is( $check, 0, 'delete worked'); + } END: {