Issue Details (XML | Word | Printable)

Key: MDL-16483
Type: Bug Bug
Status: Open Open
Priority: Blocker Blocker
Assignee: moodle.com
Reporter: Nicolas Connault
Votes: 1
Watchers: 6
Operations

Add/Edit UI Mockup to this issue
If you were logged in you would be able to see more operations.
Moodle

Strategy for generating and cleaning data for unit tests

Created: 13/Sep/08 03:57 AM   Updated: 13/Nov/09 04:08 PM
Return to search
Component/s: Database SQL/XMLDB
Affects Version/s: 2.0
Fix Version/s: 2.0

Issue Links:
Dependency
 
Relates
 

Participants: Dan Poltawski, Eloy Lafuente (stronk7), Eric Merrill, moodle.com, Nicolas Connault, Penny Leach, Petr Skoda and Tim Hunt
Security Level: None
Affected Branches: MOODLE_20_STABLE
Fixed Branches: MOODLE_20_STABLE


 Description  « Hide
This needs an urgent consensus and decision.

We have all the tools we need to write effective unit tests for our new code. However, many parts of the code require actual database records or physical files in order to be tested properly. We need to decide which strategy will be used for cleaning up generated data, among the following:

1. Use an alternate prefix for Database tables. This has the advantage of securing the "real" tables against actions by the unit tests. The main problem is that it is complex to keep these tables in sync with the "real" tables schema-wise, and it would be too slow to rebuild them entirely between each test
2. Use the real tables but restrict the use of data generation and cleaning for sites where the $CFG->developmentmode variable is set in config.php (assuming that only one user will use the site). This is much simpler than 1., but can only be used on a development site, preferably freshly installed.

In addition to deciding between real and alternate db prefix, we need to decide how the database will be "cleaned up" between each test.

1. Complete rebuild using a dump sql file previously generated on demand. This is not really feasible because it would be horrendously slow
2. Specifically hard-code a list of tables to be cleaned up after or before each test, some having special conditions if some records must be kept (like the users table). This is fast, but complex to set up and not very reliable. It is also difficult to maintain, as the db schema changes throughout the dev process.
3. Before each generation of data, save in memory the highest PK number of every non-empty table (also record tables that are empty), and use this data during clean up to truncate these tables conditionally. This is much more reliable than 2., but probably slower. It also precludes any more than one concurrent user on the site, because of the risks of parallel db activity.

Another option is available, which is more radical since it involves completely mocking the $DB object, and controlling all DB activity starting in unit tests. This puts a huge part of responsibility on the unit tests, since they have to set up return values for every get_* DB call their tested code is likely to produce. The process could be optimised using clever object orientation and abstraction, but it is a huge job nonetheless. It can however be done, and may be the strategy of choice for certain unit tests. Text or XML fixture data could easily be used in combination with this.

This may all seem like overkill or a waste of time in this very busy time of 2.0 development, but I think we have a great opportunity to strengthen the long-term integrity of our code base, and to reduce the risks of insidious bugs causing problems in the future.

Please comment and vote for your solution, as well as propose your own if you have any.

 All   Comments   Change History   Version Control      Sort Order: Ascending order - Click to sort in descending order
