Incapable of running unit tests because of CREATE INDEX and CREATE CONSTRAINT - neo4j

I've developed a module for Neo4j using Graphaware package. As a part of my module, I want to make sure that some indices and/or constraints are present in the database. To this end, I use BaseTxDrivenModule.initialize method to run a couple of Cypher statements:
#Override
public void initialize(GraphDatabaseService database)
{
database.execute("CREATE CONSTRAINT ON (n:`Label`) ASSERT n.`id` IS UNIQUE;");
database.execute("CREATE INDEX ON n:`Label2`(`id`) IS UNIQUE;");
}
These statements run successfully in production when I deploy the module in a server instance of Neo4j. But when I want to run the unit tests, as a part of build process, the execution hangs and never finishes. And when I omit the initialize method, it goes on without any error.
The worst part is that I have to build the package like: mvn package -Dmaven.test.skip=true or it won't build anything.
So my question is, why? And how can I fix this problem?
Here's a sample project demonstrating the issue:
https://github.com/ziadloo/test_neo4j_module
Just clone it and run mvn package and you'll see that the tests never finish.

There is no guarantee that the Runtime is started during your test, you'll have to assert it by invoking the waitUntilStarted method.
#Before
public void setUp() {
database = new TestGraphDatabaseFactory()
.newImpermanentDatabaseBuilder()
.loadPropertiesFromFile(this.getClass().getClassLoader().getResource("neo4j-module.conf").getPath())
.newGraphDatabase();
getRuntime(database).waitUntilStarted();
registerShutdownHook(database);
}
I would suggest you take a look at some test cases in the neo4j-uuid module for eg.

Related

Grails 3 + IntelliJ: Running integration tests yields "No GORM implementations configured. Ensure GORM has been initialized correctly"

I have a plain Grails 3.3.2 app. I can run tests just fine with gradle test integrationTest. But when I try to right click and run a test class or a single tests or the entire test suite in IntelliJ, I get:
No GORM implementations configured. Ensure GORM has been initialized correctly
This happens when I run them as jUnit tests.
If i try to run as Grails tests, I get:
No tests found for given includes
I have tried to clean the build and reset caches in IntelliJ, but nothing seems to help. Unfortunately I don't know what I've done to put Grails + IntelliJ in this state either. It used to work, but now it doesn't, and I'm not sure what has changed.
I found a "fix", sort of.
I edited the #IntegrationTest annotation on my test class from:
#Integration
to
#Integration(applicationClass = Application.class)
And now it works again.
Interestingly, if I change it back to just #Integration, it still works. But if I run a clean, and rerun, it stops working. So something is definitely wonky here.
EDIT: I wrote the following test case:
CompilerConfiguration configuration = new CompilerConfiguration()
configuration.setTolerance(1)
println new File("src/integration-test/groovy/my/package/Mytest.groovy").isFile()
// true
def source = new SourceUnit(
new File("src/integration-test/groovy/my/package/MyTest.groovy"),
configuration,
null,
new ErrorCollector(configuration))
println source
// org.codehaus.groovy.control.SourceUnit#19bbb216
println source.source
// org.codehaus.groovy.control.io.FileReaderSource#6cfac0bd
println source.source.URI
// file:/path/to/my/app/src/integration-test/groovy/my/package/MyTest.groovy
println MainClassFinder.searchMainClass(source.source.URI)
// null
The AST transformation #Integration runs MainClassFinder.searchMainClass when the applicationClass property is not set. So this seems to indicate that it for some reason isn't able to automatically find the application class for my app based on the integration test. Then again, I'm not really sure which source unit it actually gets when it runs, so my test case might not be realistic.

Grails 3 run-script - Script execution error: No transactionManager

I'm trying to use the run-script command with one of my services in Grails 3.
I have all my services marked with the #Transactional tag. I made a "standalone" groovy file in the folder src/main/groovy that declares one of the services and wants to call a method on it.
My stand-alone groovy file declares a dataSource and sets it like this:
def dataSource = new OracleDataSource(URL: 'jdbc:oracle:thin:#localhost:1521:local', user: 'test', password: '');
I can verify it works within the stand-alone groovy file by doing this:
sql = new Sql(dataSource);
sql.eachRow("SELECT COUNT(*) AS MYCOUNT FROM MYTABLE") {
myCount = it."MYCOUNT";
}
and it works fine.
I then pass the dataSource to my service class as a param. Note that when I run my Grails app as a "normal" Grails app, I don't need to do this, as the service "already knows" how to initialize the dataSource property that it has. But when running my stand-alone groovy file with "grails run-script" and wanting to use the service, I do this:
applicationFunctionService.setSession(dataSource);
But then afterwards when I call a method on my service that involves doing an SQL/database-interaction, it throws this error (when using grails run-script)
Script execution error: No transactionManager was specified. Using #Transactional or #Rollback requires a valid configured transaction manager. If you are running in a unit test ensure the test has been properly configured and that you run the test suite not an individual test method.
:runScript FAILED
Note that my intention is not to do a test per say... I actually want to have a "stand-alone" groovy file that invokes my Grails service within the Grails-context. I'd also like to leave the service as-is with the #Transactional tag. Though I wouldn't mind "turning #Transaction off" within the context of my stand-alone groovy class, if that's possible.
Is there a way to deal with this error?

Dart - How run a function after or before each test?

I'm using the Dart test package: https://pub.dartlang.org/packages/test
Often, I want to run some function before or after each tests in my test file. Does the test package provide something for this?
add a setUp(() { add your code here}) before your test() function.
There is also a tearDown() which is run after each test.
If you add the setUp function at top level in main it is run for every test, if you put it inside a group it is run for every test in that group. You can have setUp/tearDown on more than one level at the same time.
tearDown is executed in any case (like finally) no matter if the test fails or succeeds.
Recently setUpAll() and tearDownAll() was added to do some set up and tear down once before and after all tests.

How to set the transactionnal option per test class in a spock integration test?

In a grails project (version 2.3.7) , i have an integration test using Spock :
class SimpleIntegrationTests extends IntegrationSpec{
void "test an action from controller to database"(){...}
}
This integration test launch a batch with Spring Batch. Spring batch does not accept when a batch is launched from an existing transaction :
java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again
So i tell my integration test to run without transaction, with :
static transactional = false
Doing this, the test runs with success.
But there are other integration tests in my project, which need transactions to run correctly.
And the instruction "transactionnal = false" in my test class is not confined to this test class, but affects all other integration tests triggered after my test class.
I understood there is an alphabetically execution order for the tests. So i know that if i rename my test class to be the last test class to run, it works fine, and all my tests are successful. But i think it s not an ideal answer to the problem.
So my question is : how to define that a test class is non transactionnal, in a manner that does not affect other integration tests ?
I know this is an old question and I imagine you've found a solution already, but as a work around I think you need to explicitly specify static transactional = true on integration tests that you still want to be transactional. We've had a similar issue with our Grails 2.3.7 test suite...

Grails GORM in Spring tool suite

The following artical code works in 'grails console'. But when I try to run it in STS its giving compile error for the domain class.
http://timsporcic.github.io/GORM-Recipes/
Is it possible to run in STS, I want to test to GORM methods before using it in contolers. even console command from STS is not working.
trying to run like this:
class Test {
static main(args) {
new BootStrap().init()
println Person.get(1)
}
}
thanks
Run as > Groovy Script will not work. grails console works different, because you have you application fully initialized (same for run-app and test-app).
If you want to test your BootStrap class I suggest you to create one integration test. This will also ensure that, if you change your class, the logic still works.

Resources