4 * Tests for BatchRowUpdate and its components
8 class BatchRowUpdateTest
extends MediaWikiTestCase
{
10 public function testWriterBasicFunctionality() {
11 $db = $this->mockDb();
12 $writer = new BatchRowWriter( $db, 'echo_event' );
15 self
::mockUpdate( [ 'something' => 'changed' ] ),
16 self
::mockUpdate( [ 'otherthing' => 'changed' ] ),
17 self
::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
20 $db->expects( $this->exactly( count( $updates ) ) )
23 $writer->write( $updates );
26 protected static function mockUpdate( array $changes ) {
29 'primaryKey' => [ 'event_id' => $i++
],
30 'changes' => $changes,
34 public function testReaderBasicIterate() {
35 $db = $this->mockDb();
37 $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
39 $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function() {
41 return [ 'id_field' => ++
$i ];
43 $db->expects( $this->exactly( count( $response ) ) )
45 ->will( $this->consecutivelyReturnFromSelect( $response ) );
48 foreach ( $reader as $rows ) {
49 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
52 // -1 is because the final array() marks the end and isnt included
53 $this->assertEquals( count( $response ) - 1, $pos );
56 public static function provider_readerGetPrimaryKey() {
59 'some_col' => 'dvorak',
60 'other_col' => 'samurai',
65 'Must return single column pk when requested',
71 'Must return multiple column pks when requested',
72 [ 'id_field' => 42, 'other_col' => 'samurai' ],
80 * @dataProvider provider_readerGetPrimaryKey
82 public function testReaderGetPrimaryKey( $message, array $expected, array $row ) {
83 $reader = new BatchRowIterator( $this->mockDb(), 'some_table', array_keys( $expected ), 8675309 );
84 $this->assertEquals( $expected, $reader->extractPrimaryKeys( (object)$row ), $message );
87 public static function provider_readerSetFetchColumns() {
91 'Must merge primary keys into select conditions',
92 // Expected column select
101 'Must not merge primary keys into the all columns selector',
102 // Expected column select
111 'Must not duplicate primary keys into column selector',
112 // Expected column select.
113 // TODO: figure out how to only assert the array_values portion and not the keys
114 [ 0 => 'foo', 1 => 'bar', 3 => 'baz' ],
124 * @dataProvider provider_readerSetFetchColumns
126 public function testReaderSetFetchColumns(
127 $message, array $columns, array $primaryKeys, array $fetchColumns
129 $db = $this->mockDb();
130 $db->expects( $this->once() )
132 // only testing second parameter of Database::select
133 ->with( 'some_table', $columns )
134 ->will( $this->returnValue( new ArrayIterator( [] ) ) );
136 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
137 $reader->setFetchColumns( $fetchColumns );
138 // triggers first database select
142 public static function provider_readerSelectConditions() {
146 "With single primary key must generate id > 'value'",
147 // Expected second iteration
148 [ "( id_field > '3' )" ],
154 'With multiple primary keys the first conditions ' .
155 'must use >= and the final condition must use >',
156 // Expected second iteration
157 [ "( id_field = '3' AND foo > '103' ) OR ( id_field > '3' )" ],
159 [ 'id_field', 'foo' ],
166 * Slightly hackish to use reflection, but asserting different parameters
167 * to consecutive calls of Database::select in phpunit is error prone
169 * @dataProvider provider_readerSelectConditions
171 public function testReaderSelectConditionsMultiplePrimaryKeys(
172 $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
174 $results = $this->genSelectResult( $batchSize, $batchSize * 3, function() {
175 static $i = 0, $j = 100, $k = 1000;
176 return [ 'id_field' => ++
$i, 'foo' => ++
$j, 'bar' => ++
$k ];
178 $db = $this->mockDbConsecutiveSelect( $results );
180 $conditions = [ 'bar' => 42, 'baz' => 'hai' ];
181 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
182 $reader->addConditions( $conditions );
184 $buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
185 $buildConditions->setAccessible( true );
187 // On first iteration only the passed conditions must be used
188 $this->assertEquals( $conditions, $buildConditions->invoke( $reader ),
189 'First iteration must return only the conditions passed in addConditions' );
192 // Second iteration must use the maximum primary key of last set
194 $conditions +
$expectedSecondIteration,
195 $buildConditions->invoke( $reader ),
200 protected function mockDbConsecutiveSelect( array $retvals ) {
201 $db = $this->mockDb();
202 $db->expects( $this->any() )
204 ->will( $this->consecutivelyReturnFromSelect( $retvals ) );
205 $db->expects( $this->any() )
206 ->method( 'addQuotes' )
207 ->will( $this->returnCallback( function( $value ) {
208 return "'$value'"; // not real quoting: doesn't matter in test
214 protected function consecutivelyReturnFromSelect( array $results ) {
216 foreach ( $results as $rows ) {
217 // The Database::select method returns iterators, so we do too.
218 $retvals[] = $this->returnValue( new ArrayIterator( $rows ) );
221 return call_user_func_array( [ $this, 'onConsecutiveCalls' ], $retvals );
224 protected function genSelectResult( $batchSize, $numRows, $rowGenerator ) {
226 for ( $i = 0; $i < $numRows; $i +
= $batchSize ) {
228 for ( $j = 0; $j < $batchSize && $i +
$j < $numRows; $j++
) {
229 $rows [] = (object)call_user_func( $rowGenerator );
233 $res[] = []; // termination condition requires empty result for last row
237 protected function mockDb() {
238 // @TODO: mock from Database
239 // FIXME: the constructor normally sets mAtomicLevels and mSrvCache
240 $databaseMysql = $this->getMockBuilder( 'DatabaseMysqli' )
241 ->disableOriginalConstructor()
243 $databaseMysql->expects( $this->any() )
245 ->will( $this->returnValue( true ) );
246 $databaseMysql->expects( $this->any() )
247 ->method( 'getApproximateLagStatus' )
248 ->will( $this->returnValue( [ 'lag' => 0, 'since' => 0 ] ) );
249 return $databaseMysql;