Nicolas Connault committed 31 files to 'Moodle CVS' - 16/Sep/08 08:19 PM
MDL-16483 Refactored install/upgrade code into lib/adminlib. Created an intermediate MoodleUnitTestCase class that overrides simpletest's constructor, destructor and setup/teardown methods. All moodle unit tests must extend this class.
MODIFY course/simpletest/testcourselib.php   Rev. 1.3    (+2 -2 lines)
MODIFY mod/forum/simpletest/testmodforumlib.php   Rev. 1.4    (+2 -2 lines)
MODIFY lib/simpletest/portfolio_testclass.php   Rev. 1.4    (+2 -2 lines)
MODIFY lib/adminlib.php   Rev. 1.268    (+462 -1 lines)
MODIFY lib/simpletest/fixtures/gradetest.php   Rev. 1.43    (+2 -2 lines)
MODIFY lib/dml/Attic/adodb_moodle_database.php   Rev. 1.21    (+9 -4 lines)
MODIFY lib/simpletest/testcompletionlib.php   Rev. 1.3    (+118 -118 lines)
MODIFY mod/data/simpletest/testpreset.php   Rev. 1.7    (+6 -6 lines)
MODIFY lib/simpletest/testportfolioaddbutton.php   Rev. 1.2    (+2 -2 lines)
MODIFY lib/simpletest/testcode.php   Rev. 1.4    (+5 -5 lines)
MODIFY grade/simpletest/testreportlib.php   Rev. 1.3    (+2 -2 lines)
MODIFY lib/simpletest/testweblib.php   Rev. 1.8    (+9 -9 lines)
MODIFY lib/simpletest/testmoodlelib.php   Rev. 1.14    (+23 -23 lines)
MODIFY question/type/numerical/simpletest/testquestiontype.php   Rev. 1.6    (+6 -6 lines)
MODIFY lib/ddl/simpletest/testddl.php   Rev. 1.3    (+1 -1 lines)
MODIFY question/type/shortanswer/simpletest/testquestiontype.php   Rev. 1.5    (+4 -4 lines)
MODIFY lib/simpletestlib.php   Rev. 1.10    (+39 -9 lines)
MODIFY lang/en_utf8/simpletest.php   Rev. 1.7    (+5 -1 lines)
MODIFY lib/simpletest/testmathslib.php   Rev. 1.7    (+3 -3 lines)
MODIFY lib/dml/simpletest/testdml.php   Rev. 1.15    (+1 -1 lines)
MODIFY admin/report/simpletest/Attic/index.php   Rev. 1.23    (+79 -1 lines)
MODIFY lib/simpletest/testaccesslib.php   Rev. 1.3    (+1 -1 lines)
MODIFY lib/simpletest/Attic/slowtestcode.php   Rev. 1.4    (+9 -9 lines)
MODIFY admin/index.php   Rev. 1.341    (+5 -444 lines)
MODIFY lib/dml/moodle_database.php   Rev. 1.48    (+2 -2 lines)
MODIFY lib/dml/sqlite3_pdo_moodle_database.php   Rev. 1.6    (+10 -5 lines)
MODIFY lib/simpletest/testbackuplib.php   Rev. 1.6    (+3 -3 lines)
MODIFY lib/simpletest/Attic/testajaxlib.php   Rev. 1.8    (+24 -24 lines)
MODIFY lib/simpletest/Attic/testeventslib.php   Rev. 1.14    (+2 -2 lines)
MODIFY mod/quiz/simpletest/testaccessrules.php   Rev. 1.4    (+6 -6 lines)
MODIFY lib/dml/simpletest/Attic/dbspecific.php   Rev. 1.2    (+1 -1 lines)
Nicolas Connault committed 3 files to 'Moodle CVS' - 16/Sep/08 10:35 PM
MDL-16483 Reverting as Petr ordered... I mean, suggested.
MODIFY lib/dml/Attic/adodb_moodle_database.php   Rev. 1.22    (+4 -8 lines)
MODIFY lib/dml/sqlite3_pdo_moodle_database.php   Rev. 1.7    (+4 -8 lines)
MODIFY lib/dml/moodle_database.php   Rev. 1.49    (+2 -2 lines)
Nicolas Connault committed 4 files to 'Moodle CVS' - 17/Sep/08 12:40 AM
Mitsuhiro Yoshida committed 6 files to 'Lang CVS' - 17/Sep/08 12:49 AM
MDL-16520 - Translated new strings for portfolio.
MDL-16483 - Translated new strings for simple test.
MDL-16519 - Translated a new string for box.net porfolio.
MDL-16414 - Translated a new string for download portfolio.
MDL-15402 - Translated a new string for repository.
MODIFY ja_utf8/portfolio.php   Rev. 1.24    (+4 -2 lines)
MODIFY ja_utf8/repository.php   Rev. 1.26    (+2 -1 lines)
MODIFY ja_utf8/portfolio_boxnet.php   Rev. 1.9    (+2 -1 lines)
MODIFY ja_utf8/README   Rev. 1.815    (+1 -1 lines)
MODIFY ja_utf8/portfolio_download.php   Rev. 1.3    (+2 -1 lines)
MODIFY ja_utf8/simpletest.php   Rev. 1.9    (+6 -2 lines)
martignoni committed 1 file to 'Lang CVS' - 05/Oct/08 03:40 PM
MDL-16483 strings for unit test tables
MODIFY fr_utf8/simpletest.php   Rev. 1.8    (+5 -1 lines)
Petr Skoda committed 10 files to 'Moodle CVS' - 10/Jan/09 10:23 PM
Petr Skoda committed 1 file to 'Moodle CVS' - 10/Jan/09 10:37 PM
MDL-16483 - stopped using MoodleUnitTestCase in tests where $DB not needed
MODIFY course/simpletest/testcourselib.php   Rev. 1.4    (+3 -4 lines)
Petr Skoda committed 21 files to 'Moodle CVS' - 10/Jan/09 10:47 PM
MDL-16483 - renamed MoodleUnitTestCase to FakeDBUnitTestCase; MDL-17841 removed bogus remove lib/simpletest/slowcodetest.php
MODIFY mod/quiz/simpletest/testlib.php   Rev. 1.2    (+1 -1 lines)
MODIFY lib/simpletest/testsimpletestlib.php   Rev. 1.3    (+1 -1 lines)
MODIFY lib/simpletestlib.php   Rev. 1.23    (+4 -4 lines)
DEL lib/simpletest/Attic/slowtestcode.php   Rev. 1.6    (+0 -0 lines)
MODIFY lib/simpletest/testrepositorylib.php   Rev. 1.4    (+2 -2 lines)
MODIFY question/type/shortanswer/simpletest/testquestiontype.php   Rev. 1.7    (+1 -1 lines)
MODIFY lib/simpletest/portfolio_testclass.php   Rev. 1.10    (+2 -2 lines)
MODIFY lib/simpletest/Attic/testajaxlib.php   Rev. 1.9    (+2 -2 lines)
MODIFY lib/simpletest/testaccesslib.php   Rev. 1.5    (+1 -1 lines)
MODIFY lib/simpletest/testbackuplib.php   Rev. 1.7    (+1 -9 lines)
MODIFY lib/simpletest/testquestionlib.php   Rev. 1.4    (+2 -2 lines)
MODIFY mod/quiz/simpletest/testlocallib.php   Rev. 1.3    (+1 -1 lines)
MODIFY lib/simpletest/testportfolioaddbutton.php   Rev. 1.4    (+2 -2 lines)
MODIFY lib/simpletest/testcompletionlib.php   Rev. 1.6    (+1 -1 lines)
MODIFY grade/simpletest/testreportlib.php   Rev. 1.4    (+2 -2 lines)
MODIFY question/type/numerical/simpletest/testquestiontype.php   Rev. 1.7    (+1 -1 lines)
MODIFY lib/simpletest/testconditionlib.php   Rev. 1.2    (+1 -1 lines)
MODIFY lib/simpletest/testcode.php   Rev. 1.5    (+1 -1 lines)
MODIFY mod/quiz/simpletest/testaccessrules.php   Rev. 1.7    (+6 -6 lines)
MODIFY lib/simpletest/fixtures/gradetest.php   Rev. 1.46    (+2 -2 lines)
MODIFY lib/simpletest/Attic/testeventslib.php   Rev. 1.16    (+2 -2 lines)
Petr Skoda committed 1 file to 'Moodle CVS' - 10/Jan/09 10:54 PM
MDL-16483 - partial unbreaking of eventlsib test
MODIFY lib/simpletest/Attic/testeventslib.php   Rev. 1.17    (+2 -11 lines)
Petr Skoda committed 5 files to 'Moodle CVS' - 11/Jan/09 01:06 AM
MDL-16483 $CFG->unittestprefix not required anymore to run unit tests that do not need fake db; it should be saer to let admins modify the prefix in config.php instead of modifying it from php
MODIFY lib/simpletestlib/test_case.php   Rev. 1.8    (+11 -1 lines)
MODIFY lib/simpletestlib.php   Rev. 1.24    (+47 -7 lines)
MODIFY admin/report/unittest/index.php   Rev. 1.10    (+89 -138 lines)
MODIFY lib/simpletestlib/readme_moodle.txt   Rev. 1.6    (+2 -1 lines)
MODIFY config-dist.php   Rev. 1.112    (+22 -14 lines)
Petr Skoda committed 4 files to 'Moodle CVS' - 11/Jan/09 08:19 PM
MDL-16483 reverting changes done to admin/index.php, adding new /admin/report/unittest/test_tables script.php (not finished yet!)
MODIFY admin/report/unittest/index.php   Rev. 1.11    (+7 -113 lines)
ADD admin/report/unittest/test_tables.php   Rev. 1.1    (+0 -0 lines)
MODIFY lib/adminlib.php   Rev. 1.305    (+39 -393 lines)
MODIFY admin/index.php   Rev. 1.347    (+314 -9 lines)