Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make migration/seed settings flexible on database testing #3993

Merged
merged 5 commits into from
Jan 5, 2021

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Dec 16, 2020

Description
Now you can't disable migration on database testing if you want.
And running migration/seed on each test may slow tests.

  • You can disable migration: $migrate = false
  • You can run migration only once: $migrateOnce = true
  • You can run seed only once: $seedOnce = true

Checklist:

  • Securely signed commits
  • Component(s) with PHPdocs
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have to look at this one on mobile, but I like breaking these out. First and foremost we must be absolutely sure that current tests will still work after this change (I.e. still respects $refresh). Second IMO we should go ahead and deprecate $refresh and rely on these going forward.

These changes will also require updates to the User Guide.

@kenjis kenjis force-pushed the feat-database-testing-setup branch from 68eab49 to b727973 Compare December 18, 2020 11:14
@kenjis
Copy link
Member Author

kenjis commented Dec 18, 2020

Updated the User Guide.

@kenjis
Copy link
Member Author

kenjis commented Dec 19, 2020

@MGatner
Copy link
Member

MGatner commented Dec 19, 2020

@kenjis it's been happening from time to time. Not related to your PR, we need to look into why the test table is persisting.

@kenjis
Copy link
Member Author

kenjis commented Dec 19, 2020

I see. Thanks!

@kenjis
Copy link
Member Author

kenjis commented Dec 21, 2020

@paulbalandan
Copy link
Member

PHP 7.3 - Postgre failed again.
https://github.com/codeigniter4/CodeIgniter4/pull/3993/checks?check_run_id=1587713000

You just need to re-run actions until it goes green.

@kenjis
Copy link
Member Author

kenjis commented Dec 21, 2020

@paulbalandan How do I re-run?

@kenjis kenjis force-pushed the feat-database-testing-setup branch from eac4761 to bd1d69a Compare December 21, 2020 23:56
@kenjis
Copy link
Member Author

kenjis commented Dec 22, 2020

I rebased. Can #4015 help?

@kenjis
Copy link
Member Author

kenjis commented Dec 22, 2020

Oh no! PHP 7.3 - Postgre and PHP 7.4 - Postgre failed...

@kenjis
Copy link
Member Author

kenjis commented Dec 22, 2020

There was 1 failure:

1) CodeIgniter\Database\Live\ForgeTest::testAddFields
Array
(
    [0] => stdClass Object
        (
            [name] => id
            [type] => integer
            [default] => nextval('db_forge_test_fields_id_seq'::regclass)
            [max_length] => 32
        )

    [1] => stdClass Object
        (
            [name] => active
            [type] => integer
            [default] => 0
            [max_length] => 32
        )

    [2] => stdClass Object
        (
            [name] => username
            [type] => character varying
            [default] => 
            [max_length] => 255
        )

    [3] => stdClass Object
        (
            [name] => name
            [type] => character varying
            [default] => 
            [max_length] => 255
        )

)

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'integer'
+'character varying'

/home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/Live/ForgeTest.php:605

--

See #4015 (comment).

@kenjis kenjis mentioned this pull request Dec 22, 2020
4 tasks
@kenjis kenjis force-pushed the feat-database-testing-setup branch from bd1d69a to 16d880e Compare December 22, 2020 10:54
@kenjis kenjis requested a review from MGatner December 22, 2020 23:56
@kenjis
Copy link
Member Author

kenjis commented Dec 22, 2020

All tests passed.

@kenjis
Copy link
Member Author

kenjis commented Dec 29, 2020

@MGatner Is something wrong with this PR?

@MGatner
Copy link
Member

MGatner commented Dec 29, 2020

Not necessarily! It's more complicated than I am able to review on mobile, which is where I do most of my reviews. I will set aside some desktop time later to do it.

@kenjis
Copy link
Member Author

kenjis commented Dec 29, 2020

