2015-12-10 02:39:40 +00:00
# Supporters.t -*- Perl -*-
# Basic unit tests for Supporters.pm
2015-12-30 19:09:47 +00:00
#
# License: AGPLv3-or-later
# Copyright info in COPYRIGHT.md, License details in LICENSE.md with this package.
###############################################################################
2016-12-09 21:44:08 +00:00
# FIXME: Untested things: request holds, and fulfill failure
2015-12-07 01:20:14 +00:00
use strict ;
use warnings ;
2017-11-26 23:04:21 +00:00
use Test::More tests = > 353 ;
2015-12-07 02:56:59 +00:00
use Test::Exception ;
2015-12-30 19:30:22 +00:00
use Sub::Override ;
2015-12-31 00:33:03 +00:00
use File::Temp qw/tempfile/ ;
2015-12-07 02:56:59 +00:00
2015-12-17 03:24:24 +00:00
use Scalar::Util qw( looks_like_number reftype ) ;
2015-12-18 02:49:10 +00:00
use POSIX qw( strftime ) ;
2015-12-21 00:58:42 +00:00
2015-12-18 02:49:10 +00:00
# Yes, this may cause tests to fail if you run them near midnight. :)
2015-12-21 00:39:00 +00:00
my $ today = strftime "%Y-%m-%d" , gmtime ;
2015-12-09 23:44:05 +00:00
2015-12-07 02:28:49 +00:00
= pod
2015-12-10 03:38:22 +00:00
Supporters . t is the basic unit tests for Supporters . pm . It tests the
following things:
= over
= item use command for the module .
2015-12-07 02:28:49 +00:00
= cut
2015-12-10 03:38:22 +00:00
BEGIN { use_ok ( 'Supporters' ) } ;
2015-12-07 02:28:49 +00:00
require 't/CreateTestDB.pl' ;
my $ dbh = get_test_dbh ( ) ;
2015-12-31 00:33:03 +00:00
# Set up test data for ledger-related tests
my ( $ fakeLedgerFH , $ fakeLedgerFile ) = tempfile ( "fakeledgerXXXXXXXX" , UNLINK = > 1 ) ;
print $ fakeLedgerFH << FAKE_LEDGER_TEST_DATA_END ;
2015-12-31 04:11:57 +00:00
Supporters:Annual 2015 - 05 - 04 Whitman - Dick \ $ - 5 .00
2015-12-31 02:59:23 +00:00
Supporters:Monthly 2015 - 05 - 25 Olson - Margaret \ $ - 10 .00
Supporters:Monthly 2015 - 01 - 15 Olson - Margaret \ $ - 10 .00
Supporters:Monthly 2015 - 03 - 17 Olson - Margaret \ $ - 10 .00
2015-12-31 05:26:48 +00:00
Supporters:Annual 2015 - 12 - 04 Harris - Joan \ $ - 120 .00
2015-12-31 02:59:23 +00:00
Supporters:Monthly 2015 - 04 - 20 Olson - Margaret \ $ - 10 .00
2015-12-31 04:11:57 +00:00
Supporters:Match Pledge 2015 - 02 - 26 Whitman - Dick \ $ - 300 .00
2015-12-31 02:59:23 +00:00
Supporters:Monthly 2015 - 02 - 16 Olson - Margaret \ $ - 10 .00
Supporters:Monthly 2015 - 06 - 30 Olson - Margaret \ $ - 10 .00
2015-12-31 05:26:48 +00:00
Supporters:Annual 2015 - 03 - 04 Harris - Joan \ $ - 120 .00
2016-01-19 05:10:47 +00:00
Supporters:Annual 2016 - 01 - 10 \ $ - 120 .00
2015-12-31 00:33:03 +00:00
FAKE_LEDGER_TEST_DATA_END
2015-12-10 03:38:22 +00:00
= item Public - facing methods of the module , as follows:
= over
= item new
= cut
2015-12-17 03:21:01 +00:00
my $ sp ;
dies_ok { $ sp = new Supporters ( undef , "test" ) ; }
"new: dies when dbh is undefined." ;
dies_ok { $ sp = new Supporters ( bless ( { } , "Not::A::Real::Module" ) , "test" ) ; }
"new: dies when dbh is blessed into another module." ;
2015-12-30 20:08:33 +00:00
dies_ok { $ sp = new Supporters ( $ dbh , "testcmd" ) ; }
"new: dies when if the command is a string." ;
dies_ok { $ sp = new Supporters ( $ dbh , [ "testcmd" ] , { } ) ; }
"new: dies when programTypeSearch is an empty hash." ;
dies_ok { $ sp = new Supporters ( $ dbh , [ "testcmd" ] , { monthly = > 'test' , annual = > 'test' , dummy = > 'test' } ) ; }
"new: dies when programTypeSearch has stray value." ;
dies_ok { $ sp = new Supporters ( $ dbh , [ "testcmd" ] , { monthly = > 'test' } ) ; }
"new: dies when programTypeSearch key annual is missing ." ;
dies_ok { $ sp = new Supporters ( $ dbh , [ "testcmd" ] , { annual = > 'test' } ) ; }
"new: dies when programTypeSearch key monthly is missing ." ;
2015-12-31 02:59:41 +00:00
my $ cmd = [ "/bin/cat" , $ fakeLedgerFile ] ;
2015-12-07 01:20:14 +00:00
2015-12-31 04:11:57 +00:00
$ sp = new Supporters ( $ dbh , $ cmd , { monthly = > '^Supporters:Monthly' ,
annual = > '^Supporters:(?:Annual|Match Pledge)' } ) ;
2015-12-07 02:56:59 +00:00
2015-12-31 02:59:41 +00:00
is ( $ dbh , $ sp - > dbh ( ) , "new: verify dbh set" ) ;
is_deeply ( $ sp - > ledgerCmd ( ) , $ cmd , "new: verify ledgerCmd set" ) ;
2015-12-07 02:56:59 +00:00
2015-12-10 03:38:22 +00:00
= item addSupporter
2015-12-07 02:56:59 +00:00
= cut
2015-12-07 03:24:10 +00:00
dies_ok { $ sp - > addSupporter ( { } ) }
"addSupporter: ledger_entity_id required" ;
2015-12-07 03:07:34 +00:00
2015-12-31 01:12:58 +00:00
my $ campbellId ;
lives_ok { $ campbellId = $ sp - > addSupporter ( { ledger_entity_id = > "Campbell-Peter" } ) ; }
2015-12-09 23:44:05 +00:00
"addSupporter: add works for minimal acceptable settings" ;
2015-12-31 01:12:58 +00:00
ok ( ( looks_like_number ( $ campbellId ) and $ campbellId > 0 ) ,
2015-12-09 23:44:05 +00:00
"addSupporter: add works for minimal acceptable settings" ) ;
2015-12-07 03:24:10 +00:00
dies_ok { $ sp - > addSupporter ( { public_ack = > 1 , ledger_entity_id = > "Whitman-Dick" } ) }
"addSupporter: display_name required" ;
2015-12-10 03:52:31 +00:00
my $ drapperId ;
lives_ok { $ drapperId = $ sp - > addSupporter ( { display_name = > "Donald Drapper" ,
2015-12-09 23:44:05 +00:00
public_ack = > 1 , ledger_entity_id = > "Whitman-Dick" } ) ; }
2015-12-07 03:24:10 +00:00
"addSupporter: public_ack set to true with a display_name given" ;
2015-12-07 02:56:59 +00:00
2015-12-31 01:12:58 +00:00
ok ( ( looks_like_number ( $ drapperId ) and $ drapperId > $ campbellId ) ,
2015-12-09 23:44:05 +00:00
"addSupporter: add works with public_ack set to true and a display_name given" ) ;
2015-12-07 01:20:14 +00:00
2015-12-13 20:31:54 +00:00
my $ olsonId ;
lives_ok { $ olsonId = $ sp - > addSupporter ( { display_name = > "Peggy Olson" ,
public_ack = > 0 , ledger_entity_id = > "Olson-Margaret" ,
email_address = > 'olson@example.net' ,
email_address_type = > 'home' } ) ; }
"addSupporter: succeeds with email address" ;
ok ( ( looks_like_number ( $ olsonId ) and $ olsonId > $ drapperId ) ,
"addSupporter: add succeeded with email address added." ) ;
2015-12-30 11:10:07 +00:00
my $ val = $ sp - > dbh ( ) - > selectall_hashref ( "SELECT donor_id, email_address_id " .
"FROM donor_email_address_mapping " .
"WHERE donor_id = " . $ sp - > dbh - > quote ( $ olsonId , 'SQL_INTEGER' ) ,
'donor_id' ) ;
2015-12-15 00:43:12 +00:00
ok ( ( defined $ val and defined $ val - > { $ olsonId } { email_address_id } and $ val - > { $ olsonId } { email_address_id } > 0 ) ,
2017-11-26 23:00:51 +00:00
"addSupporter: email address mapping is created on addSupporter() w/ email address included" ) ;
2015-12-15 00:43:12 +00:00
2015-12-30 13:54:38 +00:00
my $ olsonFirstEmailId = $ val - > { $ olsonId } { email_address_id } ;
2015-12-31 00:51:04 +00:00
my $ sterlingId ;
lives_ok { $ sterlingId = $ sp - > addSupporter ( { display_name = > "Roger Sterling" ,
ledger_entity_id = > "Sterling-Roger" ,
email_address = > 'sterlingjr@example.com' ,
email_address_type = > 'home' } ) }
"addSupporter: succeeds with no public_ack setting specified..." ;
ok ( ( looks_like_number ( $ sterlingId ) and $ sterlingId > $ olsonId ) ,
"addSupporter: ... and return value is sane." ) ;
2015-12-31 05:26:48 +00:00
my $ harrisId ;
lives_ok { $ harrisId = $ sp - > addSupporter ( { ledger_entity_id = > 'Harris-Joan' } ) }
"addSupporter: set up one more in db (use this one for future test types on addSupporter)..." ;
ok ( ( looks_like_number ( $ harrisId ) and $ harrisId > $ sterlingId ) ,
"addSupporter: ... and return value is sane." ) ;
2015-12-31 00:51:04 +00:00
= item getPublicAck
= cut
2015-12-31 01:42:56 +00:00
my $ publicAckVal ;
dies_ok { $ publicAckVal = $ sp - > getPublicAck ( 0 ) ; }
"getPublicAck: fails supporterId invalid" ;
dies_ok { $ publicAckVal = $ sp - > getPublicAck ( "String" ) ; }
"getPublicAck: fails supporterId is string" ;
dies_ok { $ publicAckVal = $ sp - > getPublicAck ( undef ) ; }
"getPublicAck: fails supporterId is undef" ;
2015-12-31 00:51:04 +00:00
# Replace _verifyId() to always return true
2015-12-31 01:02:50 +00:00
my $ overrideSub = Sub::Override - > new ( 'Supporters::_verifyId' = > sub ($$) { return 1 ; } ) ;
2015-12-31 00:51:04 +00:00
dies_ok { my $ ledgerId = $ sp - > getPublicAck ( 0 ) ; }
"getPublicAck: fails when rows are not returned but _verifyId() somehow passed" ;
$ overrideSub - > restore ;
lives_ok { $ publicAckVal = $ sp - > getPublicAck ( $ olsonId ) ; }
"getPublicAck: lives when valid id is given for someone who does not want it..." ;
is ( $ publicAckVal , 0 , "getPublicAck: ...and return value is correct." ) ;
lives_ok { $ publicAckVal = $ sp - > getPublicAck ( $ drapperId ) ; }
"getPublicAck: lives when valid id is given for someone who wants it..." ;
is ( $ publicAckVal , 1 , "getPublicAck: ...and return value is correct." ) ;
lives_ok { $ publicAckVal = $ sp - > getPublicAck ( $ sterlingId ) ; }
"getPublicAck: lives when valid id is given for someone who is undecided..." ;
is ( $ publicAckVal , undef , "getPublicAck: ...and return value is correct." ) ;
2015-12-31 01:19:10 +00:00
= item isSupporter
= cut
my $ isSupporter ;
dies_ok { $ isSupporter = $ sp - > isSupporter ( 0 ) ; }
"isSupporter: fails when rows are not returned but _verifyId() somehow passed" ;
# Replace _verifyId() to always return true
$ overrideSub = Sub::Override - > new ( 'Supporters::_verifyId' = > sub ($$) { return 1 ; } ) ;
dies_ok { my $ ledgerId = $ sp - > isSupporter ( 0 ) ; }
"isSupporter: fails when rows are not returned but _verifyId() somehow passed" ;
$ overrideSub - > restore ;
lives_ok { $ isSupporter = $ sp - > isSupporter ( $ olsonId ) ; }
"isSupporter: lives when valid id..." ;
is ( $ isSupporter , 1 , "isSupporter: ...and return value is correct." ) ;
2015-12-31 01:12:58 +00:00
= item getDisplayName
= cut
my $ displayNameVal ;
dies_ok { $ displayNameVal = $ sp - > getDisplayName ( 0 ) ; }
"getDisplayName: fails when rows are not returned but _verifyId() somehow passed" ;
# Replace _verifyId() to always return true
$ overrideSub = Sub::Override - > new ( 'Supporters::_verifyId' = > sub ($$) { return 1 ; } ) ;
dies_ok { $ displayNameVal = $ sp - > getDisplayName ( 0 ) ; }
"getDisplayName: fails when rows are not returned but _verifyId() somehow passed" ;
$ overrideSub - > restore ;
lives_ok { $ displayNameVal = $ sp - > getDisplayName ( $ olsonId ) ; }
"getDisplayName: lives when valid id is given for someone who does not want it..." ;
is ( $ displayNameVal , "Peggy Olson" , "getDisplayName: ...and return value is correct." ) ;
lives_ok { $ displayNameVal = $ sp - > getDisplayName ( $ drapperId ) ; }
"getDisplayName: lives when valid id is given for someone who wants it..." ;
is ( $ displayNameVal , "Donald Drapper" , "getDisplayName: ...and return value is correct." ) ;
lives_ok { $ displayNameVal = $ sp - > getDisplayName ( $ campbellId ) ; }
"getDisplayName: lives when valid id is given for someone who is undecided..." ;
is ( $ displayNameVal , undef , "getDisplayName: ...and return value is correct." ) ;
2015-12-31 00:51:04 +00:00
2015-12-30 19:30:22 +00:00
= item getLedgerEntityId
= cut
dies_ok { my $ ledgerId = $ sp - > getLedgerEntityId ( 0 ) ; }
"getLedgerEntityId: fails when rows are not returned but _verifyId() somehow passed" ;
# Replace _verifyId() to always return true
2015-12-31 01:02:50 +00:00
$ overrideSub = Sub::Override - > new ( 'Supporters::_verifyId' = > sub ($$) { return 1 ; } ) ;
2015-12-30 19:30:22 +00:00
dies_ok { my $ ledgerId = $ sp - > getLedgerEntityId ( 0 ) ; }
"getLedgerEntityId: fails when rows are not returned but _verifyId() somehow passed" ;
$ overrideSub - > restore ;
my $ olsonLedgerEntity ;
lives_ok { $ olsonLedgerEntity = $ sp - > getLedgerEntityId ( $ olsonId ) ; }
"getLedgerEntityId: lives when valid id is given..." ;
is ( $ olsonLedgerEntity , "Olson-Margaret" , "getLedgerEntityId: ...and return value is correct." ) ;
2015-12-31 01:47:56 +00:00
= item setPublicAck
= cut
dies_ok { $ sp - > setPublicAck ( 0 ) ; } "setPublicAck: fails supporterId invalid" ;
dies_ok { $ sp - > setPublicAck ( "String" ) ; } "setPublicAck: fails supporterId is string" ;
dies_ok { $ sp - > setPublicAck ( undef ) ; } "setPublicAck: fails supporterId is undef" ;
is ( $ sp - > getPublicAck ( $ olsonId ) , 0 , "setPublicAck: 1 failed calls changed nothing." ) ;
is ( $ sp - > getPublicAck ( $ drapperId ) , 1 , "setPublicAck: 1 failed calls changed nothing." ) ;
is ( $ sp - > getPublicAck ( $ sterlingId ) , undef , "setPublicAck: 1 failed calls changed nothing." ) ;
lives_ok { $ sp - > setPublicAck ( $ olsonId , undef ) ; }
"setPublicAck: lives when valid id is given for undefining..." ;
is ( $ sp - > getPublicAck ( $ olsonId ) , undef , "setPublicAck: ...and suceeds in changing value." ) ;
lives_ok { $ sp - > setPublicAck ( $ drapperId , 0 ) ; }
"setPublicAck: lives when valid id is given for off..." ;
is ( $ sp - > getPublicAck ( $ drapperId ) , 0 , "setPublicAck: ...and suceeds in changing value." ) ;
lives_ok { $ sp - > setPublicAck ( $ sterlingId , 1 ) ; }
"setPublicAck: lives when valid id is given for on..." ;
is ( $ sp - > getPublicAck ( $ sterlingId ) , 1 , "setPublicAck: ...and suceeds in changing value." ) ;
2015-12-10 03:38:22 +00:00
= item addEmailAddress
2015-12-10 02:38:07 +00:00
2015-12-10 03:38:22 +00:00
= cut
2015-12-10 02:38:07 +00:00
2015-12-15 00:43:12 +00:00
$ val = $ sp - > dbh ( ) - > selectall_hashref ( "SELECT id, name FROM address_type WHERE name = 'home'" , 'name' ) ;
2015-12-13 20:31:54 +00:00
ok ( ( defined $ val and defined $ val - > { home } { id } and $ val - > { home } { id } > 0 ) ,
2017-11-26 23:00:51 +00:00
"addSupporter/addEmailAddress: emailAddressType was added when new one given to addSupporter" ) ;
2015-12-13 20:31:54 +00:00
my $ emailAddressTypeHomeId = $ val - > { home } { id } ;
2015-12-10 03:50:02 +00:00
dies_ok { $ sp - > addEmailAddress ( undef , 'drapper@example.org' , 'paypal' ) ; }
2015-12-10 03:53:20 +00:00
"addEmailAddress: dies for undefined id" ;
2015-12-10 03:50:02 +00:00
dies_ok { $ sp - > addEmailAddress ( "String" , 'drapper@example.org' , 'paypal' ) ; }
2015-12-10 03:53:20 +00:00
"addEmailAddress: dies for non-numeric id" ;
2015-12-12 02:32:59 +00:00
dies_ok { $ sp - > addEmailAddress ( $ drapperId , undef , 'work' ) }
"addEmailAddress: email address undefined fails" ;
dies_ok { $ sp - > addEmailAddress ( $ drapperId , 'drapper@ex@ample.org' , 'work' ) }
"addEmailAddress: email address with extra @ fails to add." ;
# Verify that the addressType wasn't added when the Email address is invalid
# and the address type did not already exist.
2015-12-13 20:32:36 +00:00
$ val = $ sp - > dbh ( ) - > selectall_hashref ( "SELECT id, name FROM address_type WHERE name = 'work'" , 'name' ) ;
2015-12-12 02:32:59 +00:00
2015-12-13 20:32:36 +00:00
ok ( ( not defined $ val or not defined $ val - > { 'name' } ) ,
2015-12-12 02:32:59 +00:00
"addEmailAddress: type is not added with email address is bad" ) ;
2015-12-10 03:53:20 +00:00
2015-12-30 13:54:38 +00:00
my $ sameOlsonId ;
dies_ok { $ sameOlsonId = $ sp - > addEmailAddress ( $ olsonId , 'olson@example.net' , 'paypal' ) }
"addEmailAddress: fails adding existing email address with mismatched type." ;
lives_ok { $ sameOlsonId = $ sp - > addEmailAddress ( $ olsonId , 'olson@example.net' , 'home' ) }
"addEmailAddress: succeeds when adding email that already exists..." ;
is ( $ sameOlsonId , $ olsonFirstEmailId , "addEmailAddress: ... and returns same id." ) ;
2015-12-12 02:23:24 +00:00
my $ drapperEmailId ;
lives_ok { $ drapperEmailId = $ sp - > addEmailAddress ( $ drapperId , 'drapper@example.org' , 'work' ) }
2015-12-30 13:49:12 +00:00
"addEmailAddress: inserting a valid email address works" ;
2015-12-12 02:23:24 +00:00
ok ( ( looks_like_number ( $ drapperEmailId ) and $ drapperEmailId > 0 ) , "addEmailAddress: id returned is sane." ) ;
2015-12-10 03:38:22 +00:00
2015-12-30 13:49:12 +00:00
my $ olsonEmailId2 ;
dies_ok { $ olsonEmailId2 = $ sp - > addEmailAddress ( $ olsonId , 'drapper@example.org' , 'paypal' ) }
"addEmailAddress: fails when adding the same email address for someone else, but as a different type" ;
my $ drapperEmailId2 ;
lives_ok { $ drapperEmailId2 = $ sp - > addEmailAddress ( $ drapperId , 'everyone@example.net' , 'paypal' ) }
"addEmailAddress: inserting a second valid email address works" ;
ok ( ( looks_like_number ( $ drapperEmailId2 ) and $ drapperEmailId2 > 0 and $ drapperEmailId != $ drapperEmailId2 ) ,
"addEmailAddress: id returned is sane and is not same as previous id." ) ;
lives_ok { $ olsonEmailId2 = $ sp - > addEmailAddress ( $ olsonId , 'everyone@example.net' , 'paypal' ) }
"addEmailAddress: binding known email address to another person works..." ;
ok ( ( looks_like_number ( $ olsonEmailId2 ) and $ olsonEmailId2 > 0 and $ olsonEmailId2 == $ drapperEmailId2 ) ,
"addEmailAddress: ... and id returned is sane and is same." ) ;
2015-12-10 03:48:59 +00:00
= item addAddressType
= cut
2015-12-10 03:56:22 +00:00
# This test cheats a bit -- it assumes that the database is assigning serials starting with 1
2015-12-13 20:31:54 +00:00
ok ( $ sp - > addAddressType ( 'work' ) > $ emailAddressTypeHomeId ,
2015-12-10 03:56:22 +00:00
"addEmailAddress: verify addEmailAddress added the addressType underneath" ) ;
2015-12-10 03:48:59 +00:00
dies_ok { $ sp - > addAddressType ( undef ) ; } "addAddressType: dies for undef" ;
my $ paypalPayerAddressType ;
ok ( $ paypalPayerAddressType = $ sp - > addAddressType ( "paypal payer" ) , "addAddressType: basic add works" ) ;
my $ same ;
ok ( $ same = $ sp - > addAddressType ( "paypal payer" ) , "addAddressType: lookup works" ) ;
ok ( $ same == $ paypalPayerAddressType , "addAddressType: lookup returns same as the basic add" ) ;
2017-11-26 23:04:21 +00:00
= item addEmailError
= cut
# Add an "undeliverable" delivery_error type
2017-12-26 20:32:15 +00:00
$ val = 1 ;
lives_ok { $ val = $ sp - > _lookupDeliveryError ( "undeliverable" ) ; } ,
"_lookupDeliveryError: succeeds for unknown error ..." ;
is ( $ val , undef , "_lookupDeliveryError: ... but returns undef" ) ;
2017-11-26 23:04:21 +00:00
my $ sth = $ sp - > dbh - > prepare ( "INSERT INTO delivery_error(error) VALUES(?)" ) ; $ sth - > execute ( "undeliverable" ) ; $ sth - > finish ;
my $ undeliverableId = $ sp - > dbh - > last_insert_id ( "" , "" , "delivery_error" , "" ) ;
2017-12-26 20:32:15 +00:00
$ val = - 1 ;
lives_ok { $ val = $ sp - > _lookupDeliveryError ( "undeliverable" ) ; } ,
"_lookupDeliveryError: succeeds for known error ..." ;
is ( $ val , $ undeliverable , "_lookupDeliveryError: ... and returns proper id number" ) ;
2017-11-26 23:04:21 +00:00
dies_ok { $ sp - > addEmailError ( undef ) ; }
"addEmailError: undef argument dies." ;
dies_ok { $ sp - > addEmailError ( { } ) ; } "addEmailError: dies if donorId not specified." ;
dies_ok { $ sp - > addEmailError ( { emailAddress = > undef } ) ; } "addEmailError: dies if emailAddress is undef." ;
$ val = 1 ;
lives_ok { $ val = $ sp - > addEmailError ( { emailAddress = > 'nobody@example.com' , errorCode = > 'undeliverable' , dateEncountered = > '2017-11-22' } ) ; }
"addEmailError: succeeds to run if emailAddress not in database with all other valid args...." ;
is ( $ val , undef , "... but returns undef in that situation." ) ;
dies_ok { $ sp - > addEmailError ( { emailAddress = > 'everyone@example.net' , errorCode = > 'invalidErrorcode' ,
dateEncountered = > '2017-11-22' } ) ; }
"addEmailError: dies if errorCode given is invalid." ;
$ val = - 1 ;
lives_ok { $ val = $ sp - > addEmailError ( { emailAddress = > 'everyone@example.net' , errorCode = > 'undeliverable' , dateEncountered = > '2017-11-22' } ) }
"addEmailError: succeeds when all options are valid but comment is missing" ;
is ( $ val > 0 , "addEmailError: ... and returns a value greater than 0." ) ;
$ val = $ sp - > dbh ( ) - > selectrow_hashref ( "SELECT delivery_error_code_id, email_address_id, date_encountered, comments " .
"FROM email_error_log " .
"WHERE email_address_id = " . $ sp - > dbh - > quote ( $ olsonEmailId2 , 'SQL_INTEGER' ) ) ;
ok ( ( defined $ val and defined $ val - > { email_address_id } and $ val - > { date_encountered } eq '2017-11-22'
and not defined $ val - > { comments } and $ val - > { delivery_error_code_id } == $ undeliverableId ) ,
"addSuporter: error log entry created without comment" ) ;
$ val = - 1 ;
lives_ok { $ val = $ sp - > addEmailError ( { emailAddress = > 'drapper@example.org' , errorCode = > 'undeliverable' , dateEncountered = > '2017-11-25' , comments = > "seems he has no email address" } ) }
"addEmailError: succeeds for valid email address and other options, including comment" ;
is ( $ val > 0 , "addEmailError: ... and returns > 0 in that situation." ) ;
$ val = $ sp - > dbh ( ) - > selectrow_hashref ( "SELECT delivery_error_code_id, email_address_id, date_encountered, comments " .
"FROM email_error_log " .
"WHERE email_address_id = " . $ sp - > dbh - > quote ( $ drapperEmailId , 'SQL_INTEGER' ) ) ;
ok ( ( defined $ val and defined $ val - > { email_address_id } and $ val - > { date_encountered } eq '2017-11-25'
and $ val - > { comments } eq "seems he has no email address" and
$ val - > { delivery_error_code_id } == $ undeliverableId ) ,
"addSuporter: error log entry created with comment added" ) ;
= cut
2015-12-17 02:36:47 +00:00
= item addPostalAddress
= cut
dies_ok { $ sp - > addPostalAddress ( undef , "405 Madison Avenue\nNew York, NY 10000\nUSA" , 'office' ) ; }
"addPostalAddress: dies for undefined id" ;
dies_ok { $ sp - > addPostalAddress ( "String" , "405 Madison Avenue\nNew York, NY 10000\nUSA" , 'office' ) ; }
"addPostalAddress: dies for non-numeric id" ;
dies_ok { $ sp - > addPostalAddress ( $ drapperId , undef , 'work' ) }
"addPostalAddress: postal address undefined fails" ;
# Verify that the addressType wasn't added when the Email address is invalid
# and the address type did not already exist.
$ val = $ sp - > dbh ( ) - > selectall_hashref ( "SELECT id, name FROM address_type WHERE name = 'office'" , 'name' ) ;
ok ( ( not defined $ val or not defined $ val - > { 'name' } ) ,
"addPostalAddress: type is not added when other input paramaters are invalid" ) ;
my $ drapperPostalId ;
2015-12-17 02:42:59 +00:00
lives_ok { $ drapperPostalId = $ sp - > addPostalAddress ( $ drapperId ,
2015-12-17 02:36:47 +00:00
"405 Madison Avenue\nNew York, NY 10000\nUSA" , 'office' ) ; }
"addPostalAddress: addPostalAddress of a valid formatted_address works." ;
ok ( ( looks_like_number ( $ drapperPostalId ) and $ drapperPostalId > 0 ) , "addPostalAddress: id returned is sane." ) ;
2015-12-17 02:25:07 +00:00
= item addRequestType / getRequestType
2015-12-13 20:16:14 +00:00
= cut
dies_ok { $ sp - > addRequestType ( undef ) ; }
"addRequestType: undef argument dies." ;
2015-12-13 21:04:27 +00:00
my $ tShirt0RequestTypeId ;
2015-12-13 20:16:14 +00:00
ok ( ( not defined $ sp - > getRequestType ( 't-shirt-0' ) ) , "getRequestType: returns undef when not found" ) ;
2015-12-13 21:04:27 +00:00
lives_ok { $ tShirt0RequestTypeId = $ sp - > addRequestType ( 't-shirt-0' ) ; }
2015-12-13 20:16:14 +00:00
"addRequestType: succeeds on add" ;
2015-12-13 21:04:27 +00:00
ok ( ( defined $ tShirt0RequestTypeId and looks_like_number ( $ tShirt0RequestTypeId ) and $ tShirt0RequestTypeId > 0 ) ,
2015-12-13 20:16:14 +00:00
"addRequestType: id is a number" ) ;
2016-03-11 18:58:05 +00:00
my @ allRequestsList = $ sp - > getRequestType ( ) ;
is_deeply ( \ @ allRequestsList , [ 't-shirt-0' ] , "getRequestType: no argument returns full list of request types (1)" ) ;
2015-12-13 20:16:14 +00:00
my $ testSameRequestType ;
lives_ok { $ testSameRequestType = $ sp - > addRequestType ( 't-shirt-0' ) ; }
"addRequestType: succeeds on add when type already exists" ;
2015-12-13 21:04:27 +00:00
is $ tShirt0RequestTypeId , $ testSameRequestType ,
2015-12-13 20:16:14 +00:00
"addRequestType: lookup first of existing request type before adding." ;
2015-12-10 03:48:59 +00:00
2015-12-13 21:04:27 +00:00
= item addRequestConfigurations
= cut
dies_ok { $ sp - > addRequestConfigurations ( undef , undef ) ; } "addRequestConfigurations: undef type dies" ;
is_deeply ( { $ tShirt0RequestTypeId = > { } } ,
$ sp - > addRequestConfigurations ( 't-shirt-0' ) ,
"addRequestConfigurations: existing requestType with no configuration yields same" ) ;
2015-12-13 21:28:38 +00:00
my @ sizeList = qw/LadiesS LadiesM LadiesL LadiesXL MenS MenM MenL MenXL Men2XL/ ;
my $ tShirt0Data ;
2015-12-17 04:16:26 +00:00
dies_ok { $ sp - > addRequestConfigurations ( 't-shirt-1' , [ @ sizeList , 'Men2XL' ] ) }
2015-12-13 22:06:22 +00:00
"addRequestConfigurations: dies with duplicate items on configuration list." ;
2015-12-21 02:49:52 +00:00
is ( $ sp - > { __NESTED_TRANSACTION_COUNTER__ } , 0 , "addRequestConfigurations: assure proper beginWork/commit matching." ) ;
2015-12-17 04:16:26 +00:00
is_deeply ( $ sp - > getRequestConfigurations ( 't-shirt-1' ) , undef ,
"addRequestConfigurations/getRequestConfigurations: add fails with undefined configuration list" ) ;
2015-12-13 22:06:22 +00:00
2015-12-13 21:37:52 +00:00
lives_ok { $ tShirt0Data = $ sp - > addRequestConfigurations ( 't-shirt-0' , \ @ sizeList ) }
2015-12-13 21:28:38 +00:00
"addRequestConfigurations: existing requestType with configuration runs." ;
is ( keys % { $ tShirt0Data } , ( $ tShirt0RequestTypeId ) ,
"addRequestConfigurations: reuses same requestTypeId on add of configurations" ) ;
2015-12-21 02:49:52 +00:00
is ( $ sp - > { __NESTED_TRANSACTION_COUNTER__ } , 0 , "addRequestConfigurations: assure proper beginWork/commit matching." ) ;
2015-12-13 21:37:52 +00:00
my $ cnt = 0 ;
2015-12-13 21:28:38 +00:00
foreach my $ size ( @ sizeList ) {
ok ( ( defined $ tShirt0Data - > { $ tShirt0RequestTypeId } { $ size } and
looks_like_number ( $ tShirt0Data - > { $ tShirt0RequestTypeId } { $ size } ) and
$ tShirt0Data - > { $ tShirt0RequestTypeId } { $ size } > 0 ) ,
sprintf "addRequestConfigurations: item %d added correctly" , $ cnt + + ) ;
}
2015-12-13 22:06:22 +00:00
2015-12-18 02:49:10 +00:00
= item addRequest
= cut
2015-12-30 11:13:41 +00:00
dies_ok { $ sp - > addRequest ( { } ) ; } "addRequest: dies if donorId not specified." ;
2015-12-18 02:49:10 +00:00
2015-12-30 11:13:41 +00:00
dies_ok { $ sp - > addRequest ( { donorId = > $ drapperId } ) ; }
2015-12-18 02:49:10 +00:00
"addRequest: dies if requestTypeId / requestType not specified." ;
2015-12-30 11:13:41 +00:00
dies_ok { $ sp - > addRequest ( { donorId = > 0 , requestTypeId = > $ tShirt0RequestTypeId } ) ; }
"addRequest: dies if donorId invalid." ;
2015-12-18 02:49:10 +00:00
2015-12-30 11:13:41 +00:00
dies_ok { $ sp - > addRequest ( { donorId = > $ drapperId , requestTypeId = > 0 } ) ; }
2015-12-18 02:49:10 +00:00
"addRequest: dies if requestTypeId invalid." ;
2015-12-21 02:49:52 +00:00
is ( $ sp - > { __NESTED_TRANSACTION_COUNTER__ } , 0 , "addRequest: assure proper beginWork/commit matching." ) ;
2015-12-18 02:49:10 +00:00
my $ emailListRequestId ;
lives_ok { $ emailListRequestId =
2015-12-30 11:13:41 +00:00
$ sp - > addRequest ( { donorId = > $ drapperId , requestType = > "join-announce-email-list" } ) ; }
2015-12-18 02:49:10 +00:00
"addRequest: succeeds with a requestType but no configuration parameter." ;
ok ( ( defined $ emailListRequestId and looks_like_number ( $ emailListRequestId ) and $ emailListRequestId > 0 ) ,
"addRequest: id returned on successful addRequest() is a number" ) ;
2015-12-20 20:45:15 +00:00
my $ joinEmailListRequestId = $ sp - > getRequestType ( "join-announce-email-list" ) ;
ok ( ( defined $ joinEmailListRequestId and looks_like_number ( $ joinEmailListRequestId ) and $ joinEmailListRequestId > 0 ) ,
2015-12-18 02:49:10 +00:00
"addRequest: underlying call to addRequestType works properly, per getRequestType" ) ;
2016-03-11 18:58:05 +00:00
@ allRequestsList = $ sp - > getRequestType ( ) ;
is_deeply ( \ @ allRequestsList , [ 't-shirt-0' , 'join-announce-email-list' ] ,
"getRequestType: no argument returns full list of request types (2)" ) ;
2015-12-20 21:30:33 +00:00
my $ tshirtSmallRequestId ;
lives_ok { $ tshirtSmallRequestId =
2015-12-30 11:13:41 +00:00
$ sp - > addRequest ( { donorId = > $ drapperId , requestType = > "t-shirt-small-only" ,
2015-12-18 02:49:10 +00:00
requestConfiguration = > 'Small' ,
2015-12-20 23:15:39 +00:00
notes = > 'he probably needs a larger size but this shirt has none' } ) ; }
2015-12-18 02:49:10 +00:00
"addRequest: succeeds with a requestType and requestConfiguration and a note." ;
2015-12-20 21:30:33 +00:00
ok ( ( defined $ tshirtSmallRequestId and looks_like_number ( $ tshirtSmallRequestId ) and $ tshirtSmallRequestId > 0 ) ,
"addRequest: successful call returns an integer id." ) ;
2016-03-11 18:58:05 +00:00
@ allRequestsList = $ sp - > getRequestType ( ) ;
is_deeply ( \ @ allRequestsList , [ 't-shirt-0' , 'join-announce-email-list' , 't-shirt-small-only' ] ,
"getRequestType: no argument returns full list of request types (3)" ) ;
2015-12-20 21:30:33 +00:00
my $ tShirt0RequestId ;
2016-03-11 18:58:05 +00:00
2015-12-20 21:30:33 +00:00
lives_ok { $ tShirt0RequestId =
2015-12-30 11:13:41 +00:00
$ sp - > addRequest ( { donorId = > $ drapperId , requestTypeId = > $ tShirt0RequestTypeId ,
2015-12-18 02:49:10 +00:00
requestConfigurationId = > $ tShirt0Data - > { $ tShirt0RequestTypeId } { 'MenL' } } ) ; }
"addRequest: succeeds with a requestTypeId and requestConfigurationId with no a note." ;
2015-12-20 21:30:33 +00:00
ok ( ( defined $ tShirt0RequestId and looks_like_number ( $ tShirt0RequestId ) and $ tShirt0RequestId > 0 ) ,
"addRequest: another successful call returns an integer id." ) ;
2016-01-20 01:49:20 +00:00
my $ olsonTShirtRequest ;
lives_ok { $ olsonTShirtRequest =
$ sp - > addRequest ( { donorId = > $ olsonId , requestTypeId = > $ tShirt0RequestTypeId ,
requestConfigurationId = > $ tShirt0Data - > { $ tShirt0RequestTypeId } { 'LadiesXL' } } ) ; }
"addRequest: different donor succeeds with a requestTypeId and requestConfigurationId with no a note...." ;
ok ( ( defined $ olsonTShirtRequest and looks_like_number ( $ olsonTShirtRequest ) and $ olsonTShirtRequest > 0
and $ olsonTShirtRequest != $ tShirt0RequestTypeId and $ olsonTShirtRequest != $ tshirtSmallRequestId ) ,
"addRequest: ... and successful call returns an integer id that's different from others." ) ;
2016-01-19 05:34:03 +00:00
= item holdRequest
= cut
2016-01-20 01:49:20 +00:00
my $ drapperTShirt0HoldId ;
2016-01-19 05:34:03 +00:00
my $ newHoldId ;
2016-01-20 01:49:20 +00:00
dies_ok { $ drapperTShirt0HoldId = $ sp - > holdRequest ( requestType = > "t-shirt-0" , who = > 'joe' ,
2017-01-14 22:22:40 +00:00
heldBecause = > "will see him soon and give t-shirt in person" ) ; }
2016-01-19 05:34:03 +00:00
"holdRequest: dies if donorId not specified" ;
2016-01-20 01:49:20 +00:00
dies_ok { $ drapperTShirt0HoldId = $ sp - > holdRequest ( donorId = > $ drapperId + 1000 ,
2016-01-19 05:34:03 +00:00
requestType = > "t-shirt-0" , who = > 'joe' ,
2017-01-14 22:22:40 +00:00
heldBecause = > "will see him soon and give t-shirt in person" ) ; }
2016-01-19 05:34:03 +00:00
"holdRequest: dies if donorId not found in database" ;
2016-01-20 01:49:20 +00:00
dies_ok { $ drapperTShirt0HoldId = $ sp - > holdRequest ( donorId = > $ drapperId , who = > 'joe' ,
2017-01-14 22:22:40 +00:00
heldBecause = > "in-person delivery" ) ; }
2016-01-19 05:34:03 +00:00
"holdRequest: dies if requestType not specified" ;
2016-01-20 01:49:20 +00:00
dies_ok { $ drapperTShirt0HoldId = $ sp - > holdRequest ( { donorId = > $ drapperId ,
2016-01-19 05:34:03 +00:00
requestType = > "t-shirt-0" ,
2017-01-14 22:22:40 +00:00
heldBecause = > "in-person delivery" } ) ; }
2016-01-19 05:34:03 +00:00
"holdRequest: dies if who not specified" ;
2016-01-20 01:49:20 +00:00
lives_ok { $ drapperTShirt0HoldId = $ sp - > holdRequest ( { donorId = > $ drapperId ,
2017-01-14 22:29:05 +00:00
requestType = > "t-shirt-0" , who = > 'joe' , holdReleaseDate = > '9999-12-31' ,
2017-01-15 18:50:34 +00:00
heldBecause = > "in-person delivery planned" } ) ; }
2016-01-19 05:34:03 +00:00
"holdRequest: succeeds for existing request..." ;
2016-01-20 01:49:20 +00:00
ok ( ( defined $ drapperTShirt0HoldId and looks_like_number ( $ drapperTShirt0HoldId ) and $ drapperTShirt0HoldId > 0 ) ,
2016-01-19 05:34:03 +00:00
"holdRequest: ... and id returned on successful holdRequest() is a number" ) ;
2017-01-14 22:29:05 +00:00
lives_ok { $ val = $ sp - > dbh ( ) - > selectall_hashref ( "SELECT id, hold_date, release_date, who, request_id, why FROM request_hold" , 'id' ) ; }
2016-01-19 05:34:03 +00:00
"holdRequest: sql command in database for entry succeeds." ;
2017-01-14 22:29:05 +00:00
is_deeply ( $ val , { $ drapperTShirt0HoldId = > { id = > $ drapperTShirt0HoldId , hold_date = > $ today ,
release_date = > '9999-12-31' ,
2017-01-15 18:50:34 +00:00
why = > 'in-person delivery planned' , who = > 'joe' ,
2016-01-20 01:49:20 +00:00
request_id = > $ tShirt0RequestId } } ,
2017-01-14 22:29:05 +00:00
"holdRequest: database entry from successful return is correct" ) ;
2016-01-19 05:34:03 +00:00
my $ badHold ;
2017-01-14 22:43:01 +00:00
lives_ok { $ badHold = $ sp - > holdRequest ( { donorId = > $ drapperId , who = > 'john' , holdReleaseDate = > '1983-01-05' ,
2016-01-19 05:34:03 +00:00
requestType = > "does-not-exist" ,
2017-01-14 22:22:40 +00:00
heldBecause = > "in-person delivery" } ) ; }
2016-01-19 05:34:03 +00:00
"holdRequest: attempt to hold a request never made does not die..." ;
ok ( ( not defined $ badHold ) ,
"holdRequest: ... but, rather, returns undef." ) ;
is ( $ sp - > getRequestType ( "does-not-exist" ) , undef ,
"holdRequest: requestType not created when holdRequest fails." ) ;
2016-01-20 01:49:20 +00:00
my $ reHoldId ;
2016-01-19 05:34:03 +00:00
2017-01-15 03:41:57 +00:00
# FIXME: Do the following two tests really exhibit the behavior we actually
# want? The API caller might be receiving unexpected results here,
# because as the two tests below show, it's possible, when attempting
# to hold a request that is already held, that you're returned an id
# for a hold that has different details (other than the requestType,
# of course).
lives_ok { $ reHoldId = $ sp - > holdRequest ( { donorId = > $ drapperId , holdReleaseDate = > '2112-05-15' ,
2016-01-19 05:34:03 +00:00
requestType = > "t-shirt-0" , who = > 'peggy' ,
2017-01-14 22:22:40 +00:00
heldBecause = > "will leave in his office." } ) ; }
2017-01-15 03:41:57 +00:00
"holdRequest: attempt to hold an already-held request lives ..." ;
2016-01-19 05:34:03 +00:00
2017-01-15 03:41:57 +00:00
is_deeply ( $ reHoldId , $ drapperTShirt0HoldId , "holdRequest: ... but returns the id of the old hold request." ) ;
2016-01-19 05:34:03 +00:00
my $ holdRequest ;
2017-01-15 03:41:57 +00:00
2017-01-14 22:43:01 +00:00
lives_ok { $ newHoldId = $ sp - > holdRequest ( { donorId = > $ olsonId , holdReleaseDate = > '2048-05-15' ,
2016-01-19 05:34:03 +00:00
requestTypeId = > $ tShirt0RequestTypeId , who = > 'john' ,
2017-01-14 22:22:40 +00:00
heldBecause = > "will delivery at conference" } ) ; }
2016-01-19 05:34:03 +00:00
"holdRequest: succeeds for existing request, using requestTypeId" ;
2016-01-20 01:49:20 +00:00
ok ( ( defined $ newHoldId and looks_like_number ( $ newHoldId ) and $ newHoldId > 0 and ( $ newHoldId != $ drapperTShirt0HoldId ) ) ,
2016-01-19 05:34:03 +00:00
"holdRequest: id returned on successful holdRequest() is a number and is not the one returned by previous" ) ;
2015-12-20 21:30:33 +00:00
2015-12-20 22:01:15 +00:00
= item fulfillRequest
2015-12-18 02:49:10 +00:00
= cut
2015-12-20 22:01:15 +00:00
my $ fulfillRequestId ;
2015-12-18 02:49:10 +00:00
2015-12-20 21:36:56 +00:00
2015-12-20 22:01:15 +00:00
dies_ok { $ fulfillRequestId = $ sp - > fulfillRequest ( { requestType = > "t-shirt-small-only" , who = > 'joe' ,
2015-12-20 21:36:56 +00:00
how = > "in-person delivery" } ) ; }
2015-12-30 11:13:41 +00:00
"fulfillRequest: dies if donorId not specified" ;
2015-12-20 21:36:56 +00:00
2015-12-30 11:13:41 +00:00
dies_ok { $ fulfillRequestId = $ sp - > fulfillRequest ( { donorId = > $ drapperId + 1000 ,
2015-12-20 21:46:45 +00:00
requestType = > "t-shirt-small-only" , who = > 'joe' ,
how = > "in-person delivery" } ) ; }
2015-12-30 11:13:41 +00:00
"fulfillRequest: dies if donorId not found in database" ;
2015-12-20 21:46:45 +00:00
2015-12-30 11:13:41 +00:00
dies_ok { $ fulfillRequestId = $ sp - > fulfillRequest ( { donorId = > $ drapperId , who = > 'joe' ,
2015-12-20 21:36:56 +00:00
how = > "in-person delivery" } ) ; }
2015-12-20 22:01:15 +00:00
"fulfillRequest: dies if requestType not specified" ;
2015-12-20 21:36:56 +00:00
2015-12-30 11:13:41 +00:00
dies_ok { $ fulfillRequestId = $ sp - > fulfillRequest ( { donorId = > $ drapperId ,
2015-12-20 21:36:56 +00:00
requestType = > "t-shirt-small-only" ,
how = > "in-person delivery" } ) ; }
2015-12-21 00:59:29 +00:00
"fulfillRequest: dies if who not specified" ;
2015-12-20 21:36:56 +00:00
2016-01-19 05:13:21 +00:00
my $ req ;
lives_ok { $ req = $ sp - > getRequest ( { donorId = > $ drapperId , requestType = > "t-shirt-small-only" } ) ; }
"getRequest: success after failed fulfillRequest attempts..." ;
is ( $ req - > { requestType } , "t-shirt-small-only" , "getRequest: ... with correct type" ) ;
is ( $ req - > { requestDate } , $ today , "getRequest: ... and correct request date." ) ;
is ( $ req - > { fulfillDate } , undef , "getRequest: ... but no fulfillDate." ) ;
2015-12-30 11:13:41 +00:00
lives_ok { $ fulfillRequestId = $ sp - > fulfillRequest ( { donorId = > $ drapperId ,
2015-12-18 02:49:10 +00:00
requestType = > "t-shirt-small-only" , who = > 'joe' ,
how = > "in-person delivery" } ) ; }
2015-12-20 22:01:15 +00:00
"fulfillRequest: succeeds for existing request" ;
2015-12-18 02:49:10 +00:00
2015-12-20 22:01:15 +00:00
ok ( ( defined $ fulfillRequestId and looks_like_number ( $ fulfillRequestId ) and $ fulfillRequestId > 0 ) ,
"fulfillRequest: id returned on successful fulfillRequest() is a number" ) ;
2015-12-20 21:36:56 +00:00
lives_ok { $ val = $ sp - > dbh ( ) - > selectall_hashref ( "SELECT id, date, who, how, request_id FROM fulfillment" , 'id' ) ; }
2015-12-20 22:01:15 +00:00
"fulfillRequest: sql command in database for entry succeeds." ;
is_deeply ( $ val , { $ fulfillRequestId = > { id = > $ fulfillRequestId , date = > $ today ,
2015-12-20 21:36:56 +00:00
how = > 'in-person delivery' , who = > 'joe' ,
request_id = > $ tshirtSmallRequestId } } ,
2015-12-21 00:59:29 +00:00
"fulfillRequest: databse entry from successful return is correct" ) ;
2015-12-20 21:36:56 +00:00
my $ badFR ;
2015-12-30 11:13:41 +00:00
lives_ok { $ badFR = $ sp - > fulfillRequest ( { donorId = > $ drapperId , who = > 'john' ,
2015-12-20 21:36:56 +00:00
requestType = > "does-not-exist" ,
how = > "in-person delivery" } ) ; }
2015-12-20 22:01:15 +00:00
"fulfillRequest: attempt to fulfill a request never made does not die..." ;
2015-12-20 21:36:56 +00:00
ok ( ( not defined $ badFR ) ,
2015-12-20 22:01:15 +00:00
"fulfillRequest: ... but, rather, returns undef." ) ;
2015-12-20 21:36:56 +00:00
is ( $ sp - > getRequestType ( "does-not-exist" ) , undef ,
2015-12-20 22:01:15 +00:00
"fulfillRequest: requestType not created when fulfillRequest fails." ) ;
2015-12-20 21:36:56 +00:00
2015-12-18 02:49:10 +00:00
2015-12-21 01:07:44 +00:00
my $ lookedUpFulfillmentId ;
2015-12-20 21:50:06 +00:00
2015-12-30 11:13:41 +00:00
lives_ok { $ lookedUpFulfillmentId = $ sp - > fulfillRequest ( { donorId = > $ drapperId ,
2015-12-20 21:50:06 +00:00
requestType = > "t-shirt-small-only" , who = > 'peggy' ,
how = > "left in his office." } ) ; }
2015-12-20 22:01:15 +00:00
"fulfillRequest: attempt to fulfill an already-fulfill request does not die ..." ;
2015-12-20 21:50:06 +00:00
2015-12-21 01:07:44 +00:00
is ( $ lookedUpFulfillmentId , $ fulfillRequestId ,
"fulfillRequest: ... but, rather, returns the same value from the previous fulfillRequest() call." ) ;
2015-12-20 21:50:06 +00:00
2015-12-30 12:12:21 +00:00
my $ newFRID ;
lives_ok { $ newFRID = $ sp - > fulfillRequest ( { donorId = > $ drapperId ,
requestTypeId = > $ tShirt0RequestTypeId , who = > 'john' ,
how = > "mailed" } ) ; }
2016-01-20 01:49:20 +00:00
"fulfillRequest: returns properly for an existing request, using requestTypeId for lookup, when the request his held..." ;
2015-12-30 12:12:21 +00:00
2016-01-20 01:49:20 +00:00
is ( $ newFRID , undef , "fulfillRequest: .... but undef is returned when attempting to fulfill a held request." ) ;
2015-12-30 12:12:21 +00:00
2015-12-18 02:49:10 +00:00
= item getRequest
= cut
2015-12-30 11:58:48 +00:00
dies_ok { $ sp - > getRequest ( { } ) ; } "getRequest: dies if donorId not specified." ;
2015-12-18 02:49:10 +00:00
2015-12-30 11:58:48 +00:00
dies_ok { $ sp - > getRequest ( { donorId = > 0 , requestType = > "t-shirt-small-only" } ) ; } "getRequest: dies if donorId invalid." ;
2015-12-18 02:49:10 +00:00
2015-12-30 11:58:48 +00:00
dies_ok { $ sp - > getRequest ( { donorId = > $ drapperId , requestType = > undef } ) ; }
2015-12-20 23:13:42 +00:00
"getRequest: dies if requestType not specified." ;
2015-12-18 02:49:10 +00:00
my $ tt ;
2015-12-30 11:58:48 +00:00
lives_ok { $ tt = $ sp - > getRequest ( { donorId = > $ drapperId , requestType = > 'this-one-is-not-there' } ) ; }
2015-12-18 02:49:10 +00:00
"getRequest: returns normally with non-existent request." ;
is ( $ tt , undef , "getRequest: returns undef for valid supporter and on-existent request." ) ;
2015-12-30 11:58:48 +00:00
lives_ok { $ tt = $ sp - > getRequest ( { donorId = > $ drapperId , requestType = > 't-shirt-small-only' } ) ; }
2015-12-30 12:05:21 +00:00
"getRequest: succeeds with valid parameters, using requestType." ;
2015-12-18 02:49:10 +00:00
is ( $ tt - > { requestType } , 't-shirt-small-only' , "getRequest: requestType is correct." ) ;
2015-12-20 22:01:15 +00:00
is ( $ tt - > { fulfillDate } , $ today , "getRequest: fulfilled request is today." ) ;
2015-12-18 02:49:10 +00:00
is ( $ tt - > { requestDate } , $ today , "getRequest: request date is today." ) ;
is ( $ tt - > { requestConfiguration } , 'Small' , "getRequest: configuration is correct." ) ;
is ( $ tt - > { notes } , 'he probably needs a larger size but this shirt has none' ,
"getRequest: notes are correct." ) ;
2016-01-19 05:29:45 +00:00
lives_ok { $ tt = $ sp - > getRequest ( { donorId = > $ drapperId , requestType = > 't-shirt-small-only' , ignoreFulfilledRequests = > 1 } ) ; }
"getRequest: succeeds for lookup criteria that are known to return nothing ...." ;
is ( $ tt , undef , 'getRequest: .... and undef is indeed returned' ) ;
2015-12-30 12:04:48 +00:00
lives_ok { $ tt = $ sp - > getRequest ( { donorId = > $ drapperId , requestTypeId = > $ tShirt0RequestTypeId } ) ; }
"getRequest: succeeds with valid parameters, using requestTypeId." ;
2015-12-18 02:49:10 +00:00
is ( $ tt - > { requestType } , 't-shirt-0' , "getRequest: requestType is correct." ) ;
is ( $ tt - > { requestDate } , $ today , "getRequest: request date is today." ) ;
is ( $ tt - > { requestConfiguration } , 'MenL' , "getRequest: configuration is correct." ) ;
2017-01-15 18:50:34 +00:00
is ( $ tt - > { holdReleaseDate } , '9999-12-31' , "getRequest: releaseDate is correct." ) ;
2016-01-20 01:49:20 +00:00
is ( $ tt - > { holdDate } , $ today , "getRequest: holdDate is correct." ) ;
is ( $ tt - > { holder } , 'joe' , "getRequest: holder is correct." ) ;
is ( $ tt - > { heldBecause } , 'in-person delivery planned' , "getRequest: heldBecause is correct." ) ;
2015-12-18 02:49:10 +00:00
is ( $ tt - > { notes } , undef , "getRequest: notes are undef when null in database." ) ;
2016-01-19 05:34:03 +00:00
lives_ok { $ tt = $ sp - > getRequest ( { donorId = > $ drapperId , requestTypeId = > $ tShirt0RequestTypeId ,
ignoreHeldRequests = > 1 } ) ; }
"getRequest: succeeds for lookup criteria, including ignoreHeldRequests, that are known to return nothing ...." ;
is ( $ tt , undef , 'getRequest: .... and undef is indeed returned' ) ;
2015-12-30 11:58:48 +00:00
lives_ok { $ tt = $ sp - > getRequest ( { donorId = > $ drapperId , requestType = > "join-announce-email-list" } ) ; }
2015-12-18 02:49:10 +00:00
"getRequest: succeeds with valid parameters." ;
is ( $ tt - > { requestType } , "join-announce-email-list" , "getRequest: requestType is correct." ) ;
is ( $ tt - > { requestDate } , $ today , "getRequest: request date is today." ) ;
is ( $ tt - > { requestConfiguration } , undef , "getRequest: configuration is undefined when there is none." ) ;
is ( $ tt - > { notes } , undef , "getRequest: notes are undef when null in database." ) ;
2015-12-13 21:04:27 +00:00
2016-01-20 01:49:20 +00:00
= item releaseRequestHold
= cut
my $ releasedHoldId ;
2017-11-26 22:32:17 +00:00
lives_ok { $ releasedHoldId = $ sp - > releaseRequestHold ( { donorId = > $ drapperId , requestType = > 't-shirt-0' } ) ; }
2017-01-15 19:05:51 +00:00
"releaseRequestHold: release of a known held request succeeds..." ;
2016-01-20 01:49:20 +00:00
is ( $ releasedHoldId , $ drapperTShirt0HoldId , "releaseRequestHold: ... & returns same hold id as holdRequest() call did" ) ;
2017-11-26 22:32:17 +00:00
lives_ok { $ req = $ sp - > getRequest ( { donorId = > $ drapperId , requestType = > 't-shirt-0' } ) }
"releaseRequestHold: lookup of request after release succeeds...." ;
is ( $ req - > { holdReleaseDate } , $ today , "... and the release date is today." ) ;
lives_ok { $ releasedHoldId = $ sp - > releaseRequestHold ( { donorId = > $ drapperId , requestType = > 't-shirt-0' } ) ; }
"releaseRequestHold: release again of the same a hold request also succeeds..." ;
is ( $ releasedHoldId , $ drapperTShirt0HoldId , "releaseRequestHold: ... & also returns same hold id as holdRequest() call did" ) ;
lives_ok { $ req = $ sp - > getRequest ( { donorId = > $ drapperId , requestType = > 't-shirt-0' } ) }
"releaseRequestHold: lookup of request after second release succeeds...." ;
is ( $ req - > { holdReleaseDate } , $ today , "... and the release date is still set to today." ) ;
2016-01-20 01:49:20 +00:00
2017-01-15 19:05:51 +00:00
2016-01-20 01:49:20 +00:00
lives_ok { $ newFRID = $ sp - > fulfillRequest ( { donorId = > $ drapperId ,
requestTypeId = > $ tShirt0RequestTypeId , who = > 'john' ,
how = > "mailed" } ) ; }
"fulfillRequest: succeeds once the request is no longer on hold..." ;
2017-11-26 22:30:44 +00:00
ok ( ( defined $ newFRID and looks_like_number ( $ newFRID ) and $ newFRID > 0 and ( $ newFRID != $ fulfillRequestId ) ) ,
2016-01-20 01:49:20 +00:00
"....id returned on successful fulfillRequest() is a number and is not the one returned by previous" ) ;
lives_ok { $ tt = $ sp - > getRequest ( { donorId = > $ drapperId , requestTypeId = > $ tShirt0RequestTypeId } ) ; }
"getRequest: succeeds with valid parameters, using requestTypeId." ;
is ( $ tt - > { requestType } , 't-shirt-0' , "getRequest: requestType is correct." ) ;
is ( $ tt - > { requestDate } , $ today , "getRequest: request date is today." ) ;
is ( $ tt - > { requestConfiguration } , 'MenL' , "getRequest: configuration is correct." ) ;
is ( $ tt - > { holdReleaseDate } , $ today , "getRequest: holdReleaseDate is correct." ) ;
is ( $ tt - > { holdDate } , $ today , "getRequest: holdDate is correct." ) ;
is ( $ tt - > { holder } , 'joe' , "getRequest: holder is correct." ) ;
is ( $ tt - > { heldBecause } , 'in-person delivery planned' , "getRequest: heldBecause is correct." ) ;
is ( $ tt - > { fulfillDate } , $ today , "getRequest: fulfilled request is today." ) ;
is ( $ tt - > { notes } , undef , "getRequest: notes are undef when null in database." ) ;
2015-12-13 20:50:08 +00:00
= item getRequestConfigurations
= cut
2015-12-18 02:49:10 +00:00
my $ tShirtSmallOnlyRequestId ;
lives_ok { $ tShirtSmallOnlyRequestId = $ sp - > getRequestType ( 't-shirt-small-only' ) ; }
"addRequest: added request type" ;
my $ tShirtSmallOnlyData = $ sp - > getRequestConfigurations ( 't-shirt-small-only' ) ;
is ( scalar keys % { $ tShirtSmallOnlyData - > { $ tShirtSmallOnlyRequestId } } , 1 ,
"addRequest: just one configuration added correctly" ) ;
ok ( ( defined $ tShirtSmallOnlyData - > { $ tShirtSmallOnlyRequestId } { 'Small' } and
2015-12-21 01:10:18 +00:00
looks_like_number ( $ tShirtSmallOnlyData - > { $ tShirtSmallOnlyRequestId } { 'Small' } ) and
$ tShirtSmallOnlyData - > { $ tShirtSmallOnlyRequestId } { 'Small' } > 0 ) ,
2015-12-18 02:49:10 +00:00
"addRequest: configuration added correctly" ) ;
2015-12-13 20:50:08 +00:00
is undef , $ sp - > getRequestConfigurations ( undef ) , "getRequestConfigurations: undef type returns undef" ;
is undef , $ sp - > getRequestConfigurations ( 'Hae2Ohlu' ) , "getRequestConfigurations: non-existent type returns undef" ;
2015-12-13 22:06:22 +00:00
is_deeply $ tShirt0Data ,
$ sp - > getRequestConfigurations ( 't-shirt-0' ) ,
"getRequestConfigurations: lookup of previously added items is same" ;
2015-12-31 09:59:50 +00:00
2015-12-21 01:57:30 +00:00
= item setPreferredEmailAddress / getPreferredEmailAddress
= cut
dies_ok { $ sp - > setPreferredEmailAddress ( undef , 'drapper@example.org' ) ; }
"setPreferredEmailAddress: dies for undefined id" ;
dies_ok { $ sp - > setPreferredEmailAddress ( "String" , 'drapper@example.org' ) ; }
"setPreferredEmailAddress: dies for non-numeric id" ;
dies_ok { $ sp - > setPreferredEmailAddress ( $ drapperId , undef ) }
"setPreferredEmailAddress: email address undefined fails" ;
dies_ok { $ sp - > setPreferredEmailAddress ( $ drapperId , 'drapper@ex@ample.org' ) }
"setPreferredEmailAddress: email address with extra @ fails to add." ;
dies_ok { $ sp - > getPreferredEmailAddress ( undef ) ; }
"getPreferredEmailAddress: dies for undefined id" ;
dies_ok { $ sp - > getPreferredEmailAddress ( "String" ) ; }
"getPreferredEmailAddress: dies for non-numeric id" ;
my $ ret ;
lives_ok { $ ret = $ sp - > setPreferredEmailAddress ( $ drapperId , 'drapper@example.com' ) }
"setPreferredEmailAddress: email address not found in database does not die...." ;
is ( $ ret , undef , "setPreferredEmailAddress: ....but returns undef" ) ;
lives_ok { $ ret = $ sp - > getPreferredEmailAddress ( $ drapperId ) }
"getPreferredEmailAddress: no preferred does not die...." ;
is ( $ ret , undef , "getPreferredEmailAddress: ....but returns undef" ) ;
lives_ok { $ ret = $ sp - > setPreferredEmailAddress ( $ drapperId , 'drapper@example.org' ) }
"setPreferredEmailAddress: setting preferred email address succeeds...." ;
ok ( ( defined $ ret and looks_like_number ( $ ret ) and $ ret == $ drapperEmailId ) ,
"setPreferredEmailAddress: ... and returns correct email_address_id on success" ) ;
2015-12-21 02:49:52 +00:00
is ( $ sp - > { __NESTED_TRANSACTION_COUNTER__ } , 0 , "setPreferredEmailAddress: assure proper beginWork/commit matching." ) ;
2015-12-21 01:57:30 +00:00
lives_ok { $ ret = $ sp - > getPreferredEmailAddress ( $ drapperId ) }
"getPreferredEmailAddress: lookup of known preferred email address succeeds... " ;
is ( $ ret , 'drapper@example.org' , "getPreferredEmailAddress: ....and returns the correct value." ) ;
2015-12-31 09:59:50 +00:00
= item getEmailAddresses
= cut
my % emailAddresses ;
dies_ok { % emailAddresses = $ sp - > getEmailAddresses ( 0 ) ; }
"getEmailAddresses: fails with 0 donorId" ;
dies_ok { % emailAddresses = $ sp - > getEmailAddresses ( "String" ) ; }
"getEmailAddresses: fails with string donorId" ;
dies_ok { % emailAddresses = $ sp - > getEmailAddresses ( undef ) ; }
"getEmailAddresses: fails with string donorId" ;
lives_ok { % emailAddresses = $ sp - > getEmailAddresses ( $ olsonId ) ; }
"getEmailAddresses: 1 lookup of addresses succeeds..." ;
is_deeply ( \ % emailAddresses , { 'everyone@example.net' = > { 'date_encountered' = > $ today , 'name' = > 'paypal' } ,
2016-01-07 20:52:29 +00:00
'olson@example.net' = > { 'date_encountered' = > $ today , 'name' = > 'home' } } ,
2015-12-31 09:59:50 +00:00
"getEmailAddresses: ... and returns correct results." ) ;
lives_ok { % emailAddresses = $ sp - > getEmailAddresses ( $ drapperId ) ; }
"getEmailAddresses: 2 lookup of addresses succeeds..." ;
is_deeply ( \ % emailAddresses , { 'everyone@example.net' = > { 'date_encountered' = > $ today , 'name' = > 'paypal' } ,
2016-01-07 20:52:29 +00:00
'drapper@example.org' = > { 'date_encountered' = > $ today , 'name' = > 'work' } } ,
2015-12-31 09:59:50 +00:00
"getEmailAddresses: ... and returns correct results." ) ;
lives_ok { % emailAddresses = $ sp - > getEmailAddresses ( $ sterlingId ) ; }
"getEmailAddresses: 3 lookup of addresses succeeds..." ;
is_deeply ( \ % emailAddresses , { 'sterlingjr@example.com' = > { 'name' = > 'home' , 'date_encountered' = > $ today } } ,
"getEmailAddresses: ... and returns correct results." ) ;
lives_ok { % emailAddresses = $ sp - > getEmailAddresses ( $ campbellId ) ; }
"getEmailAddresses: lookup of *empty* addresses succeeds..." ;
is_deeply ( \ % emailAddresses , { } ,
"getEmailAddresses: ... and returns correct results." ) ;
2019-12-10 17:21:46 +00:00
= item setPreferredEmailAddress / getPreferredEmailAddress
= cut
= item getPostalAddress
= cut
# Add additional postal address.
my $ drapperHomePostalId ;
lives_ok { $ drapperHomePostalId = $ sp - > addPostalAddress ( $ drapperId ,
"112 Main Street \nLong Island, NY 11000\nUSA" , 'home' ) ; }
"addPostalAddress: addPostalAddress of a valid formatted_address works." ;
ok ( ( looks_like_number ( $ drapperHomePostalId ) and $ drapperHomePostalId > 0 ) , "addPostalAddress: id returned is sane." ) ;
# Force home address to have an old date
$ dbh - > do ( "UPDATE postal_address SET date = '1000-01-01' WHERE id = " . $ sp - > dbh - > quote ( $ drapperHomePostalId ) ) ;
2015-12-10 03:38:22 +00:00
2015-12-30 13:02:39 +00:00
= item findDonor
= cut
2015-12-30 14:08:39 +00:00
my @ lookupDonorIds ;
2015-12-30 13:02:39 +00:00
2015-12-31 04:53:28 +00:00
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { } ) ; }
"findDonor: no search criteria succeeds and..." ;
my ( % vals ) ;
@ vals { @ lookupDonorIds } = @ lookupDonorIds ;
2015-12-31 05:26:48 +00:00
is_deeply ( \ % vals , { $ campbellId = > $ campbellId , $ sterlingId = > $ sterlingId , $ harrisId = > $ harrisId ,
2015-12-31 04:53:28 +00:00
$ olsonId = > $ olsonId , $ drapperId = > $ drapperId } ,
"findDonor: ... and returns all donorIds." ) ;
2015-12-30 13:02:39 +00:00
2015-12-30 14:08:39 +00:00
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { ledgerEntityId = > "NotFound" } ) ; }
2015-12-30 13:02:39 +00:00
"findDonor: 1 lookup of known missing succeeds ..." ;
2015-12-30 14:08:39 +00:00
is ( scalar ( @ lookupDonorIds ) , 0 , "findDonor: ... but finds nothing." ) ;
2015-12-30 13:02:39 +00:00
2015-12-30 14:08:39 +00:00
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { emailAddress = > "nothingthere" } ) ; }
2015-12-30 13:02:39 +00:00
"findDonor: 2 lookup of known missing succeeds ..." ;
2015-12-30 14:08:39 +00:00
is ( scalar ( @ lookupDonorIds ) , 0 , "findDonor: ... but finds nothing." ) ;
2015-12-30 13:02:39 +00:00
2015-12-30 14:08:39 +00:00
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { emailAddress = > 'drapper@example.org' , ledgerEntityId = > "NOTFOUND" } ) ; }
2015-12-30 13:02:39 +00:00
"findDonor: 1 and'ed criteria succeeds ..." ;
2015-12-30 14:08:39 +00:00
is ( scalar ( @ lookupDonorIds ) , 0 , "findDonor: ... but finds nothing." ) ;
2015-12-30 13:02:39 +00:00
2015-12-30 14:08:39 +00:00
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { emailAddress = > 'NOTFOUND' , ledgerEntityId = > "Whitman-Dick" } ) ; }
2015-12-30 13:02:39 +00:00
"findDonor: 2 and'ed criteria succeeds ..." ;
2015-12-30 14:08:39 +00:00
is ( scalar ( @ lookupDonorIds ) , 0 , "findDonor: ... but finds nothing." ) ;
2015-12-30 13:02:39 +00:00
2015-12-30 14:08:39 +00:00
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { emailAddress = > 'drapper@example.org' , ledgerEntityId = > "Whitman-Dick" } ) ; }
2015-12-30 13:02:39 +00:00
"findDonor: 1 valid multiple criteria succeeds ..." ;
2015-12-30 14:08:39 +00:00
is_deeply ( \ @ lookupDonorIds , [ $ drapperId ] , "findDonor: ... and finds right entry." ) ;
2015-12-30 13:02:39 +00:00
2015-12-30 14:08:39 +00:00
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { emailAddress = > 'everyone@example.net' , ledgerEntityId = > "Whitman-Dick" } ) ; }
"findDonor: 2 valid multiple criteria succeeds ..." ;
is_deeply ( \ @ lookupDonorIds , [ $ drapperId ] , "findDonor: ... and finds right entry." ) ;
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { emailAddress = > 'everyone@example.net' , ledgerEntityId = > "Olson-Margaret" } ) ; }
"findDonor: 3 valid multiple criteria succeeds ..." ;
is_deeply ( \ @ lookupDonorIds , [ $ olsonId ] , "findDonor: ... and finds right entry." ) ;
lives_ok { @ lookupDonorIds = $ sp - > findDonor ( { emailAddress = > 'everyone@example.net' } ) ; }
"findDonor: single criteria find expecting multiple records succeeds..." ;
2015-12-31 04:53:28 +00:00
% vals = ( ) ;
2015-12-30 14:08:39 +00:00
@ vals { @ lookupDonorIds } = @ lookupDonorIds ;
is_deeply ( \ % vals , { $ olsonId = > $ olsonId , $ drapperId = > $ drapperId } , "findDonor: ... and finds the right entires." ) ;
2015-12-30 13:02:39 +00:00
2015-12-31 02:59:34 +00:00
= item donorLastGave
2015-12-31 00:33:03 +00:00
= cut
dies_ok { $ sp - > donorLastGave ( undef ) ; } "donorLastGave(): dies with undefined donorId" ;
dies_ok { $ sp - > donorLastGave ( "str" ) ; } "donorLastGave(): dies with non-numeric donorId" ;
dies_ok { $ sp - > donorLastGave ( 0 ) ; } "donorLastGave(): dies with non-existent id" ;
my $ date ;
lives_ok { $ date = $ sp - > donorLastGave ( $ drapperId ) } "donorLastGave(): check for known annual donor success..." ;
is ( $ date , '2015-05-04' , "donorLastGave(): ...and returned value is correct. " ) ;
2015-12-31 03:01:19 +00:00
lives_ok { $ date = $ sp - > donorLastGave ( $ olsonId ) } "donorLastGave(): check for known monthly donor success..." ;
2015-12-31 00:33:03 +00:00
is ( $ date , '2015-06-30' , "donorLastGave(): ...and returned value is correct. " ) ;
2015-12-31 03:32:31 +00:00
= item donorFirstGave
= cut
dies_ok { $ sp - > donorFirstGave ( undef ) ; } "donorFirstGave(): dies with undefined donorId" ;
dies_ok { $ sp - > donorFirstGave ( "str" ) ; } "donorFirstGave(): dies with non-numeric donorId" ;
dies_ok { $ sp - > donorFirstGave ( 0 ) ; } "donorFirstGave(): dies with non-existent id" ;
lives_ok { $ date = $ sp - > donorFirstGave ( $ drapperId ) } "donorFirstGave(): check for known annual donor success..." ;
is ( $ date , '2015-02-26' , "donorFirstGave(): ...and returned value is correct. " ) ;
lives_ok { $ date = $ sp - > donorFirstGave ( $ olsonId ) } "donorFirstGave(): check for known monthly donor success..." ;
is ( $ date , '2015-01-15' , "donorFirstGave(): ...and returned value is correct. " ) ;
2016-01-07 20:53:04 +00:00
= item donorTotalGaveInPeriod
= cut
dies_ok { $ sp - > donorTotalGaveInPeriod ( donorId = > undef ) ; } "donorTotalGaveInPeriod(): dies with undefined donorId" ;
dies_ok { $ sp - > donorTotalGaveInPeriod ( donorId = > "str" ) ; } "donorTotalGaveInPeriod(): dies with non-numeric donorId" ;
dies_ok { $ sp - > donorTotalGaveInPeriod ( donorId = > 0 ) ; } "donorTotalGaveInPeriod(): dies with non-existent id" ;
2016-01-07 21:07:04 +00:00
foreach my $ arg ( qw/startDate endDate/ ) {
dies_ok { $ sp - > donorTotalGaveInPeriod ( donorId = > $ drapperId , $ arg = > '2015-1-5' ) ; }
"donorTotalGaveInPeriod(): dies with non ISO-8601 string in $arg" ;
}
2016-01-07 21:35:13 +00:00
dies_ok { $ sp - > donorTotalGaveInPeriod ( donorId = > $ drapperId , wrong = > '' ) ; }
"donorTotalGaveInPeriod(): dies if given an argument that is not recognized" ;
2016-01-07 20:53:04 +00:00
my $ amount ;
lives_ok { $ amount = $ sp - > donorTotalGaveInPeriod ( donorId = > $ drapperId ) }
"donorTotalGaveInPeriod(): total for a donor with no period named succeeds..." ;
is ( $ amount , 305.00 , "donorTotalGaveInPeriod(): ...and returned value is correct. " ) ;
lives_ok { $ amount = $ sp - > donorTotalGaveInPeriod ( donorId = > $ olsonId , startDate = > '2015-02-17' ,
endDate = > '2015-06-29' ) }
"donorTotalGaveInPeriod(): check for total with both start and end date succeeds..." ;
2016-01-07 21:35:01 +00:00
is ( $ amount , 30.00 , "donorTotalGaveInPeriod(): ...and returned value is correct. " ) ;
2016-01-07 20:53:04 +00:00
lives_ok { $ amount = $ sp - > donorTotalGaveInPeriod ( donorId = > $ harrisId , startDate = > '2015-12-04' ) ; }
"donorTotalGaveInPeriod(): check for total with just a start date succeeds..." ;
is ( $ amount , 120.00 , "donorTotalGaveInPeriod(): ...and returned value is correct. " ) ;
2016-01-07 21:06:40 +00:00
lives_ok { $ amount = $ sp - > donorTotalGaveInPeriod ( donorId = > $ olsonId , endDate = > '2015-02-16' ) ; }
2016-01-07 20:53:04 +00:00
"donorTotalGaveInPeriod(): check for total with just a end date succeeds..." ;
2016-01-07 21:35:01 +00:00
is ( $ amount , 20.00 , "donorTotalGaveInPeriod(): ...and returned value is correct. " ) ;
2016-01-07 20:53:04 +00:00
2015-12-31 05:47:29 +00:00
= item donorDonationOnDate
# FIXME: way to lookup donation on a date.
= cut
2015-12-31 04:11:57 +00:00
= item supporterExpirationDate
= cut
dies_ok { $ sp - > supporterExpirationDate ( undef ) ; } "supporterExpirationDate(): dies with undefined donorId" ;
dies_ok { $ sp - > supporterExpirationDate ( "str" ) ; } "supporterExpirationDate(): dies with non-numeric donorId" ;
dies_ok { $ sp - > supporterExpirationDate ( 0 ) ; } "supporterExpirationDate(): dies with non-existent id" ;
lives_ok { $ date = $ sp - > supporterExpirationDate ( $ drapperId ) } "supporterExpirationDate(): check for known annual donor success..." ;
is ( $ date , '2016-02-26' , "supporterExpirationDate(): ...and returned value is correct. " ) ;
lives_ok { $ date = $ sp - > supporterExpirationDate ( $ olsonId ) } "supporterExpirationDate(): check for known monthly donor success..." ;
is ( $ date , '2015-08-29' , "supporterExpirationDate(): ...and returned value is correct. " ) ;
lives_ok { $ date = $ sp - > supporterExpirationDate ( $ sterlingId ) } "supporterExpirationDate(): check for never donation success..." ;
is ( $ date , undef , "supporterExpirationDate(): ...and returned undef." ) ;
2015-12-31 05:26:48 +00:00
lives_ok { $ date = $ sp - > supporterExpirationDate ( $ harrisId ) } "supporterExpirationDate(): same donation amount in year..." ;
is ( $ date , '2016-12-04' , "supporterExpirationDate(): ...returns the latter date." ) ;
2015-12-31 04:11:57 +00:00
$ dbh - > do ( "UPDATE donor SET is_supporter = 0 WHERE id = " . $ sp - > dbh - > quote ( $ campbellId ) ) ;
lives_ok { $ date = $ sp - > supporterExpirationDate ( $ campbellId ) } "supporterExpirationDate(): check for no supporter success..." ;
is ( $ date , undef , "supporterExpirationDate(): ...and returned undef." ) ;
2015-12-31 03:32:31 +00:00
= back
2015-12-10 03:38:22 +00:00
= item Internal methods used only by the module itself .
2015-12-10 02:38:07 +00:00
2015-12-10 03:38:22 +00:00
= over
2015-12-10 02:38:07 +00:00
2015-12-10 03:38:22 +00:00
= item _verifyId
2015-12-10 02:38:07 +00:00
= cut
2015-12-10 03:52:31 +00:00
ok ( $ sp - > _verifyId ( $ drapperId ) , "_verifyId: id just added exists" ) ;
2015-12-10 02:38:07 +00:00
dies_ok { $ sp - > _verifyId ( undef ) ; } "_verifyId: dies for undefined id" ;
dies_ok { $ sp - > _verifyId ( "String" ) } "_verifyId: dies for non-numeric id" ;
# This is a hacky way to test this; but should work
2015-12-10 03:52:31 +00:00
ok ( not ( $ sp - > _verifyId ( $ drapperId + 10 ) ) , "_verifyId: non-existent id is not found" ) ;
2015-12-10 02:38:07 +00:00
2015-12-30 13:41:10 +00:00
= item _lookupEmailAddress
2015-12-30 13:33:05 +00:00
= cut
2015-12-30 13:41:10 +00:00
dies_ok { $ sp - > _lookupEmailAddress ( undef ) ; } "_lookupEmailAddressId: dies for undefined email_address" ;
2015-12-30 13:33:05 +00:00
2015-12-30 13:41:10 +00:00
is_deeply ( $ sp - > _lookupEmailAddress ( 'drapper@example.org' ) ,
{ emailAddress = > 'drapper@example.org' , id = > $ drapperEmailId , type = > 'work' , dateEncountered = > $ today } ,
2015-12-30 13:49:12 +00:00
"_lookupEmailAddressId: 1 returns email Id for known item" ) ;
is_deeply ( $ sp - > _lookupEmailAddress ( 'everyone@example.net' ) ,
{ emailAddress = > 'everyone@example.net' , id = > $ olsonEmailId2 , type = > 'paypal' , dateEncountered = > $ today } ,
"_lookupEmailAddressId: 2 returns email id for known item" ) ;
2015-12-30 13:33:05 +00:00
2015-12-30 13:41:10 +00:00
is ( $ sp - > _lookupEmailAddress ( 'drapper@example.com' ) , undef ,
2015-12-30 13:33:05 +00:00
"_lookupEmailAddressId: returns undef for unknown item." ) ;
2015-12-20 18:58:22 +00:00
2015-12-20 20:45:36 +00:00
$ sp = undef ;
2015-12-20 18:58:22 +00:00
sub ResetDB ($) {
$ _ [ 0 ] - > disconnect ( ) if defined $ _ [ 0 ] ;
my $ tempDBH = get_test_dbh ( ) ;
2015-12-30 20:08:33 +00:00
my $ tempSP = new Supporters ( $ tempDBH , [ "testcmd" ] ) ;
2015-12-20 18:58:22 +00:00
return ( $ tempDBH , $ tempSP ) ;
}
my ( $ tempDBH , $ tempSP ) = ResetDB ( $ dbh ) ;
2015-12-18 03:41:21 +00:00
= item _getOrCreateRequestType
= cut
2015-12-20 18:58:22 +00:00
dies_ok { $ tempSP - > _getOrCreateRequestType ( { } ) ; }
2015-12-18 03:41:21 +00:00
"_getOrCreateRequestType: dies on empty hash" ;
2015-12-20 18:58:22 +00:00
dies_ok { $ tempSP - > _getOrCreateRequestType ( { requestTypeId = > "NoStringsPlease" } ) ; }
2015-12-18 04:15:09 +00:00
"_getOrCreateRequestType: dies for string request id" ;
2015-12-20 18:58:22 +00:00
dies_ok { $ tempSP - > _getOrCreateRequestType ( { requestTypeId = > 0 } ) ; }
2015-12-18 03:41:21 +00:00
"_getOrCreateRequestType: dies for non-existant requestTypeId" ;
my % hh = ( requestType = > 'test-request' ) ;
2015-12-20 18:58:22 +00:00
lives_ok { $ tempSP - > _getOrCreateRequestType ( \ % hh ) ; }
2015-12-18 03:41:21 +00:00
"_getOrCreateRequestType: succeeds with just requestType" ;
my $ rr ;
2015-12-20 18:58:22 +00:00
lives_ok { $ rr = $ tempSP - > getRequestType ( "test-request" ) ; }
2015-12-18 03:41:21 +00:00
"_getOrCreateRequestType: lookup of a request works after _getOrCreateRequestType" ;
is_deeply ( \ % hh , { requestTypeId = > $ rr } ,
"_getOrCreateRequestType: lookup of a request works after _getOrCreateRequestType" ) ;
% hh = ( requestTypeId = > $ rr , requestType = > 'this-arg-matters-not' ) ;
2015-12-20 18:58:22 +00:00
lives_ok { $ tempSP - > _getOrCreateRequestType ( \ % hh ) ; }
2015-12-18 04:15:09 +00:00
"_getOrCreateRequestType: lookup of existing requestType suceeds." ;
2015-12-18 03:41:21 +00:00
is_deeply ( \ % hh , { requestTypeId = > $ rr } ,
"_getOrCreateRequestType: deletes requestType if both are provided." ) ;
2015-12-30 11:55:28 +00:00
dies_ok { $ tempSP - > _lookupRequestTypeById ( undef ) ; }
"_lookupRequestTypeById: dies for undefined requestTypeId" ;
2015-12-20 19:07:06 +00:00
2015-12-30 11:55:28 +00:00
dies_ok { $ tempSP - > _lookupRequestTypeById ( "NoStringsPlease" ) ; }
"_lookupRequestTypeById: dies for a string requestTypeId" ;
2015-12-20 19:07:06 +00:00
2015-12-30 11:55:28 +00:00
ok ( ( not $ tempSP - > _lookupRequestTypeById ( 0 ) ) , "_lookupRequestTypeById: returns false for id lookup for 0" ) ;
2015-12-20 19:07:06 +00:00
# Assumption here: that id number one more than the last added would never be in db.
2015-12-30 11:55:28 +00:00
ok ( ( not $ tempSP - > _lookupRequestTypeById ( $ rr + 1 ) ) ,
"_lookupRequestTypeById: returns false for id one greater than last added" ) ;
2015-12-20 19:07:06 +00:00
2015-12-30 11:55:28 +00:00
is ( $ tempSP - > _lookupRequestTypeById ( $ rr ) , "test-request" ,
"_lookupRequestTypeById: returns proper result for id known to be in database" ) ;
2015-12-20 19:07:06 +00:00
2015-12-20 18:58:22 +00:00
= item _getOrCreateRequestConfiguration
= cut
dies_ok { $ tempSP - > _getOrCreateRequestConfiguration ( { } ) ; }
"_getOrCreateRequestConfiguration: dies on empty hash" ;
dies_ok { $ tempSP - > _getOrCreateRequestConfiguration ( { requestConfigurationId = > "NoStringsPlease" } ) ; }
2015-12-20 19:12:46 +00:00
"_getOrCreateRequestConfiguration: dies for string requestConfigurationId" ;
2015-12-20 18:58:22 +00:00
dies_ok { $ tempSP - > _getOrCreateRequestConfiguration ( { requestConfigurationId = > 0 } ) ; }
"_getOrCreateRequestConfiguration: dies for non-existant requestConfigurationId" ;
dies_ok { $ tempSP - > _getOrCreateRequestConfiguration ( { requestTypeId = > "NoStringsPlease" } ) ; }
"_getOrCreateRequestConfiguration: dies for string request id" ;
dies_ok { $ tempSP - > _getOrCreateRequestConfiguration ( { requestTypeId = > 0 } ) ; }
"_getOrCreateRequestConfiguration: dies for non-existant requestTypeId" ;
2015-12-20 19:12:12 +00:00
dies_ok { $ tempSP - > _getOrCreateRequestConfiguration ( { requestTypeId = > $ rr ,
requestConfigurationId = > "NoStringsPlease" } ) ; }
"_getOrCreateRequestConfiguration: dies for string requestConfigurationId with valid requestTypeId" ;
2015-12-20 18:58:22 +00:00
% hh = ( requestConfiguration = > 'test-request-config' ) ;
dies_ok { $ tempSP - > _getOrCreateRequestConfiguration ( \ % hh ) ; }
"_getOrCreateRequestConfiguration: fails with just requestConfiguration." ;
$ val = $ tempSP - > dbh ( ) - > selectall_hashref ( "SELECT id, description FROM request_configuration" , 'description' ) ;
2015-12-20 20:09:52 +00:00
ok ( ( defined $ val and ( keys ( %$ val ) == 0 ) ) ,
2015-12-20 18:58:22 +00:00
"_getOrCreateRequestConfiguration: no request_configuration record added for failed attempts" ) ;
% hh = ( requestTypeId = > $ rr , requestConfiguration = > 'test-request-config' ) ;
lives_ok { $ tempSP - > _getOrCreateRequestConfiguration ( \ % hh ) ; }
"_getOrCreateRequestConfiguration: succeeds with requestConfiguration and requestType" ;
2015-12-20 20:09:52 +00:00
my ( $ fullConfig , $ rc ) ;
lives_ok { $ fullConfig = $ tempSP - > getRequestConfigurations ( 'test-request' ) ; }
"getRequestConfigurations: succeeds after successful _getOrCreateRequestConfiguration()" ;
2015-12-20 18:58:22 +00:00
2015-12-20 20:09:52 +00:00
$ rc = $ fullConfig - > { $ rr } { 'test-request-config' } ;
2015-12-20 18:58:22 +00:00
2015-12-20 20:09:52 +00:00
is_deeply ( \ % hh , { requestTypeId = > $ rr , requestConfigurationId = > $ rc } ,
"_getOrCreateRequestConfiguration: modification of paramater argument was correct after successful add" ) ;
is_deeply $ fullConfig ,
{ 1 = > { 'test-request-config' = > 1 } } ,
"_getOrCreateRequestConfiguration: lookup of a request configuration works after _getOrCreateRequestConfiguration" ;
% hh = ( requestTypeId = > $ rr , requestConfiguration = > "test-request-config" ) ;
lives_ok { $ tempSP - > _getOrCreateRequestConfiguration ( \ % hh ) ; }
"_getOrCreateRequestConfiguration: looks up one previously added by _getOrCreateRequestConfiguration()" ;
is_deeply ( \ % hh , { requestTypeId = > $ rr , requestConfigurationId = > $ rc } ,
2015-12-20 18:58:22 +00:00
"_getOrCreateRequestConfiguration: lookup of a request works after _getOrCreateRequestConfiguration" ) ;
% hh = ( requestTypeId = > $ rr , requestConfigurationId = > $ rc , requestConfiguration = > 'this-arg-matters-not' ) ;
lives_ok { $ tempSP - > _getOrCreateRequestConfiguration ( \ % hh ) ; }
2015-12-20 20:09:52 +00:00
"_getOrCreateRequestConfiguration: lookup of existing requestConfigurationId succeeds, ignoring requestConfiguration parameter." ;
2015-12-20 18:58:22 +00:00
is_deeply ( \ % hh , { requestTypeId = > $ rr , requestConfigurationId = > $ rc } ,
2015-12-20 20:09:52 +00:00
"_getOrCreateRequestConfiguration: deletes requestTypeConfiguration if both are provided." ) ;
2015-12-20 18:58:22 +00:00
2015-12-10 02:38:07 +00:00
= back
2015-12-15 01:05:42 +00:00
= item Database weirdness tests
= cut
2015-12-20 20:45:43 +00:00
( $ tempDBH , $ tempSP ) = ResetDB ( $ tempDBH ) ;
2015-12-15 01:05:42 +00:00
$ tempDBH - > do ( "DROP TABLE email_address;" ) ;
dies_ok { $ tempSP - > addSupporter ( { display_name = > "Roger Sterling" ,
public_ack = > 0 , ledger_entity_id = > "Sterling-Roger" ,
email_address = > 'sterlingjr@example.com' ,
email_address_type = > 'home' } ) }
"addSupporter: dies when email_address table does not exist & email adress given" ;
2015-12-30 19:30:22 +00:00
2015-12-15 01:28:36 +00:00
$ tempDBH - > disconnect ; $ tempDBH = reopen_test_dbh ( ) ;
2015-12-15 01:05:42 +00:00
2015-12-30 11:10:07 +00:00
$ val = $ tempDBH - > selectall_hashref ( "SELECT id FROM donor;" , 'id' ) ;
2015-12-15 01:05:42 +00:00
2015-12-15 01:28:19 +00:00
ok ( ( defined $ val and reftype $ val eq "HASH" and keys ( % { $ val } ) == 0 ) ,
2015-12-15 01:05:42 +00:00
"addSupporter: fails if email_address given but email cannot be inserted" ) ;
2015-12-30 19:30:22 +00:00
$ tempDBH - > disconnect ; $ tempDBH = reopen_test_dbh ( ) ;
2015-12-15 01:05:42 +00:00
2015-12-10 03:38:22 +00:00
= back
2015-12-10 02:38:07 +00:00
= cut
2015-12-15 01:05:42 +00:00
$ tempDBH - > disconnect ;
1 ;
2015-12-10 02:36:57 +00:00
###############################################################################
#
# Local variables:
2015-12-13 21:04:53 +00:00
# compile-command: "perl -c Supporters.t && cd ..; make clean; perl Makefile.PL && make && make test TEST_VERBOSE=1"
2015-12-10 02:36:57 +00:00
# End: