Separate MediaWiki unit and integration tests
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / utils / BatchRowUpdateTest.php
1 <?php
2
3 use Wikimedia\Rdbms\ILBFactory;
4
5 /**
6 * Tests for BatchRowUpdate and its components
7 *
8 * @group db
9 *
10 * @covers BatchRowUpdate
11 * @covers BatchRowIterator
12 * @covers BatchRowWriter
13 */
14 class BatchRowUpdateTest extends \MediaWikiUnitTestCase {
15
16 public function testWriterBasicFunctionality() {
17 $lbFactoryMock = $this->createMock( ILBFactory::class );
18 $lbFactoryMockProvider = function () use ( $lbFactoryMock ): ILBFactory {
19 return $lbFactoryMock;
20 };
21
22 $this->overrideMwServices( [ 'DBLoadBalancerFactory' => $lbFactoryMockProvider ] );
23
24 $db = $this->mockDb( [ 'update' ] );
25 $writer = new BatchRowWriter( $db, 'echo_event' );
26
27 $updates = [
28 self::mockUpdate( [ 'something' => 'changed' ] ),
29 self::mockUpdate( [ 'otherthing' => 'changed' ] ),
30 self::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
31 ];
32
33 $ticketMock = 'transaction-ticket';
34
35 $db->expects( $this->exactly( count( $updates ) ) )
36 ->method( 'update' );
37 $lbFactoryMock->expects( $this->any() )
38 ->method( 'getEmptyTransactionTicket' )
39 ->willReturn( $ticketMock );
40 $lbFactoryMock->expects( $this->once() )
41 ->method( 'commitAndWaitForReplication' )
42 ->with( $this->anything(), $ticketMock );
43
44 $writer->write( $updates );
45 }
46
47 protected static function mockUpdate( array $changes ) {
48 static $i = 0;
49 return [
50 'primaryKey' => [ 'event_id' => $i++ ],
51 'changes' => $changes,
52 ];
53 }
54
55 public function testReaderBasicIterate() {
56 $batchSize = 2;
57 $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function () {
58 static $i = 0;
59 return [ 'id_field' => ++$i ];
60 } );
61 $db = $this->mockDbConsecutiveSelect( $response );
62 $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
63
64 $pos = 0;
65 foreach ( $reader as $rows ) {
66 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
67 $pos++;
68 }
69 // -1 is because the final array() marks the end and isnt included
70 $this->assertEquals( count( $response ) - 1, $pos );
71 }
72
73 public static function provider_readerGetPrimaryKey() {
74 $row = [
75 'id_field' => 42,
76 'some_col' => 'dvorak',
77 'other_col' => 'samurai',
78 ];
79 return [
80
81 [
82 'Must return single column pk when requested',
83 [ 'id_field' => 42 ],
84 $row
85 ],
86
87 [
88 'Must return multiple column pks when requested',
89 [ 'id_field' => 42, 'other_col' => 'samurai' ],
90 $row
91 ],
92
93 ];
94 }
95
96 /**
97 * @dataProvider provider_readerGetPrimaryKey
98 */
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 );
102 }
103
104 public static function provider_readerSetFetchColumns() {
105 return [
106
107 [
108 'Must merge primary keys into select conditions',
109 // Expected column select
110 [ 'foo', 'bar' ],
111 // primary keys
112 [ 'foo' ],
113 // setFetchColumn
114 [ 'bar' ]
115 ],
116
117 [
118 'Must not merge primary keys into the all columns selector',
119 // Expected column select
120 [ '*' ],
121 // primary keys
122 [ 'foo' ],
123 // setFetchColumn
124 [ '*' ],
125 ],
126
127 [
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' ],
132 // primary keys
133 [ 'foo', 'bar', ],
134 // setFetchColumn
135 [ 'bar', 'baz' ],
136 ],
137 ];
138 }
139
140 /**
141 * @dataProvider provider_readerSetFetchColumns
142 */
143 public function testReaderSetFetchColumns(
144 $message, array $columns, array $primaryKeys, array $fetchColumns
145 ) {
146 $db = $this->mockDb( [ 'select' ] );
147 $db->expects( $this->once() )
148 ->method( 'select' )
149 // only testing second parameter of Database::select
150 ->with( 'some_table', $columns )
151 ->will( $this->returnValue( new ArrayIterator( [] ) ) );
152
153 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
154 $reader->setFetchColumns( $fetchColumns );
155 // triggers first database select
156 $reader->rewind();
157 }
158
159 public static function provider_readerSelectConditions() {
160 return [
161
162 [
163 "With single primary key must generate id > 'value'",
164 // Expected second iteration
165 [ "( id_field > '3' )" ],
166 // Primary key(s)
167 'id_field',
168 ],
169
170 [
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' )" ],
175 // Primary key(s)
176 [ 'id_field', 'foo' ],
177 ],
178
179 ];
180 }
181
182 /**
183 * Slightly hackish to use reflection, but asserting different parameters
184 * to consecutive calls of Database::select in phpunit is error prone
185 *
186 * @dataProvider provider_readerSelectConditions
187 */
188 public function testReaderSelectConditionsMultiplePrimaryKeys(
189 $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
190 ) {
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 ];
194 } );
195 $db = $this->mockDbConsecutiveSelect( $results );
196
197 $conditions = [ 'bar' => 42, 'baz' => 'hai' ];
198 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
199 $reader->addConditions( $conditions );
200
201 $buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
202 $buildConditions->setAccessible( true );
203
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' );
207 $reader->rewind();
208
209 // Second iteration must use the maximum primary key of last set
210 $this->assertEquals(
211 $conditions + $expectedSecondIteration,
212 $buildConditions->invoke( $reader ),
213 $message
214 );
215 }
216
217 protected function mockDbConsecutiveSelect( array $retvals ) {
218 $db = $this->mockDb( [ 'select', 'addQuotes' ] );
219 $db->expects( $this->any() )
220 ->method( 'select' )
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
226 } ) );
227
228 return $db;
229 }
230
231 protected function consecutivelyReturnFromSelect( array $results ) {
232 $retvals = [];
233 foreach ( $results as $rows ) {
234 // The Database::select method returns iterators, so we do too.
235 $retvals[] = $this->returnValue( new ArrayIterator( $rows ) );
236 }
237
238 return call_user_func_array( [ $this, 'onConsecutiveCalls' ], $retvals );
239 }
240
241 protected function genSelectResult( $batchSize, $numRows, $rowGenerator ) {
242 $res = [];
243 for ( $i = 0; $i < $numRows; $i += $batchSize ) {
244 $rows = [];
245 for ( $j = 0; $j < $batchSize && $i + $j < $numRows; $j++ ) {
246 $rows [] = (object)call_user_func( $rowGenerator );
247 }
248 $res[] = $rows;
249 }
250 $res[] = []; // termination condition requires empty result for last row
251 return $res;
252 }
253
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 ) )
260 ->getMock();
261 $databaseMysql->expects( $this->any() )
262 ->method( 'isOpen' )
263 ->will( $this->returnValue( true ) );
264 $databaseMysql->expects( $this->any() )
265 ->method( 'getApproximateLagStatus' )
266 ->will( $this->returnValue( [ 'lag' => 0, 'since' => 0 ] ) );
267 return $databaseMysql;
268 }
269 }