Commits

namtrankhanh committed d72ac00

add bulk insert test

Comments (0)

Files changed (1)

app/protected/extensions/zurmoinc/framework/tests/unit/DatabaseCompatibilityUtilTest.php

 <?php
-    /*********************************************************************************
+
+    /* * *******************************************************************************
      * Zurmo is a customer relationship management program developed by
      * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
      *
      *
      * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
      * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
-     ********************************************************************************/
+     * ****************************************************************************** */
 
     class DatabaseCompatibilityUtilTest extends BaseTest
     {
+
         protected $hostname;
         protected $rootUsername;
         protected $rootPassword;
             assert(preg_match("/host=([^;]+);(?:port=([^;]+);)?dbname=([^;]+)/", Yii::app()->db->connectionString, $matches) == 1); // Not Coding Standard
             if ($matches[2] != '')
             {
-                $this->databasePort      = intval($matches[2]);
+                $this->databasePort = intval($matches[2]);
             }
             else
             {
                 $this->databasePort = DatabaseCompatibilityUtil::getDatabaseDefaultPort($databaseType);
             }
 
-            $this->hostname              = $matches[1];
-            $this->rootUsername          = Yii::app()->db->username;
-            $this->rootPassword          = Yii::app()->db->password;
-            $this->existingDatabaseName  = $matches[3];
+            $this->hostname = $matches[1];
+            $this->rootUsername = Yii::app()->db->username;
+            $this->rootPassword = Yii::app()->db->password;
+            $this->existingDatabaseName = $matches[3];
             $this->temporaryDatabaseName = "zurmo_wacky";
             if ($this->rootUsername == 'zurmo')
             {
-                $this->rootUsername          = 'zurmoroot';
-                $this->rootPassword          = 'somepass';
+                $this->rootUsername = 'zurmoroot';
+                $this->rootPassword = 'somepass';
                 $this->temporaryDatabaseName = 'zurmo_wacky';
             }
             $this->superUserPassword = 'super';
         public function setup()
         {
             RedBeanDatabase::close();
-            RedBeanDatabase::setup(Yii::app()->db->connectionString,
-                                   Yii::app()->db->username,
-                                   Yii::app()->db->password,
-                                   true);
+            RedBeanDatabase::setup(Yii::app()->db->connectionString, Yii::app()->db->username, Yii::app()->db->password, true);
         }
 
         public function tearDown()
         {
             RedBeanDatabase::close();
-            RedBeanDatabase::setup(Yii::app()->db->connectionString,
-                                   Yii::app()->db->username,
-                                   Yii::app()->db->password,
-                                   true);
+            RedBeanDatabase::setup(Yii::app()->db->connectionString, Yii::app()->db->username, Yii::app()->db->password, true);
         }
 
         public function testCharLength()
 
         public function testGetDatabaseMaxAllowedPacketsSize()
         {
-            $maxAllowedPacketSize = DatabaseCompatibilityUtil::getDatabaseMaxAllowedPacketsSize('mysql',
-                                                                                                $this->hostname,
-                                                                                                $this->rootUsername,
-                                                                                                $this->rootPassword,
-                                                                                                $this->databasePort);
+            $maxAllowedPacketSize = DatabaseCompatibilityUtil::getDatabaseMaxAllowedPacketsSize('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort);
             $this->assertGreaterThan(0, $maxAllowedPacketSize);
         }
 
         public function testGetDatabaseMaxSpRecursionDepth()
         {
-            $maxSpRecursionDepth = DatabaseCompatibilityUtil::getDatabaseMaxSpRecursionDepth('mysql',
-                                                                                             $this->hostname,
-                                                                                             $this->rootUsername,
-                                                                                             $this->rootPassword,
-                                                                                             $this->databasePort);
+            $maxSpRecursionDepth = DatabaseCompatibilityUtil::getDatabaseMaxSpRecursionDepth('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort);
             $this->assertGreaterThan(0, $maxSpRecursionDepth);
         }
 
         public function testGetDatabaseThreadStackValue()
         {
-            $threadStackValue = DatabaseCompatibilityUtil::getDatabaseThreadStackValue('mysql',
-                                                                                          $this->hostname,
-                                                                                          $this->rootUsername,
-                                                                                          $this->rootPassword,
-                                                                                          $this->databasePort);
+            $threadStackValue = DatabaseCompatibilityUtil::getDatabaseThreadStackValue('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort);
             $this->assertGreaterThan(0, $threadStackValue);
         }
 
         public function testGetDatabaseDefaultCollation()
         {
-            $dbDefaultCollation = DatabaseCompatibilityUtil::getDatabaseDefaultCollation('mysql',
-                                                                                          $this->hostname,
-                                                                                          $this->existingDatabaseName,
-                                                                                          $this->rootUsername,
-                                                                                          $this->rootPassword,
-                                                                                          $this->databasePort);
+            $dbDefaultCollation = DatabaseCompatibilityUtil::getDatabaseDefaultCollation('mysql', $this->hostname, $this->existingDatabaseName, $this->rootUsername, $this->rootPassword, $this->databasePort);
             $this->assertTrue(is_string($dbDefaultCollation));
             $this->assertTrue(strlen($dbDefaultCollation) > 0);
         }
 
         public function testIsDatabaseStrictMode()
         {
-            $isDatabaseStrictMode = DatabaseCompatibilityUtil::isDatabaseStrictMode('mysql',
-                                                                                    $this->hostname,
-                                                                                    $this->rootUsername,
-                                                                                    $this->rootPassword,
-                                                                                    $this->databasePort);
+            $isDatabaseStrictMode = DatabaseCompatibilityUtil::isDatabaseStrictMode('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort);
             $this->assertTrue(is_bool($isDatabaseStrictMode));
         }
 
         public function testDatabaseConnection_mysql()
         {
-            $this->assertTrue  (DatabaseCompatibilityUtil::checkDatabaseConnection('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort));
-            $this->assertEquals(array(1045, "Access denied for user '{$this->rootUsername}'@'{$this->hostname}' (using password: YES)"),
-                DatabaseCompatibilityUtil::checkDatabaseConnection('mysql', $this->hostname, $this->rootUsername,   'wrong', $this->databasePort));
-            $this->assertEquals(array(1045, "Access denied for user 'nobody'@'{$this->hostname}' (using password: YES)"),
-                DatabaseCompatibilityUtil::checkDatabaseConnection('mysql', $this->hostname, 'nobody', 'password', $this->databasePort));
+            $this->assertTrue(DatabaseCompatibilityUtil::checkDatabaseConnection('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort));
+            $this->assertEquals(array(1045, "Access denied for user '{$this->rootUsername}'@'{$this->hostname}' (using password: YES)"), DatabaseCompatibilityUtil::checkDatabaseConnection('mysql', $this->hostname, $this->rootUsername, 'wrong', $this->databasePort));
+            $this->assertEquals(array(1045, "Access denied for user 'nobody'@'{$this->hostname}' (using password: YES)"), DatabaseCompatibilityUtil::checkDatabaseConnection('mysql', $this->hostname, 'nobody', 'password', $this->databasePort));
         }
 
         public function testCheckDatabaseExists()
             // This test cannot run as saltdev. It is therefore skipped on the server.
             if ($this->rootUsername == 'root')
             {
-                $this->assertTrue  (DatabaseCompatibilityUtil::checkDatabaseExists('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, $this->existingDatabaseName));
-                $this->assertEquals(array(1049, "Unknown database 'junk'"),
-                DatabaseCompatibilityUtil::checkDatabaseExists('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, 'junk'));
+                $this->assertTrue(DatabaseCompatibilityUtil::checkDatabaseExists('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, $this->existingDatabaseName));
+                $this->assertEquals(array(1049, "Unknown database 'junk'"), DatabaseCompatibilityUtil::checkDatabaseExists('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, 'junk'));
             }
         }
 
             // This test cannot run as saltdev. It is therefore skipped on the server.
             if ($this->rootUsername == 'root')
             {
-                $this->assertTrue (DatabaseCompatibilityUtil::checkDatabaseUserExists('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, $this->rootUsername));
+                $this->assertTrue(DatabaseCompatibilityUtil::checkDatabaseUserExists('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, $this->rootUsername));
                 $this->assertFalse(DatabaseCompatibilityUtil::checkDatabaseUserExists('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, 'dude'));
             }
         }
             // This test cannot run as saltdev. It is therefore skipped on the server.
             if ($this->rootUsername == 'root')
             {
-                $this->assertTrue(DatabaseCompatibilityUtil::createDatabase    ('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, $this->temporaryDatabaseName));
+                $this->assertTrue(DatabaseCompatibilityUtil::createDatabase('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, $this->temporaryDatabaseName));
                 $this->assertTrue(DatabaseCompatibilityUtil::createDatabaseUser('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, $this->temporaryDatabaseName, 'wacko', 'wacked'));
                 $this->assertTrue(DatabaseCompatibilityUtil::createDatabaseUser('mysql', $this->hostname, $this->rootUsername, $this->rootPassword, $this->databasePort, $this->temporaryDatabaseName, 'wacko', ''));
             }
             $compareQueryPart = "!= ''"; // Not Coding Standard
             $this->assertEquals($compareQueryPart, $queryPart);
         }
+
+        public function testBulkInsert()
+        {
+            $tableName      = 'jobinprocess';
+            $columnNames    = array('item_id', 'type');
+            $number         = 100;
+            $insertValues   = $this->createDumpDataOfJobInProcess($number);
+            DatabaseCompatibilityUtil::bulkInsert($tableName, $insertValues, $columnNames, $number);
+            $newValues      = R::getAll('select * from jobinprocess');
+            $this->assertEquals(count($newValues), $number);
+            for ($i = 0; $i < $number; $i++)
+            {
+                $newItemId      = DatabaseCompatibilityUtil::escape($newValues[$i]['item_id']);
+                $insertItemId   = DatabaseCompatibilityUtil::escape($insertValues[$i][0]);
+                $this->assertEquals($newItemId, $insertItemId);
+
+                $newType        = DatabaseCompatibilityUtil::escape($newValues[$i]['type']);
+                $insertType     = DatabaseCompatibilityUtil::escape($insertValues[$i][1]);
+                $this->assertEquals($newType, $insertType);
+            }
+
+            $number         = 501;
+            $insertValues   = $this->createDumpDataOfJobInProcess($number);
+            DatabaseCompatibilityUtil::bulkInsert($tableName, $insertValues, $columnNames, $number);
+            $newValues      = R::getAll('select * from jobinprocess limit 100,501');
+            $this->assertEquals(count($newValues), $number);
+            for ($i = 0; $i < $number; $i++)
+            {
+                $newItemId      = DatabaseCompatibilityUtil::escape($newValues[$i]['item_id']);
+                $insertItemId   = DatabaseCompatibilityUtil::escape($insertValues[$i][0]);
+                $this->assertEquals($newItemId, $insertItemId);
+
+                $newType        = DatabaseCompatibilityUtil::escape($newValues[$i]['type']);
+                $insertType     = DatabaseCompatibilityUtil::escape($insertValues[$i][1]);
+                $this->assertEquals($newType, $insertType);
+            }
+        }
+
+        /**
+         * @expectedException BulkInsertFailedException
+         */
+        public function testBulkInsertWithException()
+        {
+            $tableName      = 'jobinprocess';
+            $columnNames    = array('item_id', 'type');
+            $number         = 100;
+            $insertValues   = $this->createDumpDataOfJobInProcess($number);
+            DatabaseCompatibilityUtil::bulkInsert($tableName, $insertValues, $columnNames, 200);
+        }
+
+        protected function createDumpDataOfJobInProcess($number)
+        {
+            assert('is_numeric($number) && $number > 0');
+            $data = array();
+            for ($i = 0; $i < $number; $i++)
+            {
+                $data[$i] = array(
+                    $i, $this->randString(20)
+                );
+            }
+            return $data;
+        }
+
+        protected function randString($length)
+        {
+            $chars  = "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+            $size   = strlen($chars);
+            $str    = '';
+            for ($i = 0; $i < $length; $i++)
+            {
+                $str .= $chars[rand(0, $size - 1)];
+            }
+            return $str;
+        }
+
     }
+
 ?>