Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3: added PgBabylon\PDO::copyFromFile and PgBabylon\PDO::copyToFile m…
…ethods.
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
use PgBabylon\PDO; | ||
|
||
class CopyFromFileTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testDataType() | ||
{ | ||
if (skipTest()) { | ||
$this->markTestSkipped(); | ||
return; | ||
} | ||
|
||
if(getDB()->exec("CREATE TABLE copy_from_test(field1 TEXT NOT NULL, field2 INTEGER, field3 DATE)") === false) { | ||
$this->markTestSkipped("Create table for copy_from_test failed"); | ||
return; | ||
} | ||
|
||
$data = []; | ||
for($i=0;$i<100;$i++) | ||
{ | ||
if($i != 50) | ||
$data[] = "text_{$i}\t{$i}"; | ||
else | ||
$data[] = "text_{$i}\t\\N"; | ||
} | ||
|
||
if(!file_put_contents(__TESTS_TEMP_DIR__ . '/copy_from_test.csv', implode("\n", $data))) { | ||
$this->markTestSkipped("Unable to save the file needed for copy from test"); | ||
return; | ||
} | ||
|
||
$this->assertTrue(getDB()->copyFromFile( | ||
"copy_from_test", | ||
__TESTS_TEMP_DIR__ . '/copy_from_test.csv', | ||
null, | ||
null, | ||
['field1', 'field2'] | ||
)); | ||
|
||
$r = getDB()->query("SELECT count(field1) AS f1, count(field2) AS f2 FROM copy_from_test") | ||
->fetch(PDO::FETCH_ASSOC); | ||
|
||
$this->assertEquals($r['f1'], 100); | ||
$this->assertEquals($r['f2'], 99); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
use PgBabylon\PDO; | ||
|
||
class CopyToFileTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testDataType() | ||
{ | ||
if (skipTest()) { | ||
$this->markTestSkipped(); | ||
return; | ||
} | ||
|
||
if(getDB()->exec("CREATE TABLE copy_to_test(field1 TEXT NOT NULL, field2 INTEGER, field3 DATE)") === false) { | ||
$this->markTestSkipped("Create table for copy_to_test failed"); | ||
return; | ||
} | ||
|
||
$data = []; | ||
$s = getDB()->prepare("INSERT INTO copy_to_test(field1,field2) VALUES (:field1, :field2)"); | ||
for($i=0;$i<100;$i++) | ||
{ | ||
if($i != 50) | ||
{ | ||
$data[] = "text_{$i}\t{$i}"; | ||
$s->execute([ | ||
":field1" => "text_{$i}", | ||
":field2" => $i | ||
]); | ||
} | ||
else | ||
{ | ||
$data[] = "text_{$i}\t\\N"; | ||
$s->execute([ | ||
":field1" => "text_{$i}", | ||
":field2" => null | ||
]); | ||
} | ||
} | ||
|
||
$this->assertTrue(getDB()->copyToFile( | ||
"copy_from_test", | ||
__TESTS_TEMP_DIR__ . '/copy_to_test.csv', | ||
null, | ||
null, | ||
['field1', 'field2'] | ||
)); | ||
|
||
if(!($f = file_get_contents(__TESTS_TEMP_DIR__ . '/copy_to_test.csv'))) { | ||
$this->markTestSkipped("Unable to load the file needed for copy to test"); | ||
return; | ||
} | ||
|
||
$this->assertSame(implode("\n", $data) . "\n", $f); | ||
} | ||
} |