-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add DB seed and tests for coverart in API
Following up on PR#245, add a DataBase Seeder that uses our scrubbed database dump (https://github.com/LibriVox/librivox-ansible/blob/master/roles/db_import/files/librivox_catalog_scrubbed.sql.bz2), and start using it to test coverart in the API.
- Loading branch information
Showing
5 changed files
with
186 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
class DatabaseSeeder extends Seeder { | ||
|
||
public function run() | ||
{ | ||
$dump = fopen('compress.bzip2://'. __DIR__ . '/librivox_catalog_scrubbed.sql.bz2'); | ||
while ($line = fgets($dump) !== False) | ||
{ | ||
$this->db->query($line); | ||
} | ||
fclose($dump); | ||
} | ||
} |
Binary file not shown.
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,133 @@ | ||
<?php | ||
/** | ||
* Part of ci-phpunit-test | ||
* | ||
* @author Kenji Suzuki <https://github.com/kenjis> | ||
* @license MIT License | ||
* @copyright 2015 Kenji Suzuki | ||
* @link https://github.com/kenjis/ci-phpunit-test | ||
*/ | ||
|
||
class Seeder | ||
{ | ||
/** | ||
* @var CI_Controller | ||
*/ | ||
private $CI; | ||
|
||
/** | ||
* @var CI_DB_query_builder | ||
*/ | ||
protected $db; | ||
|
||
/** | ||
* @var CI_DB_forge | ||
*/ | ||
protected $dbforge; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $seedPath; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $depends = []; | ||
|
||
public function __construct() | ||
{ | ||
$this->CI =& get_instance(); | ||
$this->CI->load->database(); | ||
$this->CI->load->dbforge(); | ||
$this->db = $this->CI->db; | ||
$this->dbforge = $this->CI->dbforge; | ||
} | ||
|
||
/** | ||
* Run another seeder | ||
* | ||
* @param string $seeder Seeder classname | ||
* @param bool $callDependencies | ||
*/ | ||
public function call($seeder, $callDependencies = true) | ||
{ | ||
if ($this->seedPath === null) | ||
{ | ||
$this->seedPath = APPPATH . 'database/seeds/'; | ||
} | ||
|
||
$obj = $this->loadSeeder($seeder); | ||
if ($callDependencies === true && $obj instanceof Seeder) { | ||
$obj->callDependencies($this->seedPath); | ||
} | ||
$obj->run(); | ||
} | ||
|
||
/** | ||
* Get Seeder instance | ||
* | ||
* @param string $seeder | ||
* @return Seeder | ||
*/ | ||
protected function loadSeeder($seeder) | ||
{ | ||
$file = $this->seedPath . $seeder . '.php'; | ||
require_once $file; | ||
|
||
return new $seeder; | ||
} | ||
|
||
/** | ||
* Call dependency seeders | ||
* | ||
* @param string $seedPath | ||
*/ | ||
public function callDependencies($seedPath) | ||
{ | ||
foreach ($this->depends as $path => $seeders) { | ||
$this->seedPath = $seedPath; | ||
if (is_string($path)) { | ||
$this->setPath($path); | ||
} | ||
|
||
$this->callDependency($seeders); | ||
} | ||
$this->setPath($seedPath); | ||
} | ||
|
||
/** | ||
* Call dependency seeder | ||
* | ||
* @param string|array $seederName | ||
*/ | ||
protected function callDependency($seederName) | ||
{ | ||
if (is_array($seederName)) { | ||
array_map([$this, 'callDependency'], $seederName); | ||
return; | ||
} | ||
|
||
$seeder = $this->loadSeeder($seederName); | ||
if (is_string($this->seedPath)) { | ||
$seeder->setPath($this->seedPath); | ||
} | ||
|
||
$seeder->call($seederName, true); | ||
} | ||
|
||
/** | ||
* Set path for seeder files | ||
* | ||
* @param string $path | ||
*/ | ||
public function setPath($path) | ||
{ | ||
$this->seedPath = rtrim($path, '/').'/'; | ||
} | ||
|
||
public function __get($property) | ||
{ | ||
return $this->CI->$property; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
class API_test extends DbTestCase | ||
{ | ||
|
||
public function test_coverart() { | ||
$output = $this->request( | ||
'GET', | ||
'/api/feed/audiobooks/id/52', | ||
[ | ||
'coverart' => 1, | ||
'format' => 'json', | ||
] | ||
); | ||
$books = json_decode($output); | ||
$this->assertEquals( | ||
$books[0]['coverart_jpg'], | ||
'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110.jpg' | ||
); | ||
$this->assertEquals( | ||
$books[0]['coverart_pdf'], | ||
'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110.pdf' | ||
); | ||
$this->assertEquals( | ||
$books[0]['coverart_thumbnail'], | ||
'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110_thumb.jpg' | ||
); | ||
} | ||
} | ||
|
||
?> |