|
6 | 6 |
|
7 | 7 | use Atk4\Data\Exception; |
8 | 8 | use Atk4\Data\Model; |
| 9 | +use Atk4\Data\Persistence\Sql\Expression; |
9 | 10 | use Atk4\Data\Schema\TestCase; |
10 | 11 |
|
11 | 12 | class SqlTest extends TestCase |
@@ -236,4 +237,64 @@ public function testExport(): void |
236 | 237 | ['surname' => 'Jones'], |
237 | 238 | ], $m->export(['surname'])); |
238 | 239 | } |
| 240 | + |
| 241 | + public function testSameRowFieldStability(): void |
| 242 | + { |
| 243 | + $this->setDb([ |
| 244 | + 'user' => [ |
| 245 | + 1 => ['name' => 'John', 'surname' => 'Smith'], |
| 246 | + 2 => ['name' => 'Sarah', 'surname' => 'Jones'], |
| 247 | + ], |
| 248 | + ]); |
| 249 | + |
| 250 | + $m = new Model($this->db, ['table' => 'user']); |
| 251 | + $m->addField('name'); |
| 252 | + $m->addField('surname'); |
| 253 | + $randSqlFunc = [ |
| 254 | + 'sqlite' => 'random()', |
| 255 | + 'mysql' => 'rand()', |
| 256 | + 'postgresql' => 'random()', |
| 257 | + 'mssql' => 'checksum(newid())', |
| 258 | + 'oracle' => 'dbms_random.random', |
| 259 | + ][$this->getDatabasePlatform()->getName()]; |
| 260 | + $m->addExpression('rand', $randSqlFunc); |
| 261 | + $m->addExpression('rand_independent', $randSqlFunc); |
| 262 | + $m->scope()->addCondition('rand', '!=', null); |
| 263 | + $m->setOrder('rand'); |
| 264 | + $m->addExpression('rand2', new Expression('([] + 1) - 1', [$m->getField('rand')])); |
| 265 | + $createSeedForSelfHasOne = function (Model $model, string $alias, $joinByFieldName) { |
| 266 | + return ['model' => $model, 'table_alias' => $alias, 'our_field' => $joinByFieldName, 'their_field' => $joinByFieldName]; |
| 267 | + }; |
| 268 | +// $m->hasOne('one', $createSeedForSelfHasOne($m, 'one', 'name')) |
| 269 | +// ->addField('rand3', 'rand2'); |
| 270 | +// $m->hasOne('one_one', $createSeedForSelfHasOne($m->ref('one'), 'one_one', 'surname')) |
| 271 | +// ->addField('rand4', 'rand3'); |
| 272 | +// $manyModel = $m/*->ref('one')*/; // TODO MySQL Subquery returns more than 1 row |
| 273 | +// $manyModel->addExpression('rand_many', ['expr' => $manyModel->getField('rand3')]); |
| 274 | +// $m->hasMany('many_one', ['model' => $manyModel, 'our_field' => 'name', 'their_field' => 'name']); |
| 275 | +// $m->hasOne('one_many_one', $createSeedForSelfHasOne($m->ref('many_one'), 'one_many_one', 'surname')) |
| 276 | +// ->addField('rand5', 'rand_many'); |
| 277 | + |
| 278 | + $this->debug = true; // TODO |
| 279 | + |
| 280 | + $export = $m->export(); |
| 281 | + $this->assertSame([0, 1], array_keys($export)); |
| 282 | + $randRow0 = $export[0]['rand']; |
| 283 | + $randRow1 = $export[1]['rand']; |
| 284 | + $this->assertNotSame($randRow0, $randRow1); // $this->assertGreaterThan($randRow0, $randRow1); |
| 285 | + // TODO this can be the same, depending on how we implement it |
| 286 | + // already stable under some circumstances on PostgreSQL http://sqlfiddle.com/#!17/4b040/4 |
| 287 | + // $this->assertNotSame($randRow0, $export[0]['rand_independent']); |
| 288 | + |
| 289 | + $this->assertSame($randRow0, $export[0]['rand2']); |
| 290 | + $this->assertSame($randRow1, $export[1]['rand2']); |
| 291 | +// $this->assertSame($randRow0, $export[0]['rand3']); |
| 292 | +// $this->assertSame($randRow1, $export[1]['rand3']); |
| 293 | +// $this->assertSame($randRow0, $export[0]['rand4']); |
| 294 | +// $this->assertSame($randRow1, $export[1]['rand4']); |
| 295 | +// $this->assertSame($randRow0, $export[0]['rand5']); |
| 296 | +// $this->assertSame($randRow1, $export[1]['rand5']); |
| 297 | + |
| 298 | + // TODO test with hasOne group by |
| 299 | + } |
239 | 300 | } |
0 commit comments