Is there is any way to emulate database in Db Connections in UnitTests?

Part of the functionality of my plugin uses database structure obtained from a database connection.
To do Unit tests I create a fake database using code.
But in last versions of Database plugin DataSources became immutable, so my code fails to add a database to database connections.Code that stopped working:

```
DbPsiFacade facade = DbPsiFacade.getInstance(myFixture.getProject());
TestDataSource source = new TestDataSource(myFixture.getProject());
List<DbDataSource> dataSources = facade.getDataSources().add(source); <- OperationNotSupported

```
How can I create fake database in unit tests so it would be availible as it would be in Database Connections?

0

Oleg,

DbPsiFacade and DbDataSource are probably too high-level for a unit test.

Please provide a sample test source so we can figure out the right API for that.

0

Thanks for reply.

it is actually Functional tests if we follow terminology. In the documentation JetBrains says that most effective way is to use functional tests mostly, and I also like this approach.

Example test:

myFixture.configureByText(PhpFileType.INSTANCE,   "<?php \n" +
"\\test\\PersonModel::find()->where('<caret>');");
myFixture.completeBasic();
assertEquals(5, myFixture.getLookupElementStrings().size());

(This method is located in com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase descendant.)
PersonModel is an ActiveRecord, plugin figure out database table and shows column names in <caret>.
Plugin finds table columns by traversing dataSources like that 

DbPsiFacade facade = DbPsiFacade.getInstance(project);
List<DbDataSource> dataSources = facade.getDataSources();
for (DbDataSource source : dataSources) {
for (Object item : source.getModel().traverser().filter(DasTable.class)) {
TableInfo tableInfo = new TableInfo((DasTable) item);
for (DasColumn column : tableInfo.getColumns()) {
list.add(DatabaseUtils.buildLookup(column, true, project));
}
}
}

I use following SetUp code:

@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("classes.php"));

DbPsiFacade facade = DbPsiFacade.getInstance(myFixture.getProject());
if (facade.getDataSources().size() == 0) {
TestDataSource source = new TestDataSource(myFixture.getProject());
List<DbDataSource> dataSources = facade.getDataSources(); <- OperationNotSupported
dataSources.add(source);
}
}

 

List<DbDataSource> dataSources = facade.getDataSources(); <- OperationNotSupported

This line stopped working. I would like to find a "good" way to emulate database, so plugin gets fake database structure, and we can effectively test our plugin code.
Thanks.

For reference, one of affected files:
https://github.com/nvlad/yii2support/blob/master/tests/com/nvlad/yii2support/database/QueryTests.java



0

请先登录再写评论。