c89476
From b6e3965aa874cd3391eb01b106af3aa2716f1b1b Mon Sep 17 00:00:00 2001
c89476
From: Vladimir Marek <vlmarek@volny.cz>
c89476
Date: Wed, 2 Mar 2016 11:14:06 +0100
c89476
Subject: [PATCH] Tests for little-endian platform
c89476
MIME-Version: 1.0
c89476
Content-Type: text/plain; charset=UTF-8
c89476
Content-Transfer-Encoding: 8bit
c89476
c89476
Ported to 4.023:
c89476
c89476
commit 55363b41ea077778b723ee5600316141e2785541
c89476
Author: Vladimir Marek <vlmarek@volny.cz>
c89476
Date:   Wed Mar 2 11:14:06 2016 +0100
c89476
c89476
    Tests for little-endian platform
c89476
c89476
    https://rt.cpan.org/Public/Bug/Display.html?id=57266
c89476
c89476
Signed-off-by: Petr Písař <ppisar@redhat.com>
c89476
---
c89476
 t/41int_min_max.t | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
c89476
 1 file changed, 149 insertions(+)
c89476
 create mode 100644 t/41int_min_max.t
c89476
c89476
diff --git a/t/41int_min_max.t b/t/41int_min_max.t
c89476
new file mode 100644
c89476
index 0000000..fbb622c
c89476
--- /dev/null
c89476
+++ b/t/41int_min_max.t
c89476
@@ -0,0 +1,149 @@
c89476
+use strict;
c89476
+use warnings;
c89476
+use bigint;
c89476
+
c89476
+use DBI;
c89476
+use DBI::Const::GetInfoType;
c89476
+use Test::More;
c89476
+use lib 't', '.';
c89476
+use Data::Dumper;
c89476
+require 'lib.pl';
c89476
+use vars qw($test_dsn $test_user $test_password);
c89476
+
c89476
+my $dbh;
c89476
+eval {$dbh= DBI->connect($test_dsn, $test_user, $test_password,
c89476
+                      { RaiseError => 1, PrintError => 1, AutoCommit => 1 });};
c89476
+if ($@) {
c89476
+    plan skip_all => "no database connection";
c89476
+}
c89476
+
c89476
+if ($dbh->get_info($GetInfoType{SQL_DBMS_VER}) lt "4.1") {
c89476
+    plan skip_all => "ERROR: $DBI::errstr. Can't continue test";
c89476
+    plan skip_all =>
c89476
+        "SKIP TEST: You must have MySQL version 4.1 and greater for this test to run";
c89476
+}
c89476
+# nostrict tests + strict tests + init/tear down commands
c89476
+plan tests => 19*8 + 17*8 + 4;
c89476
+
c89476
+my $table = 'dbd_mysql_t41minmax'; # name of the table we will be using
c89476
+my $mode; # 'strict' or 'nostrict' corresponds to strict SQL mode
c89476
+
c89476
+sub test_int_type ($$$$) {
c89476
+    my ($perl_type, $mysql_type, $min, $max) = @_;
c89476
+
c89476
+    # Disable the warning text clobbering our output
c89476
+    local $SIG{__WARN__} = sub { 1; };
c89476
+
c89476
+    # Create the table
c89476
+    ok($dbh->do(qq{DROP TABLE IF EXISTS $table}), "removing $table");
c89476
+    ok($dbh->do(qq{
c89476
+            CREATE TABLE `$table` (
c89476
+                `id` int not null auto_increment,
c89476
+                `val` $mysql_type,
c89476
+                primary key (id)
c89476
+            )
c89476
+    }), "creating minmax table for type $mysql_type");
c89476
+
c89476
+    my ($store, $retrieve); # statements
c89476
+    my $read_value;         # retrieved value
c89476
+    ok($store = $dbh->prepare("INSERT INTO $table (val) VALUES (?)"));
c89476
+    ok($retrieve = $dbh->prepare("SELECT val from $table where id=(SELECT MAX(id) FROM $table)"));
c89476
+
c89476
+    ########################################
c89476
+    # Insert allowed min value
c89476
+    ########################################
c89476
+    ok($store->bind_param( 1, $min->bstr(), $perl_type ), "binding minimal $mysql_type, mode=$mode");
c89476
+    ok($store->execute(), "inserting min data for type $mysql_type, mode=$mode");
c89476
+
c89476
+    ########################################
c89476
+    # Read it back and compare
c89476
+    ########################################
c89476
+    ok{$retrieve->execute()};
c89476
+    ($read_value) = $retrieve->fetchrow_array();
c89476
+    cmp_ok($read_value, 'eq', $min, "retrieved minimal value for $mysql_type, mode=$mode");
c89476
+
c89476
+    ########################################
c89476
+    # Insert allowed max value
c89476
+    ########################################
c89476
+    ok($store->bind_param( 1, $max->bstr(), $perl_type ), "binding maximal $mysql_type, mode=$mode");
c89476
+    ok($store->execute(), "inserting max data for type $mysql_type, mode=$mode");
c89476
+
c89476
+    ########################################
c89476
+    # Read it back and compare
c89476
+    ########################################
c89476
+    ok{$retrieve->execute()};
c89476
+    ($read_value) = $retrieve->fetchrow_array();
c89476
+    cmp_ok($read_value, 'eq', $max, "retrieved maximal value for $mysql_type, mode=$mode");
c89476
+
c89476
+    ########################################
c89476
+    # Try to insert under the limit value
c89476
+    ########################################
c89476
+    ok($store->bind_param( 1, ($min-1)->bstr(), $perl_type ), "binding less than minimal $mysql_type, mode=$mode");
c89476
+    if ($mode eq 'strict') {
c89476
+        $@ = '';
c89476
+        eval{$store->execute()};
c89476
+        like($@, qr/Out of range value for column 'val'/, "Error, you stored ".($min-1)." into $mysql_type, mode=$mode\n".
c89476
+            Data::Dumper->Dump([$dbh->selectall_arrayref("SELECT * FROM $table")]).
c89476
+            Data::Dumper->Dump([$dbh->selectall_arrayref("describe $table")])
c89476
+        );
c89476
+    } else {
c89476
+        ok{$store->execute()};
c89476
+        ########################################
c89476
+        # Check that it was rounded correctly
c89476
+        ########################################
c89476
+        ok{$retrieve->execute()};
c89476
+        ($read_value) = $retrieve->fetchrow_array();
c89476
+        cmp_ok($read_value, 'eq', $min, "retrieved minimal value for type $mysql_type, mode=$mode");
c89476
+    };
c89476
+
c89476
+    ########################################
c89476
+    # Try to insert over the limit value
c89476
+    ########################################
c89476
+    ok($store->bind_param( 1, ($max+1)->bstr(), $perl_type ), "binding more than maximal $mysql_type, mode=$mode");
c89476
+    if ($mode eq 'strict') {
c89476
+        $@ = '';
c89476
+        eval{$store->execute()};
c89476
+        like($@, qr/Out of range value for column 'val'/, "Error, you stored ".($max+1)." into $mysql_type, mode=$mode\n".
c89476
+            Data::Dumper->Dump([$dbh->selectall_arrayref("SELECT * FROM $table")]).
c89476
+            Data::Dumper->Dump([$dbh->selectall_arrayref("describe $table")])
c89476
+        );
c89476
+    } else {
c89476
+        ok{$store->execute()};
c89476
+        ########################################
c89476
+        # Check that it was rounded correctly
c89476
+        ########################################
c89476
+        ok{$retrieve->execute()};
c89476
+        ($read_value) = $retrieve->fetchrow_array();
c89476
+        cmp_ok($read_value, 'eq', $max, "retrieved maximal value for type $mysql_type, mode=$mode");
c89476
+    };
c89476
+}
c89476
+
c89476
+# Set strict SQL mode
c89476
+ok($dbh->do("SET SQL_MODE='STRICT_ALL_TABLES'"),"Enter strict SQL mode.");
c89476
+$mode = 'strict';
c89476
+
c89476
+test_int_type(DBI::SQL_TINYINT,  'tinyint signed',     -2**7,  2**7-1);
c89476
+test_int_type(DBI::SQL_TINYINT,  'tinyint unsigned',       0,  2**8-1);
c89476
+test_int_type(DBI::SQL_SMALLINT, 'smallint signed',   -2**15, 2**15-1);
c89476
+test_int_type(DBI::SQL_SMALLINT, 'smallint unsigned',      0, 2**16-1);
c89476
+test_int_type(DBI::SQL_INTEGER,  'int signed',        -2**31, 2**31-1);
c89476
+test_int_type(DBI::SQL_INTEGER,  'int unsigned',           0, 2**32-1);
c89476
+test_int_type(DBI::SQL_BIGINT,   'bigint signed',     -2**63, 2**63-1);
c89476
+test_int_type(DBI::SQL_BIGINT,   'bigint unsigned',        0, 2**64-1);
c89476
+
c89476
+# Do not use strict SQL mode
c89476
+ok($dbh->do("SET SQL_MODE=''"),"Leave strict SQL mode.");
c89476
+$mode = 'nostrict';
c89476
+
c89476
+test_int_type(DBI::SQL_TINYINT,  'tinyint signed',     -2**7,  2**7-1);
c89476
+test_int_type(DBI::SQL_TINYINT,  'tinyint unsigned',       0,  2**8-1);
c89476
+test_int_type(DBI::SQL_SMALLINT, 'smallint signed',   -2**15, 2**15-1);
c89476
+test_int_type(DBI::SQL_SMALLINT, 'smallint unsigned',      0, 2**16-1);
c89476
+test_int_type(DBI::SQL_INTEGER,  'int signed',        -2**31, 2**31-1);
c89476
+test_int_type(DBI::SQL_INTEGER,  'int unsigned',           0, 2**32-1);
c89476
+test_int_type(DBI::SQL_BIGINT,   'bigint signed',     -2**63, 2**63-1);
c89476
+test_int_type(DBI::SQL_BIGINT,   'bigint unsigned',        0, 2**64-1);
c89476
+
c89476
+ok ($dbh->do("DROP TABLE $table"));
c89476
+
c89476
+ok $dbh->disconnect;
c89476
-- 
c89476
2.7.4
c89476