Annotation of embedaddon/php/ext/pdo_pgsql/tests/copy_from.phpt, revision 1.1.1.1

1.1       misho       1: --TEST--
                      2: PDO PgSQL pgsqlCopyFromArray and pgsqlCopyFromFile
                      3: --SKIPIF--
                      4: <?php # vim:se ft=php:
                      5: if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
                      6: require dirname(__FILE__) . '/config.inc';
                      7: require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
                      8: PDOTest::skip();
                      9: ?>
                     10: --FILE--
                     11: <?php
                     12: require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
                     13: $db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
                     14: $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                     15: $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
                     16: 
                     17: $db->exec('CREATE TABLE test (a integer not null primary key, b text, c integer)');
                     18: 
                     19: try {
                     20: 
                     21: echo "Preparing test file and array for CopyFrom tests\n";
                     22: 
                     23: $tableRows = array();
                     24: $tableRowsWithDifferentNullValues = array();
                     25: 
                     26: for($i=0;$i<3;$i++) {
                     27:        $firstParameter = $i;
                     28:        $secondParameter = "test insert {$i}";
                     29:        $tableRows[] = "{$firstParameter}\t{$secondParameter}\t\\N";
                     30:        $tableRowsWithDifferentNullValues[] = "{$firstParameter};{$secondParameter};NULL";
                     31:        $tableRowsWithDifferentNullValuesAndSelectedFields[] = "{$firstParameter};NULL";
                     32: }
                     33: $filename = 'test_pgsqlCopyFromFile.csv';
                     34: $filenameWithDifferentNullValues = 'test_pgsqlCopyFromFileWithDifferentNullValues.csv';
                     35: $filenameWithDifferentNullValuesAndSelectedFields = 'test_pgsqlCopyFromFileWithDifferentNullValuesAndSelectedFields.csv';
                     36: 
                     37: file_put_contents($filename, implode("\n",$tableRows));
                     38: file_put_contents($filenameWithDifferentNullValues, implode("\n",$tableRowsWithDifferentNullValues));
                     39: file_put_contents($filenameWithDifferentNullValuesAndSelectedFields, implode("\n",$tableRowsWithDifferentNullValuesAndSelectedFields));
                     40: 
                     41: echo "Testing pgsqlCopyFromArray() with default parameters\n";
                     42: $db->beginTransaction();
                     43: var_dump($db->pgsqlCopyFromArray('test',$tableRows));
                     44: 
                     45: $stmt = $db->query("select * from test");
                     46: foreach($stmt as $r) {
                     47:        var_dump($r);
                     48: }
                     49: $db->rollback();
                     50: 
                     51: echo "Testing pgsqlCopyFromArray() with different field separator and not null indicator\n";
                     52: $db->beginTransaction();
                     53: var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValues,";","NULL"));
                     54: $stmt = $db->query("select * from test");
                     55: foreach($stmt as $r) {
                     56:        var_dump($r);
                     57: }
                     58: $db->rollback();
                     59: 
                     60: echo "Testing pgsqlCopyFromArray() with only selected fields\n";
                     61: $db->beginTransaction();
                     62: var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
                     63: $stmt = $db->query("select * from test");
                     64: foreach($stmt as $r) {
                     65:        var_dump($r);
                     66: }
                     67: $db->rollback();
                     68: 
                     69: echo "Testing pgsqlCopyFromArray() with error\n";
                     70: $db->beginTransaction();
                     71: var_dump($db->pgsqlCopyFromArray('test_error',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
                     72: $db->rollback();
                     73: 
                     74: 
                     75: echo "Testing pgsqlCopyFromFile() with default parameters\n";
                     76: $db->beginTransaction();
                     77: var_dump($db->pgsqlCopyFromFile('test',$filename));
                     78: 
                     79: $stmt = $db->query("select * from test");
                     80: foreach($stmt as $r) {
                     81:        var_dump($r);
                     82: }
                     83: $db->rollback();
                     84: 
                     85: echo "Testing pgsqlCopyFromFile() with different field separator and not null indicator\n";
                     86: $db->beginTransaction();
                     87: var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValues,";","NULL"));
                     88: $stmt = $db->query("select * from test");
                     89: foreach($stmt as $r) {
                     90:        var_dump($r);
                     91: }
                     92: $db->rollback();
                     93: 
                     94: echo "Testing pgsqlCopyFromFile() with only selected fields\n";
                     95: $db->beginTransaction();
                     96: var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
                     97: $stmt = $db->query("select * from test");
                     98: foreach($stmt as $r) {
                     99:        var_dump($r);
                    100: }
                    101: $db->rollback();
                    102: 
                    103: echo "Testing pgsqlCopyFromFile() with error\n";
                    104: $db->beginTransaction();
                    105: var_dump($db->pgsqlCopyFromFile('test_error',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
                    106: $db->rollback();
                    107: 
                    108: } catch (Exception $e) {
                    109:        /* catch exceptions so that we can show the relative error */
                    110:        echo "Exception! at line ", $e->getLine(), "\n";
                    111:        var_dump($e->getMessage());
                    112: }
                    113: if(isset($filename)) {
                    114:        @unlink($filename);
                    115: }
                    116: ?>
                    117: --EXPECT--
                    118: Preparing test file and array for CopyFrom tests
                    119: Testing pgsqlCopyFromArray() with default parameters
                    120: bool(true)
                    121: array(6) {
                    122:   ["a"]=>
                    123:   int(0)
                    124:   [0]=>
                    125:   int(0)
                    126:   ["b"]=>
                    127:   string(13) "test insert 0"
                    128:   [1]=>
                    129:   string(13) "test insert 0"
                    130:   ["c"]=>
                    131:   NULL
                    132:   [2]=>
                    133:   NULL
                    134: }
                    135: array(6) {
                    136:   ["a"]=>
                    137:   int(1)
                    138:   [0]=>
                    139:   int(1)
                    140:   ["b"]=>
                    141:   string(13) "test insert 1"
                    142:   [1]=>
                    143:   string(13) "test insert 1"
                    144:   ["c"]=>
                    145:   NULL
                    146:   [2]=>
                    147:   NULL
                    148: }
                    149: array(6) {
                    150:   ["a"]=>
                    151:   int(2)
                    152:   [0]=>
                    153:   int(2)
                    154:   ["b"]=>
                    155:   string(13) "test insert 2"
                    156:   [1]=>
                    157:   string(13) "test insert 2"
                    158:   ["c"]=>
                    159:   NULL
                    160:   [2]=>
                    161:   NULL
                    162: }
                    163: Testing pgsqlCopyFromArray() with different field separator and not null indicator
                    164: bool(true)
                    165: array(6) {
                    166:   ["a"]=>
                    167:   int(0)
                    168:   [0]=>
                    169:   int(0)
                    170:   ["b"]=>
                    171:   string(13) "test insert 0"
                    172:   [1]=>
                    173:   string(13) "test insert 0"
                    174:   ["c"]=>
                    175:   NULL
                    176:   [2]=>
                    177:   NULL
                    178: }
                    179: array(6) {
                    180:   ["a"]=>
                    181:   int(1)
                    182:   [0]=>
                    183:   int(1)
                    184:   ["b"]=>
                    185:   string(13) "test insert 1"
                    186:   [1]=>
                    187:   string(13) "test insert 1"
                    188:   ["c"]=>
                    189:   NULL
                    190:   [2]=>
                    191:   NULL
                    192: }
                    193: array(6) {
                    194:   ["a"]=>
                    195:   int(2)
                    196:   [0]=>
                    197:   int(2)
                    198:   ["b"]=>
                    199:   string(13) "test insert 2"
                    200:   [1]=>
                    201:   string(13) "test insert 2"
                    202:   ["c"]=>
                    203:   NULL
                    204:   [2]=>
                    205:   NULL
                    206: }
                    207: Testing pgsqlCopyFromArray() with only selected fields
                    208: bool(true)
                    209: array(6) {
                    210:   ["a"]=>
                    211:   int(0)
                    212:   [0]=>
                    213:   int(0)
                    214:   ["b"]=>
                    215:   NULL
                    216:   [1]=>
                    217:   NULL
                    218:   ["c"]=>
                    219:   NULL
                    220:   [2]=>
                    221:   NULL
                    222: }
                    223: array(6) {
                    224:   ["a"]=>
                    225:   int(1)
                    226:   [0]=>
                    227:   int(1)
                    228:   ["b"]=>
                    229:   NULL
                    230:   [1]=>
                    231:   NULL
                    232:   ["c"]=>
                    233:   NULL
                    234:   [2]=>
                    235:   NULL
                    236: }
                    237: array(6) {
                    238:   ["a"]=>
                    239:   int(2)
                    240:   [0]=>
                    241:   int(2)
                    242:   ["b"]=>
                    243:   NULL
                    244:   [1]=>
                    245:   NULL
                    246:   ["c"]=>
                    247:   NULL
                    248:   [2]=>
                    249:   NULL
                    250: }
                    251: Testing pgsqlCopyFromArray() with error
                    252: bool(false)
                    253: Testing pgsqlCopyFromFile() with default parameters
                    254: bool(true)
                    255: array(6) {
                    256:   ["a"]=>
                    257:   int(0)
                    258:   [0]=>
                    259:   int(0)
                    260:   ["b"]=>
                    261:   string(13) "test insert 0"
                    262:   [1]=>
                    263:   string(13) "test insert 0"
                    264:   ["c"]=>
                    265:   NULL
                    266:   [2]=>
                    267:   NULL
                    268: }
                    269: array(6) {
                    270:   ["a"]=>
                    271:   int(1)
                    272:   [0]=>
                    273:   int(1)
                    274:   ["b"]=>
                    275:   string(13) "test insert 1"
                    276:   [1]=>
                    277:   string(13) "test insert 1"
                    278:   ["c"]=>
                    279:   NULL
                    280:   [2]=>
                    281:   NULL
                    282: }
                    283: array(6) {
                    284:   ["a"]=>
                    285:   int(2)
                    286:   [0]=>
                    287:   int(2)
                    288:   ["b"]=>
                    289:   string(13) "test insert 2"
                    290:   [1]=>
                    291:   string(13) "test insert 2"
                    292:   ["c"]=>
                    293:   NULL
                    294:   [2]=>
                    295:   NULL
                    296: }
                    297: Testing pgsqlCopyFromFile() with different field separator and not null indicator
                    298: bool(true)
                    299: array(6) {
                    300:   ["a"]=>
                    301:   int(0)
                    302:   [0]=>
                    303:   int(0)
                    304:   ["b"]=>
                    305:   string(13) "test insert 0"
                    306:   [1]=>
                    307:   string(13) "test insert 0"
                    308:   ["c"]=>
                    309:   NULL
                    310:   [2]=>
                    311:   NULL
                    312: }
                    313: array(6) {
                    314:   ["a"]=>
                    315:   int(1)
                    316:   [0]=>
                    317:   int(1)
                    318:   ["b"]=>
                    319:   string(13) "test insert 1"
                    320:   [1]=>
                    321:   string(13) "test insert 1"
                    322:   ["c"]=>
                    323:   NULL
                    324:   [2]=>
                    325:   NULL
                    326: }
                    327: array(6) {
                    328:   ["a"]=>
                    329:   int(2)
                    330:   [0]=>
                    331:   int(2)
                    332:   ["b"]=>
                    333:   string(13) "test insert 2"
                    334:   [1]=>
                    335:   string(13) "test insert 2"
                    336:   ["c"]=>
                    337:   NULL
                    338:   [2]=>
                    339:   NULL
                    340: }
                    341: Testing pgsqlCopyFromFile() with only selected fields
                    342: bool(true)
                    343: array(6) {
                    344:   ["a"]=>
                    345:   int(0)
                    346:   [0]=>
                    347:   int(0)
                    348:   ["b"]=>
                    349:   NULL
                    350:   [1]=>
                    351:   NULL
                    352:   ["c"]=>
                    353:   NULL
                    354:   [2]=>
                    355:   NULL
                    356: }
                    357: array(6) {
                    358:   ["a"]=>
                    359:   int(1)
                    360:   [0]=>
                    361:   int(1)
                    362:   ["b"]=>
                    363:   NULL
                    364:   [1]=>
                    365:   NULL
                    366:   ["c"]=>
                    367:   NULL
                    368:   [2]=>
                    369:   NULL
                    370: }
                    371: array(6) {
                    372:   ["a"]=>
                    373:   int(2)
                    374:   [0]=>
                    375:   int(2)
                    376:   ["b"]=>
                    377:   NULL
                    378:   [1]=>
                    379:   NULL
                    380:   ["c"]=>
                    381:   NULL
                    382:   [2]=>
                    383:   NULL
                    384: }
                    385: Testing pgsqlCopyFromFile() with error
                    386: bool(false)

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>