From a25e0ac628a42ba2579f1254f99ea65029284d3c Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Mon, 9 Nov 2015 11:15:36 -0300
Subject: [PATCH] [SIGNED-OFF]Bug 15159: TestBuilder behaviour on AI values
 should be tested

This patch introduces a test for t::lib::TestBuilder to check it
doesn't mess with AI values. As it is generating random values based
on the defined column type, chances are that it is creating the
AI values on its own, instead of letting the DB handle it.

This could be problematic of course. This test uses the 'biblio' table
by creating two values and checking their biblionumbers are consecutive.

To test:
- Apply the patch
- Run:
  $ prove t/db_dependent/TestBuilder.t -v
=> SUCCESS: The new tests are run and:
   - biblio.biblionumber is detected as an auto-increment column
   - generated biblionumbers are consecutive
- Sign off

Regards

Signed-off-by: Hector Castro <hector.hecaxmmx@gmail.com>
All tests successful.
---
 t/db_dependent/TestBuilder.t | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/TestBuilder.t b/t/db_dependent/TestBuilder.t
index 0602912..638d2ef 100644
--- a/t/db_dependent/TestBuilder.t
+++ b/t/db_dependent/TestBuilder.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 41;
+use Test::More tests => 42;
 
 use Koha::Database;
 
@@ -257,6 +257,29 @@ delete $bookseller->{_fk};
 $bookseller_from_db = $rs_aqbookseller->find($bookseller);
 is( $bookseller_from_db->in_storage, 1, 'build with only_fk = 0 stores the entry correctly' );
 
+subtest 'Auto-increment values tests' => sub {
+
+    plan tests => 2;
+
+    # Pick a table with AI PK
+    my $source  = 'Biblio'; # table
+    my $column  = 'biblionumber'; # ai column
+
+    my $col_info = $schema->source( $source )->column_info( $column );
+    is( $col_info->{is_auto_increment}, 1, "biblio.biblionumber is detected as autoincrement");
+
+    # Create a biblio
+    my $biblio_1 = $builder->build({ source => $source });
+    # Get the AI value
+    my $ai_value = $biblio_1->{ biblionumber };
+    # Create a biblio
+    my $biblio_2 = $builder->build({ source => $source });
+    # Get the next AI value
+    my $next_ai_value = $biblio_2->{ biblionumber };
+    is( $ai_value + 1, $next_ai_value, "AI values are consecutive");
+
+};
+
 $schema->storage->txn_rollback;
 
 1;
-- 
2.1.4