From 138e21137a396b1afeab71a9f5e3047886daac92 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Thu, 17 Mar 2022 02:41:47 +0100 Subject: [PATCH] Adding application recovery from developer. --- db_tests/00003-application-dao.t | 48 ++++++++++++++++++- lib/Peace/DAO/Application.pm | 47 ++++++++++++++++++- lib/Peace/Model/Developer.pm | 26 ++++++++++- t/00005-developer-model.t | 80 +++++++++++++++++++++++++++++++- t/00008-application-dao.t | 79 +++++++++++++++++++++++++++++-- 5 files changed, 271 insertions(+), 9 deletions(-) diff --git a/db_tests/00003-application-dao.t b/db_tests/00003-application-dao.t index e9c1699..579349b 100644 --- a/db_tests/00003-application-dao.t +++ b/db_tests/00003-application-dao.t @@ -5,7 +5,9 @@ use v5.30.0; use strict; use warnings; -use Test::Most tests => 2; +use Data::Dumper; + +use Test::Most tests => 4; use DateTime; @@ -56,3 +58,47 @@ use Peace::DAO::Application; ok $application->uuid, 'Generated uuid.'; ok $application->date_creation > $current_date, 'The date is recent.'; } +{ + ## GIVEN + my $current_date = DateTime->now; + my $peace = Peace->new; + my $home = $ENV{HOME}; + my $config = + $peace->plugin( + JSONConfig => { file => "$home/.config/peace/peace.conf" } ); + my $dbh = Peace::DB->dbh( config => $config ); + my $secret_bcrypt = 'hola'; + my $developer = Peace::Model::Developer->new( + name => 'Larry', + surname => 'Wall', + email => 'larry@perl.org', + country => 'US', + verified => 0, + secret_bcrypt => $secret_bcrypt + ); + my $developer_dao = Peace::DAO::Developer->new( dbh => $dbh ); + $developer_dao->create( developer => $developer ); + my $application = Peace::Model::Application->new( + name => 'Desfronificator', + description => 'Desfronifies the files.', + url => 'desfronificator.example.com', + developer => $developer, + price => 0, + flatpak_builder_file => './resources/com.example.desfronificator.yml', + git_repo => + 'https://git.desfronificator.example.com/larry/desfronificator.git', + verified => 0, + ); + my $application_dao = Peace::DAO::Application->new( dbh => $dbh ); + + $application_dao->create( application => $application ); + + ## WHEN + my $applicatios = + $application_dao->recover_by_developer( developer => $developer ); + + ## THEN + ok @$applicatios, 'Applications was at least a element.'; + ok $applicatios->[0]->isa('Peace::Model::Application'), + 'Application is made of developer.'; +} diff --git a/lib/Peace/DAO/Application.pm b/lib/Peace/DAO/Application.pm index 559d860..318deef 100644 --- a/lib/Peace/DAO/Application.pm +++ b/lib/Peace/DAO/Application.pm @@ -6,7 +6,7 @@ use strict; use warnings; use Params::ValidationCompiler qw/validation_for/; -use Types::Standard qw/HasMethods InstanceOf Str/; +use Types::Standard qw/HasMethods InstanceOf Str HashRef/; { my $validator = validation_for( @@ -63,6 +63,39 @@ EOF } } +{ + my $validator = validation_for( + params => { + developer => + { type => InstanceOf ['Peace::Model::Developer'], optional => 1 }, + } + ); + + sub recover_by_developer { + my $self = shift; + my %params = $validator->(@_); + my $developer = $params{developer}; + my $developer_uuid = $developer->uuid; + my $dbh = $self->_dbh; + if ( !defined $developer_uuid ) { + die "Developer has no uuid yet when recovering applications."; + } + my $result = + $dbh->selectall_arrayref( + <<'EOF', { Slice => {} }, $developer->uuid ); +SELECT uuid, date_creation, name, + description, url, developer, + price, git_repo, flatpak_builder_file, + verified +FROM applications +WHERE developer = ?; +EOF + $result = [ map { $self->_select_result_to_application($_) } @$result ]; + return $result; + + } +} + { my $validator = validation_for( params => { @@ -86,9 +119,19 @@ EOF if ( !defined $result ) { die "Application with uuid $uuid not found."; } + return $self->_select_result_to_application($result); + } +} + +{ + my $validator = validation_for( params => [ { type => HashRef }, ] ); + + sub _select_result_to_application { + my $self = shift; + my ($result) = $validator->(@_); if ( exists $result->{developer} ) { $result->{developer_uuid} = delete $result->{developer}; - $result->{dbh} = $self->_dbh; + $result->{dbh} = $self->_dbh; } if ( exists $result->{date_creation} ) { my $iso8601 = DateTime::Format::Pg->new; diff --git a/lib/Peace/Model/Developer.pm b/lib/Peace/Model/Developer.pm index 10435e0..fdbd1df 100644 --- a/lib/Peace/Model/Developer.pm +++ b/lib/Peace/Model/Developer.pm @@ -5,22 +5,25 @@ use strict; use warnings; use Params::ValidationCompiler qw/validation_for/; -use Types::Standard qw/Str InstanceOf Bool/; +use Types::Standard qw/Str InstanceOf Bool HasMethods/; use DateTime; +use Peace::DAO::Application; + { my $validator = validation_for( params => { uuid => { type => Str, optional => 1 }, date_creation => { type => InstanceOf ['DateTime'], optional => 1 }, - secret_bcrypt => { type => Str }, + secret_bcrypt => { type => Str }, name => { type => Str }, surname => { type => Str }, email => { type => Str }, stripe_id => { type => Str, optional => 1 }, country => { type => Str }, verified => { type => Bool }, + dbh => { type => HasMethods ['selectall_arrayref'], optional => 1 }, } ); @@ -32,6 +35,20 @@ use DateTime; } } +sub applications { + my $self = shift; + if (!defined $self->{applications}) { + my $dbh = $self->_dbh; + if (!defined $dbh) { + die "There is no database handle, so no chance to recover applications from the developer."; + } + my $application_dao = Peace::DAO::Application->new( dbh => $dbh ); + $self->{applications} = $application_dao->recover_by_developer(developer => $self); + } + + return $self->{applications}; +} + { my $validator = validation_for( params => [ { type => Str, optional => 1 } ] ); @@ -158,4 +175,9 @@ use DateTime; return $self->{verified}; } } + +sub _dbh { + my $self = shift; + return $self->{dbh}; +} 1; diff --git a/t/00005-developer-model.t b/t/00005-developer-model.t index c472966..ecbf3e7 100644 --- a/t/00005-developer-model.t +++ b/t/00005-developer-model.t @@ -3,7 +3,12 @@ use v5.30.0; use strict; use warnings; -use Test::Most tests => 5; +use Test::Most tests => 6; +use Test::MockModule; + +use DBI; + +use Peace::Model::Application; BEGIN { use_ok 'Peace::Model::Developer'; @@ -58,3 +63,76 @@ BEGIN { ## THEN is $developer->uuid, $uuid, 'Uuid can be set.'; } + +{ + ## GIVEN + + my $application_dao_mock = Test::MockModule->new('Peace::DAO::Application'); + ### Developer data + my $uuid = 'example'; + my $secret_bcrypt = 'hola'; + my $name = 'Larry'; + my $surname = 'Wall'; + my $email = 'larry@perl.org'; + my $country = 'US'; + my $verified = 0; + my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); + + ### Application data + my $app_name0 = 'Desfronificator'; + my $app_name1 = 'Desfronificator 1.0'; + 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_verified = 0; + + my $developer = Peace::Model::Developer->new( + secret_bcrypt => $secret_bcrypt, + name => $name, + surname => $surname, + email => $email, + country => $country, + verified => $verified, + dbh => $dbh, + ); + + my $applications = [ + Peace::Model::Application->new( + name => $app_name0, + description => $app_description, + url => $app_url, + price => $app_price, + git_repo => $app_git_repo, + flatpak_builder_file => $app_flatpak_builder_file, + verified => $app_verified, + developer => $developer, + ), + Peace::Model::Application->new( + name => $app_name0, + description => $app_description, + url => $app_url, + price => $app_price, + git_repo => $app_git_repo, + flatpak_builder_file => $app_flatpak_builder_file, + verified => $app_verified, + developer => $developer, + ), + + ]; + $application_dao_mock->mock( + recover_by_developer => sub { + return $applications; + } + ); + + ## WHEN + my $new_applications = $developer->applications; + + ## THEN + is_deeply $new_applications, $applications, + 'Applications are correctly recovered from the developer.'; +} diff --git a/t/00008-application-dao.t b/t/00008-application-dao.t index f201428..9b51eed 100644 --- a/t/00008-application-dao.t +++ b/t/00008-application-dao.t @@ -3,7 +3,7 @@ use v5.30.0; use strict; use warnings; -use Test::Most tests => 3; +use Test::Most tests => 5; use DBI; use DateTime; @@ -187,6 +187,79 @@ EOF my $application = $application_dao->recover_by_uuid( uuid => $uuid ); ## THEN - is $application->uuid, $uuid, - 'Application uuid correctly recovered.'; + 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, + verified +FROM applications +WHERE developer = ?; +EOF + + ### Developer data + my $uuid = 'hola'; + my $secret_bcrypt = 'hola'; + my $name = 'Larry'; + my $surname = 'Wall'; + my $email = 'larry@perl.org'; + my $country = 'US'; + my $verified = 0; + my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); + my $developer = Peace::Model::Developer->new( + uuid => $uuid, + secret_bcrypt => $secret_bcrypt, + name => $name, + surname => $surname, + email => $email, + country => $country, + verified => $verified, + ); + + ### 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_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', + '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_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.'; }