File tree Expand file tree Collapse file tree 6 files changed +35
-15
lines changed Expand file tree Collapse file tree 6 files changed +35
-15
lines changed Original file line number Diff line number Diff line change @@ -34,3 +34,4 @@ giza.log
34
34
.vscode *
35
35
* .swp
36
36
* .code-workspace
37
+ ** /.env
Original file line number Diff line number Diff line change
1
+ MONGODB_CONNECTION_URI = mongodb+srv://...
2
+ MONGODB_DB_NAME = test
Original file line number Diff line number Diff line change @@ -13,7 +13,12 @@ repositories {
13
13
14
14
dependencies {
15
15
testImplementation(kotlin(" test" ))
16
+ implementation(" org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.0-Beta" )
16
17
implementation(" org.mongodb:mongodb-driver-kotlin-coroutine:4.10.0-SNAPSHOT" )
18
+ implementation(" org.slf4j:slf4j-api:1.7.32" )
19
+ implementation(" ch.qos.logback:logback-classic:1.2.6" )
20
+ implementation(" io.github.cdimascio:dotenv-kotlin:6.4.1" )
21
+
17
22
}
18
23
19
24
tasks.test {
Original file line number Diff line number Diff line change 1
1
import com.mongodb.kotlin.client.coroutine.MongoClient
2
2
import com.mongodb.kotlin.client.coroutine.MongoDatabase
3
+ import org.bson.Document
3
4
4
5
class ExampleMongodbClient {
5
6
val client: MongoClient ;
@@ -14,14 +15,13 @@ class ExampleMongodbClient {
14
15
return this .database.getCollection<Any >(collectionName).countDocuments()
15
16
}
16
17
17
- suspend fun insertOne (collectionName : String , document : Any ){
18
- this .database.getCollection<Any >(collectionName).insertOne(document)
18
+ suspend fun insertOne (collectionName : String , document : Document ){
19
+ this .database.getCollection<Document >(collectionName).insertOne(document)
19
20
}
20
21
21
22
fun close (){
22
23
this .client.close()
23
24
}
24
25
25
-
26
-
27
26
}
27
+
Original file line number Diff line number Diff line change
1
+ import com.mongodb.client.model.Filters
1
2
import org.junit.jupiter.api.AfterAll
2
3
import org.junit.jupiter.api.Assertions.*
3
4
import kotlin.test.*
5
+ import io.github.cdimascio.dotenv.dotenv
6
+ import kotlinx.coroutines.runBlocking
7
+ import org.bson.BsonDocument
8
+ import org.bson.Document
9
+ import org.junit.jupiter.api.TestInstance
10
+
11
+ val dotenv = dotenv()
12
+
13
+ @TestInstance(TestInstance .Lifecycle .PER_CLASS )
4
14
internal class ExampleMongodbClientTest {
5
- val client = ExampleMongodbClient (" mongodb://localhost:27017 " , " test " )
15
+ val client = ExampleMongodbClient (dotenv[ " MONGODB_CONNECTION_URI " ], dotenv[ " MONGODB_DATABASE_NAME " ] )
6
16
@Test
7
- fun test () {
8
- assertEquals(1 , 1 )
17
+ fun testAddDocument () = runBlocking {
18
+ client.insertOne(" test" , Document ().append(" name" , " test" ))
19
+ val numDocs = client.countDocuments(" test" )
20
+ println (" NUM DOCS:: $numDocs " )
21
+ assertEquals(client.countDocuments(" test" ) == 1 .toLong(), true )
22
+ }
23
+
24
+ @AfterAll
25
+ private fun afterAll () = runBlocking {
26
+ client.database.getCollection<Document >(" test" ).deleteMany(Document ())
27
+ client.close()
<
7D33
/tr>9
28
}
29
+ }
10
30
11
- // @AfterAll
12
- // fun afterAll(){
13
- // client.close()
14
- // }
15
- }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments