Skip to content

Commit ac6476d

Browse files
committed
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.
1 parent 0a7f42b commit ac6476d

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class DatabaseSeeder extends Seeder {
4+
5+
public function run()
6+
{
7+
$filename = __DIR__ . '/librivox_catalog_scrubbed.sql.bz2';
8+
$dump = fopen("compress.bzip2://$filename", 'r');
9+
while (($line = fgets($dump)) !== False)
10+
{
11+
$line = trim($line);
12+
if ($line)
13+
{
14+
$this->db->query("$line\n");
15+
}
16+
}
17+
fclose($dump);
18+
}
19+
}
29.7 MB
Binary file not shown.

application/libraries/Seeder.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Part of ci-phpunit-test
4+
*
5+
* @author Kenji Suzuki <https://github.com/kenjis>
6+
* @license MIT License
7+
* @copyright 2015 Kenji Suzuki
8+
* @link https://github.com/kenjis/ci-phpunit-test
9+
*/
10+
11+
class Seeder
12+
{
13+
/**
14+
* @var CI_Controller
15+
*/
16+
private $CI;
17+
18+
/**
19+
* @var CI_DB_query_builder
20+
*/
21+
protected $db;
22+
23+
/**
24+
* @var CI_DB_forge
25+
*/
26+
protected $dbforge;
27+
28+
/**
29+
* @var string
30+
*/
31+
protected $seedPath;
32+
33+
/**
34+
* @var array
35+
*/
36+
protected $depends = [];
37+
38+
public function __construct()
39+
{
40+
$this->CI =& get_instance();
41+
$this->CI->load->database();
42+
$this->CI->load->dbforge();
43+
$this->db = $this->CI->db;
44+
$this->dbforge = $this->CI->dbforge;
45+
}
46+
47+
/**
48+
* Run another seeder
49+
*
50+
* @param string $seeder Seeder classname
51+
* @param bool $callDependencies
52+
*/
53+
public function call($seeder, $callDependencies = true)
54+
{
55+
if ($this->seedPath === null)
56+
{
57+
$this->seedPath = APPPATH . 'database/seeds/';
58+
}
59+
60+
$obj = $this->loadSeeder($seeder);
61+
if ($callDependencies === true && $obj instanceof Seeder) {
62+
$obj->callDependencies($this->seedPath);
63+
}
64+
$obj->run();
65+
}
66+
67+
/**
68+
* Get Seeder instance
69+
*
70+
* @param string $seeder
71+
* @return Seeder
72+
*/
73+
protected function loadSeeder($seeder)
74+
{
75+
$file = $this->seedPath . $seeder . '.php';
76+
require_once $file;
77+
78+
return new $seeder;
79+
}
80+
81+
/**
82+
* Call dependency seeders
83+
*
84+
* @param string $seedPath
85+
*/
86+
public function callDependencies($seedPath)
87+
{
88+
foreach ($this->depends as $path => $seeders) {
89+
$this->seedPath = $seedPath;
90+
if (is_string($path)) {
91+
$this->setPath($path);
92+
}
93+
94+
$this->callDependency($seeders);
95+
}
96+
$this->setPath($seedPath);
97+
}
98+
99+
/**
100+
* Call dependency seeder
101+
*
102+
* @param string|array $seederName
103+
*/
104+
protected function callDependency($seederName)
105+
{
106+
if (is_array($seederName)) {
107+
array_map([$this, 'callDependency'], $seederName);
108+
return;
109+
}
110+
111+
$seeder = $this->loadSeeder($seederName);
112+
if (is_string($this->seedPath)) {
113+
$seeder->setPath($this->seedPath);
114+
}
115+
116+
$seeder->call($seederName, true);
117+
}
118+
119+
/**
120+
* Set path for seeder files
121+
*
122+
* @param string $path
123+
*/
124+
public function setPath($path)
125+
{
126+
$this->seedPath = rtrim($path, '/').'/';
127+
}
128+
129+
public function __get($property)
130+
{
131+
return $this->CI->$property;
132+
}
133+
}

application/tests/DbTestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22

33
class DbTestCase extends CIPHPUnitTestDbTestCase
44
{
5+
public static function setUpBeforeClass(): void
6+
{
7+
parent::setUpBeforeClass();
8+
9+
$CI =& get_instance();
10+
$CI->load->library('Seeder');
11+
$CI->seeder->call('DatabaseSeeder');
12+
}
513
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
class API_test extends DbTestCase
4+
{
5+
6+
public function test_coverart() {
7+
$output = $this->request(
8+
'GET',
9+
'/api/feed/audiobooks/id/52',
10+
[
11+
'coverart' => 1,
12+
'format' => 'json',
13+
]
14+
);
15+
$books = json_decode($output);
16+
$this->assertEquals(
17+
$books[0]['coverart_jpg'],
18+
'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110.jpg'
19+
);
20+
$this->assertEquals(
21+
$books[0]['coverart_pdf'],
22+
'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110.pdf'
23+
);
24+
$this->assertEquals(
25+
$books[0]['coverart_thumbnail'],
26+
'https://www.archive.org/download/LibrivoxCdCoverArt12/Letters_Two_Brides_1110_thumb.jpg'
27+
);
28+
}
29+
}
30+
31+
?>

0 commit comments

Comments
 (0)