Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / utils / BatchRowUpdateTest.php
blobaf3fc515bed6234dfec2a33362d5e72ef9dcf0c2
1 <?php
3 use Wikimedia\Rdbms\FakeResultWrapper;
4 use Wikimedia\Rdbms\Platform\SQLPlatform;
5 use Wikimedia\Rdbms\SelectQueryBuilder;
7 /**
8 * Tests for BatchRowUpdate and its components
10 * @group db
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' );
22 $updates = [
23 self::mockUpdate( [ 'something' => 'changed' ] ),
24 self::mockUpdate( [ 'otherthing' => 'changed' ] ),
25 self::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
28 $db->expects( $this->exactly( count( $updates ) ) )
29 ->method( 'update' );
31 $writer->write( $updates );
34 protected static function mockUpdate( array $changes ) {
35 static $i = 0;
36 return [
37 'primaryKey' => [ 'event_id' => $i++ ],
38 'changes' => $changes,
42 public function testReaderBasicIterate() {
43 $batchSize = 2;
44 $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, static function () {
45 static $i = 0;
46 return [ 'id_field' => ++$i ];
47 } );
48 $db = $this->mockDbConsecutiveSelect( $response );
49 $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
51 $pos = 0;
52 foreach ( $reader as $rows ) {
53 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
54 $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() {
61 $row = [
62 'id_field' => 42,
63 'some_col' => 'dvorak',
64 'other_col' => 'samurai',
66 return [
69 'Must return single column pk when requested',
70 [ 'id_field' => 42 ],
71 $row
75 'Must return multiple column pks when requested',
76 [ 'id_field' => 42, 'other_col' => 'samurai' ],
77 $row
83 /**
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() {
92 return [
95 'Must merge primary keys into select conditions',
96 // Expected column select
97 [ 'foo', 'bar' ],
98 // primary keys
99 [ 'foo' ],
100 // setFetchColumn
101 [ 'bar' ]
105 'Must not merge primary keys into the all columns selector',
106 // Expected column select
107 [ '*' ],
108 // primary keys
109 [ 'foo' ],
110 // setFetchColumn
111 [ '*' ],
115 'Must not duplicate primary keys into column selector',
116 // Expected column select.
117 [ 'foo', 'bar', 'baz' ],
118 // primary keys
119 [ 'foo', 'bar', ],
120 // setFetchColumn
121 [ '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() )
134 ->method( 'select' )
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
142 $reader->rewind();
145 public static function provider_readerSelectConditions() {
146 return [
149 "With single primary key must generate id > 'value'",
150 // Expected second iteration
151 [ "id_field > '3'" ],
152 // Primary key(s)
153 'id_field',
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'))" ],
161 // Primary key(s)
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 ];
180 } );
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' );
193 $reader->rewind();
195 // Second iteration must use the maximum primary key of last set
196 $this->assertEquals(
197 $conditions + $expectedSecondIteration,
198 $buildConditions->invoke( $reader ),
199 $message
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 );
207 } );
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
213 } );
215 return $db;
218 protected function consecutivelyReturnFromSelect( array $results ) {
219 $retvals = [];
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 ) {
229 $res = [];
230 for ( $i = 0; $i < $numRows; $i += $batchSize ) {
231 $rows = [];
232 for ( $j = 0; $j < $batchSize && $i + $j < $numRows; $j++ ) {
233 $rows[] = (object)$rowGenerator();
235 $res[] = $rows;
237 $res[] = []; // termination condition requires empty result for last row
238 return $res;
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 ) )
247 ->getMock();
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;