Skip to content

Commit 1bc22e8

Browse files
add mysql and postgres test using testcontainer
closes #10 Signed-off-by: Jorge Aguilera <[email protected]>
1 parent 8778cfa commit 1bc22e8

File tree

5 files changed

+220
-17
lines changed

5 files changed

+220
-17
lines changed

plugins/nf-sqldb/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ dependencies {
8484

8585
testImplementation(testFixtures("io.nextflow:nextflow:$nextflowVersion"))
8686
testImplementation(testFixtures("io.nextflow:nf-commons:$nextflowVersion"))
87+
88+
testImplementation("org.testcontainers:testcontainers")
89+
testImplementation("org.testcontainers:mysql:1.17.4")
90+
testImplementation("org.testcontainers:postgresql:1.17.4")
8791
}
8892

8993
test {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2020-2022, Seqera Labs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package nextflow.sql
19+
20+
import groovy.sql.Sql
21+
import nextflow.Channel
22+
import nextflow.plugin.Plugins
23+
import nextflow.plugin.TestPluginDescriptorFinder
24+
import nextflow.plugin.TestPluginManager
25+
import nextflow.plugin.extension.PluginExtensionProvider
26+
import org.pf4j.PluginDescriptorFinder
27+
import spock.lang.Shared
28+
import spock.lang.Timeout
29+
import test.Dsl2Spec
30+
import test.MockScriptRunner
31+
32+
import java.nio.file.Path
33+
34+
/**
35+
*
36+
* @author Paolo Di Tommaso <[email protected]>
37+
*/
38+
@Timeout(10)
39+
class H2SqlDslTest extends SqlDslTest {
40+
41+
String getJdbcURL(){
42+
'jdbc:h2:mem:test_' + Random.newInstance().nextInt(1_000_000)
43+
}
44+
45+
String getUsername(){
46+
'sa'
47+
}
48+
49+
String getPasword(){
50+
null
51+
}
52+
53+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2020-2022, Seqera Labs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package nextflow.sql
19+
20+
import groovy.sql.Sql
21+
import nextflow.Channel
22+
import nextflow.plugin.Plugins
23+
import nextflow.plugin.TestPluginDescriptorFinder
24+
import nextflow.plugin.TestPluginManager
25+
import nextflow.plugin.extension.PluginExtensionProvider
26+
import org.pf4j.PluginDescriptorFinder
27+
import org.testcontainers.containers.MySQLContainer
28+
import spock.lang.Shared
29+
import spock.lang.Timeout
30+
import test.Dsl2Spec
31+
import test.MockScriptRunner
32+
33+
import java.nio.file.Path
34+
35+
/**
36+
*
37+
* @author Paolo Di Tommaso <[email protected]>
38+
*/
39+
@Timeout(10)
40+
class MySqlDslTest extends SqlDslTest {
41+
42+
@Shared MySQLContainer db = new MySQLContainer(MySQLContainer.DEFAULT_IMAGE_NAME.withTag("latest"))
43+
.withDatabaseName("test")
44+
.withUsername("user1")
45+
.withPassword("password1")
46+
47+
def setupSpec(){
48+
db.start()
49+
}
50+
51+
def cleanSpec(){
52+
db.stop()
53+
}
54+
55+
String getJdbcURL(){
56+
db.jdbcUrl
57+
}
58+
59+
String getUsername(){
60+
db.username
61+
}
62+
63+
String getPasword(){
64+
db.password
65+
}
66+
67+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020-2022, Seqera Labs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package nextflow.sql
19+
20+
21+
import org.testcontainers.containers.MySQLContainer
22+
import org.testcontainers.containers.PostgreSQLContainer
23+
import spock.lang.Shared
24+
import spock.lang.Timeout
25+
26+
/**
27+
*
28+
* @author Paolo Di Tommaso <[email protected]>
29+
*/
30+
@Timeout(10)
31+
class PostgresSqlDslTest extends SqlDslTest {
32+
33+
@Shared PostgreSQLContainer db = new PostgreSQLContainer(PostgreSQLContainer.DEFAULT_IMAGE_NAME)
34+
.withDatabaseName("test")
35+
.withUsername("user1")
36+
.withPassword("password1")
37+
38+
def setupSpec(){
39+
db.start()
40+
}
41+
42+
def cleanSpec(){
43+
db.stop()
44+
}
45+
46+
String getJdbcURL(){
47+
db.jdbcUrl
48+
}
49+
50+
String getUsername(){
51+
db.username
52+
}
53+
54+
String getPasword(){
55+
db.password
56+
}
57+
58+
}

plugins/nf-sqldb/src/test/nextflow/sql/SqlDslTest.groovy

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,20 @@ import java.nio.file.Path
3535
*
3636
* @author Paolo Di Tommaso <[email protected]>
3737
*/
38-
@Timeout(10)
39-
class SqlDslTest extends Dsl2Spec {
38+
abstract class SqlDslTest extends Dsl2Spec {
4039

4140
@Shared String pluginsMode
4241

42+
abstract String getJdbcURL()
43+
44+
abstract String getUsername()
45+
46+
abstract String getPasword()
47+
48+
Map getConfiguration(String jdbc){
49+
[sql: [db: [ds1: [url: jdbc, user: username, password: pasword]]]]
50+
}
51+
4352
def setup() {
4453
// reset previous instances
4554
PluginExtensionProvider.reset()
@@ -69,41 +78,45 @@ class SqlDslTest extends Dsl2Spec {
6978
}
7079
def 'should perform a query and create a channel' () {
7180
given:
72-
def JDBC_URL = 'jdbc:h2:mem:test_' + Random.newInstance().nextInt(1_000_000)
73-
def sql = Sql.newInstance(JDBC_URL, 'sa', null)
81+
def JDBC_URL = getJdbcURL()
82+
def sql = Sql.newInstance(JDBC_URL, getUsername(), getPasword())
7483
and:
7584
sql.execute('create table FOO(id int primary key, alpha varchar(255), omega int);')
7685
sql.execute("insert into FOO (id, alpha, omega) values (1, 'hola', 10) ")
7786
sql.execute("insert into FOO (id, alpha, omega) values (2, 'ciao', 20) ")
7887
sql.execute("insert into FOO (id, alpha, omega) values (3, 'hello', 30) ")
7988
and:
80-
def config = [sql: [db: [test: [url: JDBC_URL]]]]
89+
def config = getConfiguration(JDBC_URL)
8190

8291
when:
8392
def SCRIPT = '''
8493
include { fromQuery; sqlInsert } from 'plugin/nf-sqldb'
8594
def table = 'FOO'
8695
def sql = "select * from $table"
87-
channel.fromQuery(sql, db: "test")
96+
channel.fromQuery(sql, db: "ds1")
8897
'''
8998
and:
9099
def result = new MockScriptRunner(config).setScript(SCRIPT).execute()
100+
91101
then:
92102
result.val == [1, 'hola', 10]
93103
result.val == [2, 'ciao', 20]
94104
result.val == [3, 'hello', 30]
95105
result.val == Channel.STOP
106+
107+
cleanup:
108+
sql.execute("drop table FOO")
96109
}
97110

98111

99112
def 'should insert channel data into a db table' () {
100113
given:
101-
def JDBC_URL = 'jdbc:h2:mem:test_' + Random.newInstance().nextInt(1_000_000)
102-
def sql = Sql.newInstance(JDBC_URL, 'sa', null)
114+
def JDBC_URL = getJdbcURL()
115+
def sql = Sql.newInstance(JDBC_URL, getUsername(), getPasword())
103116
and:
104117
sql.execute('create table FOO(id int primary key, alpha varchar(255), omega int);')
105118
and:
106-
def config = [sql: [db: [ds1: [url: JDBC_URL]]]]
119+
def config= getConfiguration(JDBC_URL)
107120

108121
when:
109122
def SCRIPT = '''
@@ -125,16 +138,18 @@ class SqlDslTest extends Dsl2Spec {
125138
rows.size() == 3
126139
rows.id == [100, 200, 300]
127140

141+
cleanup:
142+
sql.execute("drop table FOO")
128143
}
129144

130145
def 'should insert channel data into a db table in batches' () {
131146
given:
132-
def JDBC_URL = 'jdbc:h2:mem:test_' + Random.newInstance().nextInt(1_000_000)
133-
def sql = Sql.newInstance(JDBC_URL, 'sa', null)
147+
def JDBC_URL = getJdbcURL()
148+
def sql = Sql.newInstance(JDBC_URL, getUsername(), getPasword())
134149
and:
135150
sql.execute('create table FOO(id int primary key, alpha varchar(255), omega int);')
136151
and:
137-
def config = [sql: [db: [ds1: [url: JDBC_URL]]]]
152+
def config = getConfiguration(JDBC_URL)
138153

139154
when:
140155
def SCRIPT = '''
@@ -158,34 +173,40 @@ class SqlDslTest extends Dsl2Spec {
158173
rows.size() == 5
159174
rows.id == [100, 200, 300, 400, 500]
160175

176+
cleanup:
177+
sql.execute("drop table FOO")
161178
}
162179

163180
def 'should perform a query with headers and create a channel' () {
164181
given:
165-
def JDBC_URL = 'jdbc:h2:mem:test_' + Random.newInstance().nextInt(1_000_000)
166-
def sql = Sql.newInstance(JDBC_URL, 'sa', null)
182+
def JDBC_URL = getJdbcURL()
183+
def sql = Sql.newInstance(JDBC_URL, getUsername(), getPasword())
167184
and:
168185
sql.execute('create table FOO(id int primary key, alpha varchar(255), omega int);')
169186
sql.execute("insert into FOO (id, alpha, omega) values (1, 'hola', 10) ")
170187
sql.execute("insert into FOO (id, alpha, omega) values (2, 'ciao', 20) ")
171188
sql.execute("insert into FOO (id, alpha, omega) values (3, 'hello', 30) ")
172189
and:
173-
def config = [sql: [db: [test: [url: JDBC_URL]]]]
190+
def config = getConfiguration(JDBC_URL)
174191

175192
when:
176193
def SCRIPT = '''
177194
include { fromQuery; sqlInsert } from 'plugin/nf-sqldb'
178195
def table = 'FOO'
179196
def sql = "select * from $table"
180-
channel.fromQuery(sql, db: "test", emitColumns:true)
197+
channel.fromQuery(sql, db: "ds1", emitColumns:true)
181198
'''
182199
and:
183200
def result = new MockScriptRunner(config).setScript(SCRIPT).execute()
201+
184202
then:
185-
result.val == ['ID', 'ALPHA', 'OMEGA']
203+
result.val*.toUpperCase() == ['ID', 'ALPHA', 'OMEGA']
186204
result.val == [1, 'hola', 10]
187205
result.val == [2, 'ciao', 20]
188206
result.val == [3, 'hello', 30]
189207
result.val == Channel.STOP
208+
209+
cleanup:
210+
sql.execute("drop table FOO")
190211
}
191212
}

0 commit comments

Comments
 (0)