From f132eaf00af6c7ecc805306ab1210042bd63d3de Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 19 Jun 2025 11:10:47 +0200 Subject: [PATCH] Bug 40176: Add tests This patch adds a new subtest to xt/api.t It ensures that maxLength will appear in the rest api spec file when there is a size constraint at the DB level --- xt/api.t | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/xt/api.t b/xt/api.t index 78cf588c245..7a51a298e7a 100755 --- a/xt/api.t +++ b/xt/api.t @@ -14,15 +14,18 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 8; +use Test::NoWarnings; use Test::Mojo; use Data::Dumper; +use Koha::Database; use FindBin(); use IPC::Cmd qw(can_run); use List::MoreUtils qw(any); use File::Slurp qw(read_file); +use YAML::XS qw(LoadFile); my $t = Test::Mojo->new('Koha::REST::V1'); my $spec = $t->get_ok( '/api/v1/', 'Correctly fetched the spec' )->tx->res->json; @@ -194,3 +197,42 @@ subtest 'POST (201) have location header' => sub { } } }; + +subtest 'maxlength + enum' => sub { + my $def_map = { + + # api def => schema + item => 'Item', + }; + plan tests => scalar keys %$def_map; + my $schema = Koha::Database->new->schema; + while ( my ( $def, $dbic_src ) = each %$def_map ) { + my @failures; + my $definition = LoadFile("api/v1/swagger/definitions/$def.yaml"); + my $source = $schema->source($dbic_src); + my $object_class = Koha::Object::_get_object_class( $source->result_class ); + eval "require $object_class"; + my $koha_object = $object_class->new; + my $api_mapping = $koha_object->to_api_mapping; + my $reversed_api_mapping = + { reverse map { defined $api_mapping->{$_} ? ( $_ => $api_mapping->{$_} ) : () } keys %$api_mapping }; + + while ( my ( $attr, $properties ) = each %{ $definition->{properties} } ) { + my $db_col = $reversed_api_mapping->{$attr}; + next unless $db_col; + my $column_info = $koha_object->_result->column_info($db_col); + next unless $column_info->{size}; + + next if ref( $column_info->{size} ) eq 'ARRAY'; # decimal # FIXME Could have a test for this as well + + next + if $properties->{enum} + ; # FIXME This is not fully correct, we might want to make sure enum is set for both DB and spec. eg. now checkprevcheckout is enum only for the api spec + + if ( !exists $properties->{maxLength} || $column_info->{size} != $properties->{maxLength} ) { + push @failures, sprintf "%s.%s should have maxLength=%s in api spec", $def, $attr, $column_info->{size}; + } + } + is( scalar(@failures), 0, "maxLength tests for $def" ) or diag Dumper @failures; + } +}; -- 2.34.1