Skip to content

Commit 581aef8

Browse files
authored
add db2 tests (ibm-functions#12)
1 parent 795dafe commit 581aef8

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

tests/dat/db2/testDB2Service.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Python test fixture to check that action can connect to db2 service"""
2+
3+
import ibm_db
4+
5+
def main(args):
6+
ssldsn = args["__bx_creds"]["dashDB"]["ssldsn"]
7+
conn = ibm_db.connect(ssldsn, "", "")
8+
if conn:
9+
print ("Connection succeeded.")
10+
else:
11+
print ("Connection failed.")
12+
return {"error":"Error connecting to db2"}
13+
14+
# Select from TestTable (the row just inserted)
15+
cmd = "SELECT HISP_DESC FROM SAMPLES.HISPANIC_ORIGIN WHERE HISP_CODE='03'"
16+
result = ibm_db.exec_immediate(conn, cmd)
17+
18+
if not result:
19+
return {"error":"error :"+cmd}
20+
else:
21+
ibm_db.fetch_both(result,0)
22+
value = ibm_db.result(result,"HISP_DESC")
23+
ibm_db.close(conn)
24+
return {"HISP_DESC":value}
25+
26+
27+
if __name__ == "__main__":
28+
# execute only if run as a script
29+
input = {"__bx_creds":{"dashDB":{"ssldsn":"<ssldsn from credentials>"}}}
30+
print(main(input))
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2017 IBM Corporation
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+
package integration
17+
18+
import common._
19+
import org.junit.runner.RunWith
20+
import org.scalatest.junit.JUnitRunner
21+
import java.io.File
22+
import common.rest.WskRest
23+
import spray.json._
24+
25+
@RunWith(classOf[JUnitRunner])
26+
class CredentialsIBMPythonDb2CloudTests extends TestHelpers with WskTestHelpers {
27+
28+
implicit val wskprops: WskProps = WskProps()
29+
val defaultKind = Some("python-jessie:3")
30+
val wsk = new WskRest
31+
val datdir = System.getProperty("user.dir") + "/dat/db2"
32+
val actionName = "testDB2Service"
33+
val actionFileName = "testDB2Service.py"
34+
35+
val creds = TestUtils.getVCAPcredentials("dashDB")
36+
val ssldsn = creds.get("ssldsn")
37+
val __bx_creds = JsObject("dashDB" -> JsObject("ssldsn" -> JsString(ssldsn)))
38+
39+
it should "Test connection to DB2 on IBM Cloud" in withAssetCleaner(wskprops) { (wp, assetHelper) =>
40+
val file = Some(new File(datdir, actionFileName).toString())
41+
42+
assetHelper.withCleaner(wsk.action, actionName) { (action, _) =>
43+
action.create(
44+
actionName,
45+
file,
46+
main = Some("main"),
47+
kind = defaultKind,
48+
parameters = Map("__bx_creds" -> __bx_creds))
49+
}
50+
51+
withActivation(wsk.activation, wsk.action.invoke(actionName)) { activation =>
52+
val response = activation.response
53+
response.result.get.fields.get("error") shouldBe empty
54+
response.result.get.fields.get("HISP_DESC") should be(Some(JsString("Puerto Rican")))
55+
}
56+
57+
}
58+
59+
}

tests/src/test/scala/integration/CredentialsIBMPythonWatsonTests.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515
*/
1616
package integration
1717

18+
import common._
1819
import org.junit.runner.RunWith
1920
import org.scalatest.junit.JUnitRunner
20-
import common.WskProps
2121
import java.io.File
2222
import spray.json._
23-
import common.TestUtils
2423
import common.rest.WskRest
25-
import common.TestHelpers
26-
import common.WskTestHelpers
2724

2825
@RunWith(classOf[JUnitRunner])
2926
class CredentialsIBMPythonWatsonTests extends TestHelpers with WskTestHelpers {

0 commit comments

Comments
 (0)