Adding a Peace::DAO::Build with its integration test.

This commit is contained in:
sergiotarxz 2022-03-31 02:39:20 +02:00
parent e92aa3dfb4
commit 693d720ede
4 changed files with 213 additions and 1 deletions

View File

@ -0,0 +1,43 @@
#!/usr/bin/env perl
use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 2;
use DateTime;
use Peace;
use Peace::DB;
use Peace::DAO::Developer;
use Peace::DAO::Application;
use Peace::DAO::Release;
use Peace::DAO::Build;
use Peace::Test::Mock::Model::Build;
{
my $home = $ENV{HOME};
my $current_date = DateTime->now;
my $peace = Peace->new;
my $config =
$peace->plugin(
JSONConfig => { file => "$home/.config/peace/peace.conf" } );
my $dbh = Peace::DB->dbh( config => $config );
my $build = Peace::Test::Mock::Model::Build->new;
my $developer_dao = Peace::DAO::Developer->new( dbh => $dbh );
my $release_dao = Peace::DAO::Release->new( dbh => $dbh );
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
my $build_dao = Peace::DAO::Build->new( dbh => $dbh );
$developer_dao->create(
developer => $build->release->application->developer );
$build->release->application->app_id(
'com.example.desfronificator' . rand(10000) );
$application_dao->create( application => $build->release->application );
$release_dao->create( release => $build->release );
$build_dao->create( build => $build );
ok $build->uuid, 'Uuid was correctly set.';
ok $build->date_creation > $current_date, 'The new date_creation is meaningful.';
}

167
lib/Peace/DAO/Build.pm Normal file
View File

@ -0,0 +1,167 @@
package Peace::DAO::Build;
use v5.30.0;
use strict;
use warnings;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/HasMethods InstanceOf
Str HashRef/;
use Peace::Model::Build;
{
my $validator = validation_for(
params => {
dbh => { type => HasMethods ['selectrow_hashref'] },
}
);
sub new {
my $class = shift;
my %params = $validator->(@_);
my $self = bless {}, $class;
$self->{dbh} = $params{dbh};
return $self;
}
}
{
my $validator = validation_for(
params => {
build => { type => InstanceOf ['Peace::Model::Build'] },
}
);
sub create {
my $self = shift;
my %params = $validator->(@_);
my $build = $params{build};
my $dbh = $self->_dbh;
my $insert = <<'EOF';
INSERT INTO builds (release, success, log,
arch)
VALUES (?, ?, ?, ?)
RETURNING uuid;
EOF
my $result =
$dbh->selectrow_hashref( $insert, undef, $build->release->uuid,
$build->success, $build->log, $build->arch );
my $uuid = $result->{uuid};
my $new_build = $self->recover_by_uuid( uuid => $uuid );
$build->uuid( $new_build->uuid );
$build->date_creation( $new_build->date_creation );
return $build;
}
}
{
my $validator = validation_for(
params => {
uuid => { type => Str },
}
);
sub recover_by_uuid {
my $self = shift;
my %params = $validator->(@_);
my $uuid = $params{uuid};
my $dbh = $self->_dbh;
my $result = $dbh->selectrow_hashref( <<'EOF', undef, $uuid );
SELECT uuid, date_creation,
release, success, log, arch
FROM builds
WHERE uuid = ?;
EOF
if ( !defined $result ) {
die "Build with uuid $uuid not found.";
}
return $self->_select_result_to_build($result);
}
}
{
my $validator = validation_for( params => [ { type => HashRef }, ] );
sub _select_result_to_build {
my $self = shift;
my ($result) = $validator->(@_);
if ( exists $result->{release} ) {
$result->{release_uuid} = delete $result->{release};
$result->{dbh} = $self->_dbh;
}
if ( exists $result->{date_creation} ) {
my $iso8601 = DateTime::Format::Pg->new;
$result->{date_creation} =
$iso8601->parse_datetime( $result->{date_creation} );
}
my $release = Peace::Model::Build->new(%$result);
return $release;
}
}
sub _dbh {
my $self = shift;
return $self->{dbh};
}
1;
=encoding utf8
=head1 NAME
Peace::DAO::Build - Database access object of Peace::Model::Build.
=head1 SYNOPSIS
my $build_dao = Peace::DAO::Build->new(
dbh => $dbh,
);
my $build = $build_dao->recover_by_uuid(
uuid => $uuid,
);
$build_dao->create( build => $build, );
=head1 DESCRIPTION
Peace::DAO::Build allows you to retrieve and create
the representation of L<Peace::Model::Build> in
the database table builds.
=head1 INSTANCE METHODS
Peace::DAO::Build implements the following instance
methods:
=head2 new
my $build_dao = Peace::DAO::Build->new(
dbh => $dbh
);
Instances a Peace::DAO::Build object.
=head1 METHODS
Peace::DAO::Build implements the following methods:
=head2 recover_by_uuid
my $build = $build_dao->recover_by_uuid( uuid => $uuid );
Recovers a L<Peace::Model::Build> from its uuid.
=head2 create
$build_dao->create( build => $build );
Creates the database representation of a
L<Peace::Model::Build>.
=head1 SEE ALSO
L<Peace::Model::Build>, L<DBI>
=cut

View File

@ -63,8 +63,8 @@ my @migrations = (
);', );',
'CREATE TABLE builds ( 'CREATE TABLE builds (
uuid UUID NOT NULL DEFAULT gen_random_uuid(), uuid UUID NOT NULL DEFAULT gen_random_uuid(),
release UUID NOT NULL,
date_creation timestamp DEFAULT NOW(), date_creation timestamp DEFAULT NOW(),
release UUID NOT NULL,
success BOOLEAN NOT NULL, success BOOLEAN NOT NULL,
log TEXT NOT NULL, log TEXT NOT NULL,
arch TEXT NOT NULL, arch TEXT NOT NULL,

View File

@ -14,6 +14,8 @@ sub new {
return Peace::Model::Build->new( return Peace::Model::Build->new(
release => $release, release => $release,
arch => 'x86', arch => 'x86',
log => 'Successfully build',
success => 1,
); );
} }
1; 1;