A partial archive of https://discourse-mediawiki.wmflabs.org as of Saturday May 21, 2022.

When is data inserted into test tables?

samwilson

If an extension inserts data in an SQL file (that’s run in the LoadExtensionSchemaUpdates hook), how can that data be available in the (cloned, empty, temporary) tables used in phpunit tests?

It seems that when the test harness clones all database tables it only clones their structure and so any data inserted (i.e. what’s usually done when update.php is run) is not included.

At the moment, I’m thinking that it is meant to be done by calling e.g. Database::insert() in MediaWikiTestCase::addDBDataOnce(), and with only the required subset of data. But this feels a bit hacky; is it?

Osnard

I think adding data into the “unit test shadow database” within a unit test itself the best idea. A unit test should have a very well defined environment. Thus all data needed by the test should also be provided by the setup of the test.

Why should a SQL file added by “LoadExtensionSchemaUpdates” have actual data in it? This is only about the schema (aka structure).

samwilson

Yeah, that’s what I’m thinking. Although, in this case it’s data that’s required for every test and doesn’t change, so I think doing it once per test suite is appropriate.

Good question! There are a bunch of extensions that do similar things, usually for creating rarely-updated lookup tables such as CentralNotice’s cn_known_devices table (values such as desktop, mobile, etc.).

I am wondering if perhaps these insert statements should be in a separate sql file that can then also be run by the test system.

Tgr

You could add the capability to MediaWikiTestCase to copy some tables wholesale instead of just cloning the structure.