View | Details | Raw Unified | Return to bug 17654
Collapse All | Expand All

(-)a/t/db_dependent/api/v1/swagger/definitions.t (-11 / +47 lines)
Lines 26-35 use Module::Load::Conditional; Link Here
26
use Swagger2;
26
use Swagger2;
27
27
28
use C4::Context;
28
use C4::Context;
29
use Koha::Database;
29
30
30
my $swaggerPath = C4::Context->config('intranetdir') . "/api/v1/swagger";
31
my $swaggerPath = C4::Context->config('intranetdir') . "/api/v1/swagger";
31
my $swagger     = Swagger2->new( $swaggerPath . "/swagger.json" )->expand;
32
my $swagger     = Swagger2->new( $swaggerPath . "/swagger.json" )->expand;
32
my $api_spec    = $swagger->api_spec->data;
33
my $api_spec    = $swagger->api_spec->data;
34
my $schema = Koha::Database->new->schema;
33
35
34
# The basic idea of this test:
36
# The basic idea of this test:
35
# 1. Find all definitions in Swagger under api/v1/definitions
37
# 1. Find all definitions in Swagger under api/v1/definitions
Lines 44-50 my $api_spec = $swagger->api_spec->data; Link Here
44
my @definition_names = keys %{ $api_spec->{definitions} };
46
my @definition_names = keys %{ $api_spec->{definitions} };
45
47
46
subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => sub {
48
subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => sub {
47
    plan tests => scalar(@definition_names);
49
    plan tests => 2*(scalar(@definition_names) - 1);
48
50
49
    foreach my $name (@definition_names) {
51
    foreach my $name (@definition_names) {
50
        my $definition = $api_spec->{definitions}->{$name};
52
        my $definition = $api_spec->{definitions}->{$name};
Lines 57-66 subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => Link Here
57
                next;
59
                next;
58
            }
60
            }
59
61
60
            my $columns = $kohaObject->_columns;
62
            my $columns_info = $schema->resultset( $kohaObject->_type )->result_source->columns_info;
61
            my $properties = $definition->{properties};
63
            my $properties = $definition->{properties};
62
            is(_is_up_to_date($properties, $columns), "",
64
            my @missing = check_columns_exist($properties, $columns_info);
63
               "$name matches ".ref($kohaObject)." and is not missing any properties.");
65
            if ( @missing ) {
66
                fail( "Columns are missing for $name: " . join(", ", @missing ) );
67
            } else {
68
                pass( "No columns are missing for $name" );
69
            }
70
            my @nullable= check_is_nullable($properties, $columns_info);
71
            if ( @nullable ) {
72
                fail( "Columns is nullable in DB, not in swagger file for $name: " . join(", ", @nullable ) );
73
            } else {
74
                pass( "No null are missing for $name" );
75
            }
64
        } else {
76
        } else {
65
            ok(1, "$name type is not an object. It is ".$definition->{type}.".");
77
            ok(1, "$name type is not an object. It is ".$definition->{type}.".");
66
        }
78
        }
Lines 77-88 sub _koha_object { Link Here
77
    }
89
    }
78
}
90
}
79
91
80
sub _is_up_to_date {
92
sub check_columns_exist {
81
    my ($properties, $columns) = @_;
93
    my ($properties, $columns_info) = @_;
94
    my @missing_column;
95
    for my $column_name ( keys %$columns_info ) {
96
        my $c_info = $columns_info->{$column_name};
97
        unless ( exists $properties->{$column_name} ) {
98
            push @missing_column, $column_name;
99
            next;
100
        }
101
    }
102
    return @missing_column;
103
}
82
104
83
    my @missing;
105
sub check_is_nullable {
84
    foreach my $column (@$columns) {
106
    my ($properties, $columns_info) = @_;
85
        push @missing, $column unless $properties->{$column};
107
    my @missing_nullable;
108
    for my $column_name ( keys %$columns_info ) {
109
        my $c_info = $columns_info->{$column_name};
110
        if ( $c_info->{is_nullable} or $c_info->{datetime_undef_if_invalid} ) {
111
            my $type = $properties->{$column_name}{type};
112
            next unless $type; # FIXME Is it ok not to have type defined?
113
            unless ( ref($type) ) {
114
                push @missing_nullable, $column_name;
115
                next;
116
            }
117
            my $null_exists = grep {/^null$/} @$type;
118
            unless ( $null_exists ) {
119
                push @missing_nullable, $column_name;
120
                next;
121
            }
122
        }
86
    }
123
    }
87
    return join(', ' , @missing);
124
    return @missing_nullable;
88
}
125
}
89
- 

Return to bug 17654