@MGatner Okay, thanks!

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally this looks good. My main concern is around the "once" aspect. As is, with the private scope on the flags, migrations & seeds will only run once for the entire run of PHPUnit, and yet will be dependent on the class-specific settings of whichever test case is run first. Consider these three test cases all in the same project:


class FooTest extends CIDatabaseTestCase
{
	protected $migrateOnce = true;
	protected $namespace   = 'Tests\Support';
}

class BarTest extends CIDatabaseTestCase
{
	protected $migrateOnce = false;
	protected $namespace   = null;
}

class BamTest extends CIDatabaseTestCase
{
	protected $migrateOnce = true;
	protected $namespace   = 'App';
}

Depending on which are run and in which order (and by which thread) they could all leave the database in very different states.

In my opinion, the "once" methods should apply per test case (file), rather than for the whole suite - and if we want to offer the option for a single static database test configuration then it should be handled in a bootstrap, not the test cases themselves.

I hope that makes sense.

@kenjis
Copy link
Member Author

kenjis commented Dec 30, 2020

@MGatner Thank you for the review.

See https://github.com/codeigniter4/CodeIgniter4/pull/3993/files#diff-7591a921e2ba957a20a6e334d50bb134de0a53a4c4c4a3f01e27d001b9bae4c6R449-R458
It resets the private properties after each test class.
So I think the "once" methods apply per test case (file).

I added test for $migrateOnce.

$ vendor/bin/phpunit --no-coverage tests/system/Database/DatabaseTestCase --debug
PHPUnit 8.5.4 by Sebastian Bergmann and contributors.

Test 'CodeIgniter\Database\DatabaseTestCase\DatabaseTestCaseMigrationOnce1Test::testMigrationDone' started
Test 'CodeIgniter\Database\DatabaseTestCase\DatabaseTestCaseMigrationOnce1Test::testMigrationDone' ended
Test 'CodeIgniter\Database\DatabaseTestCase\DatabaseTestCaseMigrationOnce2Test::testMigrationDone' started
Test 'CodeIgniter\Database\DatabaseTestCase\DatabaseTestCaseMigrationOnce2Test::testMigrationDone' ended


Time: 173 ms, Memory: 12.00 MB

OK (2 tests, 2 assertions)

@MGatner
Copy link
Member

MGatner commented Dec 30, 2020

Oh! Sorry then, that was my mistake. I wonder what kind of weird scopiness is happening that private static is applied for every file? Maybe each child class inherits the original static property with false and then sets it to true at the local scope? @samsonasik help?

Given this, I approve the PR, but I will wait for merge.

@samsonasik
Copy link
Member

I am ok with this 👍

@MGatner
Copy link
Member

MGatner commented Dec 30, 2020

@samsonasik do you understand how the scoping is working out such that the "once" variables are toggled for every file?

@samsonasik
Copy link
Member

@MGatner that rely on doneSeed ?

@MGatner
Copy link
Member

MGatner commented Dec 30, 2020

Yes. Since $doneSeed is private static I would expect it to be set once for the entire test suite and thus seeding would only happen one time. But @kenjis confirms that it actually resets between files, which makes me think it must be something with the scoping I don't understand.

@paulbalandan
Copy link
Member

This method is called after the test suite class is tested:

	/**
	 * Reset $doneMigration and $doneSeed
	 *
	 * @afterClass
	 */
	public static function resetMigrationSeedCount()
	{
		self::$doneMigration = false;
		self::$doneSeed      = false;
	}

Notice the @afterClass annotation in the doc block. PHPUnit calls this static method after all test methods have been run in the test class. It is like implicitly calling tearDownAfterClass method. See @afterClass

@MGatner
Copy link
Member

MGatner commented Jan 5, 2021

I totally missed that. Thanks for explaining 🤩

@MGatner MGatner merged commit 51d4611 into codeigniter4:develop Jan 5, 2021
@kenjis kenjis deleted the feat-database-testing-setup branch January 5, 2021 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants