Speed up category tests.

This commit is contained in:
James Cole
2018-07-13 06:52:53 +02:00
parent f3f2160d96
commit d872484607
4 changed files with 63 additions and 22 deletions

View File

@@ -181,13 +181,14 @@ class CategoryController extends Controller
$periods = new Collection;
$page = (int)$request->get('page');
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
Log::debug('Start of noCategory()');
// prep for "all" view.
if ('all' === $moment) {
$subTitle = trans('firefly.all_journals_without_category');
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon : $first->date;
$end = new Carbon;
Log::debug('Moment is all()');
}
// prep for "specific date" view.
@@ -201,6 +202,7 @@ class CategoryController extends Controller
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
$periods = $this->getNoCategoryPeriodOverview($start);
}
// prep for current period
@@ -213,6 +215,8 @@ class CategoryController extends Controller
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
}
Log::debug(sprintf('Start for noCategory() is %s', $start->format('Y-m-d')));
Log::debug(sprintf('End for noCategory() is %s', $end->format('Y-m-d')));
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
@@ -358,11 +362,15 @@ class CategoryController extends Controller
*/
private function getNoCategoryPeriodOverview(Carbon $theDate): Collection
{
Log::debug(sprintf('Now in getNoCategoryPeriodOverview(%s)', $theDate->format('Y-m-d')));
$range = Preferences::get('viewRange', '1M')->data;
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon : $first->date;
$end = $theDate ?? new Carbon;
Log::debug(sprintf('Start for getNoCategoryPeriodOverview() is %s', $start->format('Y-m-d')));
Log::debug(sprintf('End for getNoCategoryPeriodOverview() is %s', $end->format('Y-m-d')));
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start);

View File

@@ -509,7 +509,7 @@ class Navigation
{
$date = clone $theDate;
// 1D 1W 1M 3M 6M 1Y
Log::debug(sprintf('subtractPeriod: date is %s', $date->format('Y-m-d')));
Log::debug(sprintf('subtractPeriod: date is %s, repeat frequency is %s and subtract is %d', $date->format('Y-m-d'), $repeatFreq, $subtract));
$functionMap = [
'1D' => 'subDays',
'daily' => 'subDays',

View File

@@ -60,6 +60,7 @@ class CategoryControllerTest extends TestCase
*/
public function testCreate(): void
{
Log::debug('TestCreate()');
// mock stuff
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
@@ -78,6 +79,7 @@ class CategoryControllerTest extends TestCase
*/
public function testDelete(): void
{
Log::debug('Test Delete()');
// mock stuff
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
@@ -96,6 +98,7 @@ class CategoryControllerTest extends TestCase
*/
public function testDestroy(): void
{
Log::debug('Test destroy()');
// mock stuff
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
@@ -116,6 +119,7 @@ class CategoryControllerTest extends TestCase
*/
public function testEdit(): void
{
Log::debug('Test edit()');
// mock stuff
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
@@ -135,6 +139,7 @@ class CategoryControllerTest extends TestCase
*/
public function testIndex(): void
{
Log::debug('Test index()');
// mock stuff
$category = factory(Category::class)->make();
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
@@ -160,12 +165,17 @@ class CategoryControllerTest extends TestCase
*/
public function testNoCategory(string $range): void
{
Log::debug(sprintf('Test noCategory(%s)', $range));
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->twice()->andReturn(TransactionJournal::first());
// get the journal with the most recent date for firstNull:
$journal = $this->user()->transactionJournals()->orderBy('date','DESC')->first();
$journalRepos->shouldReceive('firstNull')->twice()->andReturn($journal);
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
@@ -197,6 +207,7 @@ class CategoryControllerTest extends TestCase
*/
public function testNoCategoryAll(string $range): void
{
Log::debug('Test nocategoryAll()');
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
@@ -233,6 +244,7 @@ class CategoryControllerTest extends TestCase
*/
public function testNoCategoryDate(string $range): void
{
Log::debug('Test nocategorydate()');
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
@@ -277,6 +289,7 @@ class CategoryControllerTest extends TestCase
*/
public function testShow(string $range): void
{
Log::debug('Test show()');
$transaction = factory(Transaction::class)->make();
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
@@ -326,6 +339,7 @@ class CategoryControllerTest extends TestCase
*/
public function testShowAll(string $range): void
{
Log::debug('Test showAll()');
// mock stuff
$transaction = factory(Transaction::class)->make();
$journalRepos = $this->mock(JournalRepositoryInterface::class);
@@ -364,6 +378,7 @@ class CategoryControllerTest extends TestCase
*/
public function testShowByDate(string $range): void
{
Log::debug('Test showbydate()');
// mock stuff
$transaction = factory(Transaction::class)->make();
$repository = $this->mock(CategoryRepositoryInterface::class);
@@ -412,6 +427,7 @@ class CategoryControllerTest extends TestCase
*/
public function testShowEmpty(string $range): void
{
Log::debug('Test showempty()');
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->twice()->andReturn(TransactionJournal::first());
@@ -451,6 +467,7 @@ class CategoryControllerTest extends TestCase
*/
public function testStore(): void
{
Log::debug('Test store()');
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(CategoryRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
@@ -475,6 +492,7 @@ class CategoryControllerTest extends TestCase
*/
public function testUpdate(): void
{
Log::debug('Test update()');
$category = Category::first();
$repository = $this->mock(CategoryRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);

View File

@@ -25,6 +25,7 @@ namespace Tests\Unit\TransactionRules\Triggers;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Triggers\ToAccountIs;
use Tests\TestCase;
use Log;
/**
* Class ToAccountIsTest
@@ -36,14 +37,22 @@ class ToAccountIsTest extends TestCase
*/
public function testTriggered(): void
{
$count = 0;
$loops = 0; // FINAL LOOP METHOD.
do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
$transactionCount = $journal->transactions()->count();
$account = null === $transaction ? null : $transaction->account;
$count++;
} while ($account === null && $count < 30 && $transactionCount !== 2);
$account = $transaction->account;
$count = $journal->transactions()->count();
Log::debug(sprintf('Loop: %d, transaction count: %d, account is null: %d', $loops, $count, (int)null===$account));
$loops++;
// do this until: account is not null, journal has two transactions, loops is below 30
} while (!(null !== $account && 2 === $count && $loops < 30));
$trigger = ToAccountIs::makeFromStrings($account->name, false);
$result = $trigger->triggered($journal);
@@ -55,14 +64,20 @@ class ToAccountIsTest extends TestCase
*/
public function testTriggeredNot(): void
{
$count = 0;
$loops = 0; // FINAL LOOP METHOD.
do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
$transactionCount = $journal->transactions()->count();
$account = null === $transaction ? null : $transaction->account;
$count++;
} while ($account === null && $count < 30 && $transactionCount !== 2);
$account = $transaction->account;
$count = $journal->transactions()->count();
Log::debug(sprintf('Loop: %d, transaction count: %d, account is null: %d', $loops, $count, (int)null===$account));
$loops++;
// do this until: account is not null, journal has two transactions, loops is below 30
} while (!(null !== $account && 2 === $count && $loops < 30));
$trigger = ToAccountIs::makeFromStrings('some name' . random_int(1, 234), false);
$result = $trigger->triggered($journal);