mercredi 26 février 2020

How to test this perl function?

So I have this function: customer_statistics() and I want to test it in 2 ways:

  1. test if the function gets the correct parameters for a certain request
  2. test if the function returns the correct result

I need some guidance, thanks!

sub customer_statistics {
  my $self = shift;
  my @all_access_rights = $self->db->resultset('AccessRights')->ids();

  my $selected_access_rights = $self->param('access_rights') // '';
  my $user_access_rights =
    $self->db->resultset('UserAccessRights')->active();
  if ($selected_access_rights ne '') {
    return unless validate_access_rights_existence(
      $self, $selected_access_rights);
    $user_access_rights =
      $user_access_rights->by_access_rights_id($selected_access_rights);
  }
  my $no_subscriptions = $user_access_rights->count;
  my $user_login_statistics = $user_access_rights->last_logins()
    ->with_num_comments;

  my $items_per_page = $self->param('items_per_page') // 30;
  my $page = $self->param('page') // 1;
  my $total_pages = ceil($no_subscriptions / $items_per_page);

  my $column = $self->param('column') // 'date_begin';
  my $type = $self->param('type') // 'desc';
  $user_login_statistics =
    $user_login_statistics->by_page($page, $items_per_page)
      ->ordered_by($column, $type);

  return $self->render(
    all_access_rights => \@all_access_rights,
    items_per_page => $items_per_page,
    no_subscriptions => $no_subscriptions,
    page => $page,
    selected_access_rights => $selected_access_rights,
    total_pages => $total_pages,
    user_login_statistics => $user_login_statistics,
    column => $column,
    type => $type
  );
}

Aucun commentaire:

Enregistrer un commentaire