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

(-)a/t/db_dependent/api/v1/swagger/definitions.t (-2 / +95 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
                if ($column->{is_nullable}) {
86
                    ok(grep($property->{type}, "null"), "$name: '$column_name' is nullable and has data type 'null'");
87
                } else {
88
                    is(grep($property->{type}, "null"), 0, "$name: '$column_name' is not nullable and does not have data type 'null'");
89
                }
90
                my $swagger_type = _get_property_type($property); # without "null"
91
                my $expected_swagger_type = _expected_data_type($column); # without "null"
92
93
                is($swagger_type, $expected_swagger_type, "$name: '$column_name' has correct data type '$swagger_type'");
94
            }
95
        }
96
    }
97
};
98
99
sub _expected_data_type {
100
    my ($column) = @_;
101
102
    return _sql_to_swagger_data_type_mapping($column->{data_type});
103
}
104
105
sub _get_property_type {
106
    my ($property) = @_;
107
108
    my $type = $property->{type};
109
    if (ref($type) eq "ARRAY") {
110
        @$type = grep { !/null/ } @$type;
111
        if (@$type == 1) {
112
            $type = @$type[0];
113
        } else {
114
            # If we are here, we still have multiple types even after "null"
115
            # is removed from the Swagger types.
116
            # Why would we still have multiple types?
117
            # Is "type": ["integer", "string"] allowed? If so, fix this test
118
            return;
119
        }
120
    }
121
122
    return $type;
123
}
124
70
sub _koha_object {
125
sub _koha_object {
71
    my ($name) = @_;
126
    my ($name) = @_;
72
127
Lines 86-88 sub _is_up_to_date { Link Here
86
    }
141
    }
87
    return join(', ' , @missing);
142
    return join(', ' , @missing);
88
}
143
}
89
- 
144
145
sub _sql_to_swagger_data_type_mapping {
146
    my ($column_data_type) = @_;
147
148
    # map SQL data types to Swagger data types
149
    # based on http://swagger.io/specification/
150
    my $sql_swagger_data_type_map = {
151
        ########## INTEGERS #########
152
        'bigint' => "integer",
153
        'integer' => "integer",
154
        'int' => "integer",
155
        'mediumint' => "integer",
156
        'tinyint' => "integer",
157
158
        ########## NUMBERS ##########
159
        'decimal' => "number",
160
        'double precision' => "number",
161
        'float' => "number",
162
163
        ########## STRINGS ##########
164
        'blob' => "string",
165
        'char' => "string",
166
        'date' => "string",
167
        'datetime' => "string",
168
        'enum' => "string",
169
        'longblob' => "string",
170
        'longtext' => "string",
171
        'mediumblob' => "string",
172
        'mediumtext' => "string",
173
        'text' => "string",
174
        'tinyblob' => "string",
175
        'tinytext' => "string",
176
        'timestamp' => "string",
177
        'varchar' => "string",
178
179
    };
180
181
    return $sql_swagger_data_type_map->{$column_data_type};
182
}

Return to bug 17008