Skip to content

Commit

Permalink
WIP: Add DB seed and tests for coverart in API
Browse files Browse the repository at this point in the history
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
notartom committed Dec 16, 2024
1 parent 0a7f42b commit ac6476d
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
19 changes: 19 additions & 0 deletions application/database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class DatabaseSeeder extends Seeder {

public function run()
{
$filename = __DIR__ . '/librivox_catalog_scrubbed.sql.bz2';
$dump = fopen("compress.bzip2://$filename", 'r');
while (($line = fgets($dump)) !== False)
{
$line = trim($line);
if ($line)
{
$this->db->query("$line\n");
}
}
fclose($dump);
}
}
Binary file not shown.
133 changes: 133 additions & 0 deletions application/libraries/Seeder.php
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;
}
}
8 changes: 8 additions & 0 deletions application/tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

class DbTestCase extends CIPHPUnitTestDbTestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

$CI =& get_instance();
$CI->load->library('Seeder');
$CI->seeder->call('DatabaseSeeder');
}
}
31 changes: 31 additions & 0 deletions application/tests/controllers/API_test.php
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'
);
}
}

?>

0 comments on commit ac6476d

Please sign in to comment.