Skip to content

Commit fdcf28e

Browse files
committed
Update changelog
Fix tests
1 parent 32fcd7a commit fdcf28e

File tree

4 files changed

+149
-205
lines changed

4 files changed

+149
-205
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Internally we use prepared statements, so all incoming data is
123123
validated against SQL injection, however we had to build a connection from JavaScript types to the SQL data types
124124
therefore when doing a prepared statements, you would need to add ``:type`` to **each prepared statement variable**.
125125

126-
**Note:** prepared statement variables name could contain: any word character, a digit and a character `_`.
126+
**Note:** prepared statement variables name could contain: any characters between a-z or A-Z, a digit and a character `_` (`[a-zA-Z0-9_]`).
127127

128128
For example if you have a following SQL statement:
129129

changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
### Fixed
1010
### Security
1111

12-
## [V2.0] 2018-09-19 elastic.io
12+
## [V2.0.1] 2019-06-24 elastic.io
13+
14+
### Fixed
15+
- Improvement error logging for generating metadata of `Select` action
16+
17+
## [V2.0.0] 2018-09-19 elastic.io
1318

1419
### Added
1520

src/test/groovy/io/elastic/jdbc/actions/SelectMSSQLSpec.groovy

Lines changed: 33 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ import javax.json.Json
88
import javax.json.JsonObject
99
import java.sql.Connection
1010
import java.sql.DriverManager
11-
import java.sql.ResultSet
1211

