3 use Wikimedia\Rdbms\FakeResultWrapper
;
4 use Wikimedia\Rdbms\Platform\SQLPlatform
;
5 use Wikimedia\Rdbms\SelectQueryBuilder
;
8 * Tests for BatchRowUpdate and its components
12 * @covers \BatchRowUpdate
13 * @covers \BatchRowIterator
14 * @covers \BatchRowWriter
16 class BatchRowUpdateTest
extends MediaWikiIntegrationTestCase
{
18 public function testWriterBasicFunctionality() {
19 $db = $this->mockDb( [ 'update' ] );
20 $writer = new BatchRowWriter( $db, 'echo_event' );
23 self
::mockUpdate( [ 'something' => 'changed' ] ),
24 self
::mockUpdate( [ 'otherthing' => 'changed' ] ),
25 self
::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
28 $db->expects( $this->exactly( count( $updates ) ) )
31 $writer->write( $updates );
34 protected static function mockUpdate( array $changes ) {
37 'primaryKey' => [ 'event_id' => $i++
],
38 'changes' => $changes,
42 public function testReaderBasicIterate() {
44 $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, static function () {
46 return [ 'id_field' => ++
$i ];
48 $db = $this->mockDbConsecutiveSelect( $response );
49 $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
52 foreach ( $reader as $rows ) {
53 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
56 // -1 is because the final [] marks the end and isn't 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 [ 'foo', 'bar', 'baz' ],
127 * @dataProvider provider_readerSetFetchColumns
129 public function testReaderSetFetchColumns(
130 $message, array $columns, array $primaryKeys, array $fetchColumns
132 $db = $this->mockDb( [ 'select' ] );
133 $db->expects( $this->once() )
135 // only testing second parameter of Database::select
136 ->with( [ 'some_table' ], $columns )
137 ->willReturn( new FakeResultWrapper( [] ) );
139 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
140 $reader->setFetchColumns( $fetchColumns );
141 // triggers first database select
145 public static function provider_readerSelectConditions() {
149 "With single primary key must generate id > 'value'",
150 // Expected second iteration
151 [ "id_field > '3'" ],
157 'With multiple primary keys the first conditions ' .
158 'must use >= and the final condition must use >',
159 // Expected second iteration
160 [ "id_field > '3' OR (id_field = '3' AND (foo > '103'))" ],
162 [ 'id_field', 'foo' ],
169 * Slightly hackish to use reflection, but asserting different parameters
170 * to consecutive calls of Database::select in phpunit is error prone
172 * @dataProvider provider_readerSelectConditions
174 public function testReaderSelectConditionsMultiplePrimaryKeys(
175 $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
177 $results = $this->genSelectResult( $batchSize, $batchSize * 3, static function () {
178 static $i = 0, $j = 100, $k = 1000;
179 return [ 'id_field' => ++
$i, 'foo' => ++
$j, 'bar' => ++
$k ];
181 $db = $this->mockDbConsecutiveSelect( $results );
183 $conditions = [ 'bar' => 42, 'baz' => 'hai' ];
184 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
185 $reader->addConditions( $conditions );
187 $buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
188 $buildConditions->setAccessible( true );
190 // On first iteration only the passed conditions must be used
191 $this->assertEquals( $conditions, $buildConditions->invoke( $reader ),
192 'First iteration must return only the conditions passed in addConditions' );
195 // Second iteration must use the maximum primary key of last set
197 $conditions +
$expectedSecondIteration,
198 $buildConditions->invoke( $reader ),
203 protected function mockDbConsecutiveSelect( array $retvals ) {
204 $db = $this->mockDb( [ 'select', 'newSelectQueryBuilder', 'addQuotes' ] );
205 $db->method( 'newSelectQueryBuilder' )->willReturnCallback( static function () use ( $db ) {
206 return new SelectQueryBuilder( $db );
208 $db->method( 'select' )
209 ->will( $this->consecutivelyReturnFromSelect( $retvals ) );
210 $db->method( 'addQuotes' )
211 ->willReturnCallback( static 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 result wrapper, so we do too.
222 $retvals[] = $this->returnValue( new FakeResultWrapper( $rows ) );
225 return $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)$rowGenerator();
237 $res[] = []; // termination condition requires empty result for last row
241 protected function mockDb( $methods = [] ) {
242 // @TODO: mock from Database
243 // FIXME: the constructor normally sets mAtomicLevels and mSrvCache, and platform
244 $databaseMysql = $this->getMockBuilder( Wikimedia\Rdbms\DatabaseMySQL
::class )
245 ->disableOriginalConstructor()
246 ->onlyMethods( array_merge( [ 'isOpen' ], $methods ) )
249 $reflection = new ReflectionClass( $databaseMysql );
250 $reflectionProperty = $reflection->getProperty( 'platform' );
251 $reflectionProperty->setAccessible( true );
252 $reflectionProperty->setValue( $databaseMysql, new SQLPlatform( $databaseMysql ) );
254 $databaseMysql->method( 'isOpen' )
255 ->willReturn( true );
256 return $databaseMysql;