Permalink
Browse files

test coverage for queues

  • Loading branch information...
dogmatic69 committed Mar 22, 2013
1 parent ab343a1 commit d5b1af9826ecab880c20bbe5c50724319100347e
Showing with 119 additions and 16 deletions.
  1. +31 −16 Model/InfinitasJobQueue.php
  2. +88 −0 Test/Case/Model/InfinitasJobQueueTest.php
@@ -44,7 +44,8 @@ class InfinitasJobQueue extends InfinitasJobsAppModel {
);
public $findMethods = array(
- 'idFromSlug' => true
+ 'idFromSlug' => true,
+ 'status' => true
);
/**
@@ -95,9 +96,21 @@ protected function _findIdFromSlug($state, $query = array(), $results = array())
$this->alias . '.' . $this->primaryKey
);
- $query['conditions'] = array(
- $this->alias . '.slug' => $query[0]
- );
+ if (substr($query[0], -1) == '*') {
+ $query['conditions'] = array(
+ $this->alias . '.slug LIKE "' . trim($query[0], '*') . '%"'
+ );
+
+ $query['order'] = array(
+ $this->alias . '.pending_job_count' => 'asc'
+ );
+ } else {
+ $query['conditions'] = array(
+ $this->alias . '.slug' => $query[0]
+ );
+ }
+
+ $query['limit'] = 1;
return $query;
}
@@ -120,14 +133,14 @@ protected function _findIdFromSlug($state, $query = array(), $results = array())
*
* @throws InvalidArgumentException when there is no queue specified
*/
- protected function _findStatus($state, $query = array(), $results = array()) {
+ protected function _findStatus($state, $query, $results = array()) {
if($state == 'before') {
if(empty($query[0])) {
throw new InvalidArgumentException('You must specify the queue');
}
$this->virtualFields['total_job_count'] = String::insert(
- 'SUM(:alias.:pending, :alias.:failed, :alias.:locked, :alias.:completed)',
+ ':alias.:pending + :alias.:failed + :alias.:locked + :alias.:completed',
array(
'alias' => $this->alias,
'pending' => 'pending_job_count',
@@ -149,19 +162,21 @@ protected function _findStatus($state, $query = array(), $results = array()) {
$this->alias . '.slug' => $query[0]
);
- unset($query[0]);
-
return $query;
}
- return array(
- 'outstanding' => $results[$this->alias]['pending_job_count'],
- 'locked' => $results[$this->alias]['locked_job_count'],
- 'failed' => $results[$this->alias]['failed_job_count'],
- 'completed' => $results[$this->alias]['completed_job_count'],
- 'total' => $results[$this->alias]['total_job_count']
- );
+ if (empty($results)) {
+ return array();
+ }
+
+ $results = $results[0];
- return $results;
+ return array(
+ 'outstanding' => $results[$this->alias]['pending_job_count'],
+ 'locked' => $results[$this->alias]['locked_job_count'],
+ 'failed' => $results[$this->alias]['failed_job_count'],
+ 'completed' => $results[$this->alias]['completed_job_count'],
+ 'total' => $results[$this->alias]['total_job_count']
+ );
}
}
@@ -12,6 +12,16 @@ class TestInfinitasJobQueue extends CakeTestCase {
'plugin.infinitas_jobs.infinitas_job_log',
);
+ public function setUp() {
+ parent::setUp();
+ $this->InfinitasJobQueue = ClassRegistry::init('InfinitasJobs.InfinitasJobQueue');
+ }
+
+ public function tearDown() {
+ unset($this->InfinitasJobQueue);
+ parent::tearDown();
+ }
+
/**
* @brief Tests Validation
@@ -21,4 +31,82 @@ class TestInfinitasJobQueue extends CakeTestCase {
public function testValidation() {
}
+
+/**
+ * test find id from slug exception
+ *
+ * @expectedException InvalidArgumentException
+ */
+ public function testFindIdFromSlugException() {
+ $this->InfinitasJobQueue->find('idFromSlug');
+ }
+
+/**
+ * test find id from slug
+ */
+ public function testFindIdFromSlug() {
+ $result = $this->InfinitasJobQueue->find('idFromSlug', 'queue1');
+ $this->assertEquals('queue-1', $result);
+
+ $result = $this->InfinitasJobQueue->find('idFromSlug', 'queue2');
+ $this->assertEquals('queue-2', $result);
+
+ $result = $this->InfinitasJobQueue->find('idFromSlug', 'queue*');
+ $this->assertEquals('queue-2', $result);
+
+ $this->assertTrue($this->InfinitasJobQueue->updateAll(
+ array($this->InfinitasJobQueue->alias . '.pending_job_count' => 20),
+ array($this->InfinitasJobQueue->alias . '.id' => 'queue-2')
+ ));
+
+ $result = $this->InfinitasJobQueue->find('idFromSlug', 'queue*');
+ $this->assertEquals('queue-1', $result);
+
+ $result = $this->InfinitasJobQueue->find('idFromSlug', 'foobar');
+ $this->assertFalse($result);
+ }
+
+/**
+ * test find status
+ *
+ * @dataProvider findStatusDataProvider
+ */
+ public function testFindStatus($data, $expected) {
+ $result = $this->InfinitasJobQueue->find('status', $data);
+ $this->assertEquals($expected, $result);
+ }
+
+/**
+ * find status data provider
+ *
+ * @return array
+ */
+ public function findStatusDataProvider() {
+ return array(
+ 'nothing' => array(
+ 'foo-bar',
+ array()
+ ),
+ 'queue 1' => array(
+ 'queue1',
+ array(
+ 'outstanding' => 2,
+ 'locked' => 0,
+ 'failed' => 0,
+ 'completed' => 0,
+ 'total' => 2
+ )
+ ),
+ 'queue 2' => array(
+ 'queue2',
+ array(
+ 'outstanding' => 1,
+ 'locked' => 0,
+ 'failed' => 0,
+ 'completed' => 0,
+ 'total' => 1
+ )
+ )
+ );
+ }
}

0 comments on commit d5b1af9

Please sign in to comment.