Debian Codegraph @

tests/phpunit/includes/utils/BatchRowUpdateTest.php

raw symbols SCIP
1 <?php
2
3 use Wikimedia\Rdbms\FakeResultWrapper;
4 use Wikimedia\Rdbms\Platform\SQLPlatform;
5 use Wikimedia\Rdbms\SelectQueryBuilder;
6
7 /**
8 * Tests for BatchRowUpdate and its components
9 *
10 * @group db
11 *
12 * @covers \BatchRowUpdate
13 * @covers \BatchRowIterator
14 * @covers \BatchRowWriter
15 */
16 class BatchRowUpdateTest extends MediaWikiIntegrationTestCase {
17
18 public function testWriterBasicFunctionality() {
19 $db = $this->mockDb( [ 'update' ] );
20 $writer = new BatchRowWriter( $db, 'echo_event' );
21
22 $updates = [
23 self::mockUpdate( [ 'something' => 'changed' ] ),
24 self::mockUpdate( [ 'otherthing' => 'changed' ] ),
25 self::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
26 ];
27
28 $db->expects( $this->exactly( count( $updates ) ) )
29 ->method( 'update' );
30
31 $writer->write( $updates );
32 }
33
34 protected static function mockUpdate( array $changes ) {
35 static $i = 0;
36 return [
37 'primaryKey' => [ 'event_id' => $i++ ],
38 'changes' => $changes,
39 ];
40 }
41
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 );
50
51 $pos = 0;
52 foreach ( $reader as $rows ) {
53 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
54 $pos++;
55 }
56 // -1 is because the final [] marks the end and isn't included
57 $this->assertEquals( count( $response ) - 1, $pos );
58 }
59
60 public static function provider_readerGetPrimaryKey() {
61 $row = [
62 'id_field' => 42,
63 'some_col' => 'dvorak',
64 'other_col' => 'samurai',
65 ];
66 return [
67
68 [
69 'Must return single column pk when requested',
70 [ 'id_field' => 42 ],
71 $row
72 ],
73
74 [
75 'Must return multiple column pks when requested',
76 [ 'id_field' => 42, 'other_col' => 'samurai' ],
77 $row
78 ],
79
80 ];
81 }
82
83 /**
84 * @dataProvider provider_readerGetPrimaryKey
85 */
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 );
89 }
90
91 public static function provider_readerSetFetchColumns() {
92 return [
93
94 [
95 'Must merge primary keys into select conditions',
96 // Expected column select
97 [ 'foo', 'bar' ],
98 // primary keys
99 [ 'foo' ],
100 // setFetchColumn
101 [ 'bar' ]
102 ],
103
104 [
105 'Must not merge primary keys into the all columns selector',
106 // Expected column select
107 [ '*' ],
108 // primary keys
109 [ 'foo' ],
110 // setFetchColumn
111 [ '*' ],
112 ],
113
114 [
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' ],
122 ],
123 ];
124 }
125
126 /**
127 * @dataProvider provider_readerSetFetchColumns
128 */
129 public function testReaderSetFetchColumns(
130 $message, array $columns, array $primaryKeys, array $fetchColumns
131 ) {
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( [] ) );
138
139 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
140 $reader->setFetchColumns( $fetchColumns );
141 // triggers first database select
142 $reader->rewind();
143 }
144
145 public static function provider_readerSelectConditions() {
146 return [
147
148 [
149 "With single primary key must generate id > 'value'",
150 // Expected second iteration
151 [ "id_field > '3'" ],
152 // Primary key(s)
153 'id_field',
154 ],
155
156 [
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' ],
163 ],
164
165 ];
166 }
167
168 /**
169 * Slightly hackish to use reflection, but asserting different parameters
170 * to consecutive calls of Database::select in phpunit is error prone
171 *
172 * @dataProvider provider_readerSelectConditions
173 */
174 public function testReaderSelectConditionsMultiplePrimaryKeys(
175 $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
176 ) {
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 );
182
183 $conditions = [ 'bar' => 42, 'baz' => 'hai' ];
184 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
185 $reader->addConditions( $conditions );
186
187 $buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
188
189 // On first iteration only the passed conditions must be used
190 $this->assertEquals( $conditions, $buildConditions->invoke( $reader ),
191 'First iteration must return only the conditions passed in addConditions' );
192 $reader->rewind();
193
194 // Second iteration must use the maximum primary key of last set
195 $this->assertEquals(
196 $conditions + $expectedSecondIteration,
197 $buildConditions->invoke( $reader ),
198 $message
199 );
200 }
201
202 protected function mockDbConsecutiveSelect( array $retvals ) {
203 $db = $this->mockDb( [ 'select', 'newSelectQueryBuilder', 'addQuotes' ] );
204 $db->method( 'newSelectQueryBuilder' )->willReturnCallback( static function () use ( $db ) {
205 return new SelectQueryBuilder( $db );
206 } );
207 $db->method( 'select' )
208 ->will( $this->consecutivelyReturnFromSelect( $retvals ) );
209 $db->method( 'addQuotes' )
210 ->willReturnCallback( static function ( $value ) {
211 return "'$value'"; // not real quoting: doesn't matter in test
212 } );
213
214 return $db;
215 }
216
217 protected function consecutivelyReturnFromSelect( array $results ) {
218 $retvals = [];
219 foreach ( $results as $rows ) {
220 // The Database::select method returns result wrapper, so we do too.
221 $retvals[] = $this->returnValue( new FakeResultWrapper( $rows ) );
222 }
223
224 return $this->onConsecutiveCalls( ...$retvals );
225 }
226
227 protected function genSelectResult( $batchSize, $numRows, $rowGenerator ) {
228 $res = [];
229 for ( $i = 0; $i < $numRows; $i += $batchSize ) {
230 $rows = [];
231 for ( $j = 0; $j < $batchSize && $i + $j < $numRows; $j++ ) {
232 $rows[] = (object)$rowGenerator();
233 }
234 $res[] = $rows;
235 }
236 $res[] = []; // termination condition requires empty result for last row
237 return $res;
238 }
239
240 protected function mockDb( $methods = [] ) {
241 // @TODO: mock from Database
242 // FIXME: the constructor normally sets mAtomicLevels and mSrvCache, and platform
243 $databaseMysql = $this->getMockBuilder( Wikimedia\Rdbms\DatabaseMySQL::class )
244 ->disableOriginalConstructor()
245 ->onlyMethods( array_merge( [ 'isOpen' ], $methods ) )
246 ->getMock();
247
248 $reflection = new ReflectionClass( $databaseMysql );
249 $reflectionProperty = $reflection->getProperty( 'platform' );
250 $reflectionProperty->setValue( $databaseMysql, new SQLPlatform( $databaseMysql ) );
251
252 $databaseMysql->method( 'isOpen' )
253 ->willReturn( true );
254 return $databaseMysql;
255 }
256 }