Skip to content

Commit

Permalink
test: added more tests for jdbc package
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadStingray committed Feb 4, 2025
1 parent b73cf96 commit 2a32bad
Show file tree
Hide file tree
Showing 8 changed files with 1,207 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ class RestaurantDemoSpec extends Specification with RestaurantDemoDatabaseFuncti

## Run Tests
```shell
docker rm -f mongodb;
docker run -d --publish 27017:27017 --name mongodb mongocamp/mongodb:latest;
sbt +test;
docker rm -f mongodb;
```

## Supporters
Expand Down
2 changes: 0 additions & 2 deletions docker.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import munit.FunSuite
import java.text.SimpleDateFormat
import java.util.Date

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package dev.mongocamp.driver.mongodb

import dev.mongocamp.driver.DocumentIncludes
import dev.mongocamp.driver.mongodb.database.DatabaseProvider
import munit.FunSuite
import org.mongodb.scala.Document
import org.bson.types.ObjectId
import org.apache.lucene.search.MatchAllDocsQuery

class DocumentIncludesSuite extends FunSuite with DocumentIncludes {

test("mapToBson should convert Map to Bson") {
val map = Map("key" -> "value")
val bson = mapToBson(map)
assert(bson.isInstanceOf[Document])
assertEquals(bson.asInstanceOf[Document].getString("key"), "value")
}

test("luceneQueryBson should convert Lucene Query to Bson") {
val query = new MatchAllDocsQuery()
val bson = luceneQueryBson(query)
assert(bson.isInstanceOf[Document])
}

test("documentFromJavaMap should convert java.util.Map to Document") {
val javaMap = new java.util.HashMap[String, Any]()
javaMap.put("key", "value")
val document = documentFromJavaMap(javaMap)
assert(document.isInstanceOf[Document])
assertEquals(document.getString("key"), "value")
}

test("documentFromMutableMap should convert mutable.Map to Document") {
val mutableMap: collection.mutable.Map[String, Any] = collection.mutable.Map("key" -> "value")
val document = documentFromMutableMap(mutableMap)
assert(document.isInstanceOf[Document])
assertEquals(document.getString("key"), "value")
}

test("documentFromScalaMap should convert Map to Document") {
val map = Map("key" -> "value")
val document = documentFromScalaMap(map)
assert(document.isInstanceOf[Document])
assertEquals(document.getString("key"), "value")
}

test("documentFromDocument should convert org.bson.Document to Document") {
val bsonDoc = new org.bson.Document("key", "value")
val document = documentFromDocument(bsonDoc)
assert(document.isInstanceOf[Document])
assertEquals(document.getString("key"), "value")
}

test("mapFromDocument should convert Document to Map") {
val document = Document("key" -> "value")
val map = mapFromDocument(document)
assert(map.isInstanceOf[Map[_, _]])
assertEquals(map("key"), "value")
}

test("mapListFromDocuments should convert List of Documents to List of Maps") {
val documents = List(Document("key" -> "value"))
val mapList = mapListFromDocuments(documents)
assert(mapList.isInstanceOf[List[_]])
assertEquals(mapList.head("key"), "value")
}

test("stringToObjectId should convert String to ObjectId") {
val str = "507f1f77bcf86cd799439011"
val objectId = stringToObjectId(str)
assert(objectId.isInstanceOf[ObjectId])
assertEquals(objectId.toHexString, str)
}

test("documentToObjectId should extract ObjectId from Document") {
val objectId = new ObjectId()
val document = Document(DatabaseProvider.ObjectIdKey -> objectId)
val extractedObjectId = documentToObjectId(document)
assertEquals(extractedObjectId, objectId)
}
}
193 changes: 193 additions & 0 deletions src/test/scala/dev/mongocamp/driver/mongodb/jdbc/ConnectionSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package dev.mongocamp.driver.mongodb.jdbc

import dev.mongocamp.driver.mongodb.*
import dev.mongocamp.driver.mongodb.jdbc.statement.MongoPreparedStatement

import java.sql.{Connection, DriverManager, SQLFeatureNotSupportedException, SQLWarning, Savepoint}
import java.util.Properties
import java.util.concurrent.Executor

class ConnectionSuite extends BaseJdbcSuite {

test("getDatabaseProvider should return the database provider") {
val driver = new MongoJdbcDriver()
val connectionUrl = "jdbc:mongodb://localhost:27017/mongocamp-unit-test?retryWrites=true&loadBalanced=false&serverSelectionTimeoutMS=5000&connectTimeoutMS=10000"
val propertiesInfo = driver.getPropertyInfo(connectionUrl, new Properties())
assertEquals(propertiesInfo.length, 5)
}

test("getDatabaseProvider should return the database provider") {
assertEquals(connection.asInstanceOf[MongoJdbcConnection].getDatabaseProvider.collections().results().isEmpty, false)
}

test("createStatement should return a MongoPreparedStatement") {
assert(connection.createStatement().isInstanceOf[MongoPreparedStatement])
assert(connection.createStatement(0, 0).isInstanceOf[MongoPreparedStatement])
assert(connection.createStatement(0, 0, 0).isInstanceOf[MongoPreparedStatement])
}

test("prepareStatement should return a MongoPreparedStatement") {
assert(connection.prepareStatement("SELECT * FROM people").isInstanceOf[MongoPreparedStatement])
assert(connection.prepareStatement("SELECT * FROM people", 0).isInstanceOf[MongoPreparedStatement])
assert(connection.prepareStatement("SELECT * FROM people", 0, 0).isInstanceOf[MongoPreparedStatement])
assert(connection.prepareStatement("SELECT * FROM people", 0, 0, 0).isInstanceOf[MongoPreparedStatement])
assert(connection.prepareStatement("SELECT * FROM people", Array[Int]()).isInstanceOf[MongoPreparedStatement])
assert(connection.prepareStatement("SELECT * FROM people", Array[String]()).isInstanceOf[MongoPreparedStatement])
}

test("prepareCall should return a MongoPreparedStatement") {
assert(connection.prepareCall("SELECT * FROM people").isInstanceOf[MongoPreparedStatement])
assert(connection.prepareCall("SELECT * FROM people", 0, 0).isInstanceOf[MongoPreparedStatement])
assert(connection.prepareCall("SELECT * FROM people", 0, 0, 0).isInstanceOf[MongoPreparedStatement])
}

test("nativeSQL should return the same SQL string") {
val sql = "SELECT * FROM people"
assertEquals(connection.nativeSQL(sql), sql)
}

test("setAutoCommit should not throw an exception") {
connection.setAutoCommit(true)
}

test("getAutoCommit should return true") {
assert(connection.getAutoCommit)
}

test("commit should not throw an exception") {
connection.commit()
}

test("rollback should not throw an exception") {
connection.rollback()
}

test("getMetaData should return MongoDatabaseMetaData") {
assert(connection.getMetaData.isInstanceOf[MongoDatabaseMetaData])
}

test("setReadOnly should set the connection to read-only") {
connection.setReadOnly(true)
assert(connection.isReadOnly)
}

test("setCatalog should not throw an exception") {
connection.setCatalog("testCatalog")
}

test("getCatalog should return null") {
assertEquals(connection.getCatalog, null)
}

test("intercept not implemented sql features") {
intercept[SQLFeatureNotSupportedException](connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED))
intercept[SQLFeatureNotSupportedException](connection.createNClob())
intercept[SQLFeatureNotSupportedException](connection.createBlob())
intercept[SQLFeatureNotSupportedException](connection.createClob())
intercept[SQLFeatureNotSupportedException](connection.createSQLXML())
intercept[SQLFeatureNotSupportedException](connection.createStruct("", null))
intercept[SQLFeatureNotSupportedException](connection.createArrayOf("typeName: String", null))
}

test("getTransactionIsolation should return TRANSACTION_NONE") {
assertEquals(connection.getTransactionIsolation, Connection.TRANSACTION_NONE)
}

test("getWarnings should return null") {
assertEquals(connection.getWarnings, null)
}

test("clearWarnings should not throw an exception") {
connection.clearWarnings()
}

test("getTypeMap should return null") {
assertEquals(connection.getTypeMap, null)
}

test("setTypeMap should not throw an exception") {
connection.setTypeMap(new java.util.HashMap[String, Class[_]]())
}

test("setHoldability should not throw an exception") {
connection.setHoldability(0)
}

test("getHoldability should return 0") {
assertEquals(connection.getHoldability, 0)
}

test("setSavepoint should return null") {
assertEquals(connection.setSavepoint(), null)
}

test("setSavepoint with name should return null") {
assertEquals(connection.setSavepoint("savepoint"), null)
}

test("rollback with savepoint should not throw an exception") {
connection.rollback(null.asInstanceOf[Savepoint])
}

test("releaseSavepoint should not throw an exception") {
connection.releaseSavepoint(null.asInstanceOf[Savepoint])
}

test("isValid should return true") {
assert(connection.isValid(0))
}

test("setClientInfo with name and value should not throw an exception") {
connection.setClientInfo("ApplicationName", "testApp")
}

test("setClientInfo with properties should not throw an exception") {
val properties = new Properties()
properties.setProperty("ApplicationName", "testApp")
connection.setClientInfo(properties)
}

test("getClientInfo with name should return the application name") {
connection.setClientInfo("ApplicationName", "testApp")
assertEquals(connection.getClientInfo("ApplicationName"), "testApp")
}

test("getClientInfo should return properties with application name") {
connection.setClientInfo("ApplicationName", "testApp")
val properties = connection.getClientInfo
assertEquals(properties.getProperty("ApplicationName"), "testApp")
}

test("getSchema should return the default database name") {
assertEquals(connection.getSchema, "mongocamp-unit-test")
}

test("setSchema should not throw an exception") {
connection.setSchema("testSchema")
}

test("abort should not throw an exception") {
connection.abort(null.asInstanceOf[Executor])
}

test("setNetworkTimeout should not throw an exception") {
connection.setNetworkTimeout(null.asInstanceOf[Executor], 0)
}

test("getNetworkTimeout should return 0") {
assertEquals(connection.getNetworkTimeout, 0)
}

test("unwrap should return null") {
assertEquals(connection.unwrap(classOf[Connection]), null)
}

test("isWrapperFor should return false") {
assert(!connection.isWrapperFor(classOf[Connection]))
}

test("close should close the connection") {
connection.close()
assertEquals(connection.isClosed, true)
}
}
Loading

0 comments on commit 2a32bad

Please sign in to comment.