3 use Wikimedia\Rdbms\ILBFactory
;
6 * Tests for BatchRowUpdate and its components
10 * @covers BatchRowUpdate
11 * @covers BatchRowIterator
12 * @covers BatchRowWriter
14 class BatchRowUpdateTest
extends \MediaWikiUnitTestCase
{
16 public function testWriterBasicFunctionality() {
17 $lbFactoryMock = $this->createMock( ILBFactory
::class );
18 $lbFactoryMockProvider = function () use ( $lbFactoryMock ): ILBFactory
{
19 return $lbFactoryMock;
22 $this->overrideMwServices( [ 'DBLoadBalancerFactory' => $lbFactoryMockProvider ] );
24 $db = $this->mockDb( [ 'update' ] );
25 $writer = new BatchRowWriter( $db, 'echo_event' );
28 self
::mockUpdate( [ 'something' => 'changed' ] ),
29 self
::mockUpdate( [ 'otherthing' => 'changed' ] ),
30 self
::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
33 $ticketMock = 'transaction-ticket';
35 $db->expects( $this->exactly( count( $updates ) ) )
37 $lbFactoryMock->expects( $this->any() )
38 ->method( 'getEmptyTransactionTicket' )
39 ->willReturn( $ticketMock );
40 $lbFactoryMock->expects( $this->once() )
41 ->method( 'commitAndWaitForReplication' )
42 ->with( $this->anything(), $ticketMock );
44 $writer->write( $updates );
47 protected static function mockUpdate( array $changes ) {
50 'primaryKey' => [ 'event_id' => $i++
],
51 'changes' => $changes,
55 public function testReaderBasicIterate() {
57 $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function () {
59 return [ 'id_field' => ++
$i ];
61 $db = $this->mockDbConsecutiveSelect( $response );
62 $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
65 foreach ( $reader as $rows ) {
66 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
69 // -1 is because the final array() marks the end and isnt included
70 $this->assertEquals( count( $response ) - 1, $pos );
73 public static function provider_readerGetPrimaryKey() {
76 'some_col' => 'dvorak',
77 'other_col' => 'samurai',
82 'Must return single column pk when requested',
88 'Must return multiple column pks when requested',
89 [ 'id_field' => 42, 'other_col' => 'samurai' ],
97 * @dataProvider provider_readerGetPrimaryKey
99 public function testReaderGetPrimaryKey( $message, array $expected, array $row ) {
100 $reader = new BatchRowIterator( $this->mockDb(), 'some_table', array_keys( $expected ), 8675309 );
101 $this->assertEquals( $expected, $reader->extractPrimaryKeys( (object)$row ), $message );
104 public static function provider_readerSetFetchColumns() {
108 'Must merge primary keys into select conditions',
109 // Expected column select
118 'Must not merge primary keys into the all columns selector',
119 // Expected column select
128 'Must not duplicate primary keys into column selector',
129 // Expected column select.
130 // TODO: figure out how to only assert the array_values portion and not the keys
131 [ 0 => 'foo', 1 => 'bar', 3 => 'baz' ],
141 * @dataProvider provider_readerSetFetchColumns
143 public function testReaderSetFetchColumns(
144 $message, array $columns, array $primaryKeys, array $fetchColumns
146 $db = $this->mockDb( [ 'select' ] );
147 $db->expects( $this->once() )
149 // only testing second parameter of Database::select
150 ->with( 'some_table', $columns )
151 ->will( $this->returnValue( new ArrayIterator( [] ) ) );
153 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
154 $reader->setFetchColumns( $fetchColumns );
155 // triggers first database select
159 public static function provider_readerSelectConditions() {
163 "With single primary key must generate id > 'value'",
164 // Expected second iteration
165 [ "( id_field > '3' )" ],
171 'With multiple primary keys the first conditions ' .
172 'must use >= and the final condition must use >',
173 // Expected second iteration
174 [ "( id_field = '3' AND foo > '103' ) OR ( id_field > '3' )" ],
176 [ 'id_field', 'foo' ],
183 * Slightly hackish to use reflection, but asserting different parameters
184 * to consecutive calls of Database::select in phpunit is error prone
186 * @dataProvider provider_readerSelectConditions
188 public function testReaderSelectConditionsMultiplePrimaryKeys(
189 $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
191 $results = $this->genSelectResult( $batchSize, $batchSize * 3, function () {
192 static $i = 0, $j = 100, $k = 1000;
193 return [ 'id_field' => ++
$i, 'foo' => ++
$j, 'bar' => ++
$k ];
195 $db = $this->mockDbConsecutiveSelect( $results );
197 $conditions = [ 'bar' => 42, 'baz' => 'hai' ];
198 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
199 $reader->addConditions( $conditions );
201 $buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
202 $buildConditions->setAccessible( true );
204 // On first iteration only the passed conditions must be used
205 $this->assertEquals( $conditions, $buildConditions->invoke( $reader ),
206 'First iteration must return only the conditions passed in addConditions' );
209 // Second iteration must use the maximum primary key of last set
211 $conditions +
$expectedSecondIteration,
212 $buildConditions->invoke( $reader ),
217 protected function mockDbConsecutiveSelect( array $retvals ) {
218 $db = $this->mockDb( [ 'select', 'addQuotes' ] );
219 $db->expects( $this->any() )
221 ->will( $this->consecutivelyReturnFromSelect( $retvals ) );
222 $db->expects( $this->any() )
223 ->method( 'addQuotes' )
224 ->will( $this->returnCallback( function ( $value ) {
225 return "'$value'"; // not real quoting: doesn't matter in test
231 protected function consecutivelyReturnFromSelect( array $results ) {
233 foreach ( $results as $rows ) {
234 // The Database::select method returns iterators, so we do too.
235 $retvals[] = $this->returnValue( new ArrayIterator( $rows ) );
238 return call_user_func_array( [ $this, 'onConsecutiveCalls' ], $retvals );
241 protected function genSelectResult( $batchSize, $numRows, $rowGenerator ) {
243 for ( $i = 0; $i < $numRows; $i +
= $batchSize ) {
245 for ( $j = 0; $j < $batchSize && $i +
$j < $numRows; $j++
) {
246 $rows [] = (object)call_user_func( $rowGenerator );
250 $res[] = []; // termination condition requires empty result for last row
254 protected function mockDb( $methods = [] ) {
255 // @TODO: mock from Database
256 // FIXME: the constructor normally sets mAtomicLevels and mSrvCache
257 $databaseMysql = $this->getMockBuilder( Wikimedia\Rdbms\DatabaseMysqli
::class )
258 ->disableOriginalConstructor()
259 ->setMethods( array_merge( [ 'isOpen', 'getApproximateLagStatus' ], $methods ) )
261 $databaseMysql->expects( $this->any() )
263 ->will( $this->returnValue( true ) );
264 $databaseMysql->expects( $this->any() )
265 ->method( 'getApproximateLagStatus' )
266 ->will( $this->returnValue( [ 'lag' => 0, 'since' => 0 ] ) );
267 return $databaseMysql;