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

(-)a/t/db_dependent/api/v1/swagger/definitions.t (-2 / +106 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;
29
30
30
my $swaggerPath = C4::Context->config('intranetdir')."/api/v1";
31
my $swaggerPath = C4::Context->config('intranetdir')."/api/v1";
31
my $swagger = Swagger2->new($swaggerPath."/swagger.json")->expand;
32
my $swagger = Swagger2->new($swaggerPath."/swagger.json")->expand;
Lines 67-72 subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => Link Here
67
    }
68
    }
68
};
69
};
69
70
71
subtest 'Compare property data type to column data type' => sub {
72
    foreach my $name (@definition_names) {
73
        my $definition = $api_spec->{definitions}->{$name};
74
75
        if ($definition->{type} eq "object") {
76
            my $kohaObject = _koha_object($name);
77
78
            next unless ($kohaObject && $kohaObject->can("_columns"));
79
80
            my $columns = Koha::Database->new->schema->resultset( $kohaObject->_type )->result_source->{_columns};
81
            my $properties = $definition->{properties};
82
            foreach my $column_name (keys $columns) {
83
                my $column = $columns->{$column_name};
84
                my $property = $properties->{$column_name};
85
                ok(defined $property->{type}, "$name: '$column_name' has a type definition");
86
                if ($column->{is_nullable}) {
87
                    if (ref($property->{type}) eq "ARRAY") {
88
                        ok((grep { /null/ } @{$property->{type}}), "$name: '$column_name' is nullable and has data type 'null'");
89
                    } else {
90
                        ok($property->{type} eq "null", "$name: '$column_name' is nullable and has data type 'null'");
91
                    }
92
                } else {
93
                    if (ref($property->{type}) eq "ARRAY") {
94
                        is((grep { /null/ } @{$property->{type}}), 0, "$name: '$column_name' is not nullable and does not have data type 'null'");
95
                    }
96
                    else {
97
                        ok($property->{type} ne "null", "$name: '$column_name' is not nullable and does not have data type 'null'");
98
                    }
99
                }
100
                my $swagger_type = _get_property_type($property); # without "null"
101
                my $expected_swagger_type = _expected_data_type($column); # without "null"
102
103
                is($swagger_type, $expected_swagger_type, "$name: '$column_name' has correct data type '$swagger_type'");
104
            }
105
        }
106
    }
107
};
108
109
sub _expected_data_type {
110
    my ($column) = @_;
111
112
    return _sql_to_swagger_data_type_mapping($column->{data_type});
113
}
114
115
sub _get_property_type {
116
    my ($property) = @_;
117
118
    my $type = $property->{type};
119
    if (ref($type) eq "ARRAY") {
120
        @$type = grep { !/null/ } @$type;
121
        if (@$type == 1) {
122
            $type = @$type[0];
123
        } else {
124
            # If we are here, we still have multiple types even after "null"
125
            # is removed from the Swagger types.
126
            # Why would we still have multiple types?
127
            # Is "type": ["integer", "string"] allowed? If so, fix this test
128
            return;
129
        }
130
    }
131
132
    return $type;
133
}
134
70
sub _koha_object {
135
sub _koha_object {
71
    my ($name) = @_;
136
    my ($name) = @_;
72
137
Lines 86-88 sub _is_up_to_date { Link Here
86
    }
151
    }
87
    return join(', ' , @missing);
152
    return join(', ' , @missing);
88
}
153
}
89
- 
154
155
sub _sql_to_swagger_data_type_mapping {
156
    my ($column_data_type) = @_;
157
158
    # map SQL data types to Swagger data types
159
    # based on http://swagger.io/specification/
160
    my $sql_swagger_data_type_map = {
161
        ########## INTEGERS #########
162
        'bigint' => 'integer',
163
        'integer' => 'integer',
164
        'int' => 'integer',
165
        'mediumint' => 'integer',
166
        'smallint'  => 'integer',
167
        'tinyint' => 'integer',
168
169
        ########## NUMBERS ##########
170
        'decimal' => 'number',
171
        'double precision' => 'number',
172
        'float' => 'number',
173
174
        ########## STRINGS ##########
175
        'blob' => 'string',
176
        'char' => 'string',
177
        'date' => 'string',
178
        'datetime' => 'string',
179
        'enum' => 'string',
180
        'longblob' => 'string',
181
        'longtext' => 'string',
182
        'mediumblob' => 'string',
183
        'mediumtext' => 'string',
184
        'text' => 'string',
185
        'tinyblob' => 'string',
186
        'tinytext' => 'string',
187
        'timestamp' => 'string',
188
        'varchar' => 'string',
189
190
    };
191
192
    return $sql_swagger_data_type_map->{$column_data_type};
193
}

Return to bug 17008