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

Sqlite database is not cleaned after last test #3680

Closed
richardbrinkman opened this Issue Nov 3, 2016 · 7 comments

Comments

Projects
None yet
3 participants
@richardbrinkman
Contributor

richardbrinkman commented Nov 3, 2016

What are you trying to achieve?

Run this test:

class LastTestCleanedTest extends \Codeception\Test\Unit {
        /**
        * @var \UnitTester
        */
        protected $tester;

        public function testHaveInDatabase() {
                $this->tester->haveInDatabase('users', ['username' => 'John', 'email' => null]);
                $this->tester->seeInDatabase('users', ['email' => null]);
        }

        public function testDatabaseIsCleaned() {
                $this->tester->dontSeeInDatabase('users', ['email' => null]);
        }
}

After running codecept run the database does not show John as expected. However if I remove the second test like so:

class LastTestCleanedTest extends \Codeception\Test\Unit {
        /**
        * @var \UnitTester
        */
        protected $tester;

        public function testHaveInDatabase() {
                $this->tester->haveInDatabase('users', ['username' => 'John', 'email' => null]);
                $this->tester->seeInDatabase('users', ['email' => null]);
        }
}

John does appear in the (sqlite) database. It should have been cleaned.

Details

  • Codeception version: 2.2.6
  • PHP Version: 7.0.8
  • Operating System: Lubuntu
  • Installation type: Composer
  • List of installed packages (composer show): none other than codeception/codeception and its dependencies
  • Suite configuration:
# paste suite config here
actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    config:
        Db:
            dsn: 'sqlite:tests/_data/sqlite.db'
            user: ''
            password: ''
            dump: tests/_data/dump.sql

tests/_data/dump.sql:

DROP TABLE IF EXISTS users;
CREATE TABLE USERS(username VARCHAR(20) NOT NULL PRIMARY KEY, email VARCHAR(20));
@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Nov 3, 2016

Member

I am pretty sure that this is documented as a feature somewhere.

Member

Naktibalda commented Nov 3, 2016

I am pretty sure that this is documented as a feature somewhere.

@Naktibalda Naktibalda added the Db label Nov 3, 2016

@sergeyklay

This comment has been minimized.

Show comment
Hide comment
@richardbrinkman

This comment has been minimized.

Show comment
Hide comment
@richardbrinkman

richardbrinkman Nov 3, 2016

Contributor

That documentation states:

haveInDatabase() inserts an SQL record into a database. This record will be erased after the test.

It does not say that this is not the case for the last test.

It's also pretty inconsistent that in the first run of codecept run all tests pass, whereas all subsequent runs of the same command some tests fail.

Contributor

richardbrinkman commented Nov 3, 2016

That documentation states:

haveInDatabase() inserts an SQL record into a database. This record will be erased after the test.

It does not say that this is not the case for the last test.

It's also pretty inconsistent that in the first run of codecept run all tests pass, whereas all subsequent runs of the same command some tests fail.

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Nov 3, 2016

Member

Rows inserted by haveInDatabase should be deleted after each test: https://github.com/Codeception/Codeception/blob/2.2.6/src/Codeception/Module/Db.php#L246

I will test it later.

Member

Naktibalda commented Nov 3, 2016

Rows inserted by haveInDatabase should be deleted after each test: https://github.com/Codeception/Codeception/blob/2.2.6/src/Codeception/Module/Db.php#L246

I will test it later.

@richardbrinkman

This comment has been minimized.

Show comment
Hide comment
@richardbrinkman

richardbrinkman Nov 3, 2016

Contributor

To make testing it easier, I have uploaded a minimal example project that shows the behaviour.
Please run:

git clone git@github.com:richardbrinkman/codeceptionBug.git
cd codeceptionBug
composer install
vendor/bin/codecept run
echo "select * from users;" | sqlite3 tests/_data/sqlite.db

You will see that the record for John is still there.

Contributor

richardbrinkman commented Nov 3, 2016

To make testing it easier, I have uploaded a minimal example project that shows the behaviour.
Please run:

git clone git@github.com:richardbrinkman/codeceptionBug.git
cd codeceptionBug
composer install
vendor/bin/codecept run
echo "select * from users;" | sqlite3 tests/_data/sqlite.db

You will see that the record for John is still there.

@Naktibalda Naktibalda self-assigned this Nov 4, 2016

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Nov 5, 2016

Member

This issue is specific to sqlite.

removeInserted didn't work for sqlite, because I didn't know that lastInsertId returns ROWID if sqlite table has it (default behaviour).

It only affected the last test, because cleanup: true deleted all tables before each test.

Member

Naktibalda commented Nov 5, 2016

This issue is specific to sqlite.

removeInserted didn't work for sqlite, because I didn't know that lastInsertId returns ROWID if sqlite table has it (default behaviour).

It only affected the last test, because cleanup: true deleted all tables before each test.

@Naktibalda Naktibalda changed the title from Database is not cleaned after last test to Sqlite database is not cleaned after last test Nov 5, 2016

Naktibalda added a commit to Naktibalda/Codeception that referenced this issue Nov 5, 2016

@richardbrinkman

This comment has been minimized.

Show comment
Hide comment
@richardbrinkman

richardbrinkman Nov 6, 2016

Contributor

I can confirm that PR #3689 fixes the problem.
Thanks for fixing it.

Contributor

richardbrinkman commented Nov 6, 2016

I can confirm that PR #3689 fixes the problem.
Thanks for fixing it.

Naktibalda added a commit to Naktibalda/Codeception that referenced this issue Nov 7, 2016

@DavertMik DavertMik closed this in #3689 Nov 8, 2016

DavertMik added a commit that referenced this issue Nov 8, 2016

[Sqlite] Use _ROWID_ to remove inserted rows after test (#3689)
* [Sqlite] Use _ROWID_ to remove inserted rows after test

Fixes #3680

* Don't fail on PHP 5.4

* [Sqlite test] removed id field from users table to demonstrate that cleanup works with rowid

* Removed composite_pk table from sqlite-54.sql  file

* Don't run composite primary key test on 5.4

chris1312 added a commit to chris1312/Codeception that referenced this issue Jun 16, 2017

[Sqlite] Use _ROWID_ to remove inserted rows after test (#3689)
* [Sqlite] Use _ROWID_ to remove inserted rows after test

Fixes #3680

* Don't fail on PHP 5.4

* [Sqlite test] removed id field from users table to demonstrate that cleanup works with rowid

* Removed composite_pk table from sqlite-54.sql  file

* Don't run composite primary key test on 5.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment