App-RPi-EnvUI

 view release on metacpan or  search on metacpan

t/225-route_set_aux.t  view on Meta::CPAN

use strict;
use warnings;

use Data::Dumper;
use JSON::XS;
use Test::More;

BEGIN {
    use lib 't/';
    use TestBase;
    config();
    set_testing();
    db_create();
}

use Mock::Sub no_warnings => 1;

my $m = Mock::Sub->new;
my $switch_sub = $m->mock('App::RPi::EnvUI::API::switch');

use FindBin;
use lib "$FindBin::Bin/../lib";

use HTTP::Request::Common;
use Plack::Test;
use App::RPi::EnvUI;

my $test = Plack::Test->create(App::RPi::EnvUI->to_app);

{ # /set_aux_state route
    my $p;

    # no params
    my $res = $test->request( GET "/set_aux_state" );
    like $res->content, qr/Not Found/, "/set_aux 404s if no params sent in";

    # one param
    $res = $test->request( GET "/set_aux_state/aux1" );
    like $res->content, qr/Not Found/, "/set_aux 404s if only one param sent";


    # good call
    $res = $test->request( GET "/set_aux_state/aux1/0" );
    is $res->is_success, 1, "with two valid params, /set_aux_state ok";
    $p = decode_json $res->content;

    is ref $p, 'HASH', "and is a href";
    is keys %$p, 2, "...and has proper key count";
    is exists $p->{state}, 1, "and state key exists";
    is $p->{state}, 0, "and state has correct default value";
    is exists $p->{aux}, 1, "and aux key exists";
    is $p->{aux}, 'aux1', "and aux has correct default value";

    # loop over all auxs

    for (1..8){
        my $id = "aux$_";

        my $state = aux($id)->{state};
        is $state, 0, "$id has proper default state";

        $res = $test->request( GET "/set_aux_state/$id/1" );
        is $res->is_success, 1, "/set_aux_state $id ok";

        is $switch_sub->called, 1, "switch() called for $id in /set_aux_state";



( run in 0.616 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )