Peace/t/00008-application-dao.t

208 lines
6.1 KiB
Perl

use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 5;
use DBI;
use DateTime;
use DateTime::Format::Pg;
use Peace::Test::Mock::Model::Developer;
use Peace::Test::Mock::Model::Application;
BEGIN {
use_ok 'Peace::DAO::Application';
}
{
## GIVEN
my $sql = <<'EOF';
INSERT INTO applications
(name, description, url,
developer, price, git_repo,
flatpak_builder_file, flatpak_repo, verified)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING uuid;
EOF
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
my $developer = Peace::Test::Mock::Model::Developer->new;
$developer->uuid('hola');
my $application = Peace::Test::Mock::Model::Application->new;
my $uuid = 'hola';
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [ ['uuid'], [$uuid], ]
};
$sql = <<'EOF';
SELECT uuid, date_creation, name,
description, url, developer,
price, git_repo, flatpak_builder_file,
flatpak_repo, verified
FROM applications
WHERE uuid = ?
EOF
my $datetime = DateTime->now;
my $formatter = DateTime::Format::Pg->new;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[
'uuid', 'date_creation',
'name', 'description',
'url', 'developer',
'price', 'git_repo',
'flatpak_builder_file', 'flatpak_repo',
'verified',
],
[
$uuid,
$formatter->format_datetime($datetime),
$application->name,
$application->description,
$application->url,
$developer->uuid,
$application->price,
$application->git_repo,
$application->flatpak_builder_file,
$application->flatpak_repo,
$application->verified
]
],
};
## WHEN
$application_dao->create( application => $application );
## THEN
is $application->uuid, $uuid,
'Application uuid correctly set after user creation.';
}
{
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
my $developer = Peace::Test::Mock::Model::Developer->new;
my $application_mock = Peace::Test::Mock::Model::Application->new;
my $uuid = 'hola';
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$application_mock->developer->uuid('hola');
my $sql = <<'EOF';
SELECT uuid, date_creation, name,
description, url, developer,
price, git_repo, flatpak_builder_file,
flatpak_repo, verified
FROM applications
WHERE uuid = ?
EOF
my $datetime = DateTime->now;
my $formatter = DateTime::Format::Pg->new;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[
'uuid', 'date_creation',
'name', 'description',
'url', 'developer',
'price', 'git_repo',
'flatpak_builder_file', 'flatpak_repo',
'verified',
],
[
$uuid,
$formatter->format_datetime($datetime),
$application_mock->name,
$application_mock->description,
$application_mock->url,
$application_mock->developer->uuid,
$application_mock->price,
$application_mock->git_repo,
$application_mock->flatpak_builder_file,
$application_mock->flatpak_repo,
$application_mock->verified,
]
],
};
## WHEN
my $application = $application_dao->recover_by_uuid( uuid => $uuid );
## THEN
is $application->uuid, $uuid, 'Application uuid correctly recovered.';
}
{
## GIVEN
my $sql = <<'EOF';
SELECT uuid, date_creation, name,
description, url, developer,
price, git_repo, flatpak_builder_file,
flatpak_repo, verified
FROM applications
WHERE developer = ?;
EOF
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
my $developer = Peace::Test::Mock::Model::Developer->new;
$developer->uuid('hola');
### Application data
my $app_uuid = 'denuheesuehs';
my $app_date_creation = DateTime->now;
my $app_name = 'Desfronificator';
my $app_description = 'Desfronifies the files.';
my $app_url = 'https://desfronificator.example.com';
my $app_price = '4.20';
my $app_git_repo =
'https://git.desfronificator.example.com/larry/desfronificator.git';
my $app_flatpak_builder_file =
'./resources/com.example.desfronificator.yml';
my $app_flatpak_repo =
'https://nightly.gnome.org/gnome-nightly.flatpakrepo';
my $app_verified = 0;
my $iso8601 = DateTime::Format::Pg->new;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[
'uuid', 'date_creation',
'name', 'description',
'url', 'price',
'git_repo', 'flatpak_builder_file',
'flatpak_repo', 'verified',
'developer'
],
[
$app_uuid,
$iso8601->format_datetime($app_date_creation),
$app_name,
$app_description,
$app_url,
$app_price,
$app_git_repo,
$app_flatpak_builder_file,
$app_flatpak_repo,
$app_verified,
$developer->uuid,
],
]
};
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
## WHEN
my $applications =
$application_dao->recover_by_developer( developer => $developer );
## THEN
ok @$applications, 'Applications can be recovered from developer.';
ok $applications->[0]->isa('Peace::Model::Application'),
'Application is made of developer.';
}