BusyBird

 view release on metacpan or  search on metacpan

t/WebAPI.t  view on Meta::CPAN

use strict;
use warnings;
use lib "t";
use utf8;
use Test::More;
use Test::MockObject;
use DateTime;
use DateTime::Duration;
use BusyBird::Main;
use BusyBird::Main::PSGI qw(create_psgi_app);
use BusyBird::StatusStorage::SQLite;
use BusyBird::DateTime::Format;
use testlib::HTTP;
use BusyBird::Test::StatusStorage qw(:status test_cases_for_ack);
use testlib::Timeline_Util qw(status);
use testlib::Main_Util qw(create_main);
use BusyBird::Log ();
use Plack::Test;
use Encode ();
use JSON qw(encode_json decode_json);
use Try::Tiny;

$BusyBird::Log::Logger = undef;

sub create_dying_status_storage {
    my $mock = Test::MockObject->new();
    foreach my $method (map { "${_}_statuses" } qw(ack get put delete)) {
        $mock->mock($method, sub {
            die "$method dies.";
        });
    }
    ## ** We cannot create a Timeline if get_unacked_counts throws an exception.
    $mock->mock('get_unacked_counts', sub {
        my ($self, %args) = @_;
        $args{callback}->(undef, "get_unacked_counts reports error.");
    });
    return $mock;
}

sub create_erroneous_status_storage {
    my $mock = Test::MockObject->new();
    foreach my $method ('get_unacked_counts', map { "${_}_statuses" } qw(ack get put delete)) {
        $mock->mock($method, sub {
            my ($self, %args) = @_;
            my $cb = $args{callback};
            if($cb) {
                $cb->("$method reports error.");
            }
        });
    }
    return $mock;
}

sub create_json_status {
    my ($id, $level) = @_;
    my $created_at_str = BusyBird::DateTime::Format->format_datetime(
        DateTime->from_epoch(epoch => $id, time_zone => 'UTC')
    );
    my $bb_string = defined($level) ? qq{,"busybird":{"level":$level}} : "";
    my $json_status = <<EOD;
{"id":"$id","created_at":"$created_at_str","text":"テキスト $id"$bb_string}
EOD
    return Encode::encode('utf8', $json_status);
}

sub json_array {
    my (@json_objects) = @_;
    return "[".join(",", @json_objects)."]";
}

sub test_get_statuses {
    my ($tester, $timeline_name, $query_str, $exp_id_list, $label) = @_;
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my $request_url = "/timelines/$timeline_name/statuses.json";
    if($query_str) {
        $request_url .= "?$query_str";
    }
    my $res_obj = $tester->get_json_ok($request_url, qr/^200$/, "$label: GET statuses OK");
    is($res_obj->{error}, undef, "$label: GET statuses error = null OK");
    test_status_id_list($res_obj->{statuses}, $exp_id_list, "$label: GET statuses ID list OK");
}



( run in 0.915 second using v1.01-cache-2.11-cpan-39bf76dae61 )