1312
@Ignore
1413
class SelectMSSQLSpec extends Specification {
15-
16-
@Shared
17-
def connectionString = System.getenv("CONN_URI_MSSQL")
1814
@Shared
1915
def user = System.getenv("CONN_USER_MSSQL")
2016
@Shared
@@ -23,7 +19,10 @@ class SelectMSSQLSpec extends Specification {
2319
def databaseName = System.getenv("CONN_DBNAME_MSSQL")
2420
@Shared
2521
def host = System.getenv("CONN_HOST_MSSQL")
26-
22+
@Shared
23+
def port = System.getenv("CONN_PORT_MSSQL")
24+
@Shared
25+
def connectionString = "jdbc:sqlserver://" + host + ":" + port + ";database=" + databaseName
2726
@Shared
2827
Connection connection
2928

@@ -36,6 +35,8 @@ class SelectMSSQLSpec extends Specification {
3635
@Shared
3736
EventEmitter.Callback reboundCallback
3837
@Shared
38+
EventEmitter.Callback onHttpReplyCallback
39+
@Shared
3940
EventEmitter emitter
4041
@Shared
4142
SelectAction action
@@ -45,34 +46,36 @@ class SelectMSSQLSpec extends Specification {
4546
}
4647

4748
def setup() {
48-
createAction()
49+
action = new SelectAction()
4950
}
5051

51-
def createAction() {
52+
def runAction(JsonObject config, JsonObject body, JsonObject snapshot) {
53+
Message msg = new Message.Builder().body(body).build()
5254
errorCallback = Mock(EventEmitter.Callback)
5355
snapshotCallback = Mock(EventEmitter.Callback)
5456
dataCallback = Mock(EventEmitter.Callback)
5557
reboundCallback = Mock(EventEmitter.Callback)
56-
emitter = new EventEmitter.Builder().onData(dataCallback).onSnapshot(snapshotCallback).onError(errorCallback).onRebound(reboundCallback).build()
57-
action = new SelectAction(emitter)
58-
}
59-
60-
def runAction(JsonObject config, JsonObject body, JsonObject snapshot) {
61-
Message msg = new Message.Builder().body(body).build()
62-
ExecutionParameters params = new ExecutionParameters(msg, config, snapshot)
58+
onHttpReplyCallback = Mock(EventEmitter.Callback)
59+
emitter = new EventEmitter.Builder()
60+
.onData(dataCallback)
61+
.onSnapshot(snapshotCallback)
62+
.onError(errorCallback)
63+
.onRebound(reboundCallback)
64+
.onHttpReplyCallback(onHttpReplyCallback).build()
65+
ExecutionParameters params = new ExecutionParameters(msg, emitter, config, snapshot)
6366
action.execute(params);
6467
}
6568

6669
def getStarsConfig() {
67-
JsonObject config = Json.createObjectBuilder().build();
68-
69-
config.addProperty("idColumn", "id")
70-
config.addProperty("tableName", "stars")
71-
config.addProperty("user", user)
72-
config.addProperty("password", password)
73-
config.addProperty("dbEngine", "mssql")
74-
config.addProperty("host", host)
75-
config.addProperty("databaseName", databaseName)
70+
JsonObject config = Json.createObjectBuilder()
71+
.add("user", user)
72+
.add("password", password)
73+
.add("dbEngine", "mssql")
74+
.add("host", host)
75+
.add("port", port)
76+
.add("databaseName", databaseName)
77+
.add("sqlQuery", "SELECT * from stars")
78+
.build();
7679
return config;
7780
}
7881

@@ -81,198 +84,25 @@ class SelectMSSQLSpec extends Specification {
8184
" DROP TABLE stars;"
8285
connection.createStatement().execute(sql);
8386
connection.createStatement().execute("CREATE TABLE stars (id int, name varchar(255) NOT NULL, date datetime, radius int, destination int)");
84-
}
85-
86-
def getRecords(tableName) {
87-
ArrayList<String> records = new ArrayList<String>();
88-
String sql = "SELECT * FROM " + tableName;
89-
ResultSet rs = connection.createStatement().executeQuery(sql);
90-
while (rs.next()) {
91-
records.add(rs.toRowResult().toString());
92-
}
93-
rs.close();
94-
return records;
87+
connection.createStatement().execute("INSERT INTO stars (id, name) VALUES (1,'Hello')");
9588
}
9689

9790
def cleanupSpec() {
98-
String sql = "IF OBJECT_ID('persons', 'U') IS NOT NULL\n" +
99-
" DROP TABLE persons;"
100-
101-
connection.createStatement().execute(sql)
102-
sql = "IF OBJECT_ID('stars', 'U') IS NOT NULL\n" +
91+
String sql = "IF OBJECT_ID('stars', 'U') IS NOT NULL\n" +
10392
" DROP TABLE stars;"
10493
connection.createStatement().execute(sql)
10594
connection.close()
10695
}
10796

108-
def "one insert"() {
97+
def "one select"() {
10998

11099
prepareStarsTable();
111-
112100
JsonObject snapshot = Json.createObjectBuilder().build();
101+
JsonObject body = Json.createObjectBuilder().build()
113102

114-
JsonObject body = Json.createObjectBuilder().build();
115-
body.addProperty("id", "1")
116-
body.addProperty("name", "Taurus")
117-
body.addProperty("date", "2015-02-19 10:10:10.0")
118-
body.addProperty("radius", "123")
119-
103+
when:
120104
runAction(getStarsConfig(), body, snapshot)
121-
122-
ArrayList<String> records = getRecords("stars")
123-
124-
expect:
125-
records.size() == 1
126-
records.get(0) == '{id=1, name=Taurus, date=2015-02-19 10:10:10.0, radius=123, destination=null}'
127-
}
128-
129-
def "one insert, incorrect value: string in integer field"() {
130-
131-
prepareStarsTable();
132-
133-
JsonObject snapshot = Json.createObjectBuilder().build();
134-
135-
JsonObject body = Json.createObjectBuilder().build();
136-
body.addProperty("id", "1")
137-
body.addProperty("name", "Taurus")
138-
body.addProperty("radius", "test")
139-
140-
String exceptionClass = "";
141-
142-
try {
143-
runAction(getStarsConfig(), body, snapshot)
144-
} catch (Exception e) {
145-
exceptionClass = e.getClass().getName();
146-
}
147-
148-
expect:
149-
exceptionClass.contains("Exception")
105+
then:
106+
0 * errorCallback.receive(_)
150107
}
151-
152-
def "two inserts"() {
153-
154-
prepareStarsTable();
155-
156-
JsonObject snapshot = Json.createObjectBuilder().build()
157-
158-
JsonObject body1 = Json.createObjectBuilder().build()
159-
body1.addProperty("id", "1")
160-
body1.addProperty("name", "Taurus")
161-
body1.addProperty("radius", "123")
162-
163-
runAction(getStarsConfig(), body1, snapshot)
164-
165-
JsonObject body2 = Json.createObjectBuilder().build()
166-
body2.addProperty("id", "2")
167-
body2.addProperty("name", "Eridanus")
168-
body2.addProperty("radius", "456")
169-
170-
runAction(getStarsConfig(), body2, snapshot)
171-
172-
ArrayList<String> records = getRecords("stars")
173-
174-
expect:
175-
records.size() == 2
176-
records.get(0) == '{id=1, name=Taurus, date=null, radius=123, destination=null}'
177-
records.get(1) == '{id=2, name=Eridanus, date=null, radius=456, destination=null}'
178-
}
179-
180-
def "one insert, one update by ID"() {
181-
182-
prepareStarsTable();
183-
184-
JsonObject snapshot = Json.createObjectBuilder().build()
185-
186-
JsonObject body1 = Json.createObjectBuilder().build()
187-
body1.addProperty("id", "1")
188-
body1.addProperty("name", "Taurus")
189-
body1.addProperty("radius", "123")
190-
191-
runAction(getStarsConfig(), body1, snapshot)
192-
193-
JsonObject body2 = Json.createObjectBuilder().build()
194-
body2.addProperty("id", "1")
195-
body2.addProperty("name", "Eridanus")
196-
197-
runAction(getStarsConfig(), body2, snapshot)
198-
199-
ArrayList<String> records = getRecords("stars")
200-
201-
expect:
202-
records.size() == 1
203-
records.get(0) == '{id=1, name=Eridanus, date=null, radius=123, destination=null}'
204-
}
205-
206-
207-
def getPersonsConfig() {
208-
JsonObject config = Json.createObjectBuilder().build()
209-
config.addProperty("idColumn", "email")
210-
config.addProperty("tableName", "persons")
211-
config.addProperty("user", user)
212-
config.addProperty("password", password)
213-
config.addProperty("dbEngine", "mssql")
214-
config.addProperty("host", host)
215-
config.addProperty("databaseName", databaseName)
216-
return config;
217-
}
218-
219-
def preparePersonsTable() {
220-
String sql = "IF OBJECT_ID('persons', 'U') IS NOT NULL\n" +
221-
" DROP TABLE persons;"
222-
connection.createStatement().execute(sql);
223-
connection.createStatement().execute("CREATE TABLE persons (id int, name varchar(255) NOT NULL, email varchar(255) NOT NULL)");
224-
}
225-
226-
def "one insert, name with quote"() {
227-
228-
preparePersonsTable();
229-
230-
JsonObject snapshot = Json.createObjectBuilder().build()
231-
232-
JsonObject body1 = Json.createObjectBuilder().build()
233-
body1.addProperty("id", "1")
234-
body1.addProperty("name", "O'Henry")
235-
body1.addProperty("email", "[email protected]")
236-
runAction(getPersonsConfig(), body1, snapshot)
237-
238-
ArrayList<String> records = getRecords("persons")
239-
240-
expect:
241-
records.size() == 1
242-
records.get(0) == '{id=1, name=O\'Henry, [email protected]}'
243-
}
244-
245-
def "two inserts, one update by email"() {
246-
247-
preparePersonsTable();
248-
249-
JsonObject snapshot = Json.createObjectBuilder().build()
250-
251-
JsonObject body1 = Json.createObjectBuilder().build()
252-
body1.addProperty("id", "1")
253-
body1.addProperty("name", "User1")
254-
body1.addProperty("email", "[email protected]")
255-
runAction(getPersonsConfig(), body1, snapshot)
256-
257-
JsonObject body2 = Json.createObjectBuilder().build()
258-
body2.addProperty("id", "2")
259-
body2.addProperty("name", "User2")
260-
body2.addProperty("email", "[email protected]")
261-
runAction(getPersonsConfig(), body2, snapshot)
262-
263-
JsonObject body3 = Json.createObjectBuilder().build()
264-
body3.addProperty("id", "3")
265-
body3.addProperty("name", "User3")
266-
body3.addProperty("email", "[email protected]")
267-
runAction(getPersonsConfig(), body3, snapshot)
268-
269-
ArrayList<String> records = getRecords("persons")
270-
271-
expect:
272-
records.size() == 2
273-
records.get(0) == '{id=1, name=User1, [email protected]}'
274-
records.get(1) == '{id=3, name=User3, [email protected]}'
275-
}
276-
277-
278108
}

0 commit comments

Comments
 (0)