~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package GenTest::Validator::QueryProperties;

require Exporter;
@ISA = qw(GenTest::Validator GenTest);

use strict;

use GenTest;
use Data::Dumper;
use GenTest::Constants;
use GenTest::Result;
use GenTest::Validator;

my @properties = (
	'RESULTSET_HAS_SAME_DATA_IN_EVERY_ROW',
	'RESULTSET_IS_SINGLE_INTEGER_ONE',
	'RESULTSET_HAS_ZERO_OR_ONE_ROWS'
);

my %properties;
foreach my $property (@properties) {
	$properties{$property} = 1;
};

sub validate {
	my ($validator, $executors, $results) = @_;

	my $query = $results->[0]->query();
	my @query_properties = $query =~ m{((?:RESULTSET_|ERROR_).*?)[^A-Z_0-9]}sog;

	return STATUS_WONT_HANDLE if $#query_properties == -1;

	my $query_status = STATUS_OK;

	foreach my $result (@$results) {
		foreach my $query_property (@query_properties) {
			my $property_status = STATUS_OK;
			if (exists $properties{$query_property}) {
				#
				# This is a named property, call the respective validation procedure
				#
				$property_status = $validator->$query_property($result);
			} elsif (my ($error) = $query_property =~ m{ERROR_(.*)}so) {
				#
				# This is an error code, check that the query returned that error code
				#

				if ($error !~ m{^\d*$}) {
					say("Query: $query needs to use a numeric code in in query property $query_property.");
					return STATUS_ENVIRONMENT_FAILURE;
				} elsif ($result->err() != $error) {
					say("Query: $query did not fail with error $error.");
					$property_status = STATUS_ERROR_MISMATCH;
				}
			}
			$query_status = $property_status if $property_status > $query_status;
		}
	}

	if ($query_status != STATUS_OK) {
		say("Query: $query does not have the declared properties.");
		print Dumper $results if rqg_debug();
	}
	
	return $query_status;
}


sub RESULTSET_HAS_SAME_DATA_IN_EVERY_ROW {
	my ($validator, $result) = @_;

	return STATUS_OK if not defined $result->data();
	return STATUS_OK if $result->rows() < 2;

	my %data_hash;
	foreach my $row (@{$result->data()}) {
		my $data_item = join('<field>', @{$row});
		$data_hash{$data_item}++;
	}
	
	if (keys(%data_hash) > 1) {
		return STATUS_CONTENT_MISMATCH;
	} else {
		return STATUS_OK;
	}
}

sub RESULTSET_HAS_ZERO_OR_ONE_ROWS {
	my ($validator, $result) = @_;
	
	if ($result->rows() > 1) {
		return STATUS_LENGTH_MISMATCH;
	} else {
		return STATUS_OK;
	}
}

sub RESULTSET_IS_SINGLE_INTEGER_ONE {
	my ($validator, $result) = @_;
	
	if (
		(not defined $result->data()) ||
		($#{$result->data()} != 0) ||
		($result->rows() != 1) ||
		($#{$result->data()->[0]} != 0) ||
		($result->data()->[0]->[0] != 1)
	) {
		return STATUS_CONTENT_MISMATCH;
	} else {
		return STATUS_OK;
	}
}

1;