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

minigl: Allow using embedded postgres in unit tests #180

Merged
merged 1 commit into from
Oct 1, 2020

Conversation

afk11
Copy link
Contributor

@afk11 afk11 commented Sep 22, 2020

Here's a proof of concept for #179. It uses embedded-postgres to spin up a test server, and uses the configModifier feature to load a new config file we write to a temporary directory

I wanted to get this working with parameterized tests or test templates.. but I couldn't find a way to pull this off in junit5. one reason this is so so is BalanceTest - we want the entire test class to be parameterized and to use the same database resource, but this doesn't seem to work. I think junit4 used to be able to parameterize an entire class, but not junit5. While doing test templates, I noted that although the test template can dynamically register ParameterResolvers, these aren't registered in advance of calling the classes constructor.. so we can't initialize the class in that way.

So instead I just got it working by setting a system property when calling gradle test. TestBase checks which driver to use and sets up the database accordingly.

One downside is this isn't automatic, so we'll have to decide how to tackle it in the travis-ci.yml file or look into alternative approaches. That said, if we don't mind tacking on on cd modules/minigl && gradle clean test -Dtest.minigl_db_driver=postgres as an extra test command, we're good to go :)

@ar
Copy link
Member

ar commented Sep 24, 2020

A call to gradle clean test -Dtest.minigl_db_driver=postgres --info on this branch produces:

    configModifier: file:/var/folders/yv/x7_p69j10vg5b7j6j_l_f3380000gp/T/junit7045695572522985859/hibernate.cfg.xml

org.jpos.gl.PermissionTest > testGrant() FAILED
    org.hibernate.exception.GenericJDBCException: could not prepare statement
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:151)
        at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2099)
...
...
            Caused by:
            org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "GLUSER" not found; SQL statement:
            select this_.id as id1_6_0_, this_.nick as nick2_6_0_, this_.name as name3_6_0_ from gluser this_ where this_.nick=? [42102-199]
                at org.h2.message.DbException.getJdbcSQLException(DbException.java:451)
                at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
                at org.h2.message.DbException.get(DbException.java:205)
                at org.h2.message.DbException.get(DbException.java:181)
                at org.h2.command.Parser.readTableOrView(Parser.java:7146)

Look like it still uses h2

@afk11
Copy link
Contributor Author

afk11 commented Sep 30, 2020

Thanks @ar - I'd forgotten a few places where 'new GLSession("eve")` was done in tests, but those need the correct DB/configModifier, so I've fixed the issue you ran into

@afk11
Copy link
Contributor Author

afk11 commented Sep 30, 2020

Depends on #184

@ar ar merged commit ffd02f6 into jpos:master Oct 1, 2020
@afk11 afk11 deleted the postgres-test branch October 1, 2020 14:10
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

Successfully merging this pull request may close these issues.

2 participants