Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

test coverage for queues

  • Loading branch information...
commit d5b1af9826ecab880c20bbe5c50724319100347e 1 parent ab343a1
Carl Sutton authored
47  Model/InfinitasJobQueue.php
@@ -44,7 +44,8 @@ class InfinitasJobQueue extends InfinitasJobsAppModel {
44 44
 	);
45 45
 
46 46
 	public $findMethods = array(
47  
-		'idFromSlug' => true
  47
+		'idFromSlug' => true,
  48
+		'status' => true
48 49
 	);
49 50
 
50 51
 /**
@@ -95,9 +96,21 @@ protected function _findIdFromSlug($state, $query = array(), $results = array())
95 96
 				$this->alias . '.' . $this->primaryKey
96 97
 			);
97 98
 
98  
-			$query['conditions'] = array(
99  
-				$this->alias . '.slug' => $query[0]
100  
-			);
  99
+			if (substr($query[0], -1) == '*') {
  100
+				$query['conditions'] = array(
  101
+					$this->alias . '.slug LIKE "' . trim($query[0], '*') . '%"'
  102
+				);
  103
+
  104
+				$query['order'] = array(
  105
+					$this->alias . '.pending_job_count' => 'asc'
  106
+				);
  107
+			} else {
  108
+				$query['conditions'] = array(
  109
+					$this->alias . '.slug' => $query[0]
  110
+				);
  111
+			}
  112
+
  113
+			$query['limit'] = 1;
101 114
 
102 115
 			return $query;
103 116
 		}
@@ -120,14 +133,14 @@ protected function _findIdFromSlug($state, $query = array(), $results = array())
120 133
  *
121 134
  * @throws InvalidArgumentException when there is no queue specified
122 135
  */
123  
-	protected function _findStatus($state, $query = array(), $results = array()) {
  136
+	protected function _findStatus($state, $query, $results = array()) {
124 137
 		if($state == 'before') {
125 138
 			if(empty($query[0])) {
126 139
 				throw new InvalidArgumentException('You must specify the queue');
127 140
 			}
128 141
 
129 142
 			$this->virtualFields['total_job_count'] = String::insert(
130  
-				'SUM(:alias.:pending, :alias.:failed, :alias.:locked, :alias.:completed)',
  143
+				':alias.:pending + :alias.:failed + :alias.:locked + :alias.:completed',
131 144
 				array(
132 145
 					'alias' => $this->alias,
133 146
 					'pending' => 'pending_job_count',
@@ -149,19 +162,21 @@ protected function _findStatus($state, $query = array(), $results = array()) {
149 162
 				$this->alias . '.slug' => $query[0]
150 163
 			);
151 164
 
152  
-			unset($query[0]);
153  
-
154 165
 			return $query;
155 166
 		}
156 167
 
157  
-        return array(
158  
-            'outstanding' => $results[$this->alias]['pending_job_count'],
159  
-            'locked' => $results[$this->alias]['locked_job_count'],
160  
-            'failed' => $results[$this->alias]['failed_job_count'],
161  
-            'completed' => $results[$this->alias]['completed_job_count'],
162  
-            'total'  => $results[$this->alias]['total_job_count']
163  
-        );
  168
+		if (empty($results)) {
  169
+			return array();
  170
+		}
  171
+
  172
+		$results = $results[0];
164 173
 
165  
-		return $results;
  174
+		return array(
  175
+			'outstanding' => $results[$this->alias]['pending_job_count'],
  176
+			'locked' => $results[$this->alias]['locked_job_count'],
  177
+			'failed' => $results[$this->alias]['failed_job_count'],
  178
+			'completed' => $results[$this->alias]['completed_job_count'],
  179
+			'total'  => $results[$this->alias]['total_job_count']
  180
+		);
166 181
 	}
167 182
 }
88  Test/Case/Model/InfinitasJobQueueTest.php
@@ -12,6 +12,16 @@ class TestInfinitasJobQueue extends CakeTestCase {
12 12
 		'plugin.infinitas_jobs.infinitas_job_log',
13 13
 	);
14 14
 
  15
+	public function setUp() {
  16
+		parent::setUp();
  17
+		$this->InfinitasJobQueue = ClassRegistry::init('InfinitasJobs.InfinitasJobQueue');
  18
+	}
  19
+
  20
+	public function tearDown() {
  21
+		unset($this->InfinitasJobQueue);
  22
+		parent::tearDown();
  23
+	}
  24
+
15 25
 
16 26
 	/**
17 27
 	 * @brief Tests Validation
@@ -21,4 +31,82 @@ class TestInfinitasJobQueue extends CakeTestCase {
21 31
 	public function testValidation() {
22 32
 		
23 33
 	}
  34
+
  35
+/**
  36
+ * test find id from slug exception
  37
+ * 
  38
+ * @expectedException InvalidArgumentException
  39
+ */
  40
+	public function testFindIdFromSlugException() {
  41
+		$this->InfinitasJobQueue->find('idFromSlug');
  42
+	}
  43
+
  44
+/**
  45
+ * test find id from slug
  46
+ */
  47
+	public function testFindIdFromSlug() {
  48
+		$result = $this->InfinitasJobQueue->find('idFromSlug', 'queue1');
  49
+		$this->assertEquals('queue-1', $result);
  50
+
  51
+		$result = $this->InfinitasJobQueue->find('idFromSlug', 'queue2');
  52
+		$this->assertEquals('queue-2', $result);
  53
+
  54
+		$result = $this->InfinitasJobQueue->find('idFromSlug', 'queue*');
  55
+		$this->assertEquals('queue-2', $result);
  56
+
  57
+		$this->assertTrue($this->InfinitasJobQueue->updateAll(
  58
+			array($this->InfinitasJobQueue->alias . '.pending_job_count' => 20),
  59
+			array($this->InfinitasJobQueue->alias . '.id' => 'queue-2')
  60
+		));
  61
+
  62
+		$result = $this->InfinitasJobQueue->find('idFromSlug', 'queue*');
  63
+		$this->assertEquals('queue-1', $result);
  64
+
  65
+		$result = $this->InfinitasJobQueue->find('idFromSlug', 'foobar');
  66
+		$this->assertFalse($result);
  67
+	}
  68
+
  69
+/**
  70
+ * test find status
  71
+ * 
  72
+ * @dataProvider findStatusDataProvider
  73
+ */
  74
+	public function testFindStatus($data, $expected) {
  75
+		$result = $this->InfinitasJobQueue->find('status', $data);
  76
+		$this->assertEquals($expected, $result);
  77
+	}
  78
+
  79
+/**
  80
+ * find status data provider
  81
+ * 
  82
+ * @return array
  83
+ */
  84
+	public function findStatusDataProvider() {
  85
+		return array(
  86
+			'nothing' => array(
  87
+				'foo-bar',
  88
+				array()
  89
+			),
  90
+			'queue 1' => array(
  91
+				'queue1',
  92
+				array(
  93
+					'outstanding' => 2,
  94
+					'locked' => 0,
  95
+					'failed' => 0,
  96
+					'completed' => 0,
  97
+					'total'  => 2
  98
+				)
  99
+			),
  100
+			'queue 2' => array(
  101
+				'queue2',
  102
+				array(
  103
+					'outstanding' => 1,
  104
+					'locked' => 0,
  105
+					'failed' => 0,
  106
+					'completed' => 0,
  107
+					'total'  => 1
  108
+				)
  109
+			)
  110
+		);
  111
+	}
24 112
 }

0 notes on commit d5b1af9

Please sign in to comment.
Something went wrong with that request. Please try again.