fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-06-11 05:06 am

[dw-free] t/paid-time.t breaks if the current month does not have 30 days

[commit: http://hg.dwscoalition.org/dw-free/rev/dee8e6dde855]

http://bugs.dwscoalition.org/show_bug.cgi?id=2617

Fix the tests, to be month-agnostic.

Patch by [personal profile] kareila.

Files modified:
  • t/paid-time.t
--------------------------------------------------------------------------------
diff -r 3790a30e36df -r dee8e6dde855 t/paid-time.t
--- a/t/paid-time.t	Fri Jun 11 11:40:23 2010 +0800
+++ b/t/paid-time.t	Fri Jun 11 13:12:39 2010 +0800
@@ -10,6 +10,11 @@ plan tests => 29;
 plan tests => 29;
 
 my $u1 = temp_user();
+my $paidmos = 0;
+
+my @lt = localtime();
+my $mdays = LJ::days_in_month( $lt[4] + 1, $lt[5] + 1900 );
+die "Could not calculate days in month" unless $mdays;
 
 my $dbh = LJ::get_db_writer();
 
@@ -17,17 +22,19 @@ sub rst {
 sub rst {
     $dbh->do( 'DELETE FROM dw_paidstatus WHERE userid = ?', undef, $_ )
         foreach ( $u1->id );
+    $paidmos = 0;
 }
 
 sub assert {
-    my ( $u, $type, $secs ) = @_;
+    my ( $u, $type ) = @_;
     my ($typeid) = grep { $LJ::CAP{$_}->{_account_type} eq $type } keys %LJ::CAP;
     ok( $typeid, 'valid class' );
 
     my $ps = DW::Pay::get_paid_status( $u );
+    my $secs = 86400 * $paidmos;
     ok( $ps, 'got paid status' );
     ok( $ps->{typeid} == $typeid, 'typeids match' );
-    ok( $ps->{expiresin} == $secs, 'secs match' );
+    ok( abs( $ps->{expiresin} - $secs) < 60, 'secs match within a minute' );
 }
 
 ################################################################################
@@ -36,46 +43,52 @@ rst();
 # free->paid 1 month
 DW::Pay::add_paid_time( $u1, 'paid', 1 )
     or die DW::Pay::error_text();
+$paidmos += $mdays;  # 30
 
-assert( $u1, 'paid', 30*86400 );
+assert( $u1, 'paid' );
 
 # paid +1 month
 DW::Pay::add_paid_time( $u1, 'paid', 1 )
     or die DW::Pay::error_text();
+$paidmos += $mdays;  # 60
 
-assert( $u1, 'paid', 60*86400 );
+assert( $u1, 'paid' );
 
 # premium +1 month
 DW::Pay::add_paid_time( $u1, 'premium', 1 )
     or die DW::Pay::error_text();
+$paidmos = $paidmos * 0.7 + $mdays;
 
 # should be 72 days... they bought 1 month of premium time (30 days)
 # and they had 60 days of paid.  60 days of paid converts to 42 days
 # of premium, 42+30 = 72 days premium.
-assert( $u1, 'premium', 72*86400 );
+assert( $u1, 'premium' );
 
 # premium +1 month
 DW::Pay::add_paid_time( $u1, 'premium', 1 )
     or die DW::Pay::error_text();
+$paidmos += $mdays;  # 102
 
-assert( $u1, 'premium', 102*86400 );
+assert( $u1, 'premium' );
 
 # paid +1 month == premium +21 days
 DW::Pay::add_paid_time( $u1, 'paid', 1 )
     or die DW::Pay::error_text();
+$paidmos += int( $mdays * 0.7 );  # 123
 
-assert( $u1, 'premium', 123*86400 );
+assert( $u1, 'premium' );
 
 ################################################################################
 
 # seed account
 DW::Pay::add_paid_time( $u1, 'seed', 99 )
     or die DW::Pay::error_text();
+$paidmos = 0;  # never expires
 
-assert( $u1, 'seed', 0 );
+assert( $u1, 'seed' );
 
 ok( ! DW::Pay::add_paid_time( $u1, 'paid', 1 ), 'adding paid time fails' );
 
-assert( $u1, 'seed', 0 );
+assert( $u1, 'seed' );
 
 ################################################################################
--------------------------------------------------------------------------------