Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fab2s committed Sep 18, 2022
1 parent a7cb988 commit 1e027d3
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
branches:
- master
pull_request:
types: [ opened, synchronize, reopened ]
types: [ opened, synchronize ]
jobs:
tests:
name: YaEtl QA
Expand Down
3 changes: 2 additions & 1 deletion src/Extractors/PdoUniqueKeyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ protected function fetchJoinedRecords(): bool
}

$this->joinedRecords = [];
$joinKey = $this->onClose->getJoinKeyAlias();
while ($record = $statement->fetch(\PDO::FETCH_ASSOC)) {
$this->joinedRecords[$record[$this->uniqueKeyName]] = $record;
$this->joinedRecords[$record[$joinKey]] = $record;
}

$statement->closeCursor();
Expand Down
4 changes: 2 additions & 2 deletions src/Extractors/UniqueKeyExtractorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ public function setJoinFrom(JoinableInterface $joinFrom): JoinableInterface
throw new YaEtlException('The extractor joined against is not compatible, expected implementation of: ' . self::class . "\ngot: " . \get_class($joinFrom));
}

if (preg_match('`^.+?order\s+by.*?$`is', $this->extractQuery)) {
/*if (preg_match('`^.+?order\s+by.*?$`is', $this->extractQuery)) {
throw new YaEtlException("A Joiner must not order its query got: $this->extractQuery");
}
}*/

// since we are joining, we are not a traversable anymore
$this->isATraversable = false;
Expand Down
4 changes: 2 additions & 2 deletions src/Laravel/Loaders/DbLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ public function exec($param = null)
{
// clone query object in order to prevent where clause stacking
$loadQuery = clone $this->loadQuery;
$whereClause = \array_intersect_key($param, \array_flip($this->whereFields));
$whereClause = \array_intersect_key($param, \array_flip((array) $this->whereFields));

// let's be atomic while we're at it (and where applicable ...)
// btw, multi insert are not necessarily that faster in real world
// situation where there is a lot of updates and you need ot keep
// atomicity using transactions
DB::transaction(function () use ($loadQuery, $whereClause, $param) {
if ($loadQuery->where($whereClause)->sharedLock()->exists()) {
if (!empty($whereClause) && $loadQuery->where($whereClause)->sharedLock()->exists()) {
$update = \array_diff_key($param, $whereClause);
$loadQuery->update($update);
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/Laravel/DbExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testDbExtractorException()
public function testDbExtractorExceptionType()
{
$this->expectException(YaEtlException::class);
(new DbExtractor())->setExtractQuery(TestModel::query());
(new DbExtractor)->setExtractQuery(TestModel::query());
}

protected function getTestQuery()
Expand Down
62 changes: 62 additions & 0 deletions tests/Laravel/DbLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\Tests\Laravel;

use fab2s\YaEtl\Extractors\CallableExtractor;
use fab2s\YaEtl\Laravel\Extractors\DbExtractor;
use fab2s\YaEtl\Laravel\Loaders\DbLoader;
use fab2s\YaEtl\Loaders\ArrayLoader;
use fab2s\YaEtl\YaEtl;

class DbLoaderTest extends LaravelTestCase
{
public function testDbLoader()
{
$this->createTestJoinModelTable();
$loadQuery = TestJoinModel::getQuery();
$dbLoader = new DbLoader($loadQuery);
$arrayLoader = new ArrayLoader;
(new YaEtl)
->from(new CallableExtractor(function () {
return $this->getTestJoinModelSeedData();
}))
->to($dbLoader)
->exec();

(new YaEtl)
->from(new DbExtractor($loadQuery->orderBy('id')))
->to($arrayLoader)
->exec();

$loadedData = $arrayLoader->getLoadedData();
$this->assertSame($this->getExpectedTestJoinModelData(), $loadedData);

// now go through the update branch
$loadQuery = TestJoinModel::getQuery();
$dbLoader = (new DbLoader)->setLoadQuery($loadQuery);
$arrayLoader = new ArrayLoader;
$dbLoader->setWhereFields(['id']);
$loadedData[0]['join'] = 'updated';

(new YaEtl)
->from(new CallableExtractor(function () use ($loadedData) {
return $loadedData;
}))
->to($dbLoader)
->exec();

(new YaEtl)
->from(new DbExtractor($loadQuery->orderBy('id')))
->to($arrayLoader)
->exec();

$this->assertSame($loadedData, $arrayLoader->getLoadedData());
}
}
78 changes: 71 additions & 7 deletions tests/Laravel/LaravelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ abstract class LaravelTestCase extends TestCase
/**
* @var int
*/
protected $seedNum = 50;
protected $seedNum = 3;

/**
* @var array
*/
protected $testModelSeedData = [];

/**
* @var array
*/
protected $testModelJoinSeedData = [];

/**
* Setup the test environment.
*/
Expand Down Expand Up @@ -58,17 +63,33 @@ protected function createTestModelTable()
Schema::create(TestModel::TABLE, function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});

return $this;
}

protected function createTestJoinModelTable()
{
Schema::dropIfExists(TestJoinModel::TABLE);
Schema::create(TestJoinModel::TABLE, function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('model_id')->unique();
$table->string('join');
});

return $this;
}

protected function seedTestModelTable()
{
TestModel::insert(
$this->getTestModelSeedData()
);
TestModel::insert($this->getTestModelSeedData());

return $this;
}

protected function seedTestJoinModelTable(bool $every = true)
{
TestJoinModel::insert($this->getTestJoinModelSeedData($every));

return $this;
}
Expand All @@ -89,6 +110,29 @@ protected function getTestModelSeedData(): array
return $this->testModelSeedData[$this->seedNum] = $result;
}

protected function getTestJoinModelSeedData(bool $every = true): array
{
if (isset($this->testModelJoinSeedData[$this->seedNum][$every])) {
return $this->testModelJoinSeedData[$this->seedNum][$every];
}

$result = [];
$counter = 0;
foreach ($this->getTestModelSeedData() as $testModel) {
++$counter;
if (!$every && $counter % 2) {
continue;
}

$result[] = [
'model_id' => $counter,
'join' => "join_$counter",
];
}

return $this->testModelJoinSeedData[$this->seedNum][$every] = $result;
}

protected function getExpectedTestModelData(): array
{
$counter = 0;
Expand All @@ -98,11 +142,31 @@ function (array $value) use (&$counter) {
return [
'id' => ++$counter,
'name' => $value['name'],
'created_at' => null,
'updated_at' => null,
];
},
$this->getTestModelSeedData()
);
}

protected function getExpectedTestJoinModelData(bool $every = true): array
{
$counter = 0;
$modelId = 0;
$result = [];
foreach ($this->getTestJoinModelSeedData() as $testModel) {
++$modelId;
if (!$every && $modelId % 2) {
continue;
}

++$counter;
$result[] = [
'id' => $counter,
'model_id' => $modelId,
'join' => "join_$modelId",
];
}

return $result;
}
}
27 changes: 27 additions & 0 deletions tests/Laravel/TestJoinModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of YaEtl
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
* This source file is licensed under the MIT license which you will
* find in the LICENSE file or at https://opensource.org/licenses/MIT
*/

namespace fab2s\Tests\Laravel;

use Illuminate\Database\Eloquent\Model;

class TestJoinModel extends Model
{
const TABLE = 'test_join_model';
protected $table = self::TABLE;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'join',
];
}
Loading

0 comments on commit 1e027d3

Please sign in to comment.