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

Unable to test domain using not default datasource #21

Open
bdbogjoe opened this issue Dec 13, 2017 · 8 comments
Open

Unable to test domain using not default datasource #21

bdbogjoe opened this issue Dec 13, 2017 · 8 comments

Comments

@bdbogjoe
Copy link

bdbogjoe commented Dec 13, 2017

i initially created issue but in wrong place grails/grails-core#10888

while doing test i have error :

org.grails.datastore.mapping.core.exceptions.ConfigurationException: Invalid connection [myDataSource] configured for class [class joe.Test]

I spent already days on this, cannot find correct solution.
One idea was to overwrite method getDatastore bdbogjoe/unit-test-multiple-datasource#2 but in some case does not work and don't think it is correct way to do this.

Do you have any idea how to fix this ? Was working fine in grails 3.1.x

@bdbogjoe
Copy link
Author

@bdbogjoe
Copy link
Author

@jeffbrown do you have any idea on this issue ?

@bdbogjoe
Copy link
Author

thanks to @jameskleeh, and bdbogjoe/unit-test-multiple-datasource#3

i'm now able to able to pass my previous tests, but now i'm facing issue while trying to validate my domain

Condition failed with Exception:

bean.validate()
|    |
|    java.lang.IllegalStateException: No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here
joe.Test : (unsaved)

If i add annotation @transactional(connection = 'myDataSource')

I got error :

No datastore found for connection named [myDataSource]
org.grails.datastore.mapping.core.exceptions.ConfigurationException: No datastore found for connection named [myDataSource]
	at org.grails.datastore.mapping.simple.SimpleMapDatastore.getDatastoreForConnection(SimpleMapDatastore.java:344)
	at joe.TestSpec.testValidateWithTransactional(TestSpec.groovy)

i updated my repo : https://github.com/bdbogjoe/unit-test-multiple-datasource

just run gradle test to have these errors

@jameskleeh
Copy link
Contributor

@bdbogjoe In general my advice for situations like this is to use a real datastore.

@bdbogjoe
Copy link
Author

bdbogjoe commented Apr 17, 2018

thanks @jameskleeh , i'm now back on this issue, i'm still blocked with it.

What do you mean by using real datastore ?

Seems we don't have choice :

SimpleDatastore is used, and we cannot change it, and using deprecated constructor
I try to override the bean grailsDatastore, but our code (doWithSpring) from tests is evaluated before, and so not possible to override the bean

I spent days already on this issue, i'm the only one testing domain which is not using default datasource ?

I updated my repo to illustrate another issue, maybe same, don't know

https://github.com/bdbogjoe/unit-test-multiple-datasource

if you run test you have now :

image

i have constraint defined : https://github.com/bdbogjoe/unit-test-multiple-datasource/blob/a0e35a37a698862884b5285127e7fbe7a1298ab1/grails-app/domain/joe/Test.groovy#L12

image

image

image

Same tests for other domain using default datasource works fine

I think we need better solution than overriding dataStore in test, because in some other places we need to get datastore from application context

@bdbogjoe
Copy link
Author

i found one solution : bdbogjoe/unit-test-multiple-datasource@9fe9625

but don't think it is nice solution, any other proposal ?

@anthonyMoreira
Copy link

@jameskleeh In general my advice for situations like this is to use a real datastore.
Can you elaborate ? I'm interested in your advice as I'm also currently stuck. I can't seem to find a way to have the same mocking with Domains using a different datasource than the default one.

DataTest doesn't support enhancing Domain that comes from a different datastore than default one. The only way seem to hack the connections sources to make the default datastore create children datastore...

@jameskleeh
Copy link
Contributor

@anthonyMoreira For example if you are using Hibernate, use the https://github.com/grails/gorm-hibernate5/blob/master/grails-plugin/src/main/groovy/grails/test/hibernate/HibernateSpec.groovy instead of this unit testing framework.

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

No branches or pull requests

3 participants