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

[dw-free] move bin/test/* files into t/*

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

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

More cleanup: standardize testing framework (paid time)

Patch by [personal profile] kareila.

Files modified:
  • bin/test/test-pay
  • t/paid-time.t
--------------------------------------------------------------------------------
diff -r 099e36e57f4f -r a884cd53e958 bin/test/test-pay
--- a/bin/test/test-pay	Fri Apr 23 22:40:12 2010 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,134 +0,0 @@
-#!/usr/bin/perl
-# yes, there are so many better testing modules than just doing this in a crappy
-# half-ass way.  this is even quarter-ass!
-
-use strict;
-use lib "$ENV{LJHOME}/cgi-bin";
-require 'ljlib.pl';
-use DW::Pay;
-
-use Data::Dumper;
-
-# much time passes ...
-my $u1 = LJ::load_user( 'tester0394' ) or die 'no tester0394';
-my $u2 = LJ::load_user( 'tester0395' ) or die 'no tester0395';
-my $uc = LJ::load_user( 'testcomm' ) or die 'no testcomm';
-$uc->is_community or die 'testcomm not a community :(';
-
-my @tests;
-print "Beginning tests...\n";
-print "    u1 = " . $u1->user . '(' . $u1->id . ")\n";
-print "    u2 = " . $u2->user . '(' . $u2->id . ")\n";
-print "    uc = " . $uc->user . '(' . $uc->id . ")\n\n";
-
-my $dbh = LJ::get_db_writer();
-
-# reset, delete, etc
-sub rst {
-    $dbh->do( 'DELETE FROM dw_paidstatus WHERE userid = ?', undef, $_ )
-        foreach ( $u1->id, $u2->id, $uc->id );
-}
-
-sub assert {
-    my ( $u, $type, $secs ) = @_;
-    my ($typeid) = grep { $LJ::CAP{$_}->{_account_type} eq $type } keys %LJ::CAP;
-    die 'Invalid class'
-        unless $typeid;
-
-    my $ps = DW::Pay::get_paid_status( $u );
-    die 'Failed getting paid status'
-        unless $ps;
-    die "Typeid $ps->{typeid} expected $typeid\n"
-        unless $ps->{typeid} == $typeid;
-    die "Expiresin $ps->{expiresin} expected $secs\n"
-        unless $ps->{expiresin} == $secs;
-
-    return 1;
-}
-
-################################################################################
-push @tests, [ 'free->paid 1 month', sub
-{
-    rst();
-
-    DW::Pay::add_paid_time( $u1, 'paid', 1 )
-        or die DW::Pay::error_text();
-    assert( $u1, 'paid', 30*86400 );
-} ];
-
-
-################################################################################
-push @tests, [ 'paid +1 month', sub
-{
-    DW::Pay::add_paid_time( $u1, 'paid', 1 )
-        or die DW::Pay::error_text();
-    assert( $u1, 'paid', 60*86400 );
-} ];
-
-
-################################################################################
-push @tests, [ 'premium +1 month', sub
-{
-    DW::Pay::add_paid_time( $u1, 'premium', 1 )
-        or die DW::Pay::error_text();
-
-    # 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 );
-} ];
-
-
-################################################################################
-push @tests, [ 'premium +1 month', sub
-{
-    DW::Pay::add_paid_time( $u1, 'premium', 1 )
-        or die DW::Pay::error_text();
-    assert( $u1, 'premium', 102*86400 );
-} ];
-
-
-################################################################################
-push @tests, [ 'paid +1 month', sub
-{
-    DW::Pay::add_paid_time( $u1, 'paid', 1 )
-        or die DW::Pay::error_text();
-    assert( $u1, 'premium', 123*86400 );
-} ];
-
-
-################################################################################
-push @tests, [ '+seed', sub
-{
-    DW::Pay::add_paid_time( $u1, 'seed', 99 )
-        or die DW::Pay::error_text();
-    assert( $u1, 'seed', 0 );
-} ];
-
-
-################################################################################
-push @tests, [ 'paid +1 month fails', sub
-{
-    DW::Pay::add_paid_time( $u1, 'paid', 1 )
-        and die "Shouldn't have succeeded.\n";
-    assert( $u1, 'seed', 0 );
-} ];
-
-
-################################################################################
-$| = 1;
-my $id = 1;
-foreach my $test ( @tests ) {
-    print "Test #$id: $test->[0]: ";
-    my $rv = 0;
-    eval {
-        $rv = $test->[1]->();
-    };
-    if ( $@ || $rv == 0 ) {
-        print "failure!\n\n\$@ = $@\n\$! = $!\nrv = $rv\n\n";
-        die;
-    } else {
-        print "success\n";
-    }
-    $id++;
-}
diff -r 099e36e57f4f -r a884cd53e958 t/paid-time.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/paid-time.t	Fri Apr 23 22:44:27 2010 -0700
@@ -0,0 +1,81 @@
+#!/usr/bin/perl
+
+use strict;
+use Test::More;
+use lib "$ENV{LJHOME}/cgi-bin";
+require 'ljlib.pl';
+use DW::Pay;
+use LJ::Test qw (temp_user);
+
+plan tests => 29;
+
+my $u1 = temp_user();
+
+my $dbh = LJ::get_db_writer();
+
+# reset, delete, etc
+sub rst {
+    $dbh->do( 'DELETE FROM dw_paidstatus WHERE userid = ?', undef, $_ )
+        foreach ( $u1->id );
+}
+
+sub assert {
+    my ( $u, $type, $secs ) = @_;
+    my ($typeid) = grep { $LJ::CAP{$_}->{_account_type} eq $type } keys %LJ::CAP;
+    ok( $typeid, 'valid class' );
+
+    my $ps = DW::Pay::get_paid_status( $u );
+    ok( $ps, 'got paid status' );
+    ok( $ps->{typeid} == $typeid, 'typeids match' );
+    ok( $ps->{expiresin} == $secs, 'secs match' );
+}
+
+################################################################################
+rst();
+
+# free->paid 1 month
+DW::Pay::add_paid_time( $u1, 'paid', 1 )
+    or die DW::Pay::error_text();
+
+assert( $u1, 'paid', 30*86400 );
+
+# paid +1 month
+DW::Pay::add_paid_time( $u1, 'paid', 1 )
+    or die DW::Pay::error_text();
+
+assert( $u1, 'paid', 60*86400 );
+
+# premium +1 month
+DW::Pay::add_paid_time( $u1, 'premium', 1 )
+    or die DW::Pay::error_text();
+
+# 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 );
+
+# premium +1 month
+DW::Pay::add_paid_time( $u1, 'premium', 1 )
+    or die DW::Pay::error_text();
+
+assert( $u1, 'premium', 102*86400 );
+
+# paid +1 month == premium +21 days
+DW::Pay::add_paid_time( $u1, 'paid', 1 )
+    or die DW::Pay::error_text();
+
+assert( $u1, 'premium', 123*86400 );
+
+################################################################################
+
+# seed account
+DW::Pay::add_paid_time( $u1, 'seed', 99 )
+    or die DW::Pay::error_text();
+
+assert( $u1, 'seed', 0 );
+
+ok( ! DW::Pay::add_paid_time( $u1, 'paid', 1 ), 'adding paid time fails' );
+
+assert( $u1, 'seed', 0 );
+
+################################################################################
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org