Skip to content

Commit 2a32bad

Browse files
committed
test: added more tests for jdbc package
1 parent b73cf96 commit 2a32bad

File tree

8 files changed

+1207
-6
lines changed

8 files changed

+1207
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ class RestaurantDemoSpec extends Specification with RestaurantDemoDatabaseFuncti
151151

152152
## Run Tests
153153
```shell
154+
docker rm -f mongodb;
154155
docker run -d --publish 27017:27017 --name mongodb mongocamp/mongodb:latest;
155156
sbt +test;
156-
docker rm -f mongodb;
157157
```
158158

159159
## Supporters

docker.sh

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/test/scala/dev/mongocamp/driver/mongodb/CompactSpec.scala renamed to src/test/scala/dev/mongocamp/driver/mongodb/CompactSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import munit.FunSuite
99
import java.text.SimpleDateFormat
1010
import java.util.Date
1111

12-
class CompactSpec extends FunSuite {
12+
class CompactSuite extends FunSuite {
1313
val DateFormat = new SimpleDateFormat("yyyy-MM-dd")
1414
val From: Date = DateFormat.parse("2000-01-01")
1515

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package dev.mongocamp.driver.mongodb
2+
3+
import dev.mongocamp.driver.DocumentIncludes
4+
import dev.mongocamp.driver.mongodb.database.DatabaseProvider
5+
import munit.FunSuite
6+
import org.mongodb.scala.Document
7+
import org.bson.types.ObjectId
8+
import org.apache.lucene.search.MatchAllDocsQuery
9+
10+
class DocumentIncludesSuite extends FunSuite with DocumentIncludes {
11+
12+
test("mapToBson should convert Map to Bson") {
13+
val map = Map("key" -> "value")
14+
val bson = mapToBson(map)
15+
assert(bson.isInstanceOf[Document])
16+
assertEquals(bson.asInstanceOf[Document].getString("key"), "value")
17+
}
18+
19+
test("luceneQueryBson should convert Lucene Query to Bson") {
20+
val query = new MatchAllDocsQuery()
21+
val bson = luceneQueryBson(query)
22+
assert(bson.isInstanceOf[Document])
23+
}
24+
25+
test("documentFromJavaMap should convert java.util.Map to Document") {
26+
val javaMap = new java.util.HashMap[String, Any]()
27+
javaMap.put("key", "value")
28+
val document = documentFromJavaMap(javaMap)
29+
assert(document.isInstanceOf[Document])
30+
assertEquals(document.getString("key"), "value")
31+
}
32+
33+
test("documentFromMutableMap should convert mutable.Map to Document") {
34+
val mutableMap: collection.mutable.Map[String, Any] = collection.mutable.Map("key" -> "value")
35+
val document = documentFromMutableMap(mutableMap)
36+
assert(document.isInstanceOf[Document])
37+
assertEquals(document.getString("key"), "value")
38+
}
39+
40+
test("documentFromScalaMap should convert Map to Document") {
41+
val map = Map("key" -> "value")
42+
val document = documentFromScalaMap(map)
43+
assert(document.isInstanceOf[Document])
44+
assertEquals(document.getString("key"), "value")
45+
}
46+
47+
test("documentFromDocument should convert org.bson.Document to Document") {
48+
val bsonDoc = new org.bson.Document("key", "value")
49+
val document = documentFromDocument(bsonDoc)
50+
assert(document.isInstanceOf[Document])
51+
assertEquals(document.getString("key"), "value")
52+
}
53+
54+
test("mapFromDocument should convert Document to Map") {
55+
val document = Document("key" -> "value")
56+
val map = mapFromDocument(document)
57+
assert(map.isInstanceOf[Map[_, _]])
58+
assertEquals(map("key"), "value")
59+
}
60+
61+
test("mapListFromDocuments should convert List of Documents to List of Maps") {
62+
val documents = List(Document("key" -> "value"))
63+
val mapList = mapListFromDocuments(documents)
64+
assert(mapList.isInstanceOf[List[_]])
65+
assertEquals(mapList.head("key"), "value")
66+
}
67+
68+
test("stringToObjectId should convert String to ObjectId") {
69+
val str = "507f1f77bcf86cd799439011"
70+
val objectId = stringToObjectId(str)
71+
assert(objectId.isInstanceOf[ObjectId])
72+
assertEquals(objectId.toHexString, str)
73+
}
74+
75+
test("documentToObjectId should extract ObjectId from Document") {
76+
val objectId = new ObjectId()
77+
val document = Document(DatabaseProvider.ObjectIdKey -> objectId)
78+
val extractedObjectId = documentToObjectId(document)
79+
assertEquals(extractedObjectId, objectId)
80+
}
81+
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package dev.mongocamp.driver.mongodb.jdbc
2+
3+
import dev.mongocamp.driver.mongodb.*
4+
import dev.mongocamp.driver.mongodb.jdbc.statement.MongoPreparedStatement
5+
6+
import java.sql.{Connection, DriverManager, SQLFeatureNotSupportedException, SQLWarning, Savepoint}
7+
import java.util.Properties
8+
import java.util.concurrent.Executor
9+
10+
class ConnectionSuite extends BaseJdbcSuite {
11+
12+
test("getDatabaseProvider should return the database provider") {
13+
val driver = new MongoJdbcDriver()
14+
val connectionUrl = "jdbc:mongodb://localhost:27017/mongocamp-unit-test?retryWrites=true&loadBalanced=false&serverSelectionTimeoutMS=5000&connectTimeoutMS=10000"
15+
val propertiesInfo = driver.getPropertyInfo(connectionUrl, new Properties())
16+
assertEquals(propertiesInfo.length, 5)
17+
}
18+
19+
test("getDatabaseProvider should return the database provider") {
20+
assertEquals(connection.asInstanceOf[MongoJdbcConnection].getDatabaseProvider.collections().results().isEmpty, false)
21+
}
22+
23+
test("createStatement should return a MongoPreparedStatement") {
24+
assert(connection.createStatement().isInstanceOf[MongoPreparedStatement])
25+
assert(connection.createStatement(0, 0).isInstanceOf[MongoPreparedStatement])
26+
assert(connection.createStatement(0, 0, 0).isInstanceOf[MongoPreparedStatement])
27+
}
28+
29+
test("prepareStatement should return a MongoPreparedStatement") {
30+
assert(connection.prepareStatement("SELECT * FROM people").isInstanceOf[MongoPreparedStatement])
31+
assert(connection.prepareStatement("SELECT * FROM people", 0).isInstanceOf[MongoPreparedStatement])
32+
assert(connection.prepareStatement("SELECT * FROM people", 0, 0).isInstanceOf[MongoPreparedStatement])
33+
assert(connection.prepareStatement("SELECT * FROM people", 0, 0, 0).isInstanceOf[MongoPreparedStatement])
34+
assert(connection.prepareStatement("SELECT * FROM people", Array[Int]()).isInstanceOf[MongoPreparedStatement])
35+
assert(connection.prepareStatement("SELECT * FROM people", Array[String]()).isInstanceOf[MongoPreparedStatement])
36+
}
37+
38+
test("prepareCall should return a MongoPreparedStatement") {
39+
assert(connection.prepareCall("SELECT * FROM people").isInstanceOf[MongoPreparedStatement])
40+
assert(connection.prepareCall("SELECT * FROM people", 0, 0).isInstanceOf[MongoPreparedStatement])
41+
assert(connection.prepareCall("SELECT * FROM people", 0, 0, 0).isInstanceOf[MongoPreparedStatement])
42+
}
43+
44+
test("nativeSQL should return the same SQL string") {
45+
val sql = "SELECT * FROM people"
46+
assertEquals(connection.nativeSQL(sql), sql)
47+
}
48+
49+
test("setAutoCommit should not throw an exception") {
50+
connection.setAutoCommit(true)
51+
}
52+
53+
test("getAutoCommit should return true") {
54+
assert(connection.getAutoCommit)
55+
}
56+
57+
test("commit should not throw an exception") {
58+
connection.commit()
59+
}
60+
61+
test("rollback should not throw an exception") {
62+
connection.rollback()
63+
}
64+
65+
test("getMetaData should return MongoDatabaseMetaData") {
66+
assert(connection.getMetaData.isInstanceOf[MongoDatabaseMetaData])
67+
}
68+
69+
test("setReadOnly should set the connection to read-only") {
70+
connection.setReadOnly(true)
71+
assert(connection.isReadOnly)
72+
}
73+
74+
test("setCatalog should not throw an exception") {
75+
connection.setCatalog("testCatalog")
76+
}
77+
78+
test("getCatalog should return null") {
79+
assertEquals(connection.getCatalog, null)
80+
}
81+
82+
test("intercept not implemented sql features") {
83+
intercept[SQLFeatureNotSupportedException](connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED))
84+
intercept[SQLFeatureNotSupportedException](connection.createNClob())
85+
intercept[SQLFeatureNotSupportedException](connection.createBlob())
86+
intercept[SQLFeatureNotSupportedException](connection.createClob())
87+
intercept[SQLFeatureNotSupportedException](connection.createSQLXML())
88+
intercept[SQLFeatureNotSupportedException](connection.createStruct("", null))
89+
intercept[SQLFeatureNotSupportedException](connection.createArrayOf("typeName: String", null))
90+
}
91+
92+
test("getTransactionIsolation should return TRANSACTION_NONE") {
93+
assertEquals(connection.getTransactionIsolation, Connection.TRANSACTION_NONE)
94+
}
95+
96+
test("getWarnings should return null") {
97+
assertEquals(connection.getWarnings, null)
98+
}
99+
100+
test("clearWarnings should not throw an exception") {
101+
connection.clearWarnings()
102+
}
103+
104+
test("getTypeMap should return null") {
105+
assertEquals(connection.getTypeMap, null)
106+
}
107+
108+
test("setTypeMap should not throw an exception") {
109+
connection.setTypeMap(new java.util.HashMap[String, Class[_]]())
110+
}
111+
112+
test("setHoldability should not throw an exception") {
113+
connection.setHoldability(0)
114+
}
115+
116+
test("getHoldability should return 0") {
117+
assertEquals(connection.getHoldability, 0)
118+
}
119+
120+
test("setSavepoint should return null") {
121+
assertEquals(connection.setSavepoint(), null)
122+
}
123+
124+
test("setSavepoint with name should return null") {
125+
assertEquals(connection.setSavepoint("savepoint"), null)
126+
}
127+
128+
test("rollback with savepoint should not throw an exception") {
129+
connection.rollback(null.asInstanceOf[Savepoint])
130+
}
131+
132+
test("releaseSavepoint should not throw an exception") {
133+
connection.releaseSavepoint(null.asInstanceOf[Savepoint])
134+
}
135+
136+
test("isValid should return true") {
137+
assert(connection.isValid(0))
138+
}
139+
140+
test("setClientInfo with name and value should not throw an exception") {
141+
connection.setClientInfo("ApplicationName", "testApp")
142+
}
143+
144+
test("setClientInfo with properties should not throw an exception") {
145+
val properties = new Properties()
146+
properties.setProperty("ApplicationName", "testApp")
147+
connection.setClientInfo(properties)
148+
}
149+
150+
test("getClientInfo with name should return the application name") {
151+
connection.setClientInfo("ApplicationName", "testApp")
152+
assertEquals(connection.getClientInfo("ApplicationName"), "testApp")
153+
}
154+
155+
test("getClientInfo should return properties with application name") {
156+
connection.setClientInfo("ApplicationName", "testApp")
157+
val properties = connection.getClientInfo
158+
assertEquals(properties.getProperty("ApplicationName"), "testApp")
159+
}
160+
161+
test("getSchema should return the default database name") {
162+
assertEquals(connection.getSchema, "mongocamp-unit-test")
163+
}
164+
165+
test("setSchema should not throw an exception") {
166+
connection.setSchema("testSchema")
167+
}
168+
169+
test("abort should not throw an exception") {
170+
connection.abort(null.asInstanceOf[Executor])
171+
}
172+
173+
test("setNetworkTimeout should not throw an exception") {
174+
connection.setNetworkTimeout(null.asInstanceOf[Executor], 0)
175+
}
176+
177+
test("getNetworkTimeout should return 0") {
178+
assertEquals(connection.getNetworkTimeout, 0)
179+
}
180+
181+
test("unwrap should return null") {
182+
assertEquals(connection.unwrap(classOf[Connection]), null)
183+
}
184+
185+
test("isWrapperFor should return false") {
186+
assert(!connection.isWrapperFor(classOf[Connection]))
187+
}
188+
189+
test("close should close the connection") {
190+
connection.close()
191+
assertEquals(connection.isClosed, true)
192+
}
193+
}

0 commit comments

Comments
 (0)