~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to lib/GenTest/Validator/QueryProperties.pm

merge from internal tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package GenTest::Validator::QueryProperties;
 
2
 
 
3
require Exporter;
 
4
@ISA = qw(GenTest::Validator GenTest);
 
5
 
 
6
use strict;
 
7
 
 
8
use GenTest;
 
9
use Data::Dumper;
 
10
use GenTest::Constants;
 
11
use GenTest::Result;
 
12
use GenTest::Validator;
 
13
 
 
14
my @properties = (
 
15
        'RESULTSET_HAS_SAME_DATA_IN_EVERY_ROW',
 
16
        'RESULTSET_IS_SINGLE_INTEGER_ONE',
 
17
        'RESULTSET_HAS_ZERO_OR_ONE_ROWS'
 
18
);
 
19
 
 
20
my %properties;
 
21
foreach my $property (@properties) {
 
22
        $properties{$property} = 1;
 
23
};
 
24
 
 
25
sub validate {
 
26
        my ($validator, $executors, $results) = @_;
 
27
 
 
28
        my $query = $results->[0]->query();
 
29
        my @query_properties = $query =~ m{((?:RESULTSET_|ERROR_).*?)[^A-Z_0-9]}sog;
 
30
 
 
31
        return STATUS_WONT_HANDLE if $#query_properties == -1;
 
32
 
 
33
        my $query_status = STATUS_OK;
 
34
 
 
35
        foreach my $result (@$results) {
 
36
                foreach my $query_property (@query_properties) {
 
37
                        my $property_status = STATUS_OK;
 
38
                        if (exists $properties{$query_property}) {
 
39
                                #
 
40
                                # This is a named property, call the respective validation procedure
 
41
                                #
 
42
                                $property_status = $validator->$query_property($result);
 
43
                        } elsif (my ($error) = $query_property =~ m{ERROR_(.*)}so) {
 
44
                                #
 
45
                                # This is an error code, check that the query returned that error code
 
46
                                #
 
47
 
 
48
                                if ($error !~ m{^\d*$}) {
 
49
                                        say("Query: $query needs to use a numeric code in in query property $query_property.");
 
50
                                        return STATUS_ENVIRONMENT_FAILURE;
 
51
                                } elsif ($result->err() != $error) {
 
52
                                        say("Query: $query did not fail with error $error.");
 
53
                                        $property_status = STATUS_ERROR_MISMATCH;
 
54
                                }
 
55
                        }
 
56
                        $query_status = $property_status if $property_status > $query_status;
 
57
                }
 
58
        }
 
59
 
 
60
        if ($query_status != STATUS_OK) {
 
61
                say("Query: $query does not have the declared properties.");
 
62
                print Dumper $results if rqg_debug();
 
63
        }
 
64
        
 
65
        return $query_status;
 
66
}
 
67
 
 
68
 
 
69
sub RESULTSET_HAS_SAME_DATA_IN_EVERY_ROW {
 
70
        my ($validator, $result) = @_;
 
71
 
 
72
        return STATUS_OK if not defined $result->data();
 
73
        return STATUS_OK if $result->rows() < 2;
 
74
 
 
75
        my %data_hash;
 
76
        foreach my $row (@{$result->data()}) {
 
77
                my $data_item = join('<field>', @{$row});
 
78
                $data_hash{$data_item}++;
 
79
        }
 
80
        
 
81
        if (keys(%data_hash) > 1) {
 
82
                return STATUS_CONTENT_MISMATCH;
 
83
        } else {
 
84
                return STATUS_OK;
 
85
        }
 
86
}
 
87
 
 
88
sub RESULTSET_HAS_ZERO_OR_ONE_ROWS {
 
89
        my ($validator, $result) = @_;
 
90
        
 
91
        if ($result->rows() > 1) {
 
92
                return STATUS_LENGTH_MISMATCH;
 
93
        } else {
 
94
                return STATUS_OK;
 
95
        }
 
96
}
 
97
 
 
98
sub RESULTSET_IS_SINGLE_INTEGER_ONE {
 
99
        my ($validator, $result) = @_;
 
100
        
 
101
        if (
 
102
                (not defined $result->data()) ||
 
103
                ($#{$result->data()} != 0) ||
 
104
                ($result->rows() != 1) ||
 
105
                ($#{$result->data()->[0]} != 0) ||
 
106
                ($result->data()->[0]->[0] != 1)
 
107
        ) {
 
108
                return STATUS_CONTENT_MISMATCH;
 
109
        } else {
 
110
                return STATUS_OK;
 
111
        }
 
112
}
 
113
 
 
114
1;