Skip to content

Commit 9681419

Browse files
committed
Merge pull request mauricio#117 from nyavro/issue102
mauricio#102 Provided stored procedure tests
2 parents 008b8d0 + b16812c commit 9681419

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2013 Maurício Linhares
3+
*
4+
* Maurício Linhares licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.github.mauricio.async.db.mysql
18+
19+
import com.github.mauricio.async.db.ResultSet
20+
import com.github.mauricio.async.db.util.FutureUtils._
21+
import org.specs2.mutable.Specification
22+
23+
class StoredProceduresSpec extends Specification with ConnectionHelper {
24+
25+
"connection" should {
26+
27+
"be able to execute create stored procedure" in {
28+
withConnection {
29+
connection =>
30+
val future = for(
31+
drop <- connection.sendQuery("DROP PROCEDURE IF exists helloWorld;");
32+
create <- connection.sendQuery(
33+
"""
34+
CREATE PROCEDURE helloWorld(OUT param1 VARCHAR(20))
35+
BEGIN
36+
SELECT 'hello' INTO param1;
37+
END
38+
"""
39+
)
40+
) yield create
41+
awaitFuture(future).statusMessage === ""
42+
}
43+
}
44+
45+
"be able to call stored procedure" in {
46+
withConnection {
47+
connection =>
48+
val future = for(
49+
drop <- connection.sendQuery("DROP PROCEDURE IF exists constTest;");
50+
create <- connection.sendQuery(
51+
"""
52+
CREATE PROCEDURE constTest(OUT param INT)
53+
BEGIN
54+
SELECT 125 INTO param;
55+
END
56+
"""
57+
);
58+
call <- connection.sendQuery("CALL constTest(@arg)");
59+
arg <- connection.sendQuery("SELECT @arg")
60+
) yield arg
61+
val result: Option[ResultSet] = awaitFuture(future).rows
62+
result.isDefined === true
63+
val rows = result.get
64+
rows.size === 1
65+
rows(0)(rows.columnNames.head) === 125
66+
}
67+
}
68+
69+
"be able to call stored procedure with input parameter" in {
70+
withConnection {
71+
connection =>
72+
val future = for(
73+
drop <- connection.sendQuery("DROP PROCEDURE IF exists addTest;");
74+
create <- connection.sendQuery(
75+
"""
76+
CREATE PROCEDURE addTest(IN a INT, IN b INT, OUT sum INT)
77+
BEGIN
78+
SELECT a+b INTO sum;
79+
END
80+
"""
81+
);
82+
call <- connection.sendQuery("CALL addTest(132, 245, @sm)");
83+
res <- connection.sendQuery("SELECT @sm")
84+
) yield res
85+
val result: Option[ResultSet] = awaitFuture(future).rows
86+
result.isDefined === true
87+
val rows = result.get
88+
rows.size === 1
89+
rows(0)(rows.columnNames.head) === 377
90+
}
91+
}
92+
93+
"be able to remove stored procedure" in {
94+
withConnection {
95+
connection =>
96+
val createResult: Option[ResultSet] = awaitFuture(
97+
for(
98+
drop <- connection.sendQuery("DROP PROCEDURE IF exists remTest;");
99+
create <- connection.sendQuery(
100+
"""
101+
CREATE PROCEDURE remTest(OUT cnst INT)
102+
BEGIN
103+
SELECT 987 INTO cnst;
104+
END
105+
"""
106+
);
107+
routine <- connection.sendQuery(
108+
"""
109+
SELECT routine_name FROM INFORMATION_SCHEMA.ROUTINES WHERE routine_name="remTest"
110+
"""
111+
)
112+
) yield routine
113+
).rows
114+
createResult.isDefined === true
115+
createResult.get.size === 1
116+
createResult.get(0)("routine_name") === "remTest"
117+
val removeResult: Option[ResultSet] = awaitFuture(
118+
for(
119+
drop <- connection.sendQuery("DROP PROCEDURE remTest;");
120+
routine <- connection.sendQuery(
121+
"""
122+
SELECT routine_name FROM INFORMATION_SCHEMA.ROUTINES WHERE routine_name="remTest"
123+
"""
124+
)
125+
) yield routine
126+
).rows
127+
removeResult.isDefined === true
128+
removeResult.get.isEmpty === true
129+
}
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)