4 * Tests for BatchRowUpdate and its components
8 * @covers BatchRowUpdate
9 * @covers BatchRowIterator
10 * @covers BatchRowWriter
12 class BatchRowUpdateTest
extends MediaWikiTestCase
{
14 public function testWriterBasicFunctionality() {
15 $db = $this->mockDb();
16 $writer = new BatchRowWriter( $db, 'echo_event' );
19 self
::mockUpdate( [ 'something' => 'changed' ] ),
20 self
::mockUpdate( [ 'otherthing' => 'changed' ] ),
21 self
::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
24 $db->expects( $this->exactly( count( $updates ) ) )
27 $writer->write( $updates );
30 protected static function mockUpdate( array $changes ) {
33 'primaryKey' => [ 'event_id' => $i++
],
34 'changes' => $changes,
38 public function testReaderBasicIterate() {
39 $db = $this->mockDb();
41 $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
43 $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function () {
45 return [ 'id_field' => ++
$i ];
47 $db->expects( $this->exactly( count( $response ) ) )
49 ->will( $this->consecutivelyReturnFromSelect( $response ) );
52 foreach ( $reader as $rows ) {
53 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
56 // -1 is because the final array() marks the end and isnt included
57 $this->assertEquals( count( $response ) - 1, $pos );
60 public static function provider_readerGetPrimaryKey() {
63 'some_col' => 'dvorak',
64 'other_col' => 'samurai',
69 'Must return single column pk when requested',
75 'Must return multiple column pks when requested',
76 [ 'id_field' => 42, 'other_col' => 'samurai' ],
84 * @dataProvider provider_readerGetPrimaryKey
86 public function testReaderGetPrimaryKey( $message, array $expected, array $row ) {
87 $reader = new BatchRowIterator( $this->mockDb(), 'some_table', array_keys( $expected ), 8675309 );
88 $this->assertEquals( $expected, $reader->extractPrimaryKeys( (object)$row ), $message );
91 public static function provider_readerSetFetchColumns() {
95 'Must merge primary keys into select conditions',
96 // Expected column select
105 'Must not merge primary keys into the all columns selector',
106 // Expected column select
115 'Must not duplicate primary keys into column selector',
116 // Expected column select.
117 // TODO: figure out how to only assert the array_values portion and not the keys
118 [ 0 => 'foo', 1 => 'bar', 3 => 'baz' ],
128 * @dataProvider provider_readerSetFetchColumns
130 public function testReaderSetFetchColumns(
131 $message, array $columns, array $primaryKeys, array $fetchColumns
133 $db = $this->mockDb();
134 $db->expects( $this->once() )
136 // only testing second parameter of Database::select
137 ->with( 'some_table', $columns )
138 ->will( $this->returnValue( new ArrayIterator( [] ) ) );
140 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
141 $reader->setFetchColumns( $fetchColumns );
142 // triggers first database select
146 public static function provider_readerSelectConditions() {
150 "With single primary key must generate id > 'value'",
151 // Expected second iteration
152 [ "( id_field > '3' )" ],
158 'With multiple primary keys the first conditions ' .
159 'must use >= and the final condition must use >',
160 // Expected second iteration
161 [ "( id_field = '3' AND foo > '103' ) OR ( id_field > '3' )" ],
163 [ 'id_field', 'foo' ],
170 * Slightly hackish to use reflection, but asserting different parameters
171 * to consecutive calls of Database::select in phpunit is error prone
173 * @dataProvider provider_readerSelectConditions
175 public function testReaderSelectConditionsMultiplePrimaryKeys(
176 $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
178 $results = $this->genSelectResult( $batchSize, $batchSize * 3, function () {
179 static $i = 0, $j = 100, $k = 1000;
180 return [ 'id_field' => ++
$i, 'foo' => ++
$j, 'bar' => ++
$k ];
182 $db = $this->mockDbConsecutiveSelect( $results );
184 $conditions = [ 'bar' => 42, 'baz' => 'hai' ];
185 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
186 $reader->addConditions( $conditions );
188 $buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
189 $buildConditions->setAccessible( true );
191 // On first iteration only the passed conditions must be used
192 $this->assertEquals( $conditions, $buildConditions->invoke( $reader ),
193 'First iteration must return only the conditions passed in addConditions' );
196 // Second iteration must use the maximum primary key of last set
198 $conditions +
$expectedSecondIteration,
199 $buildConditions->invoke( $reader ),
204 protected function mockDbConsecutiveSelect( array $retvals ) {
205 $db = $this->mockDb();
206 $db->expects( $this->any() )
208 ->will( $this->consecutivelyReturnFromSelect( $retvals ) );
209 $db->expects( $this->any() )
210 ->method( 'addQuotes' )
211 ->will( $this->returnCallback( function ( $value ) {
212 return "'$value'"; // not real quoting: doesn't matter in test
218 protected function consecutivelyReturnFromSelect( array $results ) {
220 foreach ( $results as $rows ) {
221 // The Database::select method returns iterators, so we do too.
222 $retvals[] = $this->returnValue( new ArrayIterator( $rows ) );
225 return call_user_func_array( [ $this, 'onConsecutiveCalls' ], $retvals );
228 protected function genSelectResult( $batchSize, $numRows, $rowGenerator ) {
230 for ( $i = 0; $i < $numRows; $i +
= $batchSize ) {
232 for ( $j = 0; $j < $batchSize && $i +
$j < $numRows; $j++
) {
233 $rows [] = (object)call_user_func( $rowGenerator );
237 $res[] = []; // termination condition requires empty result for last row
241 protected function mockDb() {
242 // @TODO: mock from Database
243 // FIXME: the constructor normally sets mAtomicLevels and mSrvCache
244 $databaseMysql = $this->getMockBuilder( 'DatabaseMysqli' )
245 ->disableOriginalConstructor()
247 $databaseMysql->expects( $this->any() )
249 ->will( $this->returnValue( true ) );
250 $databaseMysql->expects( $this->any() )
251 ->method( 'getApproximateLagStatus' )
252 ->will( $this->returnValue( [ 'lag' => 0, 'since' => 0 ] ) );
253 return $databaseMysql;