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

(-)a/t/db_dependent/api/v1/swagger/definitions.t (-3 / +68 lines)
Lines 19-31 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 2;
23
use Test::Mojo;
23
use Test::Mojo;
24
24
25
use Module::Load::Conditional;
25
use Module::Load::Conditional;
26
use Swagger2;
26
use Swagger2;
27
27
28
use C4::Context;
28
use C4::Context;
29
use Koha::Database;
30
use Koha::Util::Swagger;
29
31
30
my $swaggerPath = C4::Context->config('intranetdir') . "/api/v1/swagger";
32
my $swaggerPath = C4::Context->config('intranetdir') . "/api/v1/swagger";
31
my $swagger     = Swagger2->new( $swaggerPath . "/swagger.json" )->expand;
33
my $swagger     = Swagger2->new( $swaggerPath . "/swagger.json" )->expand;
Lines 43-49 my $api_spec = $swagger->api_spec->data; Link Here
43
#             --> If columns do not match properties, definition is not ok.
45
#             --> If columns do not match properties, definition is not ok.
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/swagger/definitions/*.json up-to-date with corresponding Koha-object' => sub {
47
    plan tests => scalar(@definition_names);
49
    plan tests => scalar(@definition_names);
48
50
49
    foreach my $name (@definition_names) {
51
    foreach my $name (@definition_names) {
Lines 67-72 subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => Link Here
67
    }
69
    }
68
};
70
};
69
71
72
subtest 'Compare property data type to column data type' => sub {
73
    foreach my $name (@definition_names) {
74
        my $definition = $api_spec->{definitions}->{$name};
75
76
        if ($definition->{type} eq "object") {
77
            my $kohaObject = _koha_object($name);
78
79
            next unless ($kohaObject && $kohaObject->can("_columns"));
80
81
            my $columns = Koha::Database->new->schema->resultset( $kohaObject->_type )->result_source->{_columns};
82
            my $properties = $definition->{properties};
83
            foreach my $column_name (keys $columns) {
84
                my $column = $columns->{$column_name};
85
                my $property = $properties->{$column_name};
86
                ok(defined $property->{type}, "$name: '$column_name' has a type definition");
87
                if ($column->{is_nullable}) {
88
                    if (ref($property->{type}) eq "ARRAY") {
89
                        ok((grep { /null/ } @{$property->{type}}), "$name: '$column_name' is nullable and has data type 'null'");
90
                    } else {
91
                        ok($property->{type} eq "null", "$name: '$column_name' is nullable and has data type 'null'");
92
                    }
93
                } else {
94
                    if (ref($property->{type}) eq "ARRAY") {
95
                        is((grep { /null/ } @{$property->{type}}), 0, "$name: '$column_name' is not nullable and does not have data type 'null'");
96
                    }
97
                    else {
98
                        ok($property->{type} ne "null", "$name: '$column_name' is not nullable and does not have data type 'null'");
99
                    }
100
                }
101
                my $swagger_type = _get_property_type($property); # without "null"
102
                my $expected_swagger_type = _expected_data_type($column); # without "null"
103
104
                is($swagger_type, $expected_swagger_type, "$name: '$column_name' has correct data type '$swagger_type'");
105
            }
106
        }
107
    }
108
};
109
110
sub _expected_data_type {
111
    my ($column) = @_;
112
113
    return Koha::Util::Swagger::column_type_to_swagger_type($column->{data_type});
114
}
115
116
sub _get_property_type {
117
    my ($property) = @_;
118
119
    my $type = $property->{type};
120
    if (ref($type) eq "ARRAY") {
121
        @$type = grep { !/null/ } @$type;
122
        if (@$type == 1) {
123
            $type = @$type[0];
124
        } else {
125
            # If we are here, we still have multiple types even after "null"
126
            # is removed from the Swagger types.
127
            # Why would we still have multiple types?
128
            # Is "type": ["integer", "string"] allowed? If so, fix this test
129
            return;
130
        }
131
    }
132
133
    return $type;
134
}
135
70
sub _koha_object {
136
sub _koha_object {
71
    my ($name) = @_;
137
    my ($name) = @_;
72
138
73
- 

Return to bug